-
Notifications
You must be signed in to change notification settings - Fork 2
/
unix_sock.c
422 lines (342 loc) · 9.64 KB
/
unix_sock.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* 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 <errno.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include "alfred.h"
#include "hash.h"
#include "packet.h"
int unix_sock_open_daemon(struct globals *globals)
{
struct sockaddr_un addr;
unlink(globals->unix_path);
globals->unix_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
if (globals->unix_sock < 0) {
perror("can't create unix socket");
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
strncpy(addr.sun_path, globals->unix_path, sizeof(addr.sun_path));
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
if (bind(globals->unix_sock, (struct sockaddr *)&addr,
sizeof(addr)) < 0) {
perror("can't bind unix socket");
return -1;
}
if (listen(globals->unix_sock, 10) < 0) {
perror("can't listen on unix socket");
return -1;
}
return 0;
}
int unix_sock_open_client(struct globals *globals)
{
struct sockaddr_un addr;
globals->unix_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
if (globals->unix_sock < 0) {
perror("can't create unix socket");
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
strncpy(addr.sun_path, globals->unix_path, sizeof(addr.sun_path));
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
if (connect(globals->unix_sock, (struct sockaddr *)&addr,
sizeof(addr)) < 0) {
close(globals->unix_sock);
globals->unix_sock = -1;
perror("can't connect to unix socket");
return -1;
}
return 0;
}
static int unix_sock_add_data(struct globals *globals,
struct alfred_push_data_v0 *push,
int client_sock)
{
struct alfred_data *data;
struct dataset *dataset;
int len, data_len, ret = -1;
struct interface *interface;
interface = netsock_first_interface(globals);
if (!interface)
goto err;
len = ntohs(push->header.length);
if (len < (int)(sizeof(*push) - sizeof(push->header)))
goto err;
/* subtract rest of push header */
len -= sizeof(*push) - sizeof(push->header);
if (len < (int)(sizeof(*data)))
goto err;
data = push->data;
data_len = ntohs(data->header.length);
memcpy(data->source, &interface->hwaddr, sizeof(interface->hwaddr));
if ((int)(data_len + sizeof(*data)) > len)
goto err;
dataset = hash_find(globals->data_hash, data);
if (!dataset) {
dataset = malloc(sizeof(*dataset));
if (!dataset)
goto err;
dataset->buf = NULL;
memcpy(&dataset->data, data, sizeof(*data));
if (hash_add(globals->data_hash, dataset)) {
free(dataset);
goto err;
}
}
dataset->data_source = SOURCE_LOCAL;
clock_gettime(CLOCK_MONOTONIC, &dataset->last_seen);
/* free old buffer */
free(dataset->buf);
dataset->buf = malloc(data_len);
/* that's not good */
if (!dataset->buf)
goto err;
dataset->data.header.length = data_len;
dataset->data.header.version = data->header.version;
memcpy(dataset->buf, data->data, data_len);
ret = 0;
err:
close(client_sock);
return ret;
}
static int unix_sock_req_data_reply(struct globals *globals, int client_sock,
uint16_t id, uint8_t requested_type)
{
int len;
struct alfred_push_data_v0 *push;
struct hash_it_t *hashit = NULL;
uint8_t buf[MAX_PAYLOAD];
uint16_t seqno = 0, ret = 0;
/* send some data back through the unix socket */
push = (struct alfred_push_data_v0 *)buf;
push->header.type = ALFRED_PUSH_DATA;
push->header.version = ALFRED_VERSION;
push->tx.id = htons(id);
while (NULL != (hashit = hash_iterate(globals->data_hash, hashit))) {
struct dataset *dataset = hashit->bucket->data;
struct alfred_data *data;
if (dataset->data.header.type != requested_type)
continue;
/* too large? - should never happen */
if (dataset->data.header.length + sizeof(*data) >
MAX_PAYLOAD - sizeof(*push))
continue;
data = push->data;
memcpy(data, &dataset->data, sizeof(*data));
data->header.length = htons(data->header.length);
memcpy(data->data, dataset->buf, dataset->data.header.length);
len = dataset->data.header.length + sizeof(*data);
len += sizeof(*push) - sizeof(push->header);
push->header.length = htons(len);
push->tx.seqno = htons(seqno++);
if (write(client_sock, buf, sizeof(push->header) + len) < 0) {
ret = -1;
hash_iterate_free(hashit);
break;
}
}
close(client_sock);
return ret;
}
static int unix_sock_req_data(struct globals *globals,
struct alfred_request_v0 *request,
int client_sock)
{
int len;
uint16_t id;
struct transaction_head *head = NULL;
struct interface *interface;
len = ntohs(request->header.length);
if (len != (sizeof(*request) - sizeof(request->header)))
return -1;
id = ntohs(request->tx_id);
interface = netsock_first_interface(globals);
/* no server to send the request to, only give back what we have now. */
if (!globals->best_server || !interface)
return unix_sock_req_data_reply(globals, client_sock, id,
request->requested_type);
/* a master already has data to respond with */
if (globals->opmode == OPMODE_MASTER)
return unix_sock_req_data_reply(globals, client_sock, id,
request->requested_type);
head = transaction_add(globals, globals->best_server->hwaddr, id);
if (!head)
return -1;
head->client_socket = client_sock;
head->requested_type = request->requested_type;
send_alfred_packet(interface, &globals->best_server->address,
request, sizeof(*request));
return 0;
}
int unix_sock_req_data_finish(struct globals *globals,
struct transaction_head *head)
{
struct alfred_status_v0 status;
int ret = 0, send_data = 1;
int client_sock;
uint16_t id;
uint8_t requested_type;
requested_type = head->requested_type;
id = head->id;
client_sock = head->client_socket;
if (head->finished != 1)
send_data = 0;
free(head);
if (send_data) {
unix_sock_req_data_reply(globals, client_sock, id,
requested_type);
return 0;
}
status.header.type = ALFRED_STATUS_ERROR;
status.header.version = ALFRED_VERSION;
status.header.length = htons(sizeof(status) - sizeof(status.header));
status.tx.id = htons(id);
status.tx.seqno = 1;
if (write(client_sock, &status, sizeof(status)) < 0)
ret = -1;
close(client_sock);
return ret;
}
static int unix_sock_modesw(struct globals *globals,
struct alfred_modeswitch_v0 *modeswitch,
int client_sock)
{
int len, ret = -1;
len = ntohs(modeswitch->header.length);
if (len < (int)(sizeof(*modeswitch) - sizeof(modeswitch->header)))
goto err;
switch (modeswitch->mode) {
case ALFRED_MODESWITCH_SLAVE:
if (!list_is_singular(&globals->interfaces))
goto err;
globals->opmode = OPMODE_SLAVE;
break;
case ALFRED_MODESWITCH_MASTER:
globals->opmode = OPMODE_MASTER;
break;
default:
goto err;
}
ret = 0;
err:
close(client_sock);
return ret;
}
static int
unix_sock_change_iface(struct globals *globals,
struct alfred_change_interface_v0 *change_iface,
int client_sock)
{
int len, ret = -1;
len = ntohs(change_iface->header.length);
if (len < (int)(sizeof(*change_iface) - sizeof(change_iface->header)))
goto err;
if (globals->opmode == OPMODE_SLAVE) {
if (strstr(change_iface->ifaces, ",") != NULL) {
ret = -EINVAL;
fprintf(stderr, "Tried to set multiple interfaces in slave mode\n");
goto err;
}
}
netsock_set_interfaces(globals, change_iface->ifaces);
ret = 0;
err:
close(client_sock);
return ret;
}
int unix_sock_read(struct globals *globals)
{
int client_sock;
struct sockaddr_un sun_addr;
socklen_t sun_size = sizeof(sun_addr);
struct alfred_tlv *packet;
uint8_t buf[MAX_PAYLOAD];
int length, headsize, ret = -1;
client_sock = accept(globals->unix_sock, (struct sockaddr *)&sun_addr,
&sun_size);
if (client_sock < 0) {
perror("can't accept unix connection");
return -1;
}
/* we assume that we can instantly read here. */
length = read(client_sock, buf, sizeof(buf));
if (length <= 0) {
perror("read from unix socket failed");
goto err;
}
/* drop too small packets */
headsize = sizeof(*packet);
if (length < headsize)
goto err;
packet = (struct alfred_tlv *)buf;
if ((length - headsize) < ((int)ntohs(packet->length)))
goto err;
if (packet->version != ALFRED_VERSION)
goto err;
switch (packet->type) {
case ALFRED_PUSH_DATA:
ret = unix_sock_add_data(globals,
(struct alfred_push_data_v0 *)packet,
client_sock);
break;
case ALFRED_REQUEST:
ret = unix_sock_req_data(globals,
(struct alfred_request_v0 *)packet,
client_sock);
break;
case ALFRED_MODESWITCH:
ret = unix_sock_modesw(globals,
(struct alfred_modeswitch_v0 *)packet,
client_sock);
break;
case ALFRED_CHANGE_INTERFACE:
ret = unix_sock_change_iface(globals,
(struct alfred_change_interface_v0 *)packet,
client_sock);
break;
default:
/* unknown packet type */
ret = -1;
goto err;
}
return ret;
err:
close(client_sock);
return ret;
}
int unix_sock_close(struct globals *globals)
{
close(globals->unix_sock);
return 0;
}