diff --git a/src/modules/lost/functions.c b/src/modules/lost/functions.c index 5f8a6d79f46..70316d0fc21 100644 --- a/src/modules/lost/functions.c +++ b/src/modules/lost/functions.c @@ -92,6 +92,9 @@ char *lost_held_type(char *type, int *exact, int *lgth) int len = 0; ret = (char *)pkg_malloc(1); + if(ret == NULL) + goto err; + memset(ret, 0, 1); *lgth = 0; @@ -269,7 +272,6 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url, held = lost_new_held(did, rtype, held_resp_time, held_exact_type); if(held == NULL) { LM_ERR("held object allocation failed\n"); - lost_free_string(&idhdr); /* clean up */ goto err; } que.s = lost_held_location_request(held, &que.len); @@ -303,7 +305,7 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url, /* is it a name or ip ... check nameinfo (reverse lookup) */ len = 0; ipstr = lost_copy_string(host, &len); - if(ipstr != NULL && len > 0) { + if(ipstr != NULL) { name.s = &(istr[0]); name.len = NI_MAXHOST; if(lost_get_nameinfo(ipstr, &name, flag) > 0) { @@ -320,20 +322,18 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url, } pkg_free(ipstr); /* clean up */ ipstr = NULL; + } else { + LM_ERR("could not copy host info\n"); } url.s = &(ustr[0]); url.len = MAX_URI_SIZE; if((naptr = lost_naptr_lookup(host, &sheld, &url)) == 0) { LM_ERR("NAPTR failed on [%.*s]\n", host.len, host.s); - lost_free_string(&que); /* clean up */ - lost_free_string(&idhdr); goto err; } } else { LM_ERR("failed to get location service for [%.*s]\n", did.len, did.s); - lost_free_string(&que); /* clean up */ - lost_free_string(&idhdr); goto err; } @@ -342,21 +342,20 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url, /* curl doesn't like str */ len = 0; lisurl = lost_copy_string(url, &len); - /* send to service */ - if(lisurl != NULL && len > 0) { - curl = httpapi.http_client_query_c( - _m, lisurl, &res, que.s, mtheld, ACCEPT_HDR); - pkg_free(lisurl); /*clean up */ - lisurl = NULL; - } else { + if(lisurl == NULL) { LM_ERR("could not copy POST url\n"); goto err; } + /* send to service */ + curl = httpapi.http_client_query_c( + _m, lisurl, &res, que.s, mtheld, ACCEPT_HDR); + pkg_free(lisurl); /*clean up */ + lisurl = NULL; } /* only HTTP 2xx responses are accepted */ if(curl >= 300 || curl < 100) { if(con.s != NULL && con.len > 0) { - LM_ERR("POST [%.*s] failed with error: %d\n", con.len, con.s, curl); + LM_ERR("[%.*s] failed with error: %d\n", con.len, con.s, curl); } else { LM_ERR("POST [%.*s] failed with error: %d\n", url.len, url.s, curl); } @@ -436,26 +435,22 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url, } else { len = 0; heldreq = lost_held_post_request(&len, 0, NULL); - if(len > 0) { - - LM_DBG("held POST request: [%.*s]\n", len, heldreq); - - curl = httpapi.http_client_query_c( - _m, geo.s, &pidfurl, heldreq, mtheld, ACCEPT_HDR); - pkg_free(heldreq); /* clean up */ - heldreq = NULL; - } else { + if(heldreq == NULL) { LM_ERR("could not create POST request\n"); - lost_free_string(&pidfurl); /* clean up */ goto err; } + + LM_DBG("held POST request: [%.*s]\n", len, heldreq); + + curl = httpapi.http_client_query_c( + _m, geo.s, &pidfurl, heldreq, mtheld, ACCEPT_HDR); + pkg_free(heldreq); /* clean up */ + heldreq = NULL; } /* only HTTP 2xx responses are accepted */ if(curl >= 300 || curl < 100) { LM_ERR("GET [%.*s] failed with error: %d\n", pidfurl.len, pidfurl.s, curl); - /* clean up */ - lost_free_string(&pidfurl); goto err; } if(pidfurl.len == 0) { @@ -470,7 +465,7 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url, res.len = pidfurl.len; } } - /* error received */ + /* error received */ } else if(xmlStrcmp(root->name, (const xmlChar *)"error") == 0) { LM_DBG("HELD error response [%.*s]\n", res.len, res.s); @@ -522,10 +517,15 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url, return (err.len > 0) ? LOST_SERVER_ERROR : LOST_SUCCESS; err: - /* clean up */ + /* clean up pointer */ + lost_free_string(&que); + lost_free_string(&idhdr); + lost_free_string(&pidfurl); + /* clean up xml */ if(doc != NULL) { xmlFreeDoc(doc); } + /* clean up string */ if(res.s != NULL && res.len > 0) { lost_free_string(&res); } @@ -600,10 +600,24 @@ int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf, goto err; } if(rtm.len == 0) { + /* default: rtime = 0 */ LM_WARN("no response time found\n"); - ltime = 0; } else { ltime = strtol(rtm.s, &ptr, 10); + /* look for a number ... */ + if((ltime > 0) && (strlen(ptr) == 0)) { + /* responseTime: milliseconds */ + rtime = ltime; + /* or a string */ + } else if((ltime == 0) && (strlen(ptr) > 0)) { + if(strncasecmp(ptr, HELD_ED, strlen(HELD_ED)) == 0) { + /* responseTime: emergencyDispatch */ + rtime = -1; + } else if(strncasecmp(ptr, HELD_ER, strlen(HELD_ER)) == 0) { + /* responseTime: emergencyRouting */ + rtime = 0; + } + } } } @@ -620,26 +634,13 @@ int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf, len = 0; /* response type string sanity check */ rtype = lost_held_type(rtp.s, &exact, &len); - if(len == 0) { + /* default value will be used if nothing was returned */ + if(rtype == NULL) { LM_WARN("cannot normalize [%.*s]\n", rtp.len, rtp.s); - rtype = NULL; } } } - if((ltime > 0) && (strlen(ptr) == 0)) { - /* responseTime: milliseconds */ - rtime = ltime; - } else if((ltime == 0) && (strlen(ptr) > 0)) { - if(strncasecmp(ptr, HELD_ED, strlen(HELD_ED)) == 0) { - /* responseTime: emergencyDispatch */ - rtime = -1; - } else if(strncasecmp(ptr, HELD_ER, strlen(HELD_ER)) == 0) { - /* responseTime: emergencyRouting */ - rtime = 0; - } - } - /* get the HELD request body */ heldreq = lost_held_post_request(&len, rtime, rtype); @@ -649,7 +650,7 @@ int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf, rtype = NULL; } - if(heldreq != NULL && len == 0) { + if(heldreq == NULL) { LM_ERR("could not create POST request\n"); goto err; } @@ -659,23 +660,22 @@ int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf, /* curl doesn't like str */ len = 0; lisurl = lost_copy_string(url, &len); - if(lisurl != NULL && len > 0) { - - LM_DBG("POST url: [%.*s]\n", len, lisurl); - - curl = httpapi.http_client_query_c( - _m, lisurl, &res, heldreq, mtheld, ACCEPT_HDR); - pkg_free(lisurl); /* clean up */ - lisurl = NULL; - pkg_free(heldreq); - heldreq = NULL; - } else { + if(lisurl == NULL) { LM_ERR("could not copy POST url\n"); pkg_free(heldreq); /* clean up */ heldreq = NULL; goto err; } + LM_DBG("POST url: [%.*s]\n", len, lisurl); + + curl = httpapi.http_client_query_c( + _m, lisurl, &res, heldreq, mtheld, ACCEPT_HDR); + pkg_free(lisurl); /* clean up */ + lisurl = NULL; + pkg_free(heldreq); + heldreq = NULL; + /* only HTTP 2xx responses are accepted */ if(curl >= 300 || curl < 100) { LM_ERR("POST [%.*s] failed with error: %d\n", url.len, url.s, curl); @@ -757,10 +757,11 @@ int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf, return (err.len > 0) ? LOST_SERVER_ERROR : LOST_SUCCESS; err: - /* clean up */ + /* clean up xml */ if(doc != NULL) { xmlFreeDoc(doc); } + /* clean up string */ if(res.s != NULL && res.len > 0) { lost_free_string(&res); } @@ -989,18 +990,17 @@ int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name, } else { len = 0; heldreq = lost_held_post_request(&len, 0, NULL); - if(len > 0) { - - LM_DBG("POST request: [%.*s]\n", len, heldreq); - - curl = httpapi.http_client_query_c( - _m, url.s, &ret, heldreq, mtheld, ACCEPT_HDR); - pkg_free(heldreq); /* clean up */ - heldreq = NULL; - } else { + if(heldreq == NULL) { LM_ERR("could not create POST request\n"); goto err; } + + LM_DBG("POST request: [%.*s]\n", len, heldreq); + + curl = httpapi.http_client_query_c( + _m, url.s, &ret, heldreq, mtheld, ACCEPT_HDR); + pkg_free(heldreq); /* clean up */ + heldreq = NULL; } /* only HTTP 2xx responses are accepted */ if(curl >= 300 || curl < 100) { @@ -1059,15 +1059,14 @@ int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name, /* copy url */ len = 0; urlrep = lost_copy_string(url, &len); - if(urlrep != NULL && len > 0) { - /* send request */ - curl = httpapi.http_client_query(_m, urlrep, &ret, req.s, mtlost); - pkg_free(urlrep); /* clean up */ - urlrep = NULL; - } else { + if(urlrep == NULL) { LM_ERR("could not copy POST url\n"); goto err; } + /* send request */ + curl = httpapi.http_client_query(_m, urlrep, &ret, req.s, mtlost); + pkg_free(urlrep); /* clean up */ + urlrep = NULL; } else { curl = httpapi.http_connect(_m, &con, NULL, &ret, mtlost, &req); } @@ -1076,7 +1075,7 @@ int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name, if(naptr) { LM_ERR("POST [%.*s] failed with error: %d\n", url.len, url.s, curl); } else { - LM_ERR("POST [%.*s] failed with error: %d\n", con.len, con.s, curl); + LM_ERR("[%.*s] failed with error: %d\n", con.len, con.s, curl); } lost_free_string(&ret); goto err; @@ -1160,11 +1159,13 @@ int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name, tmp.len = strlen(fsrdata->redirect->target); url.s = &(ustr[0]); url.len = MAX_URI_SIZE; + /* get url string via NAPTR */ naptr = lost_naptr_lookup(tmp, &shttps, &url); if(naptr == 0) { /* fallback to http */ naptr = lost_naptr_lookup(tmp, &shttp, &url); } + /* nothing found ... return */ if(naptr == 0) { LM_ERR("NAPTR failed on [%.*s]\n", tmp.len, tmp.s); goto err; @@ -1192,24 +1193,24 @@ int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name, /* copy url */ len = 0; urlrep = lost_copy_string(url, &len); - if(urlrep != NULL && len > 0) { - /* send request */ - curl = httpapi.http_client_query( - _m, urlrep, &ret, req.s, mtlost); - url.s = NULL; - url.len = 0; - pkg_free(urlrep); /*clean up */ - urlrep = NULL; - /* only HTTP 2xx responses are accepted */ - if(curl >= 300 || curl < 100) { - LM_ERR("POST [%.*s] failed with error: %d\n", - url.len, url.s, curl); - goto err; - } - } else { + if(urlrep == NULL) { LM_ERR("could not copy POST url\n"); goto err; } + /* send request */ + curl = httpapi.http_client_query( + _m, urlrep, &ret, req.s, mtlost); + pkg_free(urlrep); /*clean up */ + urlrep = NULL; + /* only HTTP 2xx responses are accepted */ + if(curl >= 300 || curl < 100) { + LM_ERR("POST [%.*s] failed with error: %d\n", + url.len, url.s, curl); + goto err; + } + /* reset url string */ + url.s = NULL; + url.len = 0; /* once more ... we got a redirect */ redirect = 1; } @@ -1268,6 +1269,7 @@ int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name, lost_free_findServiceResponse(&fsrdata); lost_free_geoheader_list(&geolist); lost_free_loc(&loc); + /* clean up string */ if(oldurl.s != NULL && oldurl.len > 0) { lost_free_string(&oldurl); } diff --git a/src/modules/lost/response.c b/src/modules/lost/response.c index 916573d7525..b3389448b8d 100644 --- a/src/modules/lost/response.c +++ b/src/modules/lost/response.c @@ -566,7 +566,6 @@ p_lost_issue_t lost_get_response_issues(xmlNodePtr node) if(node == NULL) { return NULL; - ; } LM_DBG("### LOST\t%s\n", node->name); @@ -574,19 +573,13 @@ p_lost_issue_t lost_get_response_issues(xmlNodePtr node) cur = node->children; while(cur) { if(cur->type == XML_ELEMENT_NODE) { - /* get a new list element */ - new = lost_new_response_issues(); - if(new == NULL) { - return NULL; - } - /* parse the element */ + /* get a new response type element */ issue = lost_new_response_type(); if(issue == NULL) { - PKG_MEM_ERROR; - pkg_free(list); - pkg_free(new); - return NULL; + /* didn't get it ... return */ + break; } + /* parse properties */ issue->source = lost_get_property(cur->parent, MAPP_PROP_SRC, &len); tmp.s = (char *)cur->name; tmp.len = strlen((char *)cur->name); @@ -601,12 +594,17 @@ p_lost_issue_t lost_get_response_issues(xmlNodePtr node) issue->info->text = lost_get_property(cur, PROP_MSG, &len); issue->info->lang = lost_get_property(cur, PROP_LANG, &len); } + /* get a new list element */ + new = lost_new_response_issues(); + if(new == NULL) { + /* didn't get it, delete response type element ... return */ + lost_delete_response_type(&issue); /* clean up */ + break; + } /* append to list */ new->issue = issue; new->next = list; list = new; - } else { - lost_delete_response_issues(&new); /* clean up */ } /* get next element */ cur = cur->next; diff --git a/src/modules/lost/utilities.c b/src/modules/lost/utilities.c index 6813111d240..bcb045f451f 100644 --- a/src/modules/lost/utilities.c +++ b/src/modules/lost/utilities.c @@ -272,20 +272,19 @@ void lost_free_held(p_lost_held_t *held) char *lost_copy_string(str src, int *lgth) { char *res = NULL; + *lgth = 0; - if(src.s == NULL && src.len == 0) { - *lgth = 0; - return NULL; - } - res = (char *)pkg_malloc((src.len + 1) * sizeof(char)); - if(res == NULL) { - PKG_MEM_ERROR; - *lgth = 0; - } else { - memset(res, 0, src.len + 1); - memcpy(res, src.s, src.len); - res[src.len] = '\0'; - *lgth = (int)strlen(res); + /* only copy a valid string */ + if(src.s != NULL && src.len > 0) { + res = (char *)pkg_malloc((src.len + 1) * sizeof(char)); + if(res == NULL) { + PKG_MEM_ERROR; + } else { + memset(res, 0, src.len + 1); + memcpy(res, src.s, src.len); + res[src.len] = '\0'; + *lgth = (int)strlen(res); + } } return res; @@ -590,7 +589,7 @@ int lost_parse_host(const char *uri, str *host, int *flag) host->len = end - search; if(ip6) { - *flag = AF_INET; + *flag = AF_INET6; } else { *flag = AF_INET; } @@ -1194,7 +1193,7 @@ int lost_xpath_location(xmlDocPtr doc, char *path, p_lost_loc_t loc) /* nothing found */ if((select < 0) && (i == size - 1)) { LM_ERR("could not find proper location-info element\n"); - xmlFree(xmlbuff); + xmlFree(xmlbuff); /* clean up */ xmlFreeDoc(new); xmlXPathFreeObject(result); return -1; @@ -1202,22 +1201,28 @@ int lost_xpath_location(xmlDocPtr doc, char *path, p_lost_loc_t loc) if(i == select) { /* return the current profile */ - loc->profile = (char *)pkg_malloc(strlen(s_profile) + 1); - if(loc->profile == NULL) { - xmlFree(xmlbuff); + if(s_profile != NULL) { + loc->profile = (char *)pkg_malloc(strlen(s_profile) + 1); + if(loc->profile == NULL) { + xmlFree(xmlbuff); /* clean up */ + xmlFreeDoc(new); + xmlXPathFreeObject(result); + goto err; + } + memset(loc->profile, 0, strlen(s_profile) + 1); + memcpy(loc->profile, s_profile, strlen(s_profile)); + } else { + xmlFree(xmlbuff); /* clean up */ xmlFreeDoc(new); xmlXPathFreeObject(result); goto err; } - memset(loc->profile, 0, strlen(s_profile) + 1); - memcpy(loc->profile, s_profile, strlen(s_profile)); - /* remove xml header from location element */ remove = strlen("\n"); buffersize = buffersize - remove; ptr = (char *)pkg_malloc((buffersize + 1) * sizeof(char)); if(ptr == NULL) { - xmlFree(xmlbuff); + xmlFree(xmlbuff); /* clean up */ xmlFreeDoc(new); xmlXPathFreeObject(result); goto err; @@ -1233,7 +1238,7 @@ int lost_xpath_location(xmlDocPtr doc, char *path, p_lost_loc_t loc) /* return the location DOM */ loc->xpath = (char *)pkg_malloc(len + 1); if(loc->xpath == NULL) { - pkg_free(ptr); + pkg_free(ptr); /* clean up */ ptr = NULL; xmlFree(xmlbuff); xmlFreeDoc(new); @@ -1242,8 +1247,7 @@ int lost_xpath_location(xmlDocPtr doc, char *path, p_lost_loc_t loc) } memset(loc->xpath, 0, len + 1); memcpy(loc->xpath, tmp, len); - /* free memory */ - pkg_free(ptr); + pkg_free(ptr); /* clean up */ ptr = NULL; } else { LM_WARN("xpath location-info element(%d) ignored\n", i + 1);