📄 proxy.c
字号:
* Other responses do return the Contact header of the sender. */ sip_rewrite_contact(response, DIR_INCOMING); } break; /* * from the internal masqueraded host to an external host */ case RESTYP_OUTGOING: DEBUGC(DBCLASS_PROXY,"outgoing response for %s@%s from inbound", response->from->url->username ? response->from->url->username : "*NULL*", response->from->url->host ? response->from->url->host : "*NULL*"); /* rewrite Contact header to represent the masqued address */ sip_rewrite_contact(response, DIR_OUTGOING); #define satoi atoi /* used in MSG_TEST_CODE macro ... */ /* If an 200 OK or 183 Trying answer to an INVITE request, * rewrite body */ if ((MSG_IS_RESPONSE_FOR(response,"INVITE")) && ((MSG_TEST_CODE(response, 200)) || (MSG_TEST_CODE(response, 183)))) { /* This is an outgoing response, therefore an outgoing stream */ sts = proxy_rewrite_invitation_body(response, DIR_OUTGOING); } break; default: DEBUGC(DBCLASS_PROXY, "response from/to unregistered UA (%s@%s)", response->from->url->username? response->from->url->username:"*NULL*", response->from->url->host? response->from->url->host : "*NULL*"); return STS_FAILURE; } /* * check if we need to send to an outbound proxy */ if ((type == RESTYP_OUTGOING) && (configuration.outbound_proxy_host)) { /* have an outbound proxy - use it to send the packet */ sts = get_ip_by_host(configuration.outbound_proxy_host, &sendto_addr); if (sts == STS_FAILURE) { DEBUGC(DBCLASS_PROXY, "proxy_request: cannot resolve outbound " " proxy host [%s]", configuration.outbound_proxy_host); return STS_FAILURE; } if (configuration.outbound_proxy_port) { port=configuration.outbound_proxy_port; } else { port = SIP_PORT; } } else { /* get target address and port from VIA header */ via = (osip_via_t *) osip_list_get (response->vias, 0); if (via == NULL) { ERROR("proxy_response: list_get via failed"); return STS_FAILURE; } sts = get_ip_by_host(via->host, &sendto_addr); if (sts == STS_FAILURE) { DEBUGC(DBCLASS_PROXY, "proxy_response: cannot resolve VIA [%s]", via->host); return STS_FAILURE; } if (via->port) { port=atoi(via->port); } else { port=SIP_PORT; } } sts = osip_message_to_str(response, &buffer); if (sts != 0) { ERROR("proxy_response: osip_message_to_str failed"); return STS_FAILURE; } sipsock_send_udp(&sip_socket, sendto_addr, port, buffer, strlen(buffer), 1); osip_free (buffer); return STS_SUCCESS;}/* * PROXY_REWRITE_INVITATION_BODY * * rewrites the outgoing INVITATION request or response packet * * RETURNS * STS_SUCCESS on success * STS_FAILURE on error */int proxy_rewrite_invitation_body(osip_message_t *mymsg, int direction){ osip_body_t *body; sdp_message_t *sdp; struct in_addr map_addr, msg_addr, outside_addr, inside_addr; int sts; char *bodybuff; char clen[8]; /* content length: probably never more than 7 digits !*/ int map_port, msg_port; int media_stream_no; sdp_connection_t *sdp_conn; sdp_media_t *sdp_med; int rtp_direction=0; /* * get SDP structure */ sts = osip_message_get_body(mymsg, 0, &body); if (sts != 0) { if ((MSG_IS_RESPONSE_FOR(mymsg,"INVITE")) && (MSG_TEST_CODE(mymsg, 183))) { /* 183 Trying *MAY* contain SDP data */ DEBUGC(DBCLASS_PROXY, "rewrite_invitation_body: " "no body found in message"); return STS_SUCCESS; } else { /* INVITE request and 200 response *MUST* contain SDP data */ ERROR("rewrite_invitation_body: no body found in message"); return STS_FAILURE; } } sts = osip_body_to_str(body, &bodybuff); sts = sdp_message_init(&sdp); sts = sdp_message_parse (sdp, bodybuff); osip_free(bodybuff); if (sts != 0) { ERROR("rewrite_invitation_body: unable to sdp_message_parse body"); sdp_message_free(sdp); return STS_FAILURE; }if (configuration.debuglevel){ /* just dump the buffer */ char *tmp, *tmp2; sts = osip_message_get_body(mymsg, 0, &body); sts = osip_body_to_str(body, &tmp); osip_content_length_to_str(mymsg->content_length, &tmp2); DEBUG("Body before rewrite (clen=%s, strlen=%i):\n%s\n----", tmp2, strlen(tmp), tmp); osip_free(tmp); osip_free(tmp2);} /* * RTP proxy: get ready and start forwarding * start forwarding for each media stream ('m=' item in SIP message) */ sts = get_ip_by_host(sdp_message_c_addr_get(sdp,-1,0), &msg_addr); if (sts == STS_FAILURE) { DEBUGC(DBCLASS_PROXY, "proxy_rewrite_invitation_body: cannot resolve " "m= (media) host [%s]", sdp_message_c_addr_get(sdp,-1,0)); sdp_message_free(sdp); return STS_FAILURE; } sts = get_ip_by_ifname(configuration.outbound_if, &outside_addr); if (sts == STS_FAILURE) { ERROR("can't find outbound interface %s - configuration error?", configuration.outbound_if); sdp_message_free(sdp); return STS_FAILURE; } sts = get_ip_by_ifname(configuration.inbound_if, &inside_addr); if (sts == STS_FAILURE) { ERROR("can't find inbound interface %s - configuration error?", configuration.inbound_if); sdp_message_free(sdp); return STS_FAILURE; } /* figure out what address to use for RTP masquerading */ if (MSG_IS_REQUEST(mymsg)) { if (direction == DIR_INCOMING) { map_addr = inside_addr; rtp_direction = DIR_OUTGOING; } else { map_addr = outside_addr; rtp_direction = DIR_INCOMING; } } else /* MSG_IS_REPONSE(mymsg) */ { if (direction == DIR_INCOMING) { map_addr = inside_addr; rtp_direction = DIR_OUTGOING; } else { map_addr = outside_addr; rtp_direction = DIR_INCOMING; } } /* * rewrite c= address * !! an IP address of 0.0.0.0 means *MUTE*, don't rewrite such one */ sdp_conn = sdp_message_connection_get (sdp, -1, 0); if (sdp_conn && sdp_conn->c_addr) { if (strcmp(sdp_conn->c_addr, "0.0.0.0") != 0) { /* have a valid address */ osip_free(sdp_conn->c_addr); sdp_conn->c_addr=osip_malloc(HOSTNAME_SIZE); sprintf(sdp_conn->c_addr, "%s", utils_inet_ntoa(map_addr)); } else { /* 0.0.0.0 - don't rewrite */ DEBUGC(DBCLASS_PROXY, "proxy_rewrite_invitation_body: got a MUTE c= record"); } } else { ERROR("got NULL c= address record - can't rewrite"); } /* * loop through all m= descritions, * start RTP proxy and rewrite them */ if (configuration.rtp_proxy_enable == 1) { for (media_stream_no=0;;media_stream_no++) { /* check if n'th media stream is present */ if (sdp_message_m_port_get(sdp, media_stream_no) == NULL) break; /* start an RTP proxying stream */ if (sdp_message_m_port_get(sdp, media_stream_no)) { msg_port=atoi(sdp_message_m_port_get(sdp, media_stream_no)); if (msg_port > 0) { osip_uri_t *cont_url = NULL; char *user=NULL; if (!osip_list_eol(mymsg->contacts, 0)) cont_url = ((osip_contact_t*)(mymsg->contacts->node->element))->url; if (cont_url) user=cont_url->username; rtp_start_fwd(osip_message_get_call_id(mymsg), user, rtp_direction, media_stream_no, map_addr, &map_port, msg_addr, msg_port); /* and rewrite the port */ sdp_med=osip_list_get(sdp->m_medias, media_stream_no); if (sdp_med && sdp_med->m_port) { osip_free(sdp_med->m_port); sdp_med->m_port=osip_malloc(8); /* 5 digits, \0 + align */ sprintf(sdp_med->m_port, "%i", map_port); DEBUGC(DBCLASS_PROXY, "proxy_rewrite_invitation_body: " "m= rewrote port to [%i]",map_port); } else { ERROR("rewriting port in m= failed sdp_med=%p, " "m_number_of_port=%p", sdp_med, sdp_med->m_port); } } /* if msg_port > 0 */ } else { /* no port defined - skip entry */ WARN("no port defined in m=(media) stream_no=%i", media_stream_no); continue; } } /* for media_stream_no */ } /* if rtp_proxy_enable */ /* remove old body */ sts = osip_list_remove(mymsg->bodies, 0); osip_body_free(body); /* dump new body */ sdp_message_to_str(sdp, &bodybuff); /* free sdp structure */ sdp_message_free(sdp); /* include new body */ osip_message_set_body(mymsg, bodybuff); /* free content length resource and include new one*/ osip_content_length_free(mymsg->content_length); mymsg->content_length=NULL; sprintf(clen,"%i",strlen(bodybuff)); sts = osip_message_set_content_length(mymsg, clen); /* free old body */ osip_free(bodybuff);if (configuration.debuglevel){ /* just dump the buffer */ char *tmp, *tmp2; sts = osip_message_get_body(mymsg, 0, &body); sts = osip_body_to_str(body, &tmp); osip_content_length_to_str(mymsg->content_length, &tmp2); DEBUG("Body after rewrite (clen=%s, strlen=%i):\n%s\n----", tmp2, strlen(tmp), tmp); osip_free(tmp); osip_free(tmp2);} return STS_SUCCESS;}/* * PROXY_REWRITE_REQUEST_URI * * rewrites the incoming Request URI * * RETURNS * STS_SUCCESS on success */int proxy_rewrite_request_uri(osip_message_t *mymsg, int idx){ char *host; char *port; osip_uri_t *url; if ((idx >= URLMAP_SIZE) || (idx < 0)) { WARN("proxy_rewrite_request_uri: called with invalid index"); return STS_FAILURE; } DEBUGC(DBCLASS_PROXY,"rewriting incoming Request URI"); url=osip_message_get_uri(mymsg); /* set the true host */ if(urlmap[idx].true_url->host) { osip_free(url->host);url->host=NULL; DEBUGC(DBCLASS_BABBLE,"proxy_rewrite_request_uri: host=%s", urlmap[idx].true_url->host); host = (char *)malloc(strlen(urlmap[idx].true_url->host)+1); memcpy(host, urlmap[idx].true_url->host, strlen(urlmap[idx].true_url->host)); host[strlen(urlmap[idx].true_url->host)]='\0'; osip_uri_set_host(url, host); } /* set the true port */ if(urlmap[idx].true_url->port) { osip_free(url->port);url->port=NULL; DEBUGC(DBCLASS_BABBLE,"proxy_rewrite_request_uri: port=%s", urlmap[idx].true_url->port); port = (char *)malloc(strlen(urlmap[idx].true_url->port)+1); memcpy(port, urlmap[idx].true_url->port, strlen(urlmap[idx].true_url->port)); port[strlen(urlmap[idx].true_url->port)]='\0'; osip_uri_set_port(url, port); } return STS_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -