diff --git a/Makefile b/Makefile index c22bcf8d3c6fadbfefb525fa808bed0a896598c6..130aee8e1dc9cb50864c7f625dcaba94f3149199 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc CFLAGS = -g -Wall -LIBS = -ljson-c -lubox -lubus -lcurl -lmobile +LIBS = -ljson-c -lubox -lubus -lcurl -lmobile -lblobmsg_json all: libmobile1 libmobile2 common dongle_apn dongle_pin dongle_network dongle @@ -22,20 +22,20 @@ common: ${COBJS} DAOBJS = dongle_apn.o DASRCS = dongle_apn.c dongle_apn: ${DAOBJS} - ${CC} -c ${CFLAGS} ${DASRCS} ${COBJS} -o ${DAOBJS} -L . ${LIBS} #${MOBJS} + ${CC} -c ${CFLAGS} ${DASRCS} ${COBJS} -o ${DAOBJS} -L . ${LIBS} DPOBJS = dongle_pin.o DPSRCS = dongle_pin.c dongle_pin: ${DPOBJS} - ${CC} -c ${CFLAGS} ${DPSRCS} ${COBJS} -o ${DPOBJS} -L . ${LIBS} #${MOBJS} + ${CC} -c ${CFLAGS} ${DPSRCS} ${COBJS} -o ${DPOBJS} -L . ${LIBS} DNOBJS = dongle_network.o DNSRCS = dongle_network.c dongle_network: ${DNOBJS} - ${CC} -c ${CFLAGS} ${DNSRCS} ${COBJS} -o ${DNOBJS} -L . ${LIBS} #${MOBJS} + ${CC} -c ${CFLAGS} ${DNSRCS} ${COBJS} -o ${DNOBJS} -L . ${LIBS} dongle: dongle.o - ${CC} ${CFLAGS} dongle.o ${COBJS} ${DNOBJS} ${DPOBJS} ${DAOBJS} -o dongle -L . ${LIBS} #${MOBJS} + ${CC} ${CFLAGS} dongle.o ${COBJS} ${DNOBJS} ${DPOBJS} ${DAOBJS} -o dongle -L . ${LIBS} clean: rm -f dongle_apn dongle_pin dongle_network *.o *.so diff --git a/common.c b/common.c index 466a5edf9dd880b2a1f32c40022a62ea601d7975..fa21ec1d5f2d6dc53518dd4981bf02f30929ed38 100644 --- a/common.c +++ b/common.c @@ -12,17 +12,18 @@ TODO: 1. Implement by invoking ubus */ -int get_ip(char *if_name) +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"); fscanf(fp, "%s", response); pclose(fp); printf("%s\n", response); - - return 0; + return strdup(response); } int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req) diff --git a/common.h b/common.h index 8ff9ef7a0f7830243382305ff5265b1a4e062913..e403bcc9a239d37cfc9ab19b35dca390810ec11c 100644 --- a/common.h +++ b/common.h @@ -16,7 +16,8 @@ #include "libmobile.h" #include <json-c/json.h> #include <string.h> -#include <libubox/blobmsg.h> +//#include <libubox/blobmsg.h> +#include <libubox/blobmsg_json.h> #include <libubus.h> extern int debug; @@ -27,7 +28,7 @@ extern int debug; fprintf(stderr, __VA_ARGS__); \ } while (0) -int get_ip(char *if_name); +char *get_ip(char *if_name); /** * Function: print_to_ubus * diff --git a/dongle_network.c b/dongle_network.c index afd37ee0a2c37ff14d98ae1bec88928c1cf05790..bb0ef256d6bacf2764d02aeeae1780b9965ddf9e 100644 --- a/dongle_network.c +++ b/dongle_network.c @@ -1,5 +1,7 @@ #include "dongle_network.h" +char *dongle_ip; + static enum { IP, DEV, @@ -217,6 +219,76 @@ int test_get_ip(struct ubus_context *ctx, struct ubus_object *obj, 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; +} + struct ubus_method network_object_methods[] = { UBUS_METHOD("signal_strength", get_signal_strength, ip_policy), UBUS_METHOD("connect", connect_network, ip_policy), @@ -225,7 +297,8 @@ struct ubus_method network_object_methods[] = { UBUS_METHOD("enable_roaming", enable_roaming, ip_policy), UBUS_METHOD("disable_roaming", disable_roaming, ip_policy), UBUS_METHOD("roam_status", roam_status, ip_policy), - UBUS_METHOD("get_ip", test_get_ip, ip_policy) + UBUS_METHOD("get_ip", test_get_ip, ip_policy), + UBUS_METHOD_NOARG("get_ip_invoke", get_ip_invoke) }; struct ubus_object_type network_object_type = UBUS_OBJECT_TYPE("dongle", network_object_methods);