-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwpa_ctrl.c
More file actions
212 lines (167 loc) · 4.12 KB
/
wpa_ctrl.c
File metadata and controls
212 lines (167 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2025, Muhammad Saheed <saheed@FreeBSD.org>
*
* wpa_supplicant/hostapd control interface library
* Copyright (c) 2004-2017, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*
* NOTE: This contains code taken as is or modified from
* wpa_supplicant's src/common/wpa_ctrl.c
*/
#include <sys/socket.h>
#include <sys/un.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "wpa_ctrl.h"
struct wpa_ctrl {
int s;
char *sock_dir;
struct sockaddr_un local;
struct sockaddr_un dest;
};
struct wpa_ctrl *
wpa_ctrl_open(const char *ctrl_path)
{
return (wpa_ctrl_open2(ctrl_path, NULL));
}
struct wpa_ctrl *
wpa_ctrl_open2(const char *ctrl_path, const char *cli_path)
{
struct wpa_ctrl *ctrl = NULL;
if (ctrl_path == NULL)
return (NULL);
ctrl = calloc(1, sizeof(*ctrl));
if (ctrl == NULL)
return (NULL);
ctrl->s = socket(AF_LOCAL, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
0);
if (ctrl->s == -1)
goto failure;
asprintf(&ctrl->sock_dir, "%s/wpa_ctrl_XXXXXX",
cli_path == NULL ? "/tmp" : cli_path);
if (ctrl->sock_dir == NULL)
goto failure;
if (mkdtemp(ctrl->sock_dir) == NULL)
goto failure;
ctrl->local.sun_family = AF_UNIX;
if (snprintf(ctrl->local.sun_path, SUNPATHLEN, "%s/sock",
ctrl->sock_dir) >= SUNPATHLEN)
goto failure;
ctrl->local.sun_len = SUN_LEN(&ctrl->local);
if (bind(ctrl->s, (struct sockaddr *)&ctrl->local,
ctrl->local.sun_len) == -1)
goto failure;
ctrl->dest.sun_family = AF_UNIX;
if (strlcpy(ctrl->dest.sun_path, ctrl_path, SUNPATHLEN) >= SUNPATHLEN)
goto failure;
ctrl->dest.sun_len = SUN_LEN(&ctrl->dest);
if (connect(ctrl->s, (struct sockaddr *)&ctrl->dest,
ctrl->dest.sun_len) == -1)
goto failure;
return (ctrl);
failure:
wpa_ctrl_close(ctrl);
return (NULL);
}
void
wpa_ctrl_close(struct wpa_ctrl *ctrl)
{
if (ctrl == NULL)
return;
close(ctrl->s);
if (ctrl->local.sun_path[0] != '\0')
unlink(ctrl->local.sun_path);
if (ctrl->sock_dir != NULL) {
rmdir(ctrl->sock_dir);
free(ctrl->sock_dir);
}
free(ctrl);
}
int
wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
char *reply, size_t *reply_len, void (*msg_cb)(char *msg, size_t len))
{
if (send(ctrl->s, cmd, cmd_len, 0) == -1)
return (-1);
for (;;) {
struct pollfd pfd = { .fd = ctrl->s, .events = POLLIN };
int nev = poll(&pfd, 1, 1000);
size_t recv_len = *reply_len;
if (nev == -1 || nev == 0)
return (-1);
if ((pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) != 0)
return (-1);
if (wpa_ctrl_recv(ctrl, reply, &recv_len) != 0)
return (-1);
/* unsolicited message */
if ((recv_len > 0 && reply[0] == '<') ||
(recv_len > 6 && strncmp(reply, "IFNAME=", 7) == 0)) {
if (msg_cb != NULL) {
if (recv_len == *reply_len)
recv_len = *reply_len - 1;
reply[recv_len] = '\0';
msg_cb(reply, recv_len);
}
continue;
}
*reply_len = recv_len;
break;
}
return (0);
}
static int
wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
{
const char expected[] = "OK";
size_t expected_len = sizeof(expected) - 1, len = expected_len;
char buf[expected_len];
const char *cmd = attach ? "ATTACH" : "DETACH";
int ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL);
if (ret < 0)
return (ret);
if (strncmp(buf, expected, expected_len) == 0)
return (0);
return (-1);
}
int
wpa_ctrl_attach(struct wpa_ctrl *ctrl)
{
return (wpa_ctrl_attach_helper(ctrl, 1));
}
int
wpa_ctrl_detach(struct wpa_ctrl *ctrl)
{
return (wpa_ctrl_attach_helper(ctrl, 0));
}
int
wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
{
ssize_t len = recv(ctrl->s, reply, *reply_len, 0);
if (len == -1)
return (-1);
*reply_len = len;
return (0);
}
int
wpa_ctrl_pending(struct wpa_ctrl *ctrl)
{
struct pollfd pfd = { .fd = ctrl->s, .events = POLLIN };
int ret = poll(&pfd, 1, 0);
if (ret == -1)
return (-1);
if (ret > 0 && pfd.revents & POLLIN)
return (1);
return (0);
}
int
wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
{
return (ctrl->s);
}