class NotSetup def to_s(io : IO) io << "not setup" end end class Autoconfiguration def to_s(io : IO) io << "autoconfiguration" end end class DHCP def to_s(io : IO) io << "dhcp" end end class WirelessAPSetup property ssid : String # This is a list of parameters that should be unique to each AP property up : Bool property description : String? property mtu : Int32? property main_ip_v4 : IPAddress | DHCP | NotSetup property main_ip_v6 : IPAddress | DHCP | Autoconfiguration | NotSetup property aliasses_v4 : Array(IPAddress) property aliasses_v6 : Array(IPAddress) property dns : Array(IPAddress) # we currently only support WPA2-PSK wireless security mechanism property security : WPA class WPA property key : String def initialize(@key) end end def initialize(@ssid, @security) @main_ip_v4 = NotSetup.new @main_ip_v6 = NotSetup.new @aliasses_v4 = Array(IPAddress).new @aliasses_v6 = Array(IPAddress).new @up = true @dns = Array(IPAddress).new end def to_s(io : IO) io << to_string end def to_string String.build do |str| str << "\t#{CBLUE}#{ssid}#{CRESET}\n" str << "\t\t#description #{description.not_nil!}\n" unless description.nil? str << "\t\t#{@up? "up" : "down"}\n" str << "\t\tmtu #{mtu}\n" unless mtu.nil? # ipv4 unless main_ip_v4.is_a?(NotSetup) str << "\t\tinet #{main_ip_v4}\n" aliasses_v4.each do |a| str << "\t\talias #{a}\n" end end # ipv6 unless main_ip_v6.is_a?(NotSetup) str << "\t\tinet6 #{main_ip_v6}\n" @aliasses_v6.each do |a| str << "\t\talias6 #{a}\n" end end dns.each do |ip| str << "\t\tdns: #{ip}\n" end # to improve readability str << "\n" end end end # # interface configuration # 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 | Autoconfiguration | DHCP | NotSetup property aliasses_v4 : Array(IPAddress) property aliasses_v6 : Array(IPAddress) property wireless_networks : Hash(String, WirelessAPSetup) property dns : Array(IPAddress) def initialize (@name, @up, @description, @mtu, @main_ip_v4, @main_ip_v6, aliasses, @wireless, @wireless_networks, @dns = Array(IPAddress).new ) @aliasses_v4 = Array(IPAddress).new @aliasses_v6 = Array(IPAddress).new aliasses.each do |ip| if ip.ipv4? aliasses_v4 << ip else aliasses_v6 << ip end end end def to_s(io : IO) io << to_string end def to_string String.build do |str| if NetworkCommands.interface_exists(@name) str << "#{CGREEN}#{@name}#{CRESET}\n" else str << "#{CRED}#{@name}#{CRESET}\n" end str << "\tdescription: '#{description.not_nil!}'\n" unless description.nil? str << "\t#{@up? "up" : "down"}\n" unless mtu.nil? str << "\tmtu #{mtu}\n" end # ipv4 unless main_ip_v4.is_a?(NotSetup) str << "\tinet #{main_ip_v4}\n" aliasses_v4.each do |a| str << "\talias #{a}\n" end end # warning: alias but no main ip address if main_ip_v4.is_a?(NotSetup) && ! aliasses_v4.empty? str << "\t#{CRED}alias configured but no main ipv4 configuration.#{CRESET}\n" str << "\t#{CRED}Should main ipv4 be obtained from DHCP? Static configuration?#{CRESET}\n" end # ipv6 unless main_ip_v6.is_a?(NotSetup) str << "\tinet6 #{main_ip_v6}\n" aliasses_v6.each do |a| str << "\talias6 #{a}\n" end end # warning: alias but no main ip address if main_ip_v6.is_a?(NotSetup) && ! aliasses_v6.empty? str << "\t#{CRED}alias6 configured but no main ipv6 configuration.#{CRESET}\n" str << "\t#{CRED}Should main ipv6 be obtained from autoconfiguration? DHCP? Static configuration?#{CRESET}\n" end unless dns.empty? dns.each do |ip| str << "\tdns: #{ip}\n" end end unless wireless_networks.empty? # to improve readability str << "\n" wireless_networks.each do |k,v| str << v end end end end # configure the interface def execute unless NetworkCommands.interface_exists(@name) raise "The interface #{@name} doesn't exists, yet." end # TODO: treat differently wireless and non-wireless interfaces if @wireless puts "interface #{name} is wireless" puts "TODO:" puts "1. scan for SSID" puts "2. select configured SSID then try to connect" puts "3. connectivity check with the gateway" else puts "interface #{name} is not wireless" end if up NetworkCommands.up name else puts "not marked as 'up' -- ending here" return end unless mtu.nil? NetworkCommands.mtu name, mtu end unless description.nil? NetworkCommands.description name, description.not_nil! end # ipv4 configuration main_ip_v4.tap do |ip| case ip when IPAddress NetworkCommands.set_ip name, ip when DHCP NetworkCommands.dhcp name when NotSetup # do nothing else raise "ipv4 configuration: neither static nor dynamic" end # We wont setup aliasses unless there is an actual IP address if ip != NotSetup aliasses_v4.each do |ip_alias| NetworkCommands.set_alias name, ip_alias end end end # ipv6 configuration main_ip_v6.tap do |ip| case ip when IPAddress NetworkCommands.set_ip name, ip when Autoconfiguration NetworkCommands.autoconfiguration name when DHCP NetworkCommands.dhcp6 name when NotSetup # do nothing else raise "ipv6 configuration: neither static nor dynamic" end # We wont setup aliasses unless there is an actual IP address if ip != NotSetup aliasses_v6.each do |ip_alias| NetworkCommands.set_alias name, ip_alias end end end end def down unless NetworkCommands.interface_exists(@name) raise "The interface #{@name} doesn't exists or is already down." end NetworkCommands.flush name NetworkCommands.down name end def scan unless NetworkCommands.interface_exists(@name) raise "The interface #{@name} doesn't exists or is already down." end ssid_list = NetworkCommands.scan name pp! ssid_list end end