wpaspy: Add support for TERMINATE command
[mech_eap.git] / wpaspy / wpaspy.py
1 #!/usr/bin/python
2 #
3 # wpa_supplicant/hostapd control interface using Python
4 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
5 #
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
8
9 import os
10 import stat
11 import socket
12 import select
13
14 counter = 0
15
16 class Ctrl:
17     def __init__(self, path, port=9877):
18         global counter
19         self.started = False
20         self.attached = False
21         self.path = path
22         self.port = port
23
24         try:
25             mode = os.stat(path).st_mode
26             if stat.S_ISSOCK(mode):
27                 self.udp = False
28             else:
29                 self.udp = True
30         except:
31             self.udp = True
32
33         if not self.udp:
34             self.s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
35             self.dest = path
36             self.local = "/tmp/wpa_ctrl_" + str(os.getpid()) + '-' + str(counter)
37             counter += 1
38             self.s.bind(self.local)
39             try:
40                 self.s.connect(self.dest)
41             except Exception, e:
42                 self.s.close()
43                 os.unlink(self.local)
44                 raise
45         else:
46             try:
47                 ai_list = socket.getaddrinfo(path, port, socket.AF_INET,
48                                              socket.SOCK_DGRAM)
49                 for af, socktype, proto, cn, sockaddr in ai_list:
50                     self.sockaddr = sockaddr
51                     break
52                 self.s = socket.socket(af, socktype)
53                 self.s.settimeout(5)
54                 self.s.sendto("GET_COOKIE", sockaddr)
55                 reply, server = self.s.recvfrom(4096)
56                 self.cookie = reply
57                 self.port = port
58             except:
59                 print "connect exception ", path, str(port)
60                 if self.s != None:
61                     self.s.close()
62                 raise
63         self.started = True
64
65     def __del__(self):
66         self.close()
67
68     def close(self):
69         if self.attached:
70             try:
71                 self.detach()
72             except Exception, e:
73                 # Need to ignore this allow the socket to be closed
74                 self.attached = False
75                 pass
76         if self.started:
77             self.s.close()
78             if not self.udp:
79                 os.unlink(self.local)
80             self.started = False
81
82     def request(self, cmd, timeout=10):
83         if self.udp:
84             self.s.sendto(self.cookie + cmd, self.sockaddr)
85         else:
86             self.s.send(cmd)
87         [r, w, e] = select.select([self.s], [], [], timeout)
88         if r:
89             return self.s.recv(4096)
90         raise Exception("Timeout on waiting response")
91
92     def attach(self):
93         if self.attached:
94             return None
95         res = self.request("ATTACH")
96         if "OK" in res:
97             self.attached = True
98             return None
99         raise Exception("ATTACH failed")
100
101     def detach(self):
102         if not self.attached:
103             return None
104         while self.pending():
105             ev = self.recv()
106         res = self.request("DETACH")
107         if "FAIL" not in res:
108             self.attached = False
109             return None
110         raise Exception("DETACH failed")
111
112     def terminate(self):
113         if self.attached:
114             try:
115                 self.detach()
116             except Exception, e:
117                 # Need to ignore this to allow the socket to be closed
118                 self.attached = False
119         self.request("TERMINATE")
120         self.close()
121
122     def pending(self, timeout=0):
123         [r, w, e] = select.select([self.s], [], [], timeout)
124         if r:
125             return True
126         return False
127
128     def recv(self):
129         res = self.s.recv(4096)
130         return res