Skip to content
Snippets Groups Projects
Commit d01c88a1 authored by Jakob Olsson's avatar Jakob Olsson
Browse files

more prototype code, gets ifname (unsuccessfully allocates memory for it though), gets IP & mask

parent bf0605e6
Branches
Tags
No related merge requests found
...@@ -15,7 +15,6 @@ TODO: ...@@ -15,7 +15,6 @@ TODO:
char *get_ip(char *if_name) char *get_ip(char *if_name)
{ {
char response[1024] = {0}; char response[1024] = {0};
size_t filesize;
int rv; 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"); 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");
......
...@@ -28,10 +28,10 @@ int parse_args(int argc, char **argv) ...@@ -28,10 +28,10 @@ int parse_args(int argc, char **argv)
} }
goto done; goto done;
case ':': 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; goto fail;
case '?': 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; goto fail;
} }
} }
......
...@@ -2,10 +2,16 @@ ...@@ -2,10 +2,16 @@
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdlib.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" #include "dongle_infrastructure.h"
char *dongle_ip, *if_name; char *dongle_ip;
/*int test_get_ip(struct ubus_context *ctx, struct ubus_object *obj, /*int test_get_ip(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct ubus_request_data *req, const char *method,
...@@ -24,6 +30,100 @@ char *dongle_ip, *if_name; ...@@ -24,6 +30,100 @@ char *dongle_ip, *if_name;
return 0; 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) static void parse_devices(struct ubus_request *req, int type, struct blob_attr *msg)
{ {
char *json_str, *flags, *ip; char *json_str, *flags, *ip;
...@@ -94,73 +194,17 @@ fail: ...@@ -94,73 +194,17 @@ fail:
return 0; 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 ubus_request_data *req, const char *method,
struct blob_attr *msg) struct blob_attr *msg)
{ {
char *path, *device_name, device_path[1024] = {0}; get_device_ip();
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);
return 0; return 0;
} }
struct ubus_method infrastructure_object_methods[] = { 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_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); struct ubus_object_type infrastructure_object_type = UBUS_OBJECT_TYPE("dongle", infrastructure_object_methods);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment