access point connection

dev
Philippe PITTOLI 2019-10-25 14:26:46 +02:00
parent e101913e5d
commit 553cd0a986
3 changed files with 28 additions and 21 deletions

View File

@ -19,6 +19,7 @@ end
class WirelessAPSetup class WirelessAPSetup
property ifname : String
property ssid : String property ssid : String
# This is a list of parameters that should be unique to each AP # This is a list of parameters that should be unique to each AP
@ -44,7 +45,7 @@ class WirelessAPSetup
end end
end end
def initialize(@ssid, @security) def initialize(@ifname, @ssid, @security)
@main_ip_v4 = NotSetup.new @main_ip_v4 = NotSetup.new
@main_ip_v6 = NotSetup.new @main_ip_v6 = NotSetup.new
@aliasses_v4 = Array(IPAddress).new @aliasses_v4 = Array(IPAddress).new
@ -95,6 +96,8 @@ class WirelessAPSetup
def execute def execute
puts "TODO: wireless configuration" puts "TODO: wireless configuration"
NetworkCommands.wireless_access_point_connection ifname, ssid
# DNS configuration # DNS configuration
dns.execute dns.execute
end end
@ -111,7 +114,6 @@ class InterfaceConfiguration
property up : Bool property up : Bool
property description : String? property description : String?
property mtu : Int32? property mtu : Int32?
property wireless : Bool
property main_ip_v4 : IPAddress | DHCP | NotSetup property main_ip_v4 : IPAddress | DHCP | NotSetup
property main_ip_v6 : IPAddress | Autoconfiguration | DHCP | NotSetup property main_ip_v6 : IPAddress | Autoconfiguration | DHCP | NotSetup
property aliasses_v4 : Array(IPAddress) property aliasses_v4 : Array(IPAddress)
@ -123,7 +125,7 @@ class InterfaceConfiguration
@description, @description,
@mtu, @mtu,
@main_ip_v4, @main_ip_v6, aliasses, @main_ip_v4, @main_ip_v6, aliasses,
@wireless, @wireless_networks, @wireless_networks,
@dns @dns
) )
@ -258,7 +260,7 @@ class InterfaceConfiguration
end end
# TODO: treat differently wireless and non-wireless interfaces # TODO: treat differently wireless and non-wireless interfaces
if @wireless if @wireless_networks.empty?
puts "interface #{name} is wireless: connection to the access point" puts "interface #{name} is wireless: connection to the access point"
store_access_point_keys store_access_point_keys
access_point_connection access_point_connection

View File

@ -89,13 +89,22 @@ class NetworkCommands
end end
end end
def self.connection(ssid : String, ifname : String) def self.connection(ifname : String, ssid : String)
dirpath = "#{Context.root}/#{Context.keydir}" dirpath = "#{Context.root}/#{Context.keydir}"
unless Do.run("wpa_supplicant", unless Do.run("wpa_supplicant",
[ "-B", "-c", "#{dirpath}/#{ssid}.conf", "-i", ifname ]).success? [ "-B", "-c", "#{dirpath}/#{ssid}.conf", "-i", ifname ]).success?
raise "(wpa_supplicant) cannot connect to the wireless AP #{ssid} via the interface #{ifname}" raise "(wpa_supplicant) cannot connect to the wireless AP #{ssid} via the interface #{ifname}"
end end
end end
def self.disconnection(ifname : String, ssid : String)
# TODO: this kills every running wpa_supplicant application
# not only the instance for ifname
dirpath = "#{Context.root}/#{Context.keydir}"
unless Do.run("pkill", [ "wpa_supplicant" ]).success?
raise "(wpa_supplicant) cannot stop the wpa_supplicant daemon"
end
end
end end
class UDHCPCCommand class UDHCPCCommand
@ -320,18 +329,16 @@ class NetworkCommands
end end
end end
def self.wireless_connect_wpa_psk(ifname : String, ssid : String, passwd : String)
cmd = @@cmd_wireless_configuration
case cmd
when NotSetup.class
puts "no wireless configuration program: cannot connect to ssid #{ssid}"
else
cmd.list_ssid ifname
end
end
def self.store_access_point_keys(ssid : String, security : WirelessAPSetup::WPA) def self.store_access_point_keys(ssid : String, security : WirelessAPSetup::WPA)
# TODO: only one way to do it # TODO: only one way to do it
NetworkCommands::WPASupplicant.passphrase ssid, security.key NetworkCommands::WPASupplicant.passphrase ssid, security.key
end end
def self.wireless_access_point_connection(ifname : String, ssid : String)
NetworkCommands::WPASupplicant.connection ifname, ssid
end
def self.wireless_access_point_disconnection(ifname : String, ssid : String)
NetworkCommands::WPASupplicant.disconnection ifname, ssid
end
end end

View File

@ -8,12 +8,10 @@ class NetworkConfigurationParser
raise "The interface name is not known from the filename: '#{file_name}'" raise "The interface name is not known from the filename: '#{file_name}'"
end end
wireless = false self.parse(ifname.not_nil!, content)
wireless = true unless /^wl[0-9]+$/.match(ifname)
self.parse(ifname.not_nil!, content, wireless)
end end
def self.parse (ifname : String, data : String, wireless = false) : InterfaceConfiguration def self.parse (ifname : String, data : String) : InterfaceConfiguration
up = false up = false
description = nil description = nil
mtu = nil mtu = nil
@ -77,7 +75,7 @@ class NetworkConfigurationParser
next next
end end
new_ap = WirelessAPSetup.new ssid, WirelessAPSetup::WPA.new(wpakeystr) new_ap = WirelessAPSetup.new ifname, ssid, WirelessAPSetup::WPA.new(wpakeystr)
wireless_networks[ssid] = new_ap wireless_networks[ssid] = new_ap
when /^network (?<ssid>[^ \t]+) inet6 autoconf/ when /^network (?<ssid>[^ \t]+) inet6 autoconf/
@ -180,7 +178,7 @@ class NetworkConfigurationParser
mtu, mtu,
main_ip_v4, main_ip_v6, main_ip_v4, main_ip_v6,
aliasses, aliasses,
wireless, wireless_networks, wireless_networks,
dns) dns)
end end
end end