int encode_uri (str uri, char *encoding_prefix, char *public_ip,char separator, str * result) { struct uri_format format; char *pos; int foo,res; result->s = NULL; result->len = 0; if (uri.len <= 1) return -1; /* no contact or an invalid one */ if (public_ip == NULL) { LM_ERR("invalid NULL value for public_ip parameter\n"); return -2; } #ifdef DEBUG fprintf (stdout, "Primit cerere de encodare a [%.*s] cu %s-%s\n", uri.len,uri.s, encoding_prefix, public_ip); #endif fflush (stdout); foo = encode2format (uri, &format); if (foo < 0) { LM_ERR("unable to encode Contact URI [%.*s].Return code %d\n",uri.len,uri.s,foo); return foo - 20; } #ifdef DEBUG fprintf(stdout,"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); #endif /* 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 strlen (encoding_prefix) + foo + format.username.len + foo + format.password.len + foo + format.ip.len + foo + format.port.len + foo + format.protocol.len + 1 + strlen (public_ip); /* adding one comes from @ */ result->s = pkg_malloc (result->len); pos = result->s; if (pos == NULL) { #ifdef DEBUG fprintf (stdout, "Unable to alloc result [%d] end=%d\n",result->len, format.second); #endif LM_ERR("unable to alloc pkg memory\n"); return -3; } #ifdef DEBUG fprintf (stdout, "[pass=%d][Allocated %d bytes][first=%d][lengthsec=%d]\nAdding [%d] ->%.*s\n",format.password.len,result->len,format.first,uri.len-format.second,format.first, format.first,uri.s);fflush (stdout); #endif 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; } #ifdef DEBUG fprintf(stdout,"res= %d\npos=%s\n",res,pos); #endif pos = pos + res ;/* overwriting the \0 from snprintf */ memcpy (pos, public_ip, strlen (public_ip)); pos = pos + strlen (public_ip); memcpy (pos, uri.s + format.second, uri.len - format.second); #ifdef DEBUG fprintf (stdout, "Adding2 [%d] ->%.*s\n", uri.len - format.second,uri.len - format.second, uri.s + format.second); fprintf (stdout, "NEW NEW uri is->[%.*s]\n", result->len, result->s); #endif /* Because called parse_uri format contains pointers to the inside of msg,must not deallocate */ return 0; }
int encode_uri (struct sip_msg* msg, str* uri, char *encoding_prefix, char *public_ip,char separator, str * result) { struct uri_format format; char *pos; int foo,res; result->s = NULL; result->len = 0; if (uri->len <= 1) return -1; /* no contact or an invalid one */ if (public_ip == NULL) { LOG(L_ERR,"ERROR: encode_uri: Invalid NULL value for public_ip parameter\n"); return -2; } foo = encode2format (msg, uri, &format); if (foo < 0) { LOG(L_ERR,"ERROR: encode_uri: Unable to encode Contact URI" " [%.*s].Return code %d\n",uri->len, uri->s, foo); return foo - 20; } /* 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->s+uri->len - format.rest.s + strlen (encoding_prefix) + foo + format.username.len + foo + format.password.len + foo + format.ip.len + foo + format.port.len + foo + format.protocol.len + foo + format.rcv_ip.len + foo + format.rcv_port.len + foo + format.rcv_proto.len + 1 + strlen (public_ip); /* adding one comes from @ */ result->s = pkg_malloc (result->len); pos = result->s; if (pos == NULL) { LOG(L_ERR,"ERROR: encode_uri:Unable to alloc memory\n"); return -3; } res = snprintf(pos,result->len,"%.*s%s%c%.*s%c%.*s%c%.*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, separator, format.rcv_ip.len, format.rcv_ip.s, separator, format.rcv_port.len, format.rcv_port.s, separator, format.rcv_proto.len, format.rcv_proto.s ); if ((res < 0 )||(res>result->len)) { LOG(L_ERR,"ERROR: encode_uri: Unable to construct new uri.\n"); if (result->s != NULL) pkg_free(result->s); return -4; } pos = pos + res ;/* overwriting the \0 from snprintf */ memcpy (pos, public_ip, strlen (public_ip)); pos = pos + strlen (public_ip); /* copy the rest of the parameters and the rest of uri line*/ memcpy (pos, format.rest.s, uri->s+uri->len - format.rest.s); /*memcpy (pos, uri.s + format.second, uri.len - format.second);*/ /* Because called parse_uri format contains pointers to the inside of msg, * must not deallocate */ return 0; }