diff --git a/dongle.c b/dongle.c index 4eaad79116ee344792493299eaff2b3a797f0845..afd01a0c76e550dc6da32f2dd5b70ef14d695d68 100644 --- a/dongle.c +++ b/dongle.c @@ -4,6 +4,7 @@ #include "dongle_apn.h" #include "dongle_pin.h" #include "dongle_network.h" +#include "dongle_infrastructure.h" struct ubus_context *ctx; int debug; @@ -74,6 +75,9 @@ int main(int argc, char **argv) if (rv < 0) goto fail; + rv = expose_infrastructure_object(ctx); + if (rv < 0) + goto fail; uloop_run(); return 0; diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c new file mode 100644 index 0000000000000000000000000000000000000000..1311cdf1291f1672c3cc32a0c4468379a15c5e77 --- /dev/null +++ b/dongle_infrastructure.c @@ -0,0 +1,143 @@ +#include "dongle_infrastructure.h" + +char *dongle_ip, *if_name; + +int test_get_ip(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__IP_MAX]; + char *dev; + + blobmsg_parse(ip_policy, __IP_MAX, tb, blob_data(msg), blob_len(msg)); + + if (tb[DEV]) { + dev = (char *)blobmsg_data(tb[DEV]); + get_ip(dev); + } + + return 0; +} + +static void parse_devices(struct ubus_request *req, int type, struct blob_attr *msg) +{ + char *json_str, *flags, *ip; + struct json_object *json_msg, *routes, *route, *json_ip, *json_flags; + int i; + + json_str = blobmsg_format_json(msg, true); + if (!json_str) + return; + + json_msg = json_tokener_parse(json_str); + + if (!json_msg) + goto out_str; + + if (!json_object_is_type(json_msg, json_type_object)) + goto out_json; + + json_object_object_get_ex(json_msg, "routes", &routes); + if (!routes) + goto out_json; + + for (i = 0; i < json_object_array_length(routes); i++) { + route = json_object_array_get_idx(routes, i); + json_object_object_get_ex(route, "flags", &json_flags); + flags = json_object_get_string(json_flags); + if (!strchr(flags, 'H')) + continue; + + json_object_object_get_ex(route, "destination", &json_ip); + ip = json_object_get_string(json_ip); + goto out; + } + goto out_json; + +out: + dongle_ip = strdup(ip); +out_json: + json_object_put(json_msg); +out_str: + free(json_str); +} + +int get_ip_invoke(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_buf buf; + int obj_id, rv; + + memset(&buf, 0, sizeof(buf)); + + if (!ctx) { + debug_print("No ubus context!\n"); + goto fail; + } + + ubus_lookup_id(ctx, "router.net", &obj_id); + if (!obj_id) { + debug_print("%s: ubus_lookup_id error\n", __func__); + goto fail; + } + + rv = ubus_invoke(ctx, obj_id, "ipv4_routes", NULL, parse_devices, NULL, 5000); + if (rv > 0) + printf("ubus_invoke error code %d\n", rv); +fail: + return 0; +} + +int get_ifname(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + char *path, device_path[1024], *device_name; + FILE *fp, inner_fp; + + fp = popen("find /sys/devices/platform -type d -name \"net\"", "r"); + + while ((fgets(path, 1024, fp) != NULL) { + printf("path found %s!\n", path); + snprintf(device_path, 1023, "ls %s", path); + inner_fp = popen(device_path, "r"); + fscanf(inner_fp, "%s", device_name); + printf("device name found %s!\n", device_name); + if (!strstr(device_name, "eth") || !strstr(device_name, "usb")) + continue; + if_name = strdup(device_name); + printf("And we have a winner %s!\n", if_name); + } + +fail: + 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); +}; + +struct ubus_object_type infrastructure_object_type = UBUS_OBJECT_TYPE("dongle", infrastructure_object_methods); + +struct ubus_object infrastructure_object = { + .name = "dongle.infrastructure", + .type = &infrastructure_object_type, + .methods = infrastructure_object_methods, + .n_methods = ARRAY_SIZE(infrastructure_object_methods), +}; + +int expose_infrastructure_object(struct ubus_context *ctx) +{ + int rv; + + rv = ubus_add_object(ctx, &infrastructure_object); + if (rv) { + debug_print("failed to add dongle.pin to ubus!\n"); + return -1; + } + + return 0; +} diff --git a/dongle_infrastructure.h b/dongle_infrastructure.h new file mode 100644 index 0000000000000000000000000000000000000000..48f5b8d847eef076552cbbc1c8b8417d67793627 --- /dev/null +++ b/dongle_infrastructure.h @@ -0,0 +1,6 @@ +#ifndef INFRASTRUCTURE_H +#define INFRASTRUCTURE_H +#include "common.h" + +int expose_infrastructure_object(struct ubus_context *ctx); +#endif