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

fix memory leak in library, move declaration and documentation of internal...

fix memory leak in library, move declaration and documentation of internal functions, add some error handling in library
parent 28bffa13
Branches
Tags
No related merge requests found
...@@ -5,12 +5,39 @@ struct string { ...@@ -5,12 +5,39 @@ struct string {
size_t len; size_t len;
}; };
void curl_cleaner(CURLcode *curl);
size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp);
int apn_profile_idx(struct json_object *apn_profiles, char *name);
int get_apn_profiles_len(void);
/**
* Function: curl_cleaner
*
* Free's the curl environment.
*
* Parameters:
* curl - The curl easy handle variable.
*/
void curl_cleaner(CURLcode *curl) void curl_cleaner(CURLcode *curl)
{ {
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_global_cleanup(); curl_global_cleanup();
} }
/**
* Function: write_func
*
* The callback function from performing the curl command, response from server will be parsed here.
*
* Parameters:
* buffer - Contains chunk of response from server.
* size - Size (byte) of type in buffer array.
* nmemb - Number of members in buffer.
* userp - Pointer to area allocated for storing results from server.
*
* Returns:
* Number of bytes managed (size*nmemb).
*/
size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp) size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp)
{ {
struct string *str = (struct string *)userp; struct string *str = (struct string *)userp;
...@@ -28,13 +55,26 @@ size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp) ...@@ -28,13 +55,26 @@ size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp)
return size * nmemb; return size * nmemb;
} }
/**
* Function: apn_profile_idx
*
* Finds the index of a given profile name from all available profiles.
*
* Parameters:
* apn_profiles - json_object pointer to apn_profiles (gotten from a previous call to <mobile_get_apn_profiles>)
* name - Name of the APN index for which will be searched.
*
* Returns:
* Index of the APN on success.
* -1 on failure.
*/
int apn_profile_idx(struct json_object *apn_profiles, char *name) int apn_profile_idx(struct json_object *apn_profiles, char *name)
{ {
int idx = 0; int idx = 0;
json_object_object_foreach(apn_profiles, key, val) { json_object_object_foreach(apn_profiles, key, val) {
char *apn_profile = json_object_get_string(val); char *apn_profile = json_object_get_string(val);
if (strlen(apn_profile) <= 0) if (!apn_profile || strlen(apn_profile) <= 0)
break; break;
char *apn_name = strtok(apn_profile, "($)"); char *apn_name = strtok(apn_profile, "($)");
...@@ -46,23 +86,47 @@ int apn_profile_idx(struct json_object *apn_profiles, char *name) ...@@ -46,23 +86,47 @@ int apn_profile_idx(struct json_object *apn_profiles, char *name)
return -1; return -1;
} }
/**
* Function: get_apn_profiles_len
*
* Finds the number of APN profiles available.
*
* Returns:
* Number of APN profiles available on success.
* -1 on failure.
*/
int get_apn_profiles_len(void) int get_apn_profiles_len(void)
{ {
char *response = mobile_get_apn_profiles(); char *response = mobile_get_apn_profiles();
struct json_object *apn_profiles = json_tokener_parse(response);
if (!response) {
printf("no response!\n");
goto fail;
}
struct json_object *parsed_response = json_tokener_parse(response);
if(!parsed_response) {
printf("no valid json to parse!");
goto free_response;
}
int len = 0; int len = 0;
json_object_object_foreach(apn_profiles, key, val) { json_object_object_foreach(parsed_response, key, val) {
if (strlen(json_object_get_string(val)) > 0) if (strlen(json_object_get_string(val)) > 0)
len++; len++;
else else
goto finished; goto success;
} }
json_object_put(apn_profiles); json_object_put(parsed_response);
free_response:
free(response);
fail:
return -1; return -1;
finished:
json_object_put(apn_profiles); success:
json_object_put(parsed_response);
free(response);
return len; return len;
} }
...@@ -84,26 +148,38 @@ char *mobile_disconnect_network(void) ...@@ -84,26 +148,38 @@ char *mobile_disconnect_network(void)
char *mobile_delete_apn(char *name) char *mobile_delete_apn(char *name)
{ {
char *apn_profiles = mobile_get_apn_profiles(); char *response = mobile_get_apn_profiles();
struct json_object *apn_profiles_json = json_tokener_parse(apn_profiles);
char query[1024] = {0};
int idx = apn_profile_idx(apn_profiles_json, name); if (!response) {
printf("no response!\n");
goto fail;
}
struct json_object *parsed_response = json_tokener_parse(response);
if(!parsed_response) {
printf("no valid json to parse!");
goto free_response;
}
char query[1024] = {0};
int idx = apn_profile_idx(parsed_response, name);
free(apn_profiles);
if (idx < 0) { if (idx < 0) {
printf("APN not found in list!\n"); printf("APN not found in list!\n");
goto fail; goto free_all;
} }
strncpy(query, "isTest=false&apn_action=delete&apn_mode=manual&index=", 1023); strncpy(query, "isTest=false&apn_action=delete&apn_mode=manual&index=", 1023);
sprintf(query + strlen(query), "%d", idx); sprintf(query + strlen(query), "%d", idx);
strncat(query + strlen(query), "&goformId=APN_PROC_EX", 1023); strncat(query + strlen(query), "&goformId=APN_PROC_EX", 1023);
json_object_put(apn_profiles_json); json_object_put(parsed_response);
free(response);
return mobile_post_request(query); return mobile_post_request(query);
free_all:
json_object_put(parsed_response);
free_response:
free(response);
fail: fail:
json_object_put(apn_profiles_json);
return NULL; return NULL;
} }
...@@ -168,15 +244,39 @@ char *mobile_create_apn_profile(char *name) ...@@ -168,15 +244,39 @@ char *mobile_create_apn_profile(char *name)
char *mobile_set_apn_profile(char *name) char *mobile_set_apn_profile(char *name)
{ {
char *apn_profiles = mobile_get_apn_profiles(); char *response = mobile_get_apn_profiles();
struct json_object *apn_profiles_json = json_tokener_parse(apn_profiles);
int idx = apn_profile_idx(apn_profiles_json, name); if (!response) {
printf("no response!\n");
goto fail;
}
struct json_object *parsed_response = json_tokener_parse(response);
if(!parsed_response) {
printf("no valid json to parse!");
goto free_response;
}
int idx = apn_profile_idx(parsed_response, name);
if (idx < 0) {
printf("couldnt find idx, no such profile!\n");
goto free_all;
}
char query[1024] = {0}; char query[1024] = {0};
strncpy(query, "isTest=false&goformId=APN_PROC_EX&apn_mode=manual&apn_action=set_default&set_default_flag=1&pdp_type=IP&index=", 1023); strncpy(query, "isTest=false&goformId=APN_PROC_EX&apn_mode=manual&apn_action=set_default&set_default_flag=1&pdp_type=IP&index=", 1023);
sprintf(query + strlen(query), "%d", idx); sprintf(query + strlen(query), "%d", idx);
json_object_put(parsed_response);
free(response);
return mobile_post_request(query); return mobile_post_request(query);
free_all:
json_object_put(parsed_response);
free_response:
free(response);
fail:
return NULL;
} }
......
...@@ -41,58 +41,6 @@ ...@@ -41,58 +41,6 @@
* *
***************************************************/ ***************************************************/
/**
* Function: curl_cleaner
*
* Free's the curl environment.
*
* Parameters:
* curl - The curl easy handle variable.
*/
void curl_cleaner(CURLcode *curl);
/**
* Function: write_func
*
* The callback function from performing the curl command, response from server will be parsed here.
*
* Parameters:
* buffer - Contains chunk of response from server.
* size - Size (byte) of type in buffer array.
* nmemb - Number of members in buffer.
* userp - Pointer to area allocated for storing results from server.
*
* Returns:
* Number of bytes managed (size*nmemb).
*/
size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp);
/**
* Function: apn_profile_idx
*
* Finds the index of a given profile name from all available profiles.
*
* Parameters:
* apn_profiles - json_object pointer to apn_profiles (gotten from a previous call to <mobile_get_apn_profiles>)
* name - Name of the APN index for which will be searched.
*
* Returns:
* Index of the APN on success.
* -1 on failure.
*/
int apn_profile_idx(struct json_object *apn_profiles, char *name);
/**
* Function: get_apn_profiles_len
*
* Finds the number of APN profiles available.
*
* Returns:
* Number of APN profiles available on success.
* -1 on failure.
*/
int get_apn_profiles_len(void);
/** /**
* Function: mobile_connect_network * Function: mobile_connect_network
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment