📄 register.c
字号:
(ticket->sipmsg->contacts->node->element))->url; strcpy(url1_contact->host,utils_inet_ntoa(ticket->from.sin_addr)); //printf("\n%d\n",ntohs(ticket->from.sin_port));if(url1_contact->port ==NULL){ url1_contact->port = realloc(url1_contact->port, 16); //sprintf(url1_contact->port, "%d", configuration.sip_listen_port);} sprintf(url1_contact->port,"%d", ntohs(ticket->from.sin_port));// printf("----- url1_contact->port = %s --------\n",url1_contact->port); osip_uri_clone(url1_contact, &urlmap[i].true_url);// printf("\n------------------------------***********************\n");// printf("url1_contact->host = %s\n",url1_contact->host);// printf("url1_contact->port = %s",url1_contact->port);// printf("\n************************------------------------------\n"); /* To: field */ osip_uri_clone( ticket->sipmsg->to->url, &urlmap[i].reg_url);// urlmap[i].masq_url->port = realloc(urlmap[i].masq_url->port, 16);// sprintf(urlmap[i].masq_url->port, "%i", configuration.sip_listen_port); DEBUGC(DBCLASS_REG,"create new entry for %s@%s <-> %s@%s at slot=%i", (url1_contact->username) ? url1_contact->username : "*NULL*", (url1_contact->host) ? url1_contact->host : "*NULL*", (urlmap[i].reg_url->username) ? urlmap[i].reg_url->username : "*NULL*", (urlmap[i].reg_url->host) ? urlmap[i].reg_url->host : "*NULL*", i); /* * try to figure out if we ought to do some masquerading */ osip_uri_clone( ticket->sipmsg->to->url, &urlmap[i].masq_url); if (urlmap[i].masq_url->port == NULL) { urlmap[i].masq_url->port=realloc(urlmap[i].masq_url->port, 16); sprintf(urlmap[i].masq_url->port, "%i", configuration.sip_listen_port); }//printf("\n***********************************************\n");//printf("masq_url = %s\n", urlmap[i].masq_url->host);//printf("true_url = %s\n", urlmap[i].true_url->host);//printf("reg_url = %s\n",urlmap[i].reg_url->host);//printf("\n***********************************************\n"); n=configuration.mask_host.used; if (n != configuration.masked_host.used) { ERROR("# of mask_host is not equal to # of masked_host in config!"); n=0; }//printf(" n= %d ",n); DEBUG("%i entries in MASK config table", n); for (j=0; j<n; j++) { DEBUG("compare [%s] <-> [%s]",configuration.mask_host.string[j], ticket->sipmsg->to->url->host); if (strcmp(configuration.mask_host.string[j], ticket->sipmsg->to->url->host)==0) break; } if (j<n) { /* we are masquerading this UA, replace the host part of the url */ DEBUGC(DBCLASS_REG,"masquerading UA %s@%s as %s@%s", (url1_contact->username) ? url1_contact->username : "*NULL*", (url1_contact->host) ? url1_contact->host : "*NULL*", (url1_contact->username) ? url1_contact->username : "*NULL*", configuration.masked_host.string[j]); urlmap[i].masq_url->host=realloc(urlmap[i].masq_url->host, strlen(configuration.masked_host.string[j])+1); strcpy(urlmap[i].masq_url->host, configuration.masked_host.string[j]); } /* * for transparent proxying: force device to be masqueraded * as with the outbound IP */ if (force_lcl_masq) { struct in_addr addr; char *addrstr; if (get_ip_by_ifname(configuration.outbound_if, &addr) != STS_SUCCESS) { ERROR("can't find outbound interface %s - configuration error?", configuration.outbound_if); return STS_FAILURE; } /* host part */ addrstr = utils_inet_ntoa(addr); DEBUGC(DBCLASS_REG,"masquerading UA %s@%s local %s@%s", (url1_contact->username) ? url1_contact->username : "*NULL*", (url1_contact->host) ? url1_contact->host : "*NULL*", (url1_contact->username) ? url1_contact->username : "*NULL*", addrstr); urlmap[i].masq_url->host=realloc(urlmap[i].masq_url->host, strlen(addrstr)+1); strcpy(urlmap[i].masq_url->host, addrstr);printf("\n***********************************************\n");printf("masq_url = %s\n", urlmap[i].masq_url->host);printf("true_url = %s\n", urlmap[i].true_url->host);printf("reg_url = %s\n",urlmap[i].reg_url->host);printf("\n***********************************************\n"); /* port number if required */ if (urlmap[i].masq_url->port == NULL) { urlmap[i].masq_url->port=realloc(urlmap[i].masq_url->port, 16); sprintf(urlmap[i].masq_url->port, "%i", configuration.sip_listen_port); } } } else { /* if new entry */ /* * Some phones (like BudgeTones *may* dynamically grab a SIP port * so we might want to update the true_url and reg_url each time * we get an REGISTER */ /* Contact: field */ osip_uri_free(urlmap[i].true_url); osip_uri_clone( ((osip_contact_t*) (ticket->sipmsg->contacts->node->element))->url, &urlmap[i].true_url); if(urlmap[i].true_url->port == NULL){ urlmap[i].true_url->port=realloc(urlmap[i].true_url->port, 16); sprintf(urlmap[i].true_url->port,"%i", ticket->from.sin_port); } /* To: field */ osip_uri_free(urlmap[i].reg_url); osip_uri_clone( ticket->sipmsg->to->url, &urlmap[i].reg_url); } /* give some safety margin for the next update */ if (expires > 0) expires+=30; /* update registration timeout */ urlmap[i].expires=time_now+expires; /* if(force_lcl_masq == 1) { urlmap[i].expires = 0; } */ /* * un-REGISTER */ } else { /* expires > 0 */ /* * Remove registration * Siproxd will ALWAYS remove ALL bindings for a given * address-of-record */ for (i=0; i<URLMAP_SIZE; i++) { if (urlmap[i].active == 0) continue; url2_to=urlmap[i].reg_url; if (compare_url(url1_to, url2_to)==STS_SUCCESS) { DEBUGC(DBCLASS_REG, "removing registration for %s@%s at slot=%i", (url2_to->username) ? url2_to->username : "*NULL*", (url2_to->host) ? url2_to->host : "*NULL*", i); urlmap[i].expires=0; urlmap[i].active = 0; break; } } } register_shut(); return STS_SUCCESS;}/* * cyclically called to do the aging of the URL mapping table entries * and throw out expired entries. */void register_agemap(void) { int i; time_t t; time(&t); DEBUGC(DBCLASS_BABBLE,"sip_agemap, t=%i",(int)t); for (i=0; i<URLMAP_SIZE; i++) { if ((urlmap[i].active == 1) && (urlmap[i].expires < t)) { DEBUGC(DBCLASS_REG,"cleaned entry:%i %s@%s", i, urlmap[i].masq_url->username, urlmap[i].masq_url->host); urlmap[i].active=0; osip_uri_free(urlmap[i].true_url); osip_uri_free(urlmap[i].masq_url); osip_uri_free(urlmap[i].reg_url);// osip_via_free(urlmap[i].via); } } return;}/* * send answer to a registration request. * flag = STS_SUCCESS -> positive answer (200) * flag = STS_FAILURE -> negative answer (503) * flag = STS_NEED_AUTH -> proxy authentication needed (407) * * RETURNS * STS_SUCCESS on success * STS_FAILURE on error */int register_response(sip_ticket_t *ticket, int flag) { osip_message_t *response; int code; int sts; osip_via_t *via; int port; char *buffer; struct in_addr addr; osip_header_t *expires_hdr; struct sockaddr_in *from; osip_generic_param_t *received; osip_generic_param_t *rport; /* ok -> 200, fail -> 503 */ switch (flag) { case STS_SUCCESS: code = 200; /* OK */ break; case STS_FAILURE: code = 503; /* failed */ break; case STS_NEED_AUTH: code = 407; /* proxy authentication needed */ break; default: code = 503; /* failed */ break; } from = &ticket->from; osip_message_fix_last_via_header(ticket->sipmsg, inet_ntoa (from->sin_addr), ntohs(from->sin_port)); /* create the response template */ if ((response=msg_make_template_reply(ticket, code))==NULL) { ERROR("register_response: error in msg_make_template_reply"); return STS_FAILURE; } /* insert the expiration header */ osip_message_get_expires(ticket->sipmsg, 0, &expires_hdr); if (expires_hdr) { osip_message_set_expires(response, expires_hdr->hvalue); } /* if we send back an proxy authentication needed, include the Proxy-Authenticate field */ if (code==407 || code==401) { auth_include_authrq(ticket, code, response); } /* get the IP address from existing VIA header */ osip_message_get_via (response, 0, &via); if (via == NULL) { ERROR("register_response: Cannot send response - no via field"); return STS_FAILURE; } osip_via_param_get_byname(via, "received",&received); osip_via_param_get_byname (via, "rport", &rport); if(received != NULL) { if (utils_inet_aton(received->gvalue,&addr) == 0) { /* yes, get IP address */ sts = get_ip_by_host(received->gvalue, &addr); if(sts == STS_FAILURE) { DEBUGC(DBCLASS_REG, "register_response: cannot resolve VIA [%s]", received->gvalue); return STS_FAILURE; } } } else { /* name resolution needed? */ if (utils_inet_aton(via->host,&addr) == 0) { /* yes, get IP address */ sts = get_ip_by_host(via->host, &addr); if (sts == STS_FAILURE) { DEBUGC(DBCLASS_REG, "register_response: cannot resolve VIA [%s]", via->host); return STS_FAILURE; } } }if(rport != NULL && rport->gvalue != NULL){ port = atoi(rport->gvalue);}else if (via->port) { port=atoi(via->port);} else { port=configuration.sip_listen_port;} sts = osip_message_to_str(response, &buffer); if (sts != 0) { ERROR("register_response: msg_2char failed"); return STS_FAILURE; } sipsock_send(addr, port, ticket->protocol, buffer, strlen(buffer)); /* free the resources */ osip_message_free(response); free(buffer); return STS_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -