From 2b8b2904399478984d3983ce447c98832419570a Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Thu, 11 Jun 2020 13:27:22 +0200 Subject: [PATCH] siputils: contact ops - removed old defines set on for very long time - bits of formatting code and comments --- src/modules/siputils/contact_ops.c | 107 ++++++++++++++--------------- src/modules/siputils/contact_ops.h | 7 -- 2 files changed, 50 insertions(+), 64 deletions(-) diff --git a/src/modules/siputils/contact_ops.c b/src/modules/siputils/contact_ops.c index bc48a8771bc..e384a22ff25 100644 --- a/src/modules/siputils/contact_ops.c +++ b/src/modules/siputils/contact_ops.c @@ -29,8 +29,6 @@ */ -#define STRICT_CHECK 1 - #include "contact_ops.h" #include "utils.h" #include "../../core/mem/mem.h" @@ -54,9 +52,9 @@ int ki_encode_contact (sip_msg_t *msg, str *eprefix, str *eaddr) char separator; /* - * I have a list of contacts in contact->parsed which is of type contact_body_t - * inside i have a contact->parsed->contact which is the head of the list of contacts - * inside it is a + * I have a list of contacts in contact->parsed which is of type + * contact_body_t inside i have a contact->parsed->contact which is + * the head of the list of contacts inside it is a * str uri; * struct contact *next; * I just have to visit each uri and encode each uri according to a scheme @@ -89,16 +87,13 @@ int ki_encode_contact (sip_msg_t *msg, str *eprefix, str *eaddr) if(res != 0) { LM_ERR("failed encoding contact.Code %d\n", res); -#ifdef STRICT_CHECK return res; -#endif } else if(patch(msg, uri.s, uri.len, newUri.s, newUri.len) < 0) { LM_ERR("lumping failed in mangling port \n"); return -2; } -/* encoding next contacts too?*/ -#ifdef ENCODE_ALL_CONTACTS + /* encoding next contacts too? */ while(c->next != NULL) { c = c->next; uri = c->uri; @@ -106,15 +101,12 @@ int ki_encode_contact (sip_msg_t *msg, str *eprefix, str *eaddr) res = encode_uri(uri, eprefix->s, eaddr->s, separator, &newUri); if(res != 0) { LM_ERR("failed encode_uri.Code %d\n", res); -#ifdef STRICT_CHECK return res; -#endif } else if(patch(msg, uri.s, uri.len, newUri.s, newUri.len) < 0) { LM_ERR("lumping failed in mangling port \n"); return -3; } } /* while */ -#endif /* ENCODE_ALL_CONTACTS */ } /* if c != NULL */ return 1; @@ -156,9 +148,7 @@ int ki_decode_contact(sip_msg_t *msg) uri = msg->first_line.u.request.uri; if (uri.s == NULL) return -1; - } - else - { + } else { uri = msg->new_uri; } @@ -170,9 +160,7 @@ int ki_decode_contact(sip_msg_t *msg) if (res != 0) { LM_ERR("failed decoding contact.Code %d\n", res); -#ifdef STRICT_CHECK return res; -#endif } else { @@ -239,15 +227,12 @@ int ki_decode_contact_header(sip_msg_t *msg) LM_DBG("newuri.s=[%.*s]\n", newUri.len, newUri.s); if(res != 0) { LM_ERR("failed decoding contact.Code %d\n", res); -#ifdef STRICT_CHECK return res; -#endif } else if(patch(msg, uri.s, uri.len, newUri.s, newUri.len) < 0) { LM_ERR("lumping failed in mangling port \n"); return -2; } -#ifdef DECODE_ALL_CONTACTS while(c->next != NULL) { c = c->next; uri = c->uri; @@ -255,15 +240,12 @@ int ki_decode_contact_header(sip_msg_t *msg) res = decode_uri(uri, separator, &newUri); if(res != 0) { LM_ERR("failed decoding contact.Code %d\n", res); -#ifdef STRICT_CHECK return res; -#endif } else if(patch(msg, uri.s, uri.len, newUri.s, newUri.len) < 0) { LM_ERR("lumping failed in mangling port \n"); return -3; } } // end while -#endif } // if c!= NULL return 1; @@ -276,8 +258,7 @@ int decode_contact_header(sip_msg_t *msg, char *unused1, char *unused2) } - int -encode2format (str uri, struct uri_format *format) +int encode2format (str uri, struct uri_format *format) { int foo; char *string, *pos, *start, *end; @@ -307,7 +288,8 @@ encode2format (str uri, struct uri_format *format) return -5; if (start - string < 3) return -6; - /* KD: FIXME: Looks like this code can not handle 'sips' URIs and discards all other URI parameters! */ + /* KD: FIXME: Looks like this code can not handle 'sips' + * URIs and discards all other URI parameters! */ start = start - 3; end = string + uri.len; } @@ -315,7 +297,8 @@ encode2format (str uri, struct uri_format *format) format->first = start - string + 4; /*sip: */ format->second = end - string; /* --------------------------testing ------------------------------- */ - /* sip:gva@pass@10.0.0.1;;transport=udp>;expires=2 INCORECT BEHAVIOR OF parse_uri,myfunction works good */ + /* sip:gva@pass@10.0.0.1;;transport=udp>;expires=2 INCORECT BEHAVIOR OF + * parse_uri,myfunction works good */ foo = parse_uri (start, end - start, &sipUri); if (foo != 0) { @@ -341,8 +324,8 @@ encode2format (str uri, struct uri_format *format) } - int -encode_uri (str uri, char *encoding_prefix, char *public_ip,char separator, str * result) +int encode_uri (str uri, char *encoding_prefix, char *public_ip,char separator, + str * result) { struct uri_format format; char *pos; @@ -358,23 +341,26 @@ encode_uri (str uri, char *encoding_prefix, char *public_ip,char separator, str return -2; } - LM_DBG("Encoding request for [%.*s] with [%s]-[%s]\n", uri.len,uri.s, encoding_prefix, public_ip); + LM_DBG("Encoding request for [%.*s] with [%s]-[%s]\n", uri.len,uri.s, + encoding_prefix, public_ip); foo = encode2format (uri, &format); if (foo < 0) { - LM_ERR("unable to encode Contact URI [%.*s].Return code %d\n",uri.len,uri.s,foo); + LM_ERR("unable to encode Contact URI [%.*s].Return code %d\n", + uri.len, uri.s,foo); return foo - 20; } - LM_DBG("user=%.*s ip=%.*s port=%.*s protocol=%.*s\n",format.username.len,format.username.s,format.ip.len,format.ip.s, + LM_DBG("user=%.*s ip=%.*s port=%.*s protocol=%.*s\n", format.username.len, + format.username.s,format.ip.len,format.ip.s, format.port.len,format.port.s,format.protocol.len,format.protocol.s); /* a complete uri would be sip:username@ip:port;transport=protocol goes to * sip:enc_pref*username*ip*port*protocol@public_ip */ - foo = 1; /*strlen(separator); */ - result->len = format.first + uri.len - format.second + //ar trebui sa sterg 1 + foo = 1; /* strlen(separator); */ + result->len = format.first + uri.len - format.second + strlen (encoding_prefix) + foo + format.username.len + foo + format.password.len + foo + @@ -390,15 +376,19 @@ encode_uri (str uri, char *encoding_prefix, char *public_ip,char separator, str LM_ERR("unable to alloc pkg memory\n"); return -3; } - LM_DBG("pass=[%d]i: allocated [%d], bytes.first=[%d] lengthsec=[%d]; adding [%d]->[%.*s]\n", - format.password.len, result->len, format.first, uri.len-format.second, format.first, format.first,uri.s); - - res = snprintf(pos,result->len,"%.*s%s%c%.*s%c%.*s%c%.*s%c%.*s%c%.*s@",format.first,uri.s,encoding_prefix,separator, - format.username.len,format.username.s,separator,format.password.len,format.password.s, - separator,format.ip.len,format.ip.s,separator,format.port.len,format.port.s,separator,format.protocol.len,format.protocol.s); - - if ((res < 0 )||(res>result->len)) - { + LM_DBG("pass=[%d]i: allocated [%d], bytes.first=[%d] lengthsec=[%d];" + " adding [%d]->[%.*s]\n", + format.password.len, result->len, format.first, + uri.len-format.second, format.first, format.first,uri.s); + + res = snprintf(pos,result->len,"%.*s%s%c%.*s%c%.*s%c%.*s%c%.*s%c%.*s@", + format.first,uri.s, encoding_prefix, separator, + format.username.len, format.username.s, separator, + format.password.len,format.password.s, + separator, format.ip.len, format.ip.s, separator, format.port.len, + format.port.s, separator, format.protocol.len, format.protocol.s); + + if ((res < 0 )||(res>result->len)) { LM_ERR("unable to construct new uri.\n"); if (result->s != NULL) pkg_free(result->s); return -4; @@ -413,21 +403,19 @@ encode_uri (str uri, char *encoding_prefix, char *public_ip,char separator, str uri.len - format.second, uri.s + format.second, result->len, result->s); - /* Because called parse_uri format contains pointers to the inside of msg,must not deallocate */ + /* Because called parse_uri format contains pointers to the inside of msg, + * must not deallocate */ return 0; } - int -decode2format (str uri, char separator, struct uri_format *format) +int decode2format (str uri, char separator, struct uri_format *format) { char *start, *end, *pos,*lastpos; str tmp; enum {EX_PREFIX=0,EX_USER,EX_PASS,EX_IP,EX_PORT,EX_PROT,EX_FINAL} state; - //memset (format, 0, sizeof ((struct uri_format))); - if (uri.s == NULL) { LM_ERR("invalid parameter uri.It is NULL\n"); @@ -442,7 +430,7 @@ decode2format (str uri, char separator, struct uri_format *format) LM_ERR("invalid SIP uri.Missing :\n"); return -2; } /* invalid uri */ - start = start + 1; /* jumping over sip: ATENTIE LA BUFFER OVERFLOW DACA E DOAR sip: */ + start = start + 1; /* jumping over sip: */ format->first = start - uri.s; /* start */ @@ -520,8 +508,7 @@ decode2format (str uri, char separator, struct uri_format *format) } - int -decode_uri (str uri, char separator, str * result) +int decode_uri (str uri, char separator, str * result) { char *pos; struct uri_format format; @@ -559,14 +546,20 @@ decode_uri (str uri, char separator, str * result) * sip:enc_pref#username#password#ip#port#protocol@public_ip */ result->len = format.first + (uri.len - format.second); /* not NULL terminated */ - if (format.username.len > 0) result->len += format.username.len + 1; //: or @ - if (format.password.len > 0) result->len += format.password.len + 1; //@ + if (format.username.len > 0) + result->len += format.username.len + 1; //: or @ + if (format.password.len > 0) + result->len += format.password.len + 1; //@ - /* if (format.ip.len > 0) */ result->len += format.ip.len; + /* if (format.ip.len > 0) */ + result->len += format.ip.len; - if (format.port.len > 0) result->len += 1 + format.port.len; //: - if (format.protocol.len > 0) result->len += 1 + 10 + format.protocol.len; //;transport= - LM_DBG("Result size is [%d]. Original Uri size is [%d].\n", result->len, uri.len); + if (format.port.len > 0) + result->len += 1 + format.port.len; //: + if (format.protocol.len > 0) + result->len += 1 + 10 + format.protocol.len; //;transport= + LM_DBG("Result size is [%d]. Original Uri size is [%d].\n", + result->len, uri.len); /* adding one comes from * */ result->s = pkg_malloc (result->len); diff --git a/src/modules/siputils/contact_ops.h b/src/modules/siputils/contact_ops.h index 939b80ac6c1..e4f8d0d7582 100644 --- a/src/modules/siputils/contact_ops.h +++ b/src/modules/siputils/contact_ops.h @@ -36,15 +36,8 @@ #ifndef CONTACT_OPS_H #define CONTACT_OPS_H -/* if you want to parse all contacts not just de first one */ - - - #include "../../core/parser/msg_parser.h" /* struct sip_msg */ -#define ENCODE_ALL_CONTACTS 1 -#define DECODE_ALL_CONTACTS 1 - #define DEFAULT_SEPARATOR "*"