]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/wpa_ctrl.c
This commit was generated by cvs2svn to compensate for changes in r157184,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / wpa_ctrl.c
1 /*
2  * wpa_supplicant/hostapd control interface library
3  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  *
14  * $FreeBSD$
15  */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/time.h>
23 #ifndef CONFIG_NATIVE_WINDOWS
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <sys/un.h>
27 #endif /* CONFIG_NATIVE_WINDOWS */
28
29 #include "wpa_ctrl.h"
30 #ifdef CONFIG_NATIVE_WINDOWS
31 #include "common.h"
32 #endif /* CONFIG_NATIVE_WINDOWS */
33
34
35 /**
36  * struct wpa_ctrl - Internal structure for control interface library
37  *
38  * This structure is used by the wpa_supplicant/hostapd control interface
39  * library to store internal data. Programs using the library should not touch
40  * this data directly. They can only use the pointer to the data structure as
41  * an identifier for the control interface connection and use this as one of
42  * the arguments for most of the control interface library functions.
43  */
44 struct wpa_ctrl {
45         int s;
46 #ifdef CONFIG_CTRL_IFACE_UDP
47         struct sockaddr_in local;
48         struct sockaddr_in dest;
49 #else /* CONFIG_CTRL_IFACE_UDP */
50         struct sockaddr_un local;
51         struct sockaddr_un dest;
52 #endif /* CONFIG_CTRL_IFACE_UDP */
53 };
54
55
56 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
57 {
58         struct wpa_ctrl *ctrl;
59 #ifndef CONFIG_CTRL_IFACE_UDP
60         static int counter = 0;
61 #endif /* CONFIG_CTRL_IFACE_UDP */
62
63         ctrl = malloc(sizeof(*ctrl));
64         if (ctrl == NULL)
65                 return NULL;
66         memset(ctrl, 0, sizeof(*ctrl));
67
68 #ifdef CONFIG_CTRL_IFACE_UDP
69         ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
70         if (ctrl->s < 0) {
71                 perror("socket");
72                 free(ctrl);
73                 return NULL;
74         }
75
76         ctrl->local.sin_family = AF_INET;
77         ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
78         if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
79                  sizeof(ctrl->local)) < 0) {
80                 close(ctrl->s);
81                 free(ctrl);
82                 return NULL;
83         }
84
85         ctrl->dest.sin_family = AF_INET;
86         ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
87         ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
88         if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
89                     sizeof(ctrl->dest)) < 0) {
90                 perror("connect");
91                 close(ctrl->s);
92                 free(ctrl);
93                 return NULL;
94         }
95 #else /* CONFIG_CTRL_IFACE_UDP */
96         ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
97         if (ctrl->s < 0) {
98                 free(ctrl);
99                 return NULL;
100         }
101
102         ctrl->local.sun_family = AF_UNIX;
103         snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path) - 1,
104                  "/tmp/wpa_ctrl_%d-%d", getpid(), counter++);
105         if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
106                     sizeof(ctrl->local)) < 0) {
107                 close(ctrl->s);
108                 free(ctrl);
109                 return NULL;
110         }
111
112         ctrl->dest.sun_family = AF_UNIX;
113         strncpy(ctrl->dest.sun_path, ctrl_path, sizeof(ctrl->dest.sun_path) - 1);
114         if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
115                     sizeof(ctrl->dest)) < 0) {
116                 close(ctrl->s);
117                 unlink(ctrl->local.sun_path);
118                 free(ctrl);
119                 return NULL;
120         }
121 #endif /* CONFIG_CTRL_IFACE_UDP */
122
123         return ctrl;
124 }
125
126
127 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
128 {
129 #ifndef CONFIG_CTRL_IFACE_UDP
130         unlink(ctrl->local.sun_path);
131 #endif /* CONFIG_CTRL_IFACE_UDP */
132         close(ctrl->s);
133         free(ctrl);
134 }
135
136
137 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
138                      char *reply, size_t *reply_len,
139                      void (*msg_cb)(char *msg, size_t len))
140 {
141         struct timeval tv;
142         int res;
143         fd_set rfds;
144
145         if (send(ctrl->s, cmd, cmd_len, 0) < 0)
146                 return -1;
147
148         for (;;) {
149                 tv.tv_sec = 2;
150                 tv.tv_usec = 0;
151                 FD_ZERO(&rfds);
152                 FD_SET(ctrl->s, &rfds);
153                 res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
154                 if (FD_ISSET(ctrl->s, &rfds)) {
155                         res = recv(ctrl->s, reply, *reply_len, 0);
156                         if (res < 0)
157                                 return res;
158                         if (res > 0 && reply[0] == '<') {
159                                 /* This is an unsolicited message from
160                                  * wpa_supplicant, not the reply to the
161                                  * request. Use msg_cb to report this to the
162                                  * caller. */
163                                 if (msg_cb) {
164                                         /* Make sure the message is nul
165                                          * terminated. */
166                                         if ((size_t) res == *reply_len)
167                                                 res = (*reply_len) - 1;
168                                         reply[res] = '\0';
169                                         msg_cb(reply, res);
170                                 }
171                                 continue;
172                         }
173                         *reply_len = res;
174                         break;
175                 } else {
176                         return -2;
177                 }
178         }
179         return 0;
180 }
181
182
183 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
184 {
185         char buf[10];
186         int ret;
187         size_t len = 10;
188
189         ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
190                                buf, &len, NULL);
191         if (ret < 0)
192                 return ret;
193         if (len == 3 && memcmp(buf, "OK\n", 3) == 0)
194                 return 0;
195         return -1;
196 }
197
198
199 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
200 {
201         return wpa_ctrl_attach_helper(ctrl, 1);
202 }
203
204
205 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
206 {
207         return wpa_ctrl_attach_helper(ctrl, 0);
208 }
209
210
211 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
212 {
213         int res;
214
215         res = recv(ctrl->s, reply, *reply_len, 0);
216         if (res < 0)
217                 return res;
218         *reply_len = res;
219         return 0;
220 }
221
222
223 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
224 {
225         struct timeval tv;
226         int res;
227         fd_set rfds;
228         tv.tv_sec = 0;
229         tv.tv_usec = 0;
230         FD_ZERO(&rfds);
231         FD_SET(ctrl->s, &rfds);
232         res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
233         return FD_ISSET(ctrl->s, &rfds);
234 }
235
236
237 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
238 {
239         return ctrl->s;
240 }