diff --git a/src/main.cr b/src/main.cr index 9aa5ca0..6e9403b 100644 --- a/src/main.cr +++ b/src/main.cr @@ -116,6 +116,18 @@ class NetworkCommands raise "(ifconfig) Cannot set ip address alias #{ip.to_string} for #{name}" 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 class IPCommand @@ -146,8 +158,20 @@ class NetworkCommands raise "(ip) Cannot add ip address alias #{ip.to_string} to #{name}" 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 + + 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) @@cmd_network_configuration.interface_exists(name) @@ -179,6 +203,14 @@ class NetworkCommands @@cmd_network_configuration.set_alias name, ip 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) cmd = @@cmd_wireless_configuration case cmd @@ -232,6 +264,7 @@ class InterfaceConfiguration property name : String property up : Bool property description : String? + property mtu : Int32? property wireless : Bool property main_ip_v4 : IPAddress | DHCP | NotSetup property main_ip_v6 : IPAddress | DHCP | NotSetup @@ -241,8 +274,10 @@ class InterfaceConfiguration def initialize (@name, @up, @description, + @mtu, @main_ip_v4, @main_ip_v6, aliasses, @wireless, @wireless_networks) + @aliasses_v4 = Array(IPAddress).new @aliasses_v6 = Array(IPAddress).new @@ -267,10 +302,14 @@ class InterfaceConfiguration str << "#{CRED}#{@name}#{CRESET}\n" end - str << "\t#{@up? "up" : "down"}\n" - description = @description 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 str << "\tinet #{@main_ip_v4}\n" unless @aliasses_v4.empty? @@ -302,6 +341,16 @@ class InterfaceConfiguration return end + mtu = @mtu + unless mtu.nil? + NetworkCommands.mtu @name, mtu + end + + description = @description + unless description.nil? + NetworkCommands.description @name, description + end + # ipv4 configuration @main_ip_v4.tap do |ip| case ip @@ -325,6 +374,7 @@ class InterfaceConfiguration # ipv6 configuration @main_ip_v6.tap do |ip| + case ip when IPAddress NetworkCommands.set_ip @name, ip @@ -366,6 +416,7 @@ class NetworkConfigurationParser def self.parse (ifname : String, data : String, wireless = false) : InterfaceConfiguration up = false description = nil + mtu = nil main_ip_v4 = NotSetup.new main_ip_v6 = NotSetup.new @@ -393,6 +444,9 @@ class NetworkConfigurationParser else main_ip_v6 = InterfaceConfiguration::DHCP.new end + when /^inet6 autoconf/ + # IPaddress is autoconfigured + puts "TODO: IPv6 autoconfiguration" when /^inet6? .*/ ipstr = /^inet6? ([a-f0-9:.\/]+)/.match(line).try &.[1] if ipstr.nil? @@ -413,6 +467,12 @@ class NetworkConfigurationParser when /^network [^ ]+ 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 /^#.*$/ # simple comment when /^[ \t]*$/ @@ -424,6 +484,7 @@ class NetworkConfigurationParser InterfaceConfiguration.new(ifname, up, description, + mtu, main_ip_v4, main_ip_v6, aliasses, wireless, wireless_networks) @@ -484,5 +545,6 @@ if file.nil? raise "Cannot choose files yet" else # 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