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

clean up & refactor getting device ip address through route

parent 6e733777
Branches
No related tags found
No related merge requests found
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
...@@ -381,8 +380,7 @@ fail: ...@@ -381,8 +380,7 @@ fail:
return NULL; return NULL;
} }
/*for testing, taken from stackoverflow*/ char* string_to_binary(char* s) {
char* stringToBinary(char* s) {
size_t len; size_t len;
char *binary, ch; char *binary, ch;
int i, j; int i, j;
...@@ -413,7 +411,8 @@ fail: ...@@ -413,7 +411,8 @@ fail:
char *get_device_ip_alt(char *device_name) 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; FILE *fp;
fp = fopen("/proc/net/route", "r"); fp = fopen("/proc/net/route", "r");
...@@ -426,11 +425,6 @@ char *get_device_ip_alt(char *device_name) ...@@ -426,11 +425,6 @@ char *get_device_ip_alt(char *device_name)
memset(route, 0, 1024); memset(route, 0, 1024);
while ((fgets(route, 1024, fp)) != NULL) { while ((fgets(route, 1024, fp)) != NULL) {
remove_newline(route); 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"); iface = lexer(&route, "\t");
destination = lexer(&route, "\t"); destination = lexer(&route, "\t");
...@@ -440,18 +434,13 @@ char *get_device_ip_alt(char *device_name) ...@@ -440,18 +434,13 @@ char *get_device_ip_alt(char *device_name)
if (strncmp(iface, device_name, 10) != 0) if (strncmp(iface, device_name, 10) != 0)
continue; continue;
char *tmp_var_; binary = string_to_binary(flags);
tmp_var_ = stringToBinary(flags); printf("%s %c\n", binary, binary[(strlen(binary)-3)]);
printf("%s %c\n", tmp_var_, tmp_var_[(strlen(tmp_var_)-3)]);
if (binary[(strlen(binary)-3)] == '1') {
if (tmp_var_[(strlen(tmp_var_)-3)] == '1') { dec_addr = strtoul(destination, NULL, IPV4_MAX);
printf("host addr %s\n", destination); inet_ntop(AF_INET, &dec_addr, ipv4_addr, IPV4_MAX);
char *ptr; printf("ipv4_addr %s\n", ipv4_addr);
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);
} }
} }
fclose(fp); fclose(fp);
...@@ -582,7 +571,7 @@ int test(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -582,7 +571,7 @@ int test(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)
{ {
get_device_ip_alt("eth0.1"); get_device_ip_alt("usb0");
return 0; return 0;
} }
......
#ifndef INFRASTRUCTURE_H #ifndef INFRASTRUCTURE_H
#define INFRASTRUCTURE_H #define INFRASTRUCTURE_H
#include "common.h" #include "common.h"
#define IPV4_MAX 16
struct USB { struct USB {
char *product; char *product;
char *product_id; char *product_id;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment