mtu, description, mockups

dev
Philippe PITTOLI 2019-10-05 11:25:49 +02:00
parent c7290452ad
commit edb7955f30
1 changed files with 66 additions and 4 deletions

View File

@ -116,6 +116,18 @@ class NetworkCommands
raise "(ifconfig) Cannot set ip address alias #{ip.to_string} for #{name}" raise "(ifconfig) Cannot set ip address alias #{ip.to_string} for #{name}"
end end
end end
def self.mtu(name : String, mtu : Int32?)
unless Do.run("ifconfig", [ name, "mtu", mtu.to_s ]).success?
raise "(ifconfig) Cannot set mtu #{mtu} for #{name}"
end
end
def self.description(name : String, description : String)
unless Do.run("ifconfig", [ name, "description", "\"#{description}\"" ]).success?
raise "(ifconfig) Cannot set description #{description} for #{name}"
end
end
end end
class IPCommand class IPCommand
@ -146,8 +158,20 @@ class NetworkCommands
raise "(ip) Cannot add ip address alias #{ip.to_string} to #{name}" raise "(ip) Cannot add ip address alias #{ip.to_string} to #{name}"
end end
end end
def self.mtu(name : String, mtu : Int32?)
unless Do.run("ip", [ "link", "set", "mtu", mtu.to_s, "dev", name ]).success?
raise "(ip) Cannot set mtu #{mtu} to #{name}"
end
end end
def self.description(name : String, description : String)
puts "TODO: (ip) setup description '#{description}' to interface #{name}"
# unless Do.run("ip", [ "link", "set", "description", description, "dev", name ]).success?
# raise "(ip) Cannot set description #{description} to #{name}"
# end
end
end
def self.interface_exists(name : String) def self.interface_exists(name : String)
@@cmd_network_configuration.interface_exists(name) @@cmd_network_configuration.interface_exists(name)
@ -179,6 +203,14 @@ class NetworkCommands
@@cmd_network_configuration.set_alias name, ip @@cmd_network_configuration.set_alias name, ip
end end
def self.mtu(name : String, mtu : Int32?)
@@cmd_network_configuration.mtu name, mtu
end
def self.description(name : String, description : String)
@@cmd_network_configuration.description name, description
end
def self.wireless_list_ssid(ifname : String) def self.wireless_list_ssid(ifname : String)
cmd = @@cmd_wireless_configuration cmd = @@cmd_wireless_configuration
case cmd case cmd
@ -232,6 +264,7 @@ class InterfaceConfiguration
property name : String property name : String
property up : Bool property up : Bool
property description : String? property description : String?
property mtu : Int32?
property wireless : Bool property wireless : Bool
property main_ip_v4 : IPAddress | DHCP | NotSetup property main_ip_v4 : IPAddress | DHCP | NotSetup
property main_ip_v6 : IPAddress | DHCP | NotSetup property main_ip_v6 : IPAddress | DHCP | NotSetup
@ -241,8 +274,10 @@ class InterfaceConfiguration
def initialize (@name, @up, def initialize (@name, @up,
@description, @description,
@mtu,
@main_ip_v4, @main_ip_v6, aliasses, @main_ip_v4, @main_ip_v6, aliasses,
@wireless, @wireless_networks) @wireless, @wireless_networks)
@aliasses_v4 = Array(IPAddress).new @aliasses_v4 = Array(IPAddress).new
@aliasses_v6 = Array(IPAddress).new @aliasses_v6 = Array(IPAddress).new
@ -267,10 +302,14 @@ class InterfaceConfiguration
str << "#{CRED}#{@name}#{CRESET}\n" str << "#{CRED}#{@name}#{CRESET}\n"
end end
str << "\t#{@up? "up" : "down"}\n"
description = @description
str << "\t#description #{description.not_nil!}\n" unless description.nil? str << "\t#description #{description.not_nil!}\n" unless description.nil?
str << "\t#{@up? "up" : "down"}\n"
unless mtu.nil?
str << "\tmtu #{mtu}\n"
end
# ipv4 # ipv4
str << "\tinet #{@main_ip_v4}\n" str << "\tinet #{@main_ip_v4}\n"
unless @aliasses_v4.empty? unless @aliasses_v4.empty?
@ -302,6 +341,16 @@ class InterfaceConfiguration
return return
end end
mtu = @mtu
unless mtu.nil?
NetworkCommands.mtu @name, mtu
end
description = @description
unless description.nil?
NetworkCommands.description @name, description
end
# ipv4 configuration # ipv4 configuration
@main_ip_v4.tap do |ip| @main_ip_v4.tap do |ip|
case ip case ip
@ -325,6 +374,7 @@ class InterfaceConfiguration
# ipv6 configuration # ipv6 configuration
@main_ip_v6.tap do |ip| @main_ip_v6.tap do |ip|
case ip case ip
when IPAddress when IPAddress
NetworkCommands.set_ip @name, ip NetworkCommands.set_ip @name, ip
@ -366,6 +416,7 @@ class NetworkConfigurationParser
def self.parse (ifname : String, data : String, wireless = false) : InterfaceConfiguration def self.parse (ifname : String, data : String, wireless = false) : InterfaceConfiguration
up = false up = false
description = nil description = nil
mtu = nil
main_ip_v4 = NotSetup.new main_ip_v4 = NotSetup.new
main_ip_v6 = NotSetup.new main_ip_v6 = NotSetup.new
@ -393,6 +444,9 @@ class NetworkConfigurationParser
else else
main_ip_v6 = InterfaceConfiguration::DHCP.new main_ip_v6 = InterfaceConfiguration::DHCP.new
end end
when /^inet6 autoconf/
# IPaddress is autoconfigured
puts "TODO: IPv6 autoconfiguration"
when /^inet6? .*/ when /^inet6? .*/
ipstr = /^inet6? ([a-f0-9:.\/]+)/.match(line).try &.[1] ipstr = /^inet6? ([a-f0-9:.\/]+)/.match(line).try &.[1]
if ipstr.nil? if ipstr.nil?
@ -413,6 +467,12 @@ class NetworkConfigurationParser
when /^network [^ ]+ dhcp/ when /^network [^ ]+ dhcp/
puts "TODO: network SSID dhcp" puts "TODO: network SSID dhcp"
when /^network [^ ]+ dns .*/
puts "TODO: network SSID dns"
when /^mtu [0-9]+/
mtu = /^mtu ([0-9]+)/.match(line).try &.[1].to_i
when /^#.*$/ when /^#.*$/
# simple comment # simple comment
when /^[ \t]*$/ when /^[ \t]*$/
@ -424,6 +484,7 @@ class NetworkConfigurationParser
InterfaceConfiguration.new(ifname, up, InterfaceConfiguration.new(ifname, up,
description, description,
mtu,
main_ip_v4, main_ip_v6, main_ip_v4, main_ip_v6,
aliasses, aliasses,
wireless, wireless_networks) wireless, wireless_networks)
@ -484,5 +545,6 @@ if file.nil?
raise "Cannot choose files yet" raise "Cannot choose files yet"
else else
# TODO: why having to force "not_nil!" ? Seems like a compiler bug # TODO: why having to force "not_nil!" ? Seems like a compiler bug
NetworkConfigurationParser.parse_file(file.not_nil!).execute network_configuration = NetworkConfigurationParser.parse_file(file.not_nil!)
network_configuration.execute
end end