-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.c
311 lines (252 loc) · 7.75 KB
/
client.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (C) 2012-2015 B.A.T.M.A.N. contributors:
*
* Simon Wunderlich
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <stddef.h>
#include <sys/socket.h>
#include "alfred.h"
#include "packet.h"
int alfred_client_request_data(struct globals *globals)
{
unsigned char buf[MAX_PAYLOAD], *pos;
struct alfred_request_v0 *request;
struct alfred_push_data_v0 *push;
struct alfred_status_v0 *status;
struct alfred_tlv *tlv;
struct alfred_data *data;
int ret, len, data_len, i;
const size_t buf_data_len = sizeof(buf) - sizeof(*push) - sizeof(*data);
if (unix_sock_open_client(globals))
return -1;
request = (struct alfred_request_v0 *)buf;
len = sizeof(*request);
request->header.type = ALFRED_REQUEST;
request->header.version = ALFRED_VERSION;
request->header.length = sizeof(*request) - sizeof(request->header);
request->header.length = htons(request->header.length);
request->requested_type = globals->clientmode_arg;
request->tx_id = get_random_id();
ret = write(globals->unix_sock, buf, len);
if (ret != len)
fprintf(stderr, "%s: only wrote %d of %d bytes: %s\n",
__func__, ret, len, strerror(errno));
push = (struct alfred_push_data_v0 *)buf;
tlv = (struct alfred_tlv *)buf;
while ((ret = read(globals->unix_sock, buf, sizeof(*tlv))) > 0) {
if (ret < (int)sizeof(*tlv))
break;
if (tlv->type == ALFRED_STATUS_ERROR)
goto recv_err;
if (tlv->type != ALFRED_PUSH_DATA)
break;
/* read the rest of the header */
ret = read(globals->unix_sock, buf + sizeof(*tlv),
sizeof(*push) - sizeof(*tlv));
/* too short */
if (ret < (int)(sizeof(*push) - (int)sizeof(*tlv)))
break;
/* read the rest of the header */
ret = read(globals->unix_sock, buf + sizeof(*push),
sizeof(*data));
if (ret < (ssize_t)sizeof(*data))
break;
data = push->data;
data_len = ntohs(data->header.length);
/* would it fit? it should! */
if (data_len > (int)buf_data_len)
break;
/* read the data */
ret = read(globals->unix_sock,
buf + sizeof(*push) + sizeof(*data), data_len);
/* again too short */
if (ret < data_len)
break;
pos = data->data;
printf("{ \"%02x:%02x:%02x:%02x:%02x:%02x\", \"",
data->source[0], data->source[1],
data->source[2], data->source[3],
data->source[4], data->source[5]);
for (i = 0; i < data_len; i++) {
if (pos[i] == '"')
printf("\\\"");
else if (pos[i] == '\\')
printf("\\\\");
else if (!isprint(pos[i]))
printf("\\x%02x", pos[i]);
else
printf("%c", pos[i]);
}
printf("\"");
if (globals->verbose)
printf(", %u", data->header.version);
printf(" },\n");
}
unix_sock_close(globals);
return 0;
recv_err:
/* read the rest of the status message */
ret = read(globals->unix_sock, buf + sizeof(*tlv),
sizeof(*status) - sizeof(*tlv));
/* too short */
if (ret < (int)(sizeof(*status) - sizeof(*tlv)))
return -1;
status = (struct alfred_status_v0 *)buf;
fprintf(stderr, "Request failed with %d\n", status->tx.seqno);
return status->tx.seqno;
}
int alfred_client_set_data(struct globals *globals)
{
unsigned char buf[MAX_PAYLOAD];
struct alfred_push_data_v0 *push;
struct alfred_data *data;
int ret, len;
if (unix_sock_open_client(globals))
return -1;
push = (struct alfred_push_data_v0 *)buf;
data = push->data;
len = sizeof(*push) + sizeof(*data);
while (!feof(stdin)) {
ret = fread(&buf[len], 1, sizeof(buf) - len, stdin);
len += ret;
if (sizeof(buf) == len)
break;
}
push->header.type = ALFRED_PUSH_DATA;
push->header.version = ALFRED_VERSION;
push->header.length = htons(len - sizeof(push->header));
push->tx.id = get_random_id();
push->tx.seqno = htons(0);
/* we leave data->source "empty" */
memset(data->source, 0, sizeof(data->source));
data->header.type = globals->clientmode_arg;
data->header.version = globals->clientmode_version;
data->header.length = htons(len - sizeof(*push) - sizeof(*data));
ret = write(globals->unix_sock, buf, len);
if (ret != len)
fprintf(stderr, "%s: only wrote %d of %d bytes: %s\n",
__func__, ret, len, strerror(errno));
unix_sock_close(globals);
return 0;
}
int alfred_client_modeswitch(struct globals *globals)
{
unsigned char buf[MAX_PAYLOAD];
struct alfred_modeswitch_v0 *modeswitch;
int ret, len;
if (unix_sock_open_client(globals))
return -1;
modeswitch = (struct alfred_modeswitch_v0 *)buf;
len = sizeof(*modeswitch);
modeswitch->header.type = ALFRED_MODESWITCH;
modeswitch->header.version = ALFRED_VERSION;
modeswitch->header.length = htons(len - sizeof(modeswitch->header));
switch (globals->opmode) {
case OPMODE_SLAVE:
modeswitch->mode = ALFRED_MODESWITCH_SLAVE;
break;
case OPMODE_MASTER:
modeswitch->mode = ALFRED_MODESWITCH_MASTER;
break;
default:
fprintf(stderr, "%s: unknown opmode %u in modeswitch\n",
__func__, globals->opmode);
return -1;
}
ret = write(globals->unix_sock, buf, len);
if (ret != len)
fprintf(stderr, "%s: only wrote %d of %d bytes: %s\n",
__func__, ret, len, strerror(errno));
unix_sock_close(globals);
return 0;
}
static int check_interface(const char *iface)
{
int sock = -1;
struct ifreq ifr;
if (strlen(iface) > IFNAMSIZ) {
fprintf(stderr, "%s: interface name list too long, not changing\n",
__func__);
return -1;
}
sock = socket(PF_INET6, SOCK_DGRAM, 0);
if (sock < 0) {
perror("can't open socket");
return -1;
}
strncpy(ifr.ifr_name, iface, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
if (ioctl(sock, SIOCGIFINDEX, &ifr) == -1) {
fprintf(stderr, "%s: can't find interface, not changing\n",
__func__);
close(sock);
return -1;
}
close(sock);
return 0;
}
int alfred_client_change_interface(struct globals *globals)
{
unsigned char buf[MAX_PAYLOAD];
struct alfred_change_interface_v0 *change_interface;
int ret, len;
char *input, *token, *saveptr;
size_t interface_len;
if (unix_sock_open_client(globals))
return -1;
interface_len = strlen(globals->change_interface);
if (interface_len > sizeof(change_interface->ifaces)) {
fprintf(stderr, "%s: interface name list too long, not changing\n",
__func__);
return 0;
}
change_interface = (struct alfred_change_interface_v0 *)buf;
len = sizeof(*change_interface);
change_interface->header.type = ALFRED_CHANGE_INTERFACE;
change_interface->header.version = ALFRED_VERSION;
change_interface->header.length = htons(len - sizeof(change_interface->header));
strncpy(change_interface->ifaces, globals->change_interface,
sizeof(change_interface->ifaces));
change_interface->ifaces[sizeof(change_interface->ifaces) - 1] = '\0';
/* test it before sending
* globals->change_interface is now saved in change_interface->ifaces
* and can be modified by strtok_r
*/
input = globals->change_interface;
while ((token = strtok_r(input, ",", &saveptr))) {
input = NULL;
ret = check_interface(token);
if (ret < 0)
return 0;
}
ret = write(globals->unix_sock, buf, len);
if (ret != len)
fprintf(stderr, "%s: only wrote %d of %d bytes: %s\n",
__func__, ret, len, strerror(errno));
unix_sock_close(globals);
return 0;
}