Re-enable hardware UDP/TCP checksum calculation with pseudo header on
[dragonfly/port-amd64.git] / contrib / wpa_supplicant-0.4.9 / wpa_ctrl.c
blob98e0669a4738efd44e3112e7a4243438ac8ff51f
1 /*
2 * wpa_supplicant/hostapd control interface library
3 * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
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 */
27 #include "wpa_ctrl.h"
28 #ifdef CONFIG_NATIVE_WINDOWS
29 #include "common.h"
30 #endif /* CONFIG_NATIVE_WINDOWS */
33 /**
34 * struct wpa_ctrl - Internal structure for control interface library
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.
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 */
54 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
56 struct wpa_ctrl *ctrl;
57 #ifndef CONFIG_CTRL_IFACE_UDP
58 static int counter = 0;
59 #endif /* CONFIG_CTRL_IFACE_UDP */
61 ctrl = malloc(sizeof(*ctrl));
62 if (ctrl == NULL)
63 return NULL;
64 memset(ctrl, 0, sizeof(*ctrl));
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;
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;
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;
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;
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;
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;
120 #endif /* CONFIG_CTRL_IFACE_UDP */
122 return ctrl;
126 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
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);
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))
140 struct timeval tv;
141 int res;
142 fd_set rfds;
144 if (send(ctrl->s, cmd, cmd_len, 0) < 0)
145 return -1;
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);
170 continue;
172 *reply_len = res;
173 break;
174 } else {
175 return -2;
178 return 0;
182 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
184 char buf[10];
185 int ret;
186 size_t len = 10;
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;
198 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
200 return wpa_ctrl_attach_helper(ctrl, 1);
204 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
206 return wpa_ctrl_attach_helper(ctrl, 0);
210 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
212 int res;
214 res = recv(ctrl->s, reply, *reply_len, 0);
215 if (res < 0)
216 return res;
217 *reply_len = res;
218 return 0;
222 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
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);
236 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
238 return ctrl->s;