mangler: pkg free in case of errors
authorDaniel-Constantin Mierla <miconda@gmail.com>
Fri, 28 Jul 2017 08:06:11 +0000 (10:06 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Fri, 28 Jul 2017 08:30:19 +0000 (10:30 +0200)
src/modules/mangler/contact_ops.c
src/modules/mangler/ip_helper.c
src/modules/mangler/sdp_mangler.c

index 4e72672..de94edb 100644 (file)
@@ -73,7 +73,11 @@ encode_contact (struct sip_msg *msg, char *encoding_prefix,char *public_ip)
                if (strlen(contact_flds_separator)>=1)
                        separator = contact_flds_separator[0];
        
-       if (msg->contact->parsed == NULL)       parse_contact (msg->contact);
+       if (msg->contact->parsed == NULL) {
+               if(parse_contact (msg->contact)<0) {
+                       LM_DBG("failed to parse contact\n");
+               }
+       }
        if (msg->contact->parsed != NULL)
        {
                cb = (contact_body_t *) msg->contact->parsed;
@@ -188,8 +192,6 @@ decode_contact_header (struct sip_msg *msg,char *unused1,char *unused2)
        str newUri;
        char separator;
        int res;
-       
-       
 
        if ((msg->contact == NULL)&&((parse_headers(msg,HDR_CONTACT_F,0) == -1) || 
                                (msg->contact== NULL) ))
@@ -203,7 +205,11 @@ decode_contact_header (struct sip_msg *msg,char *unused1,char *unused2)
                if (strlen(contact_flds_separator)>=1)
                        separator = contact_flds_separator[0];
                
-       if (msg->contact->parsed == NULL) parse_contact (msg->contact);
+       if (msg->contact->parsed == NULL) {
+               if(parse_contact (msg->contact)<0) {
+                       LM_DBG("failed to parse contact\n");
+               }
+       }
        if (msg->contact->parsed != NULL)
        {
                cb = (contact_body_t *) msg->contact->parsed;
index 5dba117..6da5c9e 100644 (file)
@@ -91,7 +91,7 @@ parse_ip_address (char *c, unsigned int *address)     //inet_aton
        addrp = (unsigned char *) address;
 
        /* make a copy of the dotted string, because we modify it */
-       strncpy (buf, c, 20);
+       strcpy (buf, c);
        p = buf;
 
        /* search three times for a dot in the string */
index 769b96d..105c55b 100644 (file)
@@ -279,225 +279,222 @@ continue1:
 }
 
 
-int
-sdp_mangle_ip (struct sip_msg *msg, char *oldip, char *newip)
+int sdp_mangle_ip(struct sip_msg *msg, char *oldip, char *newip)
 {
-       int i, oldContentLength, newContentLength, diff, oldlen,len,off,ret,needToDealocate;
+       int i, oldContentLength, newContentLength, diff, oldlen, len, off, ret,
+                       needToDealocate;
        unsigned int mask, address, locatedIp;
        struct lump *l;
        regmatch_t pmatch;
        regex_t *re;
-       char *s, *pos,*begin,*key;
-       char buffer[16];        /* 123.456.789.123\0 */
+       char *s, *pos, *begin, *key;
+       char buffer[16]; /* 123.456.789.123\0 */
 
 #ifdef EXTRA_DEBUG
-       fprintf (stdout,"---START--------MANGLE IP-----------------\n");
+       fprintf(stdout, "---START--------MANGLE IP-----------------\n");
 #endif
 
-       
+       pos = NULL;
        key = IP_REGEX;
 
        /*
         * Checking if msg has a payload
         */
-       if (msg == NULL)
-               {
-               LOG(L_ERR,"ERROR: sdp_mangle_ip: Received NULL for msg\n");
+       if(msg == NULL) {
+               LOG(L_ERR, "ERROR: sdp_mangle_ip: Received NULL for msg\n");
                return -1;
-               }
-       if ((msg->content_length==0) &&
-                               ((parse_headers(msg,HDR_CONTENTLENGTH_F,0)==-1) ||
-                                (msg->content_length==0) )){
-                       LOG(L_ERR,"ERROR: sdp_mangle_port: bad or missing "
-                                       "Content-Length \n");
-                       return -2;
-               }
-        oldContentLength = get_content_length(msg);
-        
-       if (oldContentLength <= 0)
-               {
-               LOG(L_ERR,"ERROR: sdp_mangle_ip: Received <= for Content-Length\n");
+       }
+       if((msg->content_length == 0)
+                       && ((parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1)
+                                          || (msg->content_length == 0))) {
+               LOG(L_ERR, "ERROR: sdp_mangle_port: bad or missing "
+                                  "Content-Length \n");
                return -2;
-               }
+       }
+       oldContentLength = get_content_length(msg);
+
+       if(oldContentLength <= 0) {
+               LOG(L_ERR, "ERROR: sdp_mangle_ip: Received <= for Content-Length\n");
+               return -2;
+       }
 
        /* checking oldip */
-       if (oldip == NULL)
-               {
-               LOG(L_ERR,"ERROR: sdp_mangle_ip: Received NULL for oldip\n");
+       if(oldip == NULL) {
+               LOG(L_ERR, "ERROR: sdp_mangle_ip: Received NULL for oldip\n");
                return -3;
-               }
+       }
        /* checking newip */
-       if (newip == NULL)
-               {
-               LOG(L_ERR,"ERROR: sdp_mangle_ip: Received NULL for newip\n");
+       if(newip == NULL) {
+               LOG(L_ERR, "ERROR: sdp_mangle_ip: Received NULL for newip\n");
                return -4;
-               }
-       i = parse_ip_netmask (oldip, &pos, &mask);
+       }
+       i = parse_ip_netmask(oldip, &pos, &mask);
 
-       if (i == -1)
-       {
+       if(i == -1 || pos == NULL) {
                /* invalid value for the netmask specified in oldip */
-               LOG(L_ERR,"ERROR: sdp_mangle_ip: invalid value for the netmask specified in oldip\n");
+               LOG(L_ERR, "ERROR: sdp_mangle_ip: invalid value for the netmask "
+                                  "specified in oldip\n");
+               if(pos)
+                       free(pos);
                return -5;
-       }
-       else
-       {
-               i = parse_ip_address (pos, &address);
-               if (pos != NULL) free (pos);
-               if (i == 0)
-                       {
-                       LOG(L_ERR,"ERROR: sdp_mangle_ip: invalid value for the ip specified in oldip\n");
-                       return -6;      /* parse error in ip */
-                       }
+       } else {
+               i = parse_ip_address(pos, &address);
+               free(pos);
+               if(i == 0) {
+                       LOG(L_ERR, "ERROR: sdp_mangle_ip: invalid value for the ip "
+                                          "specified in oldip\n");
+                       return -6; /* parse error in ip */
+               }
        }
 
        /* now we have in address/netmask binary values */
 
-       begin = get_body(msg);//msg->buf + msg->first_line.len; // inlocuiesc cu begin = getbody */
+       begin = get_body(
+                       msg); //msg->buf + msg->first_line.len; // inlocuiesc cu begin = getbody */
        ret = -1;
-       len = strlen (newip);
+       len = strlen(newip);
 
        /* try to use pre-compiled expressions */
        needToDealocate = 0;
-       if (ipExpression != NULL) 
-               {
+       if(ipExpression != NULL) {
                re = ipExpression;
 #ifdef EXTRA_DEBUG
-               fprintf(stdout,"Using PRECOMPILED expression for ip ...\n");
+               fprintf(stdout, "Using PRECOMPILED expression for ip ...\n");
 #endif
 
+       } else /* we are not using pre-compiled expressions */
+       {
+               re = pkg_malloc(sizeof(regex_t));
+               if(re == NULL) {
+                       LOG(L_ERR, "ERROR: sdp_mangle_ip: Unable to allocate re\n");
+                       return -7;
+               }
+               needToDealocate = 1;
+               if((regcomp(re, key, REG_EXTENDED)) != 0) {
+                       LOG(L_ERR, "ERROR: sdp_mangle_ip: Unable to compile %s \n", key);
+                       pkg_free(re);
+                       return -8;
                }
-               else /* we are not using pre-compiled expressions */
-                       {
-                       re = pkg_malloc(sizeof(regex_t));
-                       if (re == NULL)
-                               {
-                               LOG(L_ERR,"ERROR: sdp_mangle_ip: Unable to allocate re\n");
-                               return -7;
-                               }
-                       needToDealocate = 1;
-                       if ((regcomp (re, key, REG_EXTENDED)) != 0)
-                               {
-                               LOG(L_ERR,"ERROR: sdp_mangle_ip: Unable to compile %s \n",key);
-                               return -8;
-                               }
 #ifdef EXTRA_DEBUG
-               fprintf(stdout,"Using ALLOCATED expression for ip ...\n");
+               fprintf(stdout, "Using ALLOCATED expression for ip ...\n");
 #endif
-                       }
+       }
 
        diff = 0;
-       while ((begin < msg->buf + msg->len) && (regexec (re, begin, 1, &pmatch, 0) == 0))
-       {
+       while((begin < msg->buf + msg->len)
+                       && (regexec(re, begin, 1, &pmatch, 0) == 0)) {
                off = begin - msg->buf;
-               if (pmatch.rm_so == -1)
-               {
-                       LOG (L_ERR,"ERROR: sdp_mangler_ip: offset unknown\n");
+               if(pmatch.rm_so == -1) {
+                       LOG(L_ERR, "ERROR: sdp_mangler_ip: offset unknown\n");
+                       pkg_free(re);
                        return -9;
                }
-       
+
 #ifdef STRICT_CHECK
-               pmatch.rm_eo --; /* return with one space,\n,\r */
+               pmatch.rm_eo--; /* return with one space,\n,\r */
 #endif
-       
+
                /* 
                 for BSD and Solaris we avoid memrchr
                 pos = (char *) memrchr (begin + pmatch.rm_so, ' ',pmatch.rm_eo - pmatch.rm_so); 
                 */
-                pos = begin+pmatch.rm_eo;
-                do pos--; while (*pos != ' '); /* we should find ' ' because we matched c=IN IP4 ip */
-
-               pos++;          /* jumping over space */
-               oldlen = (pmatch.rm_eo - pmatch.rm_so) - (pos - (begin + pmatch.rm_so));        /* ip length */
-               if (oldlen > 15)
-               {
-                       LOG(L_WARN,"WARNING: sdp_mangle_ip: Silent fail because oldlen > 15\n");
+               pos = begin + pmatch.rm_eo;
+               do {
+                       pos--;
+               } while(*pos
+                               != ' '); /* we should find ' ' because we matched c=IN IP4 ip */
+
+               pos++; /* jumping over space */
+               oldlen = (pmatch.rm_eo - pmatch.rm_so)
+                                - (pos - (begin + pmatch.rm_so)); /* ip length */
+               if(oldlen > 15) {
+                       LOG(L_WARN, "WARNING: sdp_mangle_ip: Silent fail because oldlen > "
+                                               "15\n");
 #ifdef STRICT_CHECK
+                       pkg_free(re);
                        return -10;
-#else 
-                       goto continue2; /* silent fail return -10; invalid ip format ,probably like 1000.3.12341.2 */
+#else
+                       goto continue2; /* silent fail return -10; invalid ip format ,probably like 1000.3.12341.2 */
 #endif
-
-                       
                }
                buffer[0] = '\0';
-               strncat ((char *) buffer, pos, oldlen); 
+               strncat((char *)buffer, pos, oldlen);
                buffer[oldlen] = '\0';
-               i = parse_ip_address (buffer, &locatedIp);
-               if (i == 0)
-               {
-                       LOG(L_WARN,"WARNING: sdp_mangle_ip: Silent fail on parsing matched address \n");
-                       
+               i = parse_ip_address(buffer, &locatedIp);
+               if(i == 0) {
+                       LOG(L_WARN, "WARNING: sdp_mangle_ip: Silent fail on parsing "
+                                               "matched address \n");
+
 #ifdef STRICT_CHECK
+                       pkg_free(re);
                        return -11;
-#else 
-                       goto continue2; 
+#else
+                       goto continue2;
 #endif
                }
-               if (same_net (locatedIp, address, mask) == 0)
-               {
-                       LOG(L_WARN,"WARNING: sdp_mangle_ip: Silent fail because matched address is not in network\n");
+               if(same_net(locatedIp, address, mask) == 0) {
+                       LOG(L_WARN, "WARNING: sdp_mangle_ip: Silent fail because matched "
+                                               "address is not in network\n");
 #ifdef EXTRA_DEBUG
-               fprintf(stdout,"Extracted ip is %s and not mangling \n",buffer);
+                       fprintf(stdout, "Extracted ip is %s and not mangling \n", buffer);
 #endif
-                       goto continue2; /* not in the same net, skipping */
+                       goto continue2; /* not in the same net, skipping */
                }
 #ifdef EXTRA_DEBUG
-               fprintf(stdout,"Extracted ip is %s and mangling to %s\n",buffer,newip);
+               fprintf(stdout, "Extracted ip is %s and mangling to %s\n", buffer,
+                               newip);
 #endif
 
-
                /* replacing ip */
 
                /* deleting old ip */
-               if ((l = del_lump (msg,pmatch.rm_so + off + 
-                                               (pos - (begin + pmatch.rm_so)),oldlen, 0)) == 0)
-               {
-                       LOG (L_ERR,"ERROR: sdp_mangle_ip: del_lump failed\n");
+               if((l = del_lump(msg,
+                                       pmatch.rm_so + off + (pos - (begin + pmatch.rm_so)), oldlen,
+                                       0))
+                               == 0) {
+                       LOG(L_ERR, "ERROR: sdp_mangle_ip: del_lump failed\n");
+                       pkg_free(re);
                        return -12;
                }
-               s = pkg_malloc (len);
-               if (s == 0)
-               {
-                       LOG (L_ERR,"ERROR: sdp_mangle_ip: mem. allocation failure\n");
+               s = pkg_malloc(len);
+               if(s == 0) {
+                       LOG(L_ERR, "ERROR: sdp_mangle_ip: mem. allocation failure\n");
+                       pkg_free(re);
                        return -13;
                }
-               memcpy (s, newip, len);
+               memcpy(s, newip, len);
 
-               if (insert_new_lump_after (l, s, len, 0) == 0)
-               {
-                       LOG (L_ERR, "ERROR: sdp_mangle_ip: could not insert new lump\n");
-                       pkg_free (s);
+               if(insert_new_lump_after(l, s, len, 0) == 0) {
+                       LOG(L_ERR, "ERROR: sdp_mangle_ip: could not insert new lump\n");
+                       pkg_free(re);
+                       pkg_free(s);
                        return -14;
                }
-               diff = diff + len /*new length */  - oldlen;
+               diff = diff + len /*new length */ - oldlen;
                /* new cycle */
                ret++;
-continue2:
+       continue2:
                begin = begin + pmatch.rm_eo;
 
-       }                       /* while */
-       if (needToDealocate)
-       {
-       regfree (re);           /* if I am going to use pre-compiled expressions to be removed */
-       pkg_free(re);
+       } /* while */
+       if(needToDealocate) {
+               regfree(re); /* if I am going to use pre-compiled expressions to be removed */
+               pkg_free(re);
 #ifdef EXTRA_DEBUG
-               fprintf(stdout,"Deallocating expression for ip ...\n");
+               fprintf(stdout, "Deallocating expression for ip ...\n");
 #endif
        }
-       
-       if (diff != 0)
-       {
+
+       if(diff != 0) {
                newContentLength = oldContentLength + diff;
-               patch_content_length (msg, newContentLength);
+               patch_content_length(msg, newContentLength);
        }
 
 #ifdef EXTRA_DEBUG
-       fprintf (stdout,"---END--------MANGLE IP-----------------\n");
+       fprintf(stdout, "---END--------MANGLE IP-----------------\n");
 #endif
 
-       return ret+2;
-
+       return ret + 2;
 }
 
 int compile_expresions(char *port,char *ip)