aboutsummaryrefslogtreecommitdiffstats
path: root/wpaspy/wpaspy.py
diff options
context:
space:
mode:
authorJanusz Dziedzic <janusz.dziedzic@tieto.com>2016-03-04 09:20:32 (GMT)
committerJouni Malinen <j@w1.fi>2016-03-05 15:44:51 (GMT)
commita2c88a8025b2d76718e862489c37c5252ffc4a67 (patch)
tree8560e3d517b7287e674c199cf2dacfbbcf0ce11d /wpaspy/wpaspy.py
parent3e6717184695d49995548c6b59e40ecbac767347 (diff)
downloadhostap-a2c88a8025b2d76718e862489c37c5252ffc4a67.zip
hostap-a2c88a8025b2d76718e862489c37c5252ffc4a67.tar.gz
hostap-a2c88a8025b2d76718e862489c37c5252ffc4a67.tar.bz2
wpaspy: Add support for UDP connection
hostname and port can now be specified when using wpaspy.Ctrl, so we can connect to remote clients now. This can also be tested using test.py application with ./test.py <hostname> <port> Signed-off-by: Janusz Dziedzic <janusz.dziedzic@tieto.com>
Diffstat (limited to 'wpaspy/wpaspy.py')
-rw-r--r--wpaspy/wpaspy.py62
1 files changed, 49 insertions, 13 deletions
diff --git a/wpaspy/wpaspy.py b/wpaspy/wpaspy.py
index 2f57d74..861bee6 100644
--- a/wpaspy/wpaspy.py
+++ b/wpaspy/wpaspy.py
@@ -7,27 +7,59 @@
# See README for more details.
import os
+import stat
import socket
import select
counter = 0
class Ctrl:
- def __init__(self, path):
+ def __init__(self, path, port=9877):
global counter
self.started = False
self.attached = False
- self.s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
- self.dest = path
- self.local = "/tmp/wpa_ctrl_" + str(os.getpid()) + '-' + str(counter)
- counter += 1
- self.s.bind(self.local)
+ self.path = path
+ self.port = port
+
try:
- self.s.connect(self.dest)
- except Exception, e:
- self.s.close()
- os.unlink(self.local)
- raise
+ mode = os.stat(path).st_mode
+ if stat.S_ISSOCK(mode):
+ self.udp = False
+ else:
+ self.udp = True
+ except:
+ self.udp = True
+
+ if not self.udp:
+ self.s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
+ self.dest = path
+ self.local = "/tmp/wpa_ctrl_" + str(os.getpid()) + '-' + str(counter)
+ counter += 1
+ self.s.bind(self.local)
+ try:
+ self.s.connect(self.dest)
+ except Exception, e:
+ self.s.close()
+ os.unlink(self.local)
+ raise
+ else:
+ try:
+ ai_list = socket.getaddrinfo(path, port, socket.AF_INET,
+ socket.SOCK_DGRAM)
+ for af, socktype, proto, cn, sockaddr in ai_list:
+ self.sockaddr = sockaddr
+ break
+ self.s = socket.socket(af, socktype)
+ self.s.settimeout(5)
+ self.s.sendto("GET_COOKIE", sockaddr)
+ reply, server = self.s.recvfrom(4096)
+ self.cookie = reply
+ self.port = port
+ except:
+ print "connect exception ", path, str(port)
+ if self.s != None:
+ self.s.close()
+ raise
self.started = True
def __del__(self):
@@ -43,11 +75,15 @@ class Ctrl:
pass
if self.started:
self.s.close()
- os.unlink(self.local)
+ if not self.udp:
+ os.unlink(self.local)
self.started = False
def request(self, cmd, timeout=10):
- self.s.send(cmd)
+ if self.udp:
+ self.s.sendto(self.cookie + cmd, self.sockaddr)
+ else:
+ self.s.send(cmd)
[r, w, e] = select.select([self.s], [], [], timeout)
if r:
return self.s.recv(4096)