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
12 * See README and COPYING for more details.
21 #include <sys/types.h>
23 #ifndef CONFIG_NATIVE_WINDOWS
24 #include <sys/socket.h>
25 #include <netinet/in.h>
27 #endif /* CONFIG_NATIVE_WINDOWS */
30 #ifdef CONFIG_NATIVE_WINDOWS
32 #endif /* CONFIG_NATIVE_WINDOWS */
36 * struct wpa_ctrl - Internal structure for control interface library
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.
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 */
56 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
58 struct wpa_ctrl *ctrl;
59 #ifndef CONFIG_CTRL_IFACE_UDP
60 static int counter = 0;
61 #endif /* CONFIG_CTRL_IFACE_UDP */
63 ctrl = malloc(sizeof(*ctrl));
66 memset(ctrl, 0, sizeof(*ctrl));
68 #ifdef CONFIG_CTRL_IFACE_UDP
69 ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
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) {
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) {
95 #else /* CONFIG_CTRL_IFACE_UDP */
96 ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
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) {
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) {
117 unlink(ctrl->local.sun_path);
121 #endif /* CONFIG_CTRL_IFACE_UDP */
127 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
129 #ifndef CONFIG_CTRL_IFACE_UDP
130 unlink(ctrl->local.sun_path);
131 #endif /* CONFIG_CTRL_IFACE_UDP */
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))
145 if (send(ctrl->s, cmd, cmd_len, 0) < 0)
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);
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
164 /* Make sure the message is nul
166 if ((size_t) res == *reply_len)
167 res = (*reply_len) - 1;
183 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
189 ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
193 if (len == 3 && memcmp(buf, "OK\n", 3) == 0)
199 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
201 return wpa_ctrl_attach_helper(ctrl, 1);
205 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
207 return wpa_ctrl_attach_helper(ctrl, 0);
211 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
215 res = recv(ctrl->s, reply, *reply_len, 0);
223 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
231 FD_SET(ctrl->s, &rfds);
232 res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
233 return FD_ISSET(ctrl->s, &rfds);
237 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)