/* returns -1 on error. */ int osip_message_set_contact(osip_message_t * sip, const char *hvalue) { int i; osip_contact_t *contact; if (hvalue == NULL || hvalue[0] == '\0') return OSIP_SUCCESS; i = osip_contact_init(&contact); if (i != 0) return i; i = osip_contact_parse(contact, hvalue); if (i != 0) { osip_contact_free(contact); return i; } sip->message_property = 2; osip_list_add(&sip->contacts, contact, -1); return OSIP_SUCCESS; /* ok */ }
/* * SIP_REWRITE_CONTACT * * rewrite the Contact header * * RETURNS * STS_SUCCESS on success * STS_FAILURE on error */ int sip_rewrite_contact (sip_ticket_t *ticket, int direction, struct in_addr *local_ip) { osip_message_t *sip_msg=ticket->sipmsg; osip_contact_t *contact; int i; #if UNREG_WITHOUT_REG_HG522 char tmpUserName[128] = {0}; char tmpPort[6] = {0}; #endif if (sip_msg == NULL) return STS_FAILURE; osip_message_get_contact(sip_msg, 0, &contact); if (contact == NULL) { printsip("sip: rewrite_contact contact is NULL \r\n"); return STS_FAILURE; } for (i=0;i<URLMAP_SIZE;i++){ if (urlmap[i].active == 0) continue; if ((direction == DIR_OUTGOING) && (compare_url(contact->url, urlmap[i].true_url)==STS_SUCCESS)) break; if ((direction == DIR_INCOMING) && (compare_url(contact->url, urlmap[i].masq_url)==STS_SUCCESS)) break; #if UNREG_WITHOUT_REG_HG522 if ((direction == DIR_OUTGOING) && (NULL != contact->url) && (NULL != urlmap[i].true_url) && (NULL != contact->url->host) && (NULL != urlmap[i].true_url->host) && (strcmp(contact->url->host, urlmap[i].true_url->host) == 0)) { strcpy(tmpUserName, contact->url->username); if (NULL != contact->url->port) { strncpy(tmpPort, contact->url->port, 6); } break; } #endif } /* found a mapping entry */ if (i<URLMAP_SIZE) { char *tmp; if (NULL != contact->url) { printsip("************* sip: rewrote Contact header %s@%s:%s -> %s@%s:%s \r\n", (contact->url->username)? contact->url->username : "******", (contact->url->host)? contact->url->host : "*NULL*", (contact->url->port)? contact->url->port : "NULL port", urlmap[i].masq_url->username, urlmap[i].masq_url->host, (urlmap[i].masq_url->port)? urlmap[i].masq_url->port : "NULL port"); } /* remove old entry */ osip_list_remove(sip_msg->contacts,0); osip_contact_to_str(contact, &tmp); osip_contact_free(contact); /* clone the url from urlmap */ osip_contact_init(&contact); osip_contact_parse(contact,tmp); osip_free(tmp); osip_uri_free(contact->url); if (direction == DIR_OUTGOING) { /* outgoing, use masqueraded url */ osip_uri_clone(urlmap[i].masq_url, &contact->url); if (local_ip != NULL) { char *ip = malloc(20); strncpy(ip, inet_ntoa(*local_ip), 20); free(contact->url->host); contact->url->host = ip; } #if UNREG_WITHOUT_REG_HG522 if ('\0' != tmpUserName[0]) { char *username = malloc(strlen(tmpUserName)+1); strncpy(username, tmpUserName, strlen(tmpUserName)+1); free(contact->url->username); contact->url->username = username; } if ((NULL == urlmap[i].masq_url->port) && ('\0' != tmpPort[0])) { char *port = malloc(strlen(tmpPort)+1); strncpy(port, tmpPort, strlen(tmpPort)+1); contact->url->port = port; } #endif } else { /* incoming, use true url */ osip_uri_clone(urlmap[i].true_url, &contact->url); printsip("incoming: osip_uri_clone from %s \r\n", urlmap[i].true_url->host); } osip_list_add(sip_msg->contacts,contact,-1); } else { return STS_FAILURE; } return STS_SUCCESS; }
/* * SIP_REWRITE_CONTACT * * rewrite the Contact header * * RETURNS * STS_SUCCESS on success * STS_FAILURE on error */ int sip_rewrite_contact (sip_ticket_t *ticket, int direction) { osip_message_t *sip_msg=ticket->sipmsg; osip_contact_t *contact; int i, j; int replaced=0; if (sip_msg == NULL) return STS_FAILURE; /* at least one contact header present? */ osip_message_get_contact(sip_msg, 0, &contact); if (contact == NULL) return STS_FAILURE; /* loop for all existing contact headers in message */ for (j=0; contact != NULL; j++) { osip_message_get_contact(sip_msg, j, &contact); if (contact == NULL) break; if (contact->url == NULL) continue; /* search for an entry */ for (i=0;i<URLMAP_SIZE;i++){ if (urlmap[i].active == 0) continue; if ((direction == DIR_OUTGOING) && (compare_url(contact->url, urlmap[i].true_url)==STS_SUCCESS)) break; if ((direction == DIR_INCOMING) && (compare_url(contact->url, urlmap[i].masq_url)==STS_SUCCESS)) break; } /* found a mapping entry */ if (i<URLMAP_SIZE) { char *tmp; if (direction == DIR_OUTGOING) { DEBUGC(DBCLASS_PROXY, "rewriting Contact header %s@%s -> %s@%s", (contact->url->username)? contact->url->username : "******", (contact->url->host)? contact->url->host : "*NULL*", urlmap[i].masq_url->username, urlmap[i].masq_url->host); } else { DEBUGC(DBCLASS_PROXY, "rewriting Contact header %s@%s -> %s@%s", (contact->url->username)? contact->url->username : "******", (contact->url->host)? contact->url->host : "*NULL*", urlmap[i].true_url->username, urlmap[i].true_url->host); } /* remove old entry */ osip_list_remove(sip_msg->contacts,j); osip_contact_to_str(contact, &tmp); osip_contact_free(contact); /* clone the url from urlmap*/ osip_contact_init(&contact); osip_contact_parse(contact,tmp); osip_free(tmp); osip_uri_free(contact->url); if (direction == DIR_OUTGOING) { /* outgoing, use masqueraded url */ osip_uri_clone(urlmap[i].masq_url, &contact->url); } else { /* incoming, use true url */ osip_uri_clone(urlmap[i].true_url, &contact->url); } osip_list_add(sip_msg->contacts,contact,j); replaced=1; } } if (replaced == 0) { DEBUGC(DBCLASS_PROXY, "no Contact header rewritten"); return STS_FAILURE; } return STS_SUCCESS; }