Newer
Older
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <limits.h>
#include <netdb.h>
#include <ifaddrs.h>
#include "dongle_infrastructure.h"
Jakob Olsson
committed
struct ubus_context *global_ctx;
const struct blobmsg_policy dev_policy[__DEV_MAX] = {
[DEV] = {.name = "dev", .type = BLOBMSG_TYPE_STRING},
};
LIST_HEAD(devices);
LIST_HEAD(stack);
LIST_HEAD(visited);
int parse_args(int argc, char **argv)
{
while ((ch = getopt_long(argc, argv, "d:", long_options, NULL)) != -1)
{
switch (ch)
{
printf("%s: option '-%c' is invalid.\n", argv[0], optopt); //cant exactly do debug print in here...
Jakob Olsson
committed
fprintf(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt);
Jakob Olsson
committed
fprintf(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], optopt);
return 0;
fail:
return -1;
}
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
int devices_status(struct uloop_timeout *t)
{
get_devices();
tag_missing_devices();
uloop_add_get_devices(t);
return 0;
}
int tag_missing_devices(void)
{
struct device *dev, *tmp;
list_for_each_entry_safe(dev, tmp, &devices, list) {
if (dev->present) {
dev->missing = 0;
dev->present = false;
continue;
}
dev->missing++;
if (dev->missing == 1)
delete_device(dev);
}
return 0;
}
int add_device(struct device *new_dev)
{
struct device *dev;
dev = search_list(new_dev->usb.if_name);
if (dev) {
if (dev->ip)
free(dev->ip);
dev->ip = new_dev->ip;
if (!dev->ubus_obj && dev->ip)
dev->ubus_obj = dongle_create_dynamic_object(new_dev);
else if (dev->ubus_obj && !dev->ip) {
unpublish_ubus_object(global_ctx, dev->ubus_obj);
dongle_destroy_dynamic_object(dev->ubus_obj);
}
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
dev->present = true;
goto already_present;
}
new_dev->present = true;
if (list_empty(&devices))
INIT_LIST_HEAD(&devices);
list_add_tail(&new_dev->list, &devices);
if (new_dev->ip) {
new_dev->ubus_obj = dongle_create_dynamic_object(new_dev);
if (new_dev->ubus_obj)
publish_ubus_object(global_ctx, new_dev->ubus_obj);
}
return 0;
already_present:
return -1;
}
int delete_all_devices(void)
{
struct device *dev, *tmp;
list_for_each_entry_safe(dev, tmp, &devices, list) {
delete_device(dev);
}
return 0;
}
int delete_device_by_name(char *name)
{
struct device *dev, *tmp;
list_for_each_entry_safe(dev, tmp, &devices, list) {
if (strncmp(dev->usb.if_name, name, 128) != 0)
continue;
delete_device(dev);
return 0;
}
return -1;
}
int delete_device(struct device *dev)
{
unpublish_ubus_object(global_ctx, dev->ubus_obj);
dongle_destroy_dynamic_object(dev->ubus_obj);
}
list_del(&dev->list);
free_device(dev);
return 0;
}
void free_device(struct device *dev)
{
if (!dev)
return;
free(dev->ip);
free(&(dev->usb));
}
void free_usb(struct USB *usb)
{
if (!usb)
return;
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
free(usb->product);
free(usb->product_id);
free(usb->vendor_id);
free(usb->if_name);
}
struct device *search_list(char *name)
{
struct device *dev;
list_for_each_entry(dev, &devices, list) {
if (strncmp(name, dev->usb.if_name, 128) == 0)
return dev;
}
return NULL;
}
char *lexer(char **input, char *delimiter)
{
char *token, *substr;
if (strlen(*input) == 0) {
debug_print("empty input!\n");
return NULL;
} else if (strlen(delimiter) == 0) {
debug_print("empty delimiter!\n");
return NULL;
}
token = strstr(*input, delimiter);
if (token) {
*token = '\0';
substr = strdup(*input);
if (!substr) {
perror("strdup");
goto fail_strdup;
}
*input = token + strlen(delimiter);
} else {
substr = strdup(*input);
if (!substr) {
perror("strdup");
goto fail_strdup;
}
*input[0] = '\0';
}
fail_strdup:
return substr;
}
void remove_newline(char *input)
{
char *pos;
pos = strchr(input, '\n');
if (!pos)
return;
*pos = '\0';
}
char *get_usb_stat(char *path, char *dir, char *stat)
{
char stat_path[PATH_MAX], contents[1024];
FILE *f;
snprintf(stat_path, PATH_MAX, "%s/%s/%s", path, dir, stat);
f = fopen(stat_path, "r");
if (!f) {
perror("fopen");
goto fail;
}
fgets(contents, 1024, f);
remove_newline(contents);
fclose(f);
return strdup(contents);
fail:
return NULL;
}
char *get_device_name(char *dir_name)
{
char path[PATH_MAX], *name = NULL;
struct directory *dr, *sub_dr;
struct dirent *de;
struct stat st;
DIR *dir;
dr = (struct directory *)malloc(sizeof(*dr));
if (!dr) {
perror("malloc");
goto fail_dr;
}
dr->path = strdup(dir_name);
if (!dr->path)
goto fail_path;
push(dr, &stack);
while (!list_empty(&stack) && !name) {
dr = pop(&stack);
dir = opendir(dr->path);
if (!dir)
continue;
push(dr, &visited);
while ((de = readdir(dir)) != NULL) {
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
perror("fstatat");
continue;
}
if (!S_ISDIR(st.st_mode))
continue;
if (!strstr(dr->path, "net")) {
snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name);
if (search(path, &visited))
continue;
sub_dr = (struct directory *)malloc(sizeof(*sub_dr));
if (!sub_dr) {
perror("malloc");
continue;
}
sub_dr->path = strdup(path);
if (!sub_dr->path) {
free(sub_dr);
continue;
}
push(sub_dr, &stack);
continue;
}
if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb"))
break;
name = strdup(de->d_name);
// how to manage failure on this strdup
break;
}
closedir(dir);
}
clear_list(&visited);
clear_list(&stack);
return name;
fail_path:
free(dr);
fail_dr:
return NULL;
}
int get_devices(void)
{
char usb_path[PATH_MAX], product_path[PATH_MAX], path[PATH_MAX], *name;
struct dirent *de;
DIR *dir;
struct stat st;
struct device *dev;
int rv;
strncpy(usb_path, "/sys/bus/usb/devices/", PATH_MAX);
dir = opendir(usb_path);
if (!dir) {
perror("opendir");
goto fail_dr;
}
while ((de = readdir(dir)) != NULL) {
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
if (strstr(de->d_name, ":") || strstr(de->d_name, "usb"))
continue;
snprintf(product_path, PATH_MAX, "%s%s/product", usb_path, de->d_name);
memset(&st, 0, sizeof(st));
rv = stat(product_path, &st);
if (rv < 0) {
//perror("stat");
continue;
}
snprintf(path, PATH_MAX, "%s%s/", usb_path, de->d_name);
name = get_device_name(path);
if (!name)
continue;
dev = (struct device *)calloc(1, sizeof(*dev));
if (!dev) {
perror("calloc");
goto fail_dev;
}
dev->usb.product = get_usb_stat(usb_path, de->d_name, "product");
dev->usb.product_id = get_usb_stat(usb_path, de->d_name, "idProduct");
dev->usb.vendor_id = get_usb_stat(usb_path, de->d_name, "idVendor");
dev->usb.if_name = name;
dev->ip = get_device_ip(dev->usb.if_name);
add_device(dev);
}
closedir(dir);
return 0;
fail_dev:
closedir(dir);
fail_dr:
return -1;
}
char *get_device_ip(char *device_name)
{
char *iface, *destination, *gateway, *flags, *route, *ipv4_addr = NULL;
bool host_flag;
struct in_addr addr;
FILE *fp;
fp = fopen("/proc/net/route", "r");
if (!fp) {
perror("fopen");
goto fail;
}
route = (char *)malloc(1024);
if (!route) {
perror("malloc");
goto fail_route;
}
memset(route, 0, 1024);
while ((fgets(route, 1024, fp)) != NULL) {
remove_newline(route);
iface = lexer(&route, "\t");
destination = lexer(&route, "\t");
gateway = lexer(&route, "\t");
flags = lexer(&route, "\t");
if (strncmp(iface, device_name, 10) != 0)
continue;
host_flag = atoi(flags) & 4;
if (!host_flag)
continue;
ipv4_addr = (char *)calloc(1, IPV4_MAX);
if (!ipv4_addr)
break;
addr.s_addr = strtoul(destination, NULL, IPV4_MAX);
inet_ntop(AF_INET, &(addr.s_addr), ipv4_addr, IPV4_MAX);
printf("ipv4_addr %s\n", ipv4_addr);
}
free(iface);
free(destination);
free(gateway);
free(flags);
fail_route:
fclose(fp);
fail:
return ipv4_addr;
}
{
struct device *net_dev;
void *arr, *table;
int rv;
memset(bb, 0, sizeof(*bb));
rv = blob_buf_init(bb, 0);
arr = blobmsg_open_array(bb, "network_devices");
list_for_each_entry(net_dev, &devices, list) {
table = blobmsg_open_table(bb, "");
rv = blobmsg_add_string(bb, "product", net_dev->usb.product);
rv = blobmsg_add_string(bb, "interface_name", net_dev->usb.if_name);
rv = blobmsg_add_string(bb, "idproduct", net_dev->usb.product_id);
rv = blobmsg_add_string(bb, "idvendor", net_dev->usb.vendor_id);
if (rv < 0)
goto fail_string;
if (net_dev->ip)
{
rv = blobmsg_add_string(bb, "ip", net_dev->ip);
return 0;
fail_string:
fail_table:
fail_arr:
fail:
return UBUS_STATUS_UNKNOWN_ERROR;
}
int print_list(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_buf bb;
int rv;
rv = list_to_blob(&bb);
goto fail;
ubus_send_reply(ctx, req, bb.head);
fail:
blob_buf_free(&bb);
return rv;
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
}
int clear(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
printf("%d\n", delete_all_devices());
return 0;
}
int test(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
printf("%s\n", get_device_ip("usb0"));
return 0;
}
int remove_device(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__DEV_MAX];
char *dev;
blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[DEV])
goto fail;
dev = (char *)blobmsg_data(tb[DEV]);
printf("%d\n", delete_device_by_name(dev));
return 0;
fail:
return UBUS_STATUS_INVALID_ARGUMENT;
}
struct ubus_method dongle_object_methods[] = {
UBUS_METHOD_NOARG("test", test),
UBUS_METHOD_NOARG("list", print_list),
UBUS_METHOD_NOARG("clear", clear),
UBUS_METHOD("remove_device", remove_device, dev_policy)};
struct ubus_object_type dongle_object_type = UBUS_OBJECT_TYPE("dongle", dongle_object_methods);
.type = &dongle_object_type,
.methods = dongle_object_methods,
.n_methods = ARRAY_SIZE(dongle_object_methods),
};
void uloop_add_get_devices(struct uloop_timeout *t)
{
uloop_timeout_set(t, 5000);
uloop_timeout_add(t);
}
Jakob Olsson
committed
void init_ubus(void)
{
global_ctx = ubus_connect(NULL);
if (!global_ctx) {
Jakob Olsson
committed
perror("ubus");
exit(1);
}
ubus_add_uloop(global_ctx);
Jakob Olsson
committed
}
int publish_object(struct ubus_context *ctx)
{
int rv;
if (rv) {
debug_print("failed to add dongle.pin to ubus!\n");
return -1;
}
return 0;
}
Jakob Olsson
committed
int main(int argc, char **argv)
{
int rv;
rv = parse_args(argc, argv);
if (rv < 0)
goto fail;
Jakob Olsson
committed
uloop_init();
init_ubus();
uloop_timeout_set(&timeout, 0);
uloop_timeout_add(&timeout);
Jakob Olsson
committed
uloop_run();
return 0;
fail:
return -1;