diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index 00dbad1193b048496d0db2fc4d721585c22ca5f8..9a1873dd802996f0457ae2e20e2516a788a97aef 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -3,7 +3,6 @@ #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> -#include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> @@ -381,8 +380,7 @@ fail: return NULL; } -/*for testing, taken from stackoverflow*/ -char* stringToBinary(char* s) { +char* string_to_binary(char* s) { size_t len; char *binary, ch; int i, j; @@ -413,7 +411,8 @@ fail: char *get_device_ip_alt(char *device_name) { - char *iface, *destination, *gateway, *flags, *route; + char *iface, *destination, *gateway, *flags, *route, *binary, ipv4_addr[IPV4_MAX]; + uint32_t dec_addr; FILE *fp; fp = fopen("/proc/net/route", "r"); @@ -426,11 +425,6 @@ char *get_device_ip_alt(char *device_name) memset(route, 0, 1024); while ((fgets(route, 1024, fp)) != NULL) { remove_newline(route); - /* - 1. delimiter on first tab, check if = usb0, else continue - 2. delimiter on 4th tab, convert to binary, check if 3rd bit = 1, else continue - 3. get destintion ip - */ iface = lexer(&route, "\t"); destination = lexer(&route, "\t"); @@ -440,18 +434,13 @@ char *get_device_ip_alt(char *device_name) if (strncmp(iface, device_name, 10) != 0) continue; - char *tmp_var_; - tmp_var_ = stringToBinary(flags); - printf("%s %c\n", tmp_var_, tmp_var_[(strlen(tmp_var_)-3)]); - - if (tmp_var_[(strlen(tmp_var_)-3)] == '1') { - printf("host addr %s\n", destination); - char *ptr; - char test2[1024]; - uint32_t test = strtoul(destination, &ptr, 16); - test = strtoul(destination, &ptr, 16); - inet_ntop(AF_INET, &test, test2, 1024); - printf("%s\n", test2); + binary = string_to_binary(flags); + printf("%s %c\n", binary, binary[(strlen(binary)-3)]); + + if (binary[(strlen(binary)-3)] == '1') { + dec_addr = strtoul(destination, NULL, IPV4_MAX); + inet_ntop(AF_INET, &dec_addr, ipv4_addr, IPV4_MAX); + printf("ipv4_addr %s\n", ipv4_addr); } } fclose(fp); @@ -582,7 +571,7 @@ int test(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { - get_device_ip_alt("eth0.1"); + get_device_ip_alt("usb0"); return 0; } diff --git a/dongle_infrastructure.h b/dongle_infrastructure.h index 7fe4efeaada75d67c4547039a6c63b71dd56f2b0..74ea748a8607b90154d44dc9b62312230d421f07 100644 --- a/dongle_infrastructure.h +++ b/dongle_infrastructure.h @@ -1,7 +1,7 @@ #ifndef INFRASTRUCTURE_H #define INFRASTRUCTURE_H #include "common.h" - +#define IPV4_MAX 16 struct USB { char *product; char *product_id;