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>
Jakob Olsson
committed
struct ubus_context *global_ctx;
const struct blobmsg_policy dev_policy[__DEV_MAX] = {
[DEV] = {.name = "dev", .type = BLOBMSG_TYPE_STRING},
};
enum {
USB_PATH,
__USB_MAX
};
const struct blobmsg_policy usb_policy[__USB_MAX] = {
[USB_PATH] = {.name = "path", .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) {
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;
}
{
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) {
/* TEMPORARY FIX -- TODO: HOW TO CHECK IF IT CONTAINS ELEMENT */
list_del(&dev->list);
}
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);
if (new_dev->ip)
dev->ip = strdup(new_dev->ip);
else
dev->ip = NULL;
if (!dev->ubus_obj && dev->ip) {
dev->ubus_obj = dongle_create_dynamic_object(dev);
if (!dev->ubus_obj)
goto fail;
rv = publish_ubus_object(global_ctx, dev->ubus_obj);
if (rv)
goto fail;
} else if (dev->ubus_obj && !dev->ip) {
unpublish_ubus_object(global_ctx, dev->ubus_obj);
dongle_destroy_dynamic_object(&(dev->ubus_obj));
delete_device(new_dev);
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)
goto fail;
rv = publish_ubus_object(global_ctx, new_dev->ubus_obj);
if (rv)
goto fail;
return 0;
already_present:
return -1;
}
int delete_all_devices(void)
{
struct device *dev, *tmp;
list_for_each_entry_safe(dev, tmp, &devices, list) {
/* TEMPORARY FIX -- TODO: HOW TO CHECK IF IT CONTAINS ELEMENT */
list_del(&dev->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;
/* TEMPORARY FIX -- TODO: HOW TO CHECK IF IT CONTAINS ELEMENT */
list_del(&dev->list);
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));
/*if (dev->list) // how to check if list contains this element?
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;
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
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 *file)
{
char stat_path[PATH_MAX], contents[1024];
FILE *f;
snprintf(stat_path, PATH_MAX, "%s%s", path, file);
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
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_from_path(char *input_path)
{
char product_path[PATH_MAX], path[PATH_MAX], *name;
struct dirent *de;
DIR *dir;
struct stat st;
struct device *dev;
int rv;
dir = opendir(input_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;
Jakob Olsson
committed
snprintf(product_path, PATH_MAX, "%s/product", input_path);
memset(&st, 0, sizeof(st));
rv = stat(product_path, &st);
if (rv < 0) {
//perror("stat");
continue;
}
Jakob Olsson
committed
snprintf(path, PATH_MAX, "%s/", input_path);
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(path, "product");
dev->usb.product_id = get_usb_stat(path, "idProduct");
dev->usb.vendor_id = get_usb_stat(path, "idVendor");
dev->usb.if_name = name;
dev->ip = get_device_ip(dev->usb.if_name);
add_device(dev);
Jakob Olsson
committed
break;
}
closedir(dir);
return 0;
fail_dev:
closedir(dir);
fail_dr:
return -1;
}
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(path, "product");
dev->usb.product_id = get_usb_stat(path, "idProduct");
dev->usb.vendor_id = get_usb_stat(path, "idVendor");
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
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);
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;
}
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 alert(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__USB_MAX];
char *usb_path;
blobmsg_parse(usb_policy, __USB_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[USB_PATH])
goto fail;
usb_path = (char *)blobmsg_data(tb[USB_PATH]);
/* FIX THROUGH BASH SCRIPT INSTEAD... */
get_devices_from_path(usb_path);
return 0;
fail:
return UBUS_STATUS_INVALID_ARGUMENT;
}
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;
}
int update(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
get_devices();
tag_missing_devices();
return 0;
}
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),
UBUS_METHOD("alert", alert, usb_policy),
UBUS_METHOD_NOARG("update", update)
};
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, 60000);
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;