From 553cd0a986c0aeb17a2a52f5472a02b1d5029767 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Fri, 25 Oct 2019 14:26:46 +0200
Subject: [PATCH] access point connection
---
src/configuration.cr | 10 ++++++----
src/network_commands.cr | 29 ++++++++++++++++++-----------
src/network_configuration_parser.cr | 10 ++++------
3 files changed, 28 insertions(+), 21 deletions(-)
diff --git a/src/configuration.cr b/src/configuration.cr
index 7838309..d20c1b0 100644
--- a/src/configuration.cr
+++ b/src/configuration.cr
@@ -19,6 +19,7 @@ end
class WirelessAPSetup
+ property ifname : String
property ssid : String
# This is a list of parameters that should be unique to each AP
@@ -44,7 +45,7 @@ class WirelessAPSetup
end
end
- def initialize(@ssid, @security)
+ def initialize(@ifname, @ssid, @security)
@main_ip_v4 = NotSetup.new
@main_ip_v6 = NotSetup.new
@aliasses_v4 = Array(IPAddress).new
@@ -95,6 +96,8 @@ class WirelessAPSetup
def execute
puts "TODO: wireless configuration"
+ NetworkCommands.wireless_access_point_connection ifname, ssid
+
# DNS configuration
dns.execute
end
@@ -111,7 +114,6 @@ class InterfaceConfiguration
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)
@@ -123,7 +125,7 @@ class InterfaceConfiguration
@description,
@mtu,
@main_ip_v4, @main_ip_v6, aliasses,
- @wireless, @wireless_networks,
+ @wireless_networks,
@dns
)
@@ -258,7 +260,7 @@ class InterfaceConfiguration
end
# TODO: treat differently wireless and non-wireless interfaces
- if @wireless
+ if @wireless_networks.empty?
puts "interface #{name} is wireless: connection to the access point"
store_access_point_keys
access_point_connection
diff --git a/src/network_commands.cr b/src/network_commands.cr
index 278d8e0..9c8cf3c 100644
--- a/src/network_commands.cr
+++ b/src/network_commands.cr
@@ -89,13 +89,22 @@ class NetworkCommands
end
end
- def self.connection(ssid : String, ifname : String)
+ def self.connection(ifname : String, ssid : String)
dirpath = "#{Context.root}/#{Context.keydir}"
unless Do.run("wpa_supplicant",
[ "-B", "-c", "#{dirpath}/#{ssid}.conf", "-i", ifname ]).success?
raise "(wpa_supplicant) cannot connect to the wireless AP #{ssid} via the interface #{ifname}"
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
class UDHCPCCommand
@@ -320,18 +329,16 @@ class NetworkCommands
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)
# TODO: only one way to do it
NetworkCommands::WPASupplicant.passphrase ssid, security.key
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
diff --git a/src/network_configuration_parser.cr b/src/network_configuration_parser.cr
index 5eb550e..c08286e 100644
--- a/src/network_configuration_parser.cr
+++ b/src/network_configuration_parser.cr
@@ -8,12 +8,10 @@ class NetworkConfigurationParser
raise "The interface name is not known from the filename: '#{file_name}'"
end
- wireless = false
- wireless = true unless /^wl[0-9]+$/.match(ifname)
- self.parse(ifname.not_nil!, content, wireless)
+ self.parse(ifname.not_nil!, content)
end
- def self.parse (ifname : String, data : String, wireless = false) : InterfaceConfiguration
+ def self.parse (ifname : String, data : String) : InterfaceConfiguration
up = false
description = nil
mtu = nil
@@ -77,7 +75,7 @@ class NetworkConfigurationParser
next
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
when /^network (?[^ \t]+) inet6 autoconf/
@@ -180,7 +178,7 @@ class NetworkConfigurationParser
mtu,
main_ip_v4, main_ip_v6,
aliasses,
- wireless, wireless_networks,
+ wireless_networks,
dns)
end
end