diff --git a/dongle.c b/dongle.c index b3e709106d7e3d06419e719b24a68b34b675d80e..771468db2d98c5ccd07b39ed488ee3a09cb170a0 100644 --- a/dongle.c +++ b/dongle.c @@ -8,9 +8,9 @@ struct ubus_context *ctx; int debug; -static struct option long_options[] = -{ - {"debug", required_argument, NULL, 'd'} +static struct option long_options[] = { + {"debug", required_argument, NULL, 'd'}, + {0, 0, 0, 0} }; int parse_args(int argc, char **argv) @@ -25,18 +25,20 @@ int parse_args(int argc, char **argv) printf("%s: option '-%c' is invalid.\n", argv[0], optopt); //cant exactly do debug print in here... goto fail; } + goto done; break; case ':': - debug_print(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt); + printf(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt); goto fail; break; case '?': - debug_print(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], optopt); + printf(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], optopt); goto fail; break; } } +done: return 0; fail: return -1; @@ -61,6 +63,8 @@ int main(int argc, char **argv) if (rv < 0) goto fail; + printf("hello\n"); + uloop_init(); init_ubus(); diff --git a/libmobile.c b/libmobile.c index ed179bcb9ef54116d55a94fb9dd9e5a4a0fb13e6..6e0d91b629f6f2d3b3ff7f9176dc392d5c5865ae 100644 --- a/libmobile.c +++ b/libmobile.c @@ -10,13 +10,10 @@ enum { GET }; -char *referer_url = "http://192.168.0.1/index.html"; -char *base_url = "http://192.168.0.1/goform/goform_set_cmd_process"; - static void curl_cleaner(CURLcode *curl); static size_t write_func(void *buffer, size_t size, size_t nmemb, void *data); static int apn_profile_idx(struct json_object *apn_profiles, char *name); -static int get_apn_profiles_len(void); +static int get_apn_profiles_len(char *ip_addr); static char *lexer(char **input, char *delimiter); static char *get_query_wrapper(char *vars); static CURLcode perform_post_request(CURL *curl, char *query, struct string *str); @@ -118,7 +115,7 @@ fail: * Number of APN profiles available on success. * -1 on failure. */ -static int get_apn_profiles_len(void) +static int get_apn_profiles_len(char *ip_addr) { struct json_object *apn_profiles = mobile_get_apn_profiles(); int len = 0; @@ -198,7 +195,7 @@ static char *lexer(char **input, char *delimiter) * The entire query on success. * NULL on failure. */ -static char *get_query_wrapper(char *vars) +static char *get_query_wrapper(char *ip_addr, char *vars) { char query[1024] = {0}; @@ -206,7 +203,7 @@ static char *get_query_wrapper(char *vars) //debug_print("No GET input provided!\n"); return NULL; } - snprintf(query, 1023, "http://192.168.0.1/goform/goform_get_cmd_process?isTest=false&cmd=%s&multi_data=1", vars); + snprintf(query, 1023, "http://%s/goform/goform_get_cmd_process?isTest=false&cmd=%s&multi_data=1", ip_addr, vars); //debug_print("query %s\n", query); return strdup(query); } @@ -224,9 +221,13 @@ static char *get_query_wrapper(char *vars) * Returns: * The CURLcode indicating the success or failure of the curl execution. */ -static CURLcode perform_post_request(CURL *curl, char *query, struct string *str) +static CURLcode perform_post_request(char *ip_addr, CURL *curl, char *query, struct string *str) { - curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.1/goform/goform_set_cmd_process"); + char post_url[1024] = {0}, referer_url[1024] = {0}; + + sprintf(post_url, 1023, "http://%s/index.html", ip_addr); + sprintf(referer_url, 1023, "http://%s/goform/goform_set_cmd_process", ip_addr); + curl_easy_setopt(curl, CURLOPT_URL, post_url); //curl_easy_setopt(curl, CURLOPT_URL, base_url); //why doesn't this work? curl_easy_setopt(curl, CURLOPT_REFERER, referer_url); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query); @@ -271,7 +272,7 @@ static CURLcode perform_get_request(CURL *curl, char *query, struct string *str) * A pointer to a json_object containing the JSON response from the server. * NULL on failure. */ -static struct json_object *prepare_request(char *query, int option) +static struct json_object *prepare_request(char *ip_addr, char *query, int option) { CURL *curl; CURLcode rv; @@ -291,9 +292,9 @@ static struct json_object *prepare_request(char *query, int option) } if (option == POST) - rv = perform_post_request(curl, query, &str); + rv = perform_post_request(ip_addr, curl, query, &str); else if (option == GET) { - query = get_query_wrapper(query); + query = get_query_wrapper(ip_addr, query); rv = perform_get_request(curl, query, &str); } else goto fail_request; @@ -374,17 +375,17 @@ finished: } -struct json_object *mobile_connect_network(void) +struct json_object *mobile_connect_network(char *ip_addr) { - return prepare_request("isTest=false&goformId=CONNECT_NETWORK", POST); + return prepare_request(ip_addr, "isTest=false&goformId=CONNECT_NETWORK", POST); } -struct json_object *mobile_disconnect_network(void) +struct json_object *mobile_disconnect_network(char *ip_addr) { - return prepare_request("isTest=false&goformId=DISCONNECT_NETWORK", POST); + return prepare_request(ip_addr, "isTest=false&goformId=DISCONNECT_NETWORK", POST); } -struct json_object *mobile_delete_apn(char *name) +struct json_object *mobile_delete_apn(char *ip_addr, char *name) { struct json_object *apn_profiles = mobile_get_apn_profiles(); char query[1024] = {0}; @@ -402,7 +403,7 @@ struct json_object *mobile_delete_apn(char *name) snprintf(query, 1023, "isTest=false&apn_action=delete&apn_mode=manual&index=%d&goformId=APN_PROC_EX", idx); json_object_put(apn_profiles); - return prepare_request(query, POST); + return prepare_request(ip_addr, query, POST); fail_idx: json_object_put(apn_profiles); @@ -410,27 +411,27 @@ fail: return NULL; } -struct json_object *mobile_enable_roaming(void) +struct json_object *mobile_enable_roaming(char *ip_addr) { - return prepare_request("isTest=false&goformId=SET_CONNECTION_MODE&roam_setting_option=on", POST); + return prepare_request(ip_addr, "isTest=false&goformId=SET_CONNECTION_MODE&roam_setting_option=on", POST); } -struct json_object *mobile_disable_roaming(void) +struct json_object *mobile_disable_roaming(char *ip_addr) { - return prepare_request("isTest=false&goformId=SET_CONNECTION_MODE&roam_setting_option=off", POST); + return prepare_request(ip_addr, "isTest=false&goformId=SET_CONNECTION_MODE&roam_setting_option=off", POST); } -struct json_object *mobile_get_roam_status(void) +struct json_object *mobile_get_roam_status(char *ip_addr) { - return prepare_request("roam_setting_option", GET); + return prepare_request(ip_addr, "roam_setting_option", GET); } -struct json_object *mobile_get_current_apn(void) +struct json_object *mobile_get_current_apn(char *ip_addr) { struct json_object *idx_json, *apn_profiles, *tmp, *current_profile; int idx, counter = 0; - idx_json = prepare_request("Current_index", GET); + idx_json = prepare_request(ip_addr, "Current_index", GET); if (!idx_json) { //debug_print("couldn't find index\n"); goto fail; @@ -461,34 +462,34 @@ fail: return NULL; } -struct json_object *mobile_get_wan_apn(void) +struct json_object *mobile_get_wan_apn(char *ip_addr) { - return prepare_request("wan_apn", GET); + return prepare_request(ip_addr, "wan_apn", GET); } -struct json_object *mobile_get_remaining_tries(void) +struct json_object *mobile_get_remaining_tries(char *ip_addr) { - return prepare_request("pinnumber", GET); + return prepare_request(ip_addr, "pinnumber", GET); } -struct json_object *mobile_get_pin_status(void) +struct json_object *mobile_get_pin_status(char *ip_addr) { - return prepare_request("pin_status", GET); + return prepare_request(ip_addr, "pin_status", GET); } -struct json_object *mobile_get_rssi(void) +struct json_object *mobile_get_rssi(char *ip_addr) { - return prepare_request("rssi", GET); + return prepare_request(ip_addr, "rssi", GET); } -struct json_object *mobile_get_modem_state(void) +struct json_object *mobile_get_modem_state(char *ip_addr) { - return prepare_request("&sms_received_flag_flag=0&sts_received_flag_flag=0&cmd=modem_main_state%2Cpin_status%2Cloginfo%2Cnew_version_state%2Ccurrent_upgrade_state%2Cis_mandatory%2Csms_received_flag%2Csts_received_flag%2Csignalbar%2Cnetwork_type%2Cnetwork_provider%2Cppp_status%2CEX_SSID1%2Cex_wifi_status%2CEX_wifi_profile%2Cm_ssid_enable%2Csms_unread_num%2CRadioOff%2Csimcard_roam%2Clan_ipaddr%2Cstation_mac%2Cbattery_charging%2Cbattery_vol_percent%2Cbattery_pers%2Cspn_display_flag%2Cplmn_display_flag%2Cspn_name_data%2Cspn_b1_flag%2Cspn_b2_flag%2Crealtime_tx_bytes%2Crealtime_rx_bytes%2Crealtime_time%2Crealtime_tx_thrpt%2Crealtime_rx_thrpt%2Cmonthly_rx_bytes%2Cmonthly_tx_bytes%2Cmonthly_time%2Cdate_month%2Cdata_volume_limit_switch%2Cdata_volume_limit_size%2Cdata_volume_alert_percent%2Cdata_volume_limit_unit%2Croam_setting_option%2Cupg_roam_switch%2Chplmn", GET); + return prepare_request(ip_addr, "&sms_received_flag_flag=0&sts_received_flag_flag=0&cmd=modem_main_state%2Cpin_status%2Cloginfo%2Cnew_version_state%2Ccurrent_upgrade_state%2Cis_mandatory%2Csms_received_flag%2Csts_received_flag%2Csignalbar%2Cnetwork_type%2Cnetwork_provider%2Cppp_status%2CEX_SSID1%2Cex_wifi_status%2CEX_wifi_profile%2Cm_ssid_enable%2Csms_unread_num%2CRadioOff%2Csimcard_roam%2Clan_ipaddr%2Cstation_mac%2Cbattery_charging%2Cbattery_vol_percent%2Cbattery_pers%2Cspn_display_flag%2Cplmn_display_flag%2Cspn_name_data%2Cspn_b1_flag%2Cspn_b2_flag%2Crealtime_tx_bytes%2Crealtime_rx_bytes%2Crealtime_time%2Crealtime_tx_thrpt%2Crealtime_rx_thrpt%2Cmonthly_rx_bytes%2Cmonthly_tx_bytes%2Cmonthly_time%2Cdate_month%2Cdata_volume_limit_switch%2Cdata_volume_limit_size%2Cdata_volume_alert_percent%2Cdata_volume_limit_unit%2Croam_setting_option%2Cupg_roam_switch%2Chplmn", GET); } -struct json_object *mobile_get_apn_profiles(void) +struct json_object *mobile_get_apn_profiles(char *ip_addr) { - struct json_object *apn_profiles = prepare_request("APN_config0,APN_config1,APN_config2,APN_config3,APN_config4,APN_config5,APN_config6,APN_config7,APN_config8,APN_config9,APN_config10,APN_config11,APN_config12,APN_config13,APN_config14,APN_config15,APN_config16,APN_config17,APN_config18,APN_config19", GET); + struct json_object *apn_profiles = prepare_request(ip_addr, "APN_config0,APN_config1,APN_config2,APN_config3,APN_config4,APN_config5,APN_config6,APN_config7,APN_config8,APN_config9,APN_config10,APN_config11,APN_config12,APN_config13,APN_config14,APN_config15,APN_config16,APN_config17,APN_config18,APN_config19", GET); if (!apn_profiles) { //debug_print("Error getting profiles!\n"); @@ -499,16 +500,16 @@ fail: return NULL; } -struct json_object *mobile_create_apn_profile(char *profile_name, char *wan_apn, char *pdp_type) +struct json_object *mobile_create_apn_profile(char *ip_addr, char *profile_name, char *wan_apn, char *pdp_type) { char query[1024] = {0}; snprintf(query, 1023, "isTest=false&goformId=APN_PROC_EX&apn_action=save&apn_mode=manual&profile_name=%s&wan_dial=*99%23&apn_select=manual&pdp_type=%s&pdp_select=auto&pdp_addr=&index=%d&wan_apn=%s&ppp_auth_mode=none&ppp_username=&ppp_passwd=&dns_mode=auto&prefer_dns_manual=&standby_dns_manual=", profile_name, pdp_type, get_apn_profiles_len(), wan_apn); - return prepare_request(query, POST); + return prepare_request(ip_addr, query, POST); } -struct json_object *mobile_set_apn_profile(char *name) +struct json_object *mobile_set_apn_profile(char *ip_addr, char *name) { struct json_object *apn_profiles = mobile_get_apn_profiles(); int idx; @@ -527,36 +528,36 @@ struct json_object *mobile_set_apn_profile(char *name) snprintf(query, 1023, "isTest=false&goformId=APN_PROC_EX&apn_mode=manual&apn_action=set_default&set_default_flag=1&pdp_type=IP&index=%d", idx); json_object_put(apn_profiles); - return prepare_request(query, POST); + return prepare_request(ip_addr, query, POST); free_idx: json_object_put(apn_profiles); fail: return NULL; } -struct json_object *mobile_enable_pin(char *pin) +struct json_object *mobile_enable_pin(char *ip_addr, char *pin) { char query[1024] = {0}; snprintf(query, 1023, "isTest=false&goformId=ENABLE_PIN&OldPinNumber=%s&pin_save_flag=0&isTest=false", pin); - return prepare_request(query, POST); + return prepare_request(ip_addr, query, POST); } -struct json_object *mobile_set_pin(char *current_pin, char *new_pin) +struct json_object *mobile_set_pin(char *ip_addr, char *current_pin, char *new_pin) { char query[1024] = {0}; snprintf(query, 1023, "isTest=false&goformId=ENABLE_PIN&OldPinNumber=%s&NewPinNumber=%s&pin_save_flag=0&isTest=false", current_pin, new_pin); - return prepare_request(query, POST); + return prepare_request(ip_addr, query, POST); } -struct json_object *mobile_disable_pin(char *pin) +struct json_object *mobile_disable_pin(char *ip_addr, char *pin) { char query[1024] = {0}; snprintf(query, 1023, "isTest=false&goformId=DISABLE_PIN&OldPinNumber=%s&pin_save_flag=0&isTest=false", pin); - return prepare_request(query, POST); + return prepare_request(ip_addr, query, POST); } diff --git a/libmobile.h b/libmobile.h index b5c3b4afc5ddc268a7ed16a3ed4f5cca8f49ce7a..1e378d02a1fd93f23cb794d0c4b23d5266cb3d0b 100644 --- a/libmobile.h +++ b/libmobile.h @@ -50,7 +50,7 @@ * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_connect_network(void); +struct json_object *mobile_connect_network(char *ip_addr, void); /** * Function: mobile_disconnect_network @@ -61,7 +61,7 @@ struct json_object *mobile_connect_network(void); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_disconnect_network(void); +struct json_object *mobile_disconnect_network(char *ip_addr, void); /** * Function: mobile_delete_apn @@ -75,7 +75,7 @@ struct json_object *mobile_disconnect_network(void); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_delete_apn(char *name); +struct json_object *mobile_delete_apn(char *ip_addr, char *name); /** * Function: mobile_enable_roaming @@ -86,7 +86,7 @@ struct json_object *mobile_delete_apn(char *name); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_enable_roaming(void); +struct json_object *mobile_enable_roaming(char *ip_addr, void); /** * Function: mobile_disable_roaming @@ -97,7 +97,7 @@ struct json_object *mobile_enable_roaming(void); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_disable_roaming(void); +struct json_object *mobile_disable_roaming(char *ip_addr, void); /** * Function: mobile_disable_roaming @@ -108,7 +108,7 @@ struct json_object *mobile_disable_roaming(void); * A pointer to a json_object containing the JSON response from the server. {"roam_setting_option": "on"/"off" } * NULL on failure. */ -struct json_object *mobile_get_roam_status(void); +struct json_object *mobile_get_roam_status(char *ip_addr, void); /** * Function: mobile_get_current_apn @@ -119,7 +119,7 @@ struct json_object *mobile_get_roam_status(void); * A pointer to a json_object containing the JSON profile on success. * NULL on failure. */ -struct json_object *mobile_get_current_apn(void); +struct json_object *mobile_get_current_apn(char *ip_addr, void); /** * Function: mobile_get_wan_apn @@ -130,7 +130,7 @@ struct json_object *mobile_get_current_apn(void); * A pointer to a json_object containing the JSON response from the server. {"wan_apn": "<name>" } on success. * NULL on failure. */ -struct json_object *mobile_get_wan_apn(void); +struct json_object *mobile_get_wan_apn(char *ip_addr, void); /** * Function: mobile_get_remaining_tries @@ -141,7 +141,7 @@ struct json_object *mobile_get_wan_apn(void); * A pointer to a json_object containing the JSON response from the server. {"pinnumber": {1..3} } on success. * NULL on failure. */ -struct json_object *mobile_get_remaining_tries(void); +struct json_object *mobile_get_remaining_tries(char *ip_addr, void); /** * Function: mobile_get_pin_status @@ -152,7 +152,7 @@ struct json_object *mobile_get_remaining_tries(void); * A pointer to a json_object containing the JSON response from the server. {"pinnumber": {0..1} } on success. * NULL on failure. */ -struct json_object *mobile_get_pin_status(void); +struct json_object *mobile_get_pin_status(char *ip_addr, void); /** * Function: mobile_get_rssi @@ -163,7 +163,7 @@ struct json_object *mobile_get_pin_status(void); * A pointer to a json_object containing the JSON response from the server. {"rssi": {} } on success. * NULL on failure. */ -struct json_object *mobile_get_rssi(void); +struct json_object *mobile_get_rssi(char *ip_addr, void); /** * Function: mobile_get_modem_state @@ -174,7 +174,7 @@ struct json_object *mobile_get_rssi(void); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_get_modem_state(void); +struct json_object *mobile_get_modem_state(char *ip_addr, void); /** * Function: mobile_get_apn_profiles @@ -185,7 +185,7 @@ struct json_object *mobile_get_modem_state(void); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_get_apn_profiles(void); +struct json_object *mobile_get_apn_profiles(char *ip_addr, void); /** * Function: mobile_create_apn_profile @@ -201,7 +201,7 @@ struct json_object *mobile_get_apn_profiles(void); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_create_apn_profile(char *profile_name, char *wan_apn, char *pdp_type); +struct json_object *mobile_create_apn_profile(char *ip_addr, char *profile_name, char *wan_apn, char *pdp_type); /** * Function: mobile_set_apn_profile @@ -215,7 +215,7 @@ struct json_object *mobile_create_apn_profile(char *profile_name, char *wan_apn, * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_set_apn_profile(char *name); +struct json_object *mobile_set_apn_profile(char *ip_addr, char *name); /** * Function: mobile_enable_pin @@ -229,7 +229,7 @@ struct json_object *mobile_set_apn_profile(char *name); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_enable_pin(char *pin); +struct json_object *mobile_enable_pin(char *ip_addr, char *pin); /** * Function: mobile_set_pin @@ -244,7 +244,7 @@ struct json_object *mobile_enable_pin(char *pin); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_set_pin(char *current_pin, char *new_pin); +struct json_object *mobile_set_pin(char *ip_addr, char *current_pin, char *new_pin); /** * Function: mobile_disable_pin @@ -258,5 +258,5 @@ struct json_object *mobile_set_pin(char *current_pin, char *new_pin); * A pointer to a json_object containing the JSON response from the server. {"result": "success"/"failure"} * NULL on failure. */ -struct json_object *mobile_disable_pin(char *pin); +struct json_object *mobile_disable_pin(char *ip_addr, char *pin); #endif