]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
tests: Add UDP ctrl_iface support to wpasupplicant.py
authorJanusz Dziedzic <janusz.dziedzic@tieto.com>
Fri, 4 Mar 2016 09:20:34 +0000 (10:20 +0100)
committerJouni Malinen <j@w1.fi>
Sat, 5 Mar 2016 15:44:51 +0000 (17:44 +0200)
Allow use of a remote host using wpaspy.Ctrl with UDP ctrl_iface
support.

Signed-off-by: Janusz Dziedzic <janusz.dziedzic@tieto.com>
tests/hwsim/wpasupplicant.py

index 13a93eca15dcc493e7af23040d97d0ad8aafee88..e5a07e2b57fd257ebd82ad0cdb000fb972b851f2 100644 (file)
@@ -17,18 +17,24 @@ logger = logging.getLogger()
 wpas_ctrl = '/var/run/wpa_supplicant'
 
 class WpaSupplicant:
-    def __init__(self, ifname=None, global_iface=None):
+    def __init__(self, ifname=None, global_iface=None, hostname=None,
+                 port=9877, global_port=9878):
+        self.hostname = hostname
         self.group_ifname = None
         self.gctrl_mon = None
         if ifname:
-            self.set_ifname(ifname)
+            self.set_ifname(ifname, hostname, port)
         else:
             self.ifname = None
 
         self.global_iface = global_iface
         if global_iface:
-            self.global_ctrl = wpaspy.Ctrl(global_iface)
-            self.global_mon = wpaspy.Ctrl(global_iface)
+            if hostname != None:
+                self.global_ctrl = wpaspy.Ctrl(hostname, global_port)
+                self.global_mon = wpaspy.Ctrl(hostname, global_port)
+            else:
+                self.global_ctrl = wpaspy.Ctrl(global_iface)
+                self.global_mon = wpaspy.Ctrl(global_iface)
             self.global_mon.attach()
         else:
             self.global_mon = None
@@ -40,10 +46,14 @@ class WpaSupplicant:
             self.global_ctrl = None
         self.remove_ifname()
 
-    def set_ifname(self, ifname):
+    def set_ifname(self, ifname, hostname=None, port=9877):
         self.ifname = ifname
-        self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
-        self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
+        if hostname != None:
+            self.ctrl = wpaspy.Ctrl(hostname, port)
+            self.mon = wpaspy.Ctrl(hostname, port)
+        else:
+            self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
+            self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
         self.mon.attach()
 
     def remove_ifname(self):
@@ -53,6 +63,26 @@ class WpaSupplicant:
             self.ctrl = None
             self.ifname = None
 
+    def get_ctrl_iface_port(self, ifname):
+        if self.hostname is None:
+            return None
+
+        res = self.global_request("INTERFACES ctrl")
+        lines = res.splitlines()
+        found = False
+        for line in lines:
+            words = line.split()
+            if words[0] == ifname:
+                found = True
+                break
+        if not found:
+            raise Exception("Could not find UDP port for " + ifname)
+        res = line.find("ctrl_iface=udp:")
+        if res == -1:
+            raise Exception("Wrong ctrl_interface format")
+        words = line.split(":")
+        return int(words[1])
+
     def interface_add(self, ifname, config="", driver="nl80211",
                       drv_params=None, br_ifname=None, create=False,
                       set_ifname=True, all_params=False, if_type=None):
@@ -85,7 +115,8 @@ class WpaSupplicant:
         if "FAIL" in self.global_request(cmd):
             raise Exception("Failed to add a dynamic wpa_supplicant interface")
         if not create and set_ifname:
-            self.set_ifname(ifname)
+            port = self.get_ctrl_iface_port(ifname)
+            self.set_ifname(ifname, self.hostname, port)
 
     def interface_remove(self, ifname):
         self.remove_ifname()