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