From a426b93ec772fb0e9cafe460b95fbf48b73f84da Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakobols@kth.se>
Date: Thu, 17 May 2018 14:59:32 +0200
Subject: [PATCH] new debug print, smaller structural changes

---
 libmobile.c | 102 ++++++++++++++++++++++++++++------------------------
 libmobile.h |   8 +++++
 2 files changed, 63 insertions(+), 47 deletions(-)

diff --git a/libmobile.c b/libmobile.c
index 67ad8e2..c892856 100644
--- a/libmobile.c
+++ b/libmobile.c
@@ -1,6 +1,7 @@
 /**
  * TODO:
- * 	1. Add more input options when creating APN profile
+ *	1. Add more input options when creating APN profile
+ *	2. Add DEBUG printout
  */
 #include "libmobile.h"
 
@@ -14,6 +15,9 @@ 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);
@@ -62,10 +66,11 @@ static size_t write_func(void *buffer, size_t size, size_t nmemb, void *data)
 
 	str->ptr = realloc(str->ptr, new_len + 1);
 	if (!str->ptr) {
-		printf("not enough memory (realloc returned NULL)\n");
+		debug_print("not enough memory (realloc returned NULL)\n");
 		free(tmp_ptr);
 		return 0;
 	}
+
 	memcpy(str->ptr + str->len, buffer, len);
 	str->ptr[new_len] = '\0';
 	str->len = new_len;
@@ -92,12 +97,13 @@ static int apn_profile_idx(struct json_object *apn_profiles, char *name)
 
 	json_object_object_foreach(apn_profiles, key, val) {
 		struct json_object *profile_name_json;
+		char *profile_name;
 
 		json_object_object_get_ex(val, "profile_name", &profile_name_json);
 
 		if (!profile_name_json)
 			goto fail;
-		char *profile_name = json_object_get_string(profile_name_json);
+		profile_name = json_object_get_string(profile_name_json);
 
 		if (strncmp(profile_name, name, 1024) == 0)
 			return idx;
@@ -120,12 +126,12 @@ fail:
 static int get_apn_profiles_len(void)
 {
 	struct json_object *apn_profiles = mobile_get_apn_profiles();
+	int len = 0;
 
 	if (!apn_profiles) {
-		printf("Failed to get apn profiles from server!\n");
+		debug_print("Failed to get apn profiles from server!\n");
 		goto fail;
 	}
-	int len = 0;
 
 	json_object_object_foreach(apn_profiles, key, val) {
 		int val_type = json_object_get_type(val);
@@ -163,23 +169,24 @@ fail:
  */
 static char *lexer(char **input, char *delimiter)
 {
+	char *token, *substr;
+
 	if (strlen(*input) == 0) {
-		printf("empty input!\n");
+		debug_print("empty input!\n");
 		return NULL;
-	}
-	if (strlen(delimiter) == 0) {
-		printf("empty delimiter!\n");
+	} else if (strlen(delimiter) == 0) {
+		debug_print("empty delimiter!\n");
 		return NULL;
 	}
-	char *token = strstr(*input, delimiter);
+
+	token = strstr(*input, delimiter);
 
 	if (token)
 		*token = '\0';
 	else
 		return NULL;
 
-	char *substr = strdup(*input);
-
+	substr = strdup(*input);
 	*input = token + strlen(delimiter);
 
 	return substr;
@@ -198,15 +205,14 @@ static char *lexer(char **input, char *delimiter)
  */
 static char *get_query_wrapper(char *vars)
 {
+	char query[1024] = {0};
+
 	if (strlen(vars) == 0) {
-		printf("No GET input provided!\n");
+		debug_print("No GET input provided!\n");
 		return NULL;
 	}
-
-	char query[1024] = {0};
-
 	snprintf(query, 1023, "http://192.168.0.1/goform/goform_get_cmd_process?isTest=false&cmd=%s&multi_data=1", vars);
-
+	debug_print("query %s\n", query);
 	return strdup(query);
 }
 
@@ -217,7 +223,7 @@ static char *get_query_wrapper(char *vars)
  *
  * Parameter:
  *		curl - the CURL environment.
- * 		query - The HTTP POST query to execute.
+ *		query - The HTTP POST query to execute.
  *		str - a string struct containing the current data pointer and the length of the data currently written.
  *
  * Returns:
@@ -226,7 +232,8 @@ static char *get_query_wrapper(char *vars)
 static CURLcode perform_post_request(CURL *curl, char *query, struct string *str)
 {
 	curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.1/goform/goform_set_cmd_process");
-	curl_easy_setopt(curl, CURLOPT_REFERER, "http://192.168.0.1/index.html");
+	//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);
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
 	curl_easy_setopt(curl, CURLOPT_WRITEDATA, str);
@@ -241,7 +248,7 @@ static CURLcode perform_post_request(CURL *curl, char *query, struct string *str
  *
  * Parameter:
  *		curl - the CURL environment.
- * 		query - The HTTP GET query to execute.
+ *		query - The HTTP GET query to execute.
  *		str - a string struct containing the current data pointer and the length of the data currently written.
  *
  * Returns:
@@ -250,7 +257,7 @@ static CURLcode perform_post_request(CURL *curl, char *query, struct string *str
 static CURLcode perform_get_request(CURL *curl, char *query, struct string *str)
 {
 	curl_easy_setopt(curl, CURLOPT_URL, query);
-	curl_easy_setopt(curl, CURLOPT_REFERER, "http://192.168.0.1/index.html");
+	curl_easy_setopt(curl, CURLOPT_REFERER, referer_url);
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
 	curl_easy_setopt(curl, CURLOPT_WRITEDATA, str);
 
@@ -263,7 +270,7 @@ static CURLcode perform_get_request(CURL *curl, char *query, struct string *str)
  * Function that prepares curl environment and final POST query, then executes said query.
  *
  * Parameter:
- * 		query - The POST query content to execute.
+ *		query - The POST query content to execute.
  *
  * Returns:
  *		A pointer to a json_object containing the JSON response from the server.
@@ -274,33 +281,38 @@ static struct json_object *prepare_request(char *query, int option)
 	CURL *curl;
 	CURLcode rv;
 	struct string str = {NULL, 0};
+	struct json_object *parsed_response;
 
 	str.ptr = calloc(1, 1);
 	if (!str.ptr) {
-		printf("Error allocating memory with calloc");
+		debug_print("Error allocating memory with calloc");
 		goto fail;
 	}
+
 	curl = curl_easy_init();
 	if (!curl) {
-		printf("Failed to prepare curl environment!\n");
+		debug_print("Failed to prepare curl environment!\n");
 		goto fail_curl;
 	}
+
 	if (option == POST)
 		rv = perform_post_request(curl, query, &str);
 	else if (option == GET) {
 		query = get_query_wrapper(query);
 		rv = perform_get_request(curl, query, &str);
 	}
+
 	if (rv) {
-		printf("error performing curl, %s\n", curl_easy_strerror(rv));
+		debug_print("error performing curl, %s\n", curl_easy_strerror(rv));
 		goto fail;
 	}
 	if (!str.ptr)
 		goto fail_ptr;
-	struct json_object *parsed_response = json_tokener_parse(str.ptr);
 
+	debug_print("%s\n", str.ptr);
+	parsed_response = json_tokener_parse(str.ptr);
 	if (!parsed_response) {
-		printf("No valid JSON, failed parsing!\n");
+		debug_print("No valid JSON, failed parsing!\n");
 		goto fail_json;
 	}
 
@@ -325,7 +337,7 @@ fail:
  * and transforms them into a more easily read and worked with JSON format.
  *
  * Parameters:
- * 		apn_profiles - A json_object pointer containing the original apn configuration returned by the dongle.
+ *		apn_profiles - A json_object pointer containing the original apn configuration returned by the dongle.
  *
  * Returns:
  *		A json_object pointer to apn configurations in a more representative and easier to use format.
@@ -340,20 +352,18 @@ static struct json_object *parse_apn_profiles(struct json_object *apn_profiles)
 
 	json_object_object_foreach(apn_profiles, key, val) {
 		char *apn_string = json_object_get_string(val);
-
-		if (strlen(apn_string) <= 0)
-			goto finished;
 		int i = 0;
 		struct json_object *apn_profile = json_object_new_object();
 		char *field_val;
+		char name[1024] = {0};
 
+		if (strlen(apn_string) <= 0)
+			goto finished;
 		while ((field_val = lexer(&apn_string, "($)")) != NULL) {
 			json_object_object_add(apn_profile, field_names[i], json_object_new_string(field_val));
 			i++;
 			free(field_val);
 		}
-		char name[1024] = {0};
-
 		snprintf(name, 1023, "apn_config_%d", apn_counter);
 
 		json_object_object_add(parsed_profiles, name, json_object_get(apn_profile));
@@ -380,16 +390,16 @@ struct json_object *mobile_disconnect_network(void)
 struct json_object *mobile_delete_apn(char *name)
 {
 	struct json_object *apn_profiles = mobile_get_apn_profiles();
+	char query[1024] = {0};
+	int idx;
 
 	if (!apn_profiles) {
-		printf("no response!\n");
+		debug_print("no response!\n");
 		goto fail;
 	}
-	char query[1024] = {0};
-	int idx = apn_profile_idx(apn_profiles, name);
-
+	idx = apn_profile_idx(apn_profiles, name);
 	if (idx < 0) {
-		printf("APN not found in list!\n");
+		debug_print("APN not found in list!\n");
 		goto fail_idx;
 	}
 
@@ -448,7 +458,7 @@ struct json_object *mobile_get_apn_profiles(void)
 	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);
 
 	if (!apn_profiles) {
-		printf("Error getting profiles!\n");
+		debug_print("Error getting profiles!\n");
 		goto fail;
 	}
 	return parse_apn_profiles(apn_profiles);
@@ -460,9 +470,7 @@ struct json_object *mobile_create_apn_profile(char *profile_name, char *wan_apn,
 {
 	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);
+	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);
 }
@@ -470,19 +478,19 @@ struct json_object *mobile_create_apn_profile(char *profile_name, char *wan_apn,
 struct json_object *mobile_set_apn_profile(char *name)
 {
 	struct json_object *apn_profiles = mobile_get_apn_profiles();
+	int idx;
+	char query[1024] = {0};
 
 	if (!apn_profiles) {
-		printf("no response!\n");
+		debug_print("no response!\n");
 		goto fail;
 	}
-	int idx = apn_profile_idx(apn_profiles, name);
+	idx = apn_profile_idx(apn_profiles, name);
 
 	if (idx < 0) {
-		printf("couldnt find idx, no such profile!\n");
+		debug_print("couldnt find idx, no such profile!\n");
 		goto free_idx;
 	}
-	char query[1024] = {0};
-
 	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);
diff --git a/libmobile.h b/libmobile.h
index 71586a4..9c3e8a2 100644
--- a/libmobile.h
+++ b/libmobile.h
@@ -4,6 +4,14 @@
 #include <json-c/json.h>
 #include <string.h>
 
+#define DEBUG 1
+#define debug_print(...)                  \
+	do                                    \
+	{                                     \
+		if (DEBUG)                        \
+			fprintf(stderr, __VA_ARGS__); \
+	} while (0)
+
 /***************************************************
  * Libmobile - A 4G Dongle library
  *
-- 
GitLab