diff --git a/common.c b/common.c index fa21ec1d5f2d6dc53518dd4981bf02f30929ed38..19c33385ec30955a572e6cea086031d1dd30e407 100644 --- a/common.c +++ b/common.c @@ -15,7 +15,6 @@ TODO: char *get_ip(char *if_name) { char response[1024] = {0}; - size_t filesize; int rv; FILE *fp = popen("ubus call router.net ipv4_routes | grep -B 7 \"usb0\" | grep \"flag.*H\" -B 4 | grep destination | cut -d ' ' -f2 | cut -d '\"' -f2", "r"); diff --git a/dongle.c b/dongle.c index afd01a0c76e550dc6da32f2dd5b70ef14d695d68..0846510e22abc76f85e04f02bcca43da82d66f90 100644 --- a/dongle.c +++ b/dongle.c @@ -28,10 +28,10 @@ int parse_args(int argc, char **argv) } goto done; case ':': - printf(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt); + fprintf(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt); goto fail; case '?': - printf(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], optopt); + fprintf(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], optopt); goto fail; } } diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index e5e5edb9577e7ce635a2303551fe78e1bb8631bb..44e91020d159e9047105d5c804d80b6fa21efc00 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -2,10 +2,16 @@ #include <dirent.h> #include <sys/stat.h> #include <stdlib.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 "dongle_infrastructure.h" -char *dongle_ip, *if_name; +char *dongle_ip; /*int test_get_ip(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, @@ -24,6 +30,100 @@ char *dongle_ip, *if_name; return 0; }*/ +char *get_device(void) +{ + char *path, *device_name, device_path[1024] = {0}; + struct dirent *de; + DIR *dr; + FILE *fp; + struct stat st; + + fp = popen("find /sys/devices/platform -type d -name \"net\"", "r"); + while ((fgets(path, 1024, fp)) != NULL) { + printf("11%s\n", path); + strncpy(device_path, path, (strlen(path)-1)); + printf("%s%s\n", path, device_path); + dr = opendir(device_path); + //free(device_path); + if (!dr) { + perror("opendir"); + goto fail; + } + + while ((de = readdir(dr)) != NULL) { + printf("device name found %s!\n", de->d_name); + if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) + continue; + + if (fstatat(dirfd(dr), de->d_name, &st, 0) < 0) { + perror("fstatat"); + continue; + } + + if (!S_ISDIR(st.st_mode)) + continue; + + if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) + continue; + + /*device_name = (char *)calloc(strlen(de->d_name), sizeof(char)); + if (!device_path) { + perror("calloc"); + goto fail; + }*/ + //free(device_name); + //strncpy(device_name, de->d_name, strlen(de->d_name)); + goto success; + } + closedir(dr); + } + +fail: + pclose(fp); + return NULL; +success: + closedir(dr); + pclose(fp); + return NULL; +} + +char *get_device_ip(void) +{ + int fd; + struct ifreq ifr; + char *device_name, *device_ip, *device_mask; + + //device_name = get_device(); + //printf("device_name %s\n", device_name); + strncpy(ifr.ifr_name, "usb0", IFNAMSIZ-1); + fd = socket(AF_INET, SOCK_DGRAM, 0); + ifr.ifr_addr.sa_family = AF_INET; + ifr.ifr_netmask.sa_family = AF_INET; //maybe? + + ioctl(fd, SIOCGIFADDR, &ifr); + device_ip = strdup(inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); + + ioctl(fd, SIOCGIFNETMASK, &ifr); + device_mask = strdup(inet_ntoa(((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr)); + + close(fd); + + /*device_ip = malloc(strlen(inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr))); + if (!device_ip) { + perror("malloc"); + goto fail; + }*/ + + printf("ip %s, mask %s\n", device_ip, device_mask); + free(device_ip); + free(device_name); + return NULL; +fail: + //free(device_name); + return NULL; +} + + static void parse_devices(struct ubus_request *req, int type, struct blob_attr *msg) { char *json_str, *flags, *ip; @@ -94,73 +194,17 @@ fail: return 0; } -int get_ifname(struct ubus_context *ctx, struct ubus_object *obj, +int get_ifname_ip(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { - char *path, *device_name, device_path[1024] = {0}; - struct dirent *de; - DIR *dr; - FILE *fp, *inner_fp; - struct stat st; - - fp = popen("find /sys/devices/platform -type d -name \"net\"", "r"); - while ((fgets(path, 1024, fp)) != NULL) { - /* why doesn't this work... - device_name = (char *) calloc(strlen(path), sizeof(char)); - if (!device_path) { - perror("calloc"); - goto fail; - } - */ - printf("%s\n", path); - strncpy(device_path, path, (strlen(path)-1)); - printf("%s%s\n", path, device_path); - dr = opendir(device_path); - free(device_path); - if (!dr) { - perror("opendir"); - goto fail; - } - - while ((de = readdir(dr)) != NULL) { - printf("device name found %s!\n", de->d_name); - if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) - continue; - - if (fstatat(dirfd(dr), de->d_name, &st, 0) < 0) { - perror("fstatat"); - continue; - } - - if (!S_ISDIR(st.st_mode)) - continue; - - if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) - continue; - - //if_name = strdup(de->d_name); - printf("And we have a winner %s!\n", de->d_name); - - goto success; - } - printf("and null it is!\n"); - closedir(dr); - } - -fail: - pclose(fp); - return -1; -success: - closedir(dr); - pclose(fp); + get_device_ip(); return 0; } struct ubus_method infrastructure_object_methods[] = { - //UBUS_METHOD("get_ip", test_get_ip, ip_policy), UBUS_METHOD_NOARG("get_ip_invoke", get_ip_invoke), - UBUS_METHOD_NOARG("get_ifname", get_ifname), + UBUS_METHOD_NOARG("get_ifname_ip", get_ifname_ip) }; struct ubus_object_type infrastructure_object_type = UBUS_OBJECT_TYPE("dongle", infrastructure_object_methods);