From 115b6b9ee0b1f7a4f935d4e9f0b637e857b0a0ad Mon Sep 17 00:00:00 2001 From: Thomas Ries Date: Fri, 23 Jan 2004 00:02:18 +0000 Subject: [PATCH] - ./configure option: --enable-static to build a completely statically linked executable - REGISTER takes honors the expires parameter of the contact header - Contact header of REGISTER response must be rewritten back to the local (true) URL --- ChangeLog | 15 +++++++ configure.in | 12 +++++- src/proxy.c | 100 +++++++++++++------------------------------ src/register.c | 29 ++++++++++++- src/rtpproxy.c | 16 +++---- src/rtpproxy.h | 10 ++--- src/rtpproxy_masq.c | 21 ++++----- src/rtpproxy_relay.c | 29 ++++--------- src/security.c | 6 +++ src/sip_utils.c | 83 ++++++++++++++++++++++++++++++++--- src/siproxd.h | 19 ++++---- src/sock.c | 10 ++++- 12 files changed, 217 insertions(+), 133 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9acd1b6..9a77c8b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +0.5.2 +===== + 22-Jan-2004: - ./configure option: --enable-static to build + a completely statically linked executable + - REGISTER takes honors the expires parameter + of the contact header + - Contact header of REGISTER response must be + rewritten back to the local (true) URL + 18-Jan-2004: - security_check_raw: + size check: >= 16 bytes + - at exit, check registration file to be writable + - no WARNING if SIP user-agent header is not supplied. + - Call logging: distinguish between In & Out + - include branch parameter for via headers + 0.5.1 ===== 21-Dec-2003: - possibility to log call establishment diff --git a/configure.in b/configure.in index 205dd48..4464dbf 100644 --- a/configure.in +++ b/configure.in @@ -20,6 +20,7 @@ dnl 1-Nov-2003 tries check for NETFILTER (IPTABLES) dnl 18-Nov-2003 tries include sysconfdir to search for config dnl 19-Nov-2003 tries changes to support Solaris & BSD/OS dnl 22-Nov-2003 tries test for gethostbyname_r() & # of args +dnl 22-Jan-2004 tries add --enable-static option dnl dnl dnl @@ -35,7 +36,7 @@ dnl Release Version dnl SPD_MAJOR_VERSION=0 SPD_MINOR_VERSION=5 -SPD_MICRO_VERSION=1 +SPD_MICRO_VERSION=2 SPD_VERSION=$SPD_MAJOR_VERSION.$SPD_MINOR_VERSION.$SPD_MICRO_VERSION @@ -85,6 +86,15 @@ for each in $extra_libs; do LIBS="$LIBS -L$each"; done +dnl +dnl add +dnl --enable-static + AC_MSG_CHECKING(build statically linked executable) + AC_ARG_ENABLE(static, + [ --enable-static build statically linked executable], + LDFLAGS="$LDFLAGS -static"; + AC_MSG_RESULT(yes), AC_MSG_RESULT(no)) + dnl dnl add dnl --enable-dmalloc diff --git a/src/proxy.c b/src/proxy.c index 8165999..6d00c68 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -60,7 +60,6 @@ int proxy_request (osip_message_t *request) { int sts; int type; struct in_addr sendto_addr; - osip_contact_t *contact; osip_uri_t *url; int port; char *buffer; @@ -144,11 +143,13 @@ int proxy_request (osip_message_t *request) { /* INVITE */ if(MSG_IS_INVITE(request)) { if (cont_url) { - INFO("Incomming Call from: %s:%s", + INFO("%s Call from: %s:%s", + (type==REQTYP_INCOMING) ? "Incoming":"Outgoing", cont_url->username ? cont_url->username:"*NULL*", cont_url->host ? cont_url->host : "*NULL*"); } else { - INFO("Incomming Call (w/o contact header) from: %s:%s", + INFO("%s Call (w/o contact header) from: %s:%s", + (type==REQTYP_INCOMING) ? "Incoming":"Outgoing", request->from->url->username ? request->from->url->username:"*NULL*", request->from->url->host ? @@ -186,7 +187,7 @@ int proxy_request (osip_message_t *request) { } /* rewrite request URI to point to the real host */ - /* i still holds the valid index into the URLMAP table */ + /* 'i' still holds the valid index into the URLMAP table */ if (check_rewrite_rq_uri(request)==STS_TRUE) { proxy_rewrite_request_uri(request, i); } @@ -201,13 +202,13 @@ int proxy_request (osip_message_t *request) { /* if this is CANCEL/BYE request, stop RTP proxying */ if (MSG_IS_BYE(request) || MSG_IS_CANCEL(request)) { /* stop the RTP proxying stream(s) */ - rtp_stop_fwd(osip_message_get_call_id(request), incoming); - rtp_stop_fwd(osip_message_get_call_id(request), outgoing); + rtp_stop_fwd(osip_message_get_call_id(request), DIR_INCOMING); + rtp_stop_fwd(osip_message_get_call_id(request), DIR_OUTGOING); /* check for incoming request */ } else if (MSG_IS_INVITE(request)) { /* First, rewrite the body */ - sts = proxy_rewrite_invitation_body(request, incoming); + sts = proxy_rewrite_invitation_body(request, DIR_INCOMING); /* * Note: Incoming requests have no need to rewrite Contact @@ -248,37 +249,11 @@ int proxy_request (osip_message_t *request) { /* if an INVITE, rewrite body */ if (MSG_IS_INVITE(request)) { - sts = proxy_rewrite_invitation_body(request, outgoing); + sts = proxy_rewrite_invitation_body(request, DIR_OUTGOING); } /* rewrite Contact header to represent the masqued address */ - osip_message_get_contact(request,0,&contact); - if (contact != NULL) { - for (i=0;iurl, urlmap[i].true_url)==STS_SUCCESS) - break; - } - /* found a mapping entry */ - if (i %s@%s", - (contact->url->username)? contact->url->username : "*NULL*", - (contact->url->host)? contact->url->host : "*NULL*", - urlmap[i].masq_url->username, urlmap[i].masq_url->host); - /* remove old entry */ - osip_list_remove(request->contacts,0); - osip_contact_to_str(contact, &tmp); - osip_contact_free(contact); - /* clone the masquerading url */ - osip_contact_init(&contact); - osip_contact_parse(contact,tmp); - osip_free(tmp); - osip_uri_free(contact->url); - osip_uri_clone(urlmap[i].masq_url, &contact->url); - osip_list_add(request->contacts,contact,-1); - } - } + sip_rewrite_contact(request, DIR_OUTGOING); /* add my Via header line (outbound interface)*/ sts = sip_add_myvia(request, IF_OUTBOUND); @@ -288,8 +263,8 @@ int proxy_request (osip_message_t *request) { /* if this is CANCEL/BYE request, stop RTP proxying */ if (MSG_IS_BYE(request) || MSG_IS_CANCEL(request)) { - rtp_stop_fwd(osip_message_get_call_id(request), incoming); - rtp_stop_fwd(osip_message_get_call_id(request), outgoing); + rtp_stop_fwd(osip_message_get_call_id(request), DIR_INCOMING); + rtp_stop_fwd(osip_message_get_call_id(request), DIR_OUTGOING); } break; @@ -371,7 +346,6 @@ int proxy_response (osip_message_t *response) { int type; struct in_addr sendto_addr; osip_via_t *via; - osip_contact_t *contact; int port; char *buffer; @@ -446,13 +420,19 @@ int proxy_response (osip_message_t *response) { ((MSG_TEST_CODE(response, 200)) || (MSG_TEST_CODE(response, 183)))) { /* This is an incoming response, therefore we need an incoming stream */ - sts = proxy_rewrite_invitation_body(response, incoming); + sts = proxy_rewrite_invitation_body(response, DIR_INCOMING); - /* - * Note: Incoming request has no need to rewrite Contact - * header as we are not masquerading something there - */ } + + if (MSG_IS_RESPONSE_FOR(response,"REGISTER")) { + /* + * REGISTER returns *my* Contact header information. + * Rewrite Contact header back to represent the true address. + * Other responses do return the Contact header of the sender. + */ + sip_rewrite_contact(response, DIR_INCOMING); + } + break; /* @@ -466,32 +446,11 @@ int proxy_response (osip_message_t *response) { ((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, outgoing); + sts = proxy_rewrite_invitation_body(response, DIR_OUTGOING); } /* rewrite Contact header to represent the masqued address */ - osip_message_get_contact(response,0,&contact); - if (contact != NULL) { - for (i=0;iurl, urlmap[i].true_url)==STS_SUCCESS) - break; - } - /* found a mapping entry */ - if (i %s@%s", - (contact->url->username) ? contact->url->username:"*NULL*", - (contact->url->host) ? contact->url->host : "*NULL*", - urlmap[i].masq_url->username, urlmap[i].masq_url->host); - /* remove old entry */ - osip_list_remove(response->contacts,0); - osip_contact_free(contact); - /* clone the masquerading url */ - osip_contact_init(&contact); - osip_uri_clone(urlmap[i].masq_url, &contact->url); - osip_list_add(response->contacts,contact,-1); - } - } + sip_rewrite_contact(response, DIR_OUTGOING); break; @@ -562,7 +521,7 @@ int proxy_response (osip_message_t *response) { * STS_SUCCESS on success * STS_FAILURE on error */ -int proxy_rewrite_invitation_body(osip_message_t *mymsg, rtp_direction dir){ +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; @@ -640,12 +599,12 @@ if (configuration.debuglevel) /* figure out what address to use for RTP masquerading */ if (MSG_IS_REQUEST(mymsg)) { - if (dir == incoming) + if (direction == DIR_INCOMING) map_addr = inside_addr; else map_addr = outside_addr; } else /* MSG_IS_REPONSE(mymsg) */ { - if (dir == incoming) + if (direction == DIR_INCOMING) map_addr = inside_addr; else map_addr = outside_addr; @@ -683,7 +642,8 @@ if (configuration.debuglevel) msg_port=atoi(sdp_message_m_port_get(sdp, media_stream_no)); if (msg_port > 0) { - rtp_start_fwd(osip_message_get_call_id(mymsg), dir, media_stream_no, + rtp_start_fwd(osip_message_get_call_id(mymsg), direction, + media_stream_no, map_addr, &map_port, msg_addr, msg_port); /* and rewrite the port */ diff --git a/src/register.c b/src/register.c index 685e8c7..545593e 100644 --- a/src/register.c +++ b/src/register.c @@ -120,6 +120,17 @@ void register_shut(void) { if (configuration.registrationfile) { /* write urlmap back to file */ stream = fopen(configuration.registrationfile, "w+"); + if (!stream) { + /* try to unlink it and open again */ + unlink(configuration.registrationfile); + stream = fopen(configuration.registrationfile, "w+"); + + /* open file for write failed, complain */ + if (!stream) { + ERROR("unable to write registration file"); + return; + } + } for (i=0;i < URLMAP_SIZE; i++) { fprintf(stream, "***:%i:%i\n", urlmap[i].active, urlmap[i].expires); @@ -161,6 +172,7 @@ int register_client(osip_message_t *my_msg, int force_lcl_masq) { osip_uri_t *url1_to, *url1_contact; osip_uri_t *url2_to, *url2_contact; osip_header_t *expires_hdr; + osip_uri_param_t *expires_param=NULL; /* check for proxy authentication */ sts = authenticate_proxy(my_msg); @@ -195,10 +207,23 @@ int register_client(osip_message_t *my_msg, int force_lcl_masq) { /* evaluate Expires Header field */ osip_message_get_expires(my_msg, 0, &expires_hdr); - if (expires_hdr && expires_hdr->hvalue) { + /* + * look for an Contact expires parameter - in case of REGISTER + * these two are equal. The Contact expires has higher priority! + */ + + osip_contact_param_get_byname( + (osip_contact_t*) my_msg->contacts->node->element, + "expires", &expires_param); + + if (expires_param && expires_param->gvalue) { + /* get expires from contact Header */ + expires=atoi(expires_param->gvalue); + } else if (expires_hdr && expires_hdr->hvalue) { + /* get expires from expires Header */ expires=atoi(expires_hdr->hvalue); } else { - /* it seems the expires field is not present everywhere... */ + /* it seems, the expires field is not present everywhere... */ WARN("no 'expires' header found - set time to 600 sec"); expires=600; osip_message_set_expires(my_msg, "600"); diff --git a/src/rtpproxy.c b/src/rtpproxy.c index 08e8522..5872468 100644 --- a/src/rtpproxy.c +++ b/src/rtpproxy.c @@ -68,7 +68,7 @@ int rtpproxy_init( void ) { * STS_SUCCESS on success * STS_FAILURE on error */ -int rtp_start_fwd (osip_call_id_t *callid, rtp_direction dir, +int rtp_start_fwd (osip_call_id_t *callid, int direction, int media_stream_no, struct in_addr outbound_ipaddr, int *outboundport, struct in_addr lcl_client_ipaddr, int lcl_clientport) { @@ -77,15 +77,15 @@ int rtp_start_fwd (osip_call_id_t *callid, rtp_direction dir, if (configuration.rtp_proxy_enable == 0) { sts = STS_SUCCESS; } else if (configuration.rtp_proxy_enable == 1) { // Relay - sts = rtp_relay_start_fwd (callid, dir, media_stream_no, + sts = rtp_relay_start_fwd (callid, direction, media_stream_no, outbound_ipaddr, outboundport, lcl_client_ipaddr, lcl_clientport); } else if (configuration.rtp_proxy_enable == 2) { // MASQ tunnels (ipchains) - sts = rtp_masq_start_fwd (callid, dir, media_stream_no, + sts = rtp_masq_start_fwd (callid, direction, media_stream_no, outbound_ipaddr, outboundport, lcl_client_ipaddr, lcl_clientport); } else if (configuration.rtp_proxy_enable == 3) { // MASQ tunnels (netfilter) - sts = rtp_masq_start_fwd (callid, dir, media_stream_no, + sts = rtp_masq_start_fwd (callid, direction, media_stream_no, outbound_ipaddr, outboundport, lcl_client_ipaddr, lcl_clientport); } else { @@ -104,17 +104,17 @@ int rtp_start_fwd (osip_call_id_t *callid, rtp_direction dir, * STS_SUCCESS on success * STS_FAILURE on error */ -int rtp_stop_fwd (osip_call_id_t *callid, rtp_direction dir) { +int rtp_stop_fwd (osip_call_id_t *callid, int direction) { int sts = STS_FAILURE; if (configuration.rtp_proxy_enable == 0) { sts = STS_SUCCESS; } else if (configuration.rtp_proxy_enable == 1) { // Relay - sts = rtp_relay_stop_fwd(callid, dir, 0); + sts = rtp_relay_stop_fwd(callid, direction, 0); } else if (configuration.rtp_proxy_enable == 2) { // MASQ tunnels (ipchains) - sts = rtp_masq_stop_fwd(callid, dir); + sts = rtp_masq_stop_fwd(callid, direction); } else if (configuration.rtp_proxy_enable == 3) { // MASQ tunnels (netfilter) - sts = rtp_masq_stop_fwd(callid, dir); + sts = rtp_masq_stop_fwd(callid, direction); } else { ERROR("CONFIG: rtp_proxy_enable has invalid value", configuration.rtp_proxy_enable); diff --git a/src/rtpproxy.h b/src/rtpproxy.h index b96dd4b..edf7301 100644 --- a/src/rtpproxy.h +++ b/src/rtpproxy.h @@ -25,7 +25,7 @@ typedef struct { int sock; char callid_number[CALLIDNUM_SIZE]; /* call ID */ char callid_host[CALLIDHOST_SIZE]; /* --"-- */ - rtp_direction direction; /* Direction of RTP stream */ + int direction; /* Direction of RTP stream */ int media_stream_no; struct in_addr outbound_ipaddr; /* lcl outbound IP */ int outboundport; /* lcl allocd outbound port */ @@ -38,22 +38,22 @@ typedef struct { * RTP relay */ int rtp_relay_init(void); -int rtp_relay_start_fwd (osip_call_id_t *callid, rtp_direction dir, +int rtp_relay_start_fwd (osip_call_id_t *callid, int direction, int media_stream_no, struct in_addr outbound_ipaddr, int *outboundport, struct in_addr lcl_client_ipaddr, int lcl_clientport); -int rtp_relay_stop_fwd (osip_call_id_t *callid, rtp_direction dir, int nolock); +int rtp_relay_stop_fwd (osip_call_id_t *callid, int direction, int nolock); /* * RTP masquerading */ int rtp_masq_init(void); -int rtp_masq_start_fwd (osip_call_id_t *callid, rtp_direction dir, +int rtp_masq_start_fwd (osip_call_id_t *callid, int direction, int media_stream_no, struct in_addr outbound_ipaddr, int *outboundport, struct in_addr lcl_client_ipaddr, int lcl_clientport); -int rtp_masq_stop_fwd (osip_call_id_t *callid, rtp_direction dir); +int rtp_masq_stop_fwd (osip_call_id_t *callid, int direction); /* * RTP masquerading (ipchains part) diff --git a/src/rtpproxy_masq.c b/src/rtpproxy_masq.c index 5a57b01..3f7e06f 100644 --- a/src/rtpproxy_masq.c +++ b/src/rtpproxy_masq.c @@ -85,7 +85,7 @@ int rtp_masq_init( void ) { } -int rtp_masq_start_fwd(osip_call_id_t *callid, rtp_direction dir, +int rtp_masq_start_fwd(osip_call_id_t *callid, int direction, int media_stream_no, struct in_addr outbound_ipaddr, int *outbound_lcl_port, struct in_addr lcl_client_ipaddr, int lcl_clientport) { @@ -161,7 +161,8 @@ int rtp_masq_start_fwd(osip_call_id_t *callid, rtp_direction dir, DEBUGC(DBCLASS_RTP,"rtp_masq_start_fwd: starting RTP proxy " "stream for: %s@%s (%s) #=%i", callid->number, callid->host, - ((dir == incoming) ? "incoming" : "outgoing"), media_stream_no); + ((direction == DIR_INCOMING) ? "incoming" : "outgoing"), + media_stream_no); /* * figure out, if this is an request to start an RTP proxy stream @@ -175,7 +176,7 @@ int rtp_masq_start_fwd(osip_call_id_t *callid, rtp_direction dir, cid.host = rtp_proxytable[i].callid_host; if (rtp_proxytable[i].sock != 0) { if((compare_callid(callid, &cid) == STS_SUCCESS) && - (rtp_proxytable[i].direction == dir) && + (rtp_proxytable[i].direction == direction) && (rtp_proxytable[i].media_stream_no == media_stream_no)) { /* return the already known port number */ *outbound_lcl_port=rtp_proxytable[i].outboundport; @@ -251,7 +252,7 @@ int rtp_masq_start_fwd(osip_call_id_t *callid, rtp_direction dir, rtp_proxytable[freeidx].callid_host[0]='\0'; } - rtp_proxytable[freeidx].direction = dir; + rtp_proxytable[freeidx].direction = direction; rtp_proxytable[freeidx].media_stream_no = media_stream_no; memcpy(&rtp_proxytable[freeidx].outbound_ipaddr, &outbound_ipaddr, sizeof(struct in_addr)); @@ -268,7 +269,7 @@ int rtp_masq_start_fwd(osip_call_id_t *callid, rtp_direction dir, } -int rtp_masq_stop_fwd (osip_call_id_t *callid, rtp_direction dir) { +int rtp_masq_stop_fwd (osip_call_id_t *callid, int direction) { int sts=STS_FAILURE; int i; int got_match=0; @@ -284,7 +285,7 @@ int rtp_masq_stop_fwd (osip_call_id_t *callid, rtp_direction dir) { DEBUGC(DBCLASS_RTP,"rtp_masq_stop_fwd: stopping RTP proxy " "stream for: %s@%s (%s)", callid->number, callid->host, - ((dir == incoming) ? "incoming" : "outgoing")); + ((direction == DIR_INCOMING) ? "incoming" : "outgoing")); for (i=0; inumber, callid->host, - ((dir == incoming) ? "incoming" : "outgoing")); + ((direction == DIR_INCOMING) ? "incoming" : "outgoing")); return STS_FAILURE; } @@ -342,14 +343,14 @@ int rtp_masq_init( void ) { ERROR("Masquerading support is not enabled (compile time config option)"); return STS_FAILURE; } -int rtp_masq_start_fwd(osip_call_id_t *callid, rtp_direction dir, +int rtp_masq_start_fwd(osip_call_id_t *callid, int direction, int media_stream_no, struct in_addr outbound_ipaddr, int *outbound_lcl_port, struct in_addr lcl_client_ipaddr, int lcl_clientport) { outbound_lcl_port=0; return STS_FAILURE; } -int rtp_masq_stop_fwd (osip_call_id_t *callid, rtp_direction dir) { +int rtp_masq_stop_fwd (osip_call_id_t *callid, int direction) { return STS_FAILURE; } #endif diff --git a/src/rtpproxy_relay.c b/src/rtpproxy_relay.c index dee0d04..cb33ccc 100644 --- a/src/rtpproxy_relay.c +++ b/src/rtpproxy_relay.c @@ -140,18 +140,6 @@ static void *rtpproxy_main(void *arg) { continue; } -//#ifdef MOREDEBUG /*&&&&*/ -//if (num_fd<0) { -// int i; -// WARN("select() returned error [%s]",strerror(errno)); -// for (i=0;i socket=%i",i, rtp_proxytable[i].sock); -// } -// } /* for i */ -//} -//#endif time(&t); /* @@ -226,7 +214,7 @@ static void *rtpproxy_main(void *arg) { * STS_SUCCESS on success * STS_FAILURE on error */ -int rtp_relay_start_fwd (osip_call_id_t *callid, rtp_direction dir, +int rtp_relay_start_fwd (osip_call_id_t *callid, int direction, int media_stream_no, struct in_addr outbound_ipaddr, int *outboundport, struct in_addr lcl_client_ipaddr, int lcl_clientport) { @@ -264,7 +252,8 @@ int rtp_relay_start_fwd (osip_call_id_t *callid, rtp_direction dir, DEBUGC(DBCLASS_RTP,"rtp_relay_start_fwd: starting RTP proxy " "stream for: %s@%s (%s) #=%i", callid->number, callid->host, - ((dir == incoming) ? "incoming" : "outgoing"), media_stream_no); + ((direction == DIR_INCOMING) ? "incoming" : "outgoing"), + media_stream_no); /* lock mutex */ #define return is_forbidden_in_this_code_section @@ -289,7 +278,7 @@ int rtp_relay_start_fwd (osip_call_id_t *callid, rtp_direction dir, cid.host = rtp_proxytable[i].callid_host; if (rtp_proxytable[i].sock && (compare_callid(callid, &cid) == STS_SUCCESS) && - (rtp_proxytable[i].direction == dir) && + (rtp_proxytable[i].direction == direction) && (rtp_proxytable[i].media_stream_no == media_stream_no) ) { /* * The RTP port number reported by the UA MAY change @@ -381,7 +370,7 @@ int rtp_relay_start_fwd (osip_call_id_t *callid, rtp_direction dir, rtp_proxytable[freeidx].callid_host[0]='\0'; } - rtp_proxytable[freeidx].direction = dir; + rtp_proxytable[freeidx].direction = direction; rtp_proxytable[freeidx].media_stream_no = media_stream_no; memcpy(&rtp_proxytable[freeidx].outbound_ipaddr, &outbound_ipaddr, sizeof(struct in_addr)); @@ -416,7 +405,7 @@ unlock_and_exit: * STS_SUCCESS on success * STS_FAILURE on error */ -int rtp_relay_stop_fwd (osip_call_id_t *callid, rtp_direction dir, +int rtp_relay_stop_fwd (osip_call_id_t *callid, int direction, int nolock) { int i, sts; int retsts=STS_SUCCESS; @@ -431,7 +420,7 @@ int rtp_relay_stop_fwd (osip_call_id_t *callid, rtp_direction dir, DEBUGC(DBCLASS_RTP,"rtp_relay_stop_fwd: stopping RTP proxy " "stream for: %s@%s (%s)", callid->number, callid->host, - ((dir == incoming) ? "incoming" : "outgoing")); + ((direction == DIR_INCOMING) ? "incoming" : "outgoing")); /* * lock mutex - only if not requested to skip the lock. @@ -469,7 +458,7 @@ int rtp_relay_stop_fwd (osip_call_id_t *callid, rtp_direction dir, cid.host = rtp_proxytable[i].callid_host; if (rtp_proxytable[i].sock && (compare_callid(callid, &cid) == STS_SUCCESS) && - (rtp_proxytable[i].direction == dir)) { + (rtp_proxytable[i].direction == direction)) { sts = close(rtp_proxytable[i].sock); DEBUGC(DBCLASS_RTP,"closed socket %i for RTP stream " "%s:%s == %s:%s (idx=%i) sts=%i", @@ -494,7 +483,7 @@ int rtp_relay_stop_fwd (osip_call_id_t *callid, rtp_direction dir, DEBUGC(DBCLASS_RTP, "rtp_relay_stop_fwd: can't find active stream for %s@%s (%s)", callid->number, callid->host, - ((dir == incoming) ? "incoming" : "outgoing")); + ((direction == DIR_INCOMING) ? "incoming" : "outgoing")); retsts = STS_FAILURE; goto unlock_and_exit; } diff --git a/src/security.c b/src/security.c index adbb567..377fc98 100644 --- a/src/security.c +++ b/src/security.c @@ -47,6 +47,12 @@ static char const ident[]="$Id: " __FILE__ ": " PACKAGE "-" VERSION "-"\ */ int security_check_raw(char *sip_buffer, int size){ + /* + * empiric: size must be >= 16 bytes + * 2 byte packets have been seen in the wild + */ + if (size<16) return STS_FAILURE; + /* TODO: still way to go here ... */ return STS_SUCCESS; } diff --git a/src/sip_utils.c b/src/sip_utils.c index 2eb3b14..3eb2327 100644 --- a/src/sip_utils.c +++ b/src/sip_utils.c @@ -51,6 +51,8 @@ extern struct siproxd_config configuration; extern int h_errno; extern int sip_socket; /* sending SIP datagrams */ +extern struct urlmap_s urlmap[]; /* URL mapping table */ + /* * create a reply template from an given SIP request @@ -452,7 +454,8 @@ int check_rewrite_rq_uri (osip_message_t *sip) { /* extract UA string */ osip_message_get_user_agent (sip, 0, &ua_hdr); if ((ua_hdr==NULL) || (ua_hdr->hvalue==NULL)) { - WARN("check_rewrite_rq_uri: NULL UA in Header, using default"); + DEBUGC(DBCLASS_SIP, "check_rewrite_rq_uri: NULL UA in Header, " + "using default"); i=dflidx; } else { /* loop through the knowledge base */ @@ -491,7 +494,7 @@ int check_rewrite_rq_uri (osip_message_t *sip) { /* - * PROXY_GEN_RESPONSE + * SIP_GEN_RESPONSE * * send an proxy generated response back to the client. * Only errors are reported from the proxy itself. @@ -562,7 +565,7 @@ int sip_gen_response(osip_message_t *request, int code) { /* - * PROXY_ADD_MYVIA + * SIP_ADD_MYVIA * * interface == IF_OUTBOUND, IF_INBOUND * @@ -575,6 +578,8 @@ int sip_add_myvia (osip_message_t *request, int interface) { char tmp[URL_STRING_SIZE]; osip_via_t *via; int sts; + char branch_id[64]; + struct timeval tv; if (interface == IF_OUTBOUND) { sts = get_ip_by_ifname(configuration.outbound_if, &addr); @@ -592,8 +597,14 @@ int sip_add_myvia (osip_message_t *request, int interface) { } } - sprintf(tmp, "SIP/2.0/UDP %s:%i", utils_inet_ntoa(addr), - configuration.sip_listen_port); + /* prepare branch ID (the magic cookie z9hG4bK is added) */ + gettimeofday (&tv, NULL); + sprintf(branch_id, "z9hG4bK%8.8lx%8.8lx%8.8x", + (long)tv.tv_sec, (long)tv.tv_usec, rand() ); + + + sprintf(tmp, "SIP/2.0/UDP %s:%i;branch=%s;", utils_inet_ntoa(addr), + configuration.sip_listen_port, branch_id); DEBUGC(DBCLASS_BABBLE,"adding VIA:%s",tmp); sts = osip_via_init(&via); @@ -609,7 +620,7 @@ int sip_add_myvia (osip_message_t *request, int interface) { /* - * PROXY_DEL_MYVIA + * SIP_DEL_MYVIA * * RETURNS * STS_SUCCESS on success @@ -633,3 +644,63 @@ int sip_del_myvia (osip_message_t *response) { } +/* + * SIP_REWRITE_CONTACT + * + * rewrite the Contact header + * + * RETURNS + * STS_SUCCESS on success + * STS_FAILURE on error + */ +int sip_rewrite_contact (osip_message_t *sip_msg, int direction) { + osip_contact_t *contact; + int i; + + if (sip_msg == NULL) return STS_FAILURE; + + osip_message_get_contact(sip_msg, 0, &contact); + if (contact == NULL) return STS_FAILURE; + + for (i=0;iurl, 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 %s@%s", + (contact->url->username)? contact->url->username : "*NULL*", + (contact->url->host)? contact->url->host : "*NULL*", + urlmap[i].masq_url->username, urlmap[i].masq_url->host); + + /* 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); + } else { + /* incoming, use true url */ + osip_uri_clone(urlmap[i].true_url, &contact->url); + } + + osip_list_add(sip_msg->contacts,contact,-1); + } else { + return STS_FAILURE; + } + + return STS_SUCCESS; +} + diff --git a/src/siproxd.h b/src/siproxd.h index 1982f19..4280b28 100644 --- a/src/siproxd.h +++ b/src/siproxd.h @@ -22,11 +22,6 @@ #include #endif -/* typedef for specifying the direction of an RTP stream [being proxied] */ -typedef enum { - incoming, - outgoing, -} rtp_direction; /* function returns STS_* status values vvv */ @@ -48,7 +43,7 @@ int register_response(osip_message_t *request, int flag); /*X*/ /* proxy.c */ int proxy_request (osip_message_t *request); /*X*/ int proxy_response (osip_message_t *response); /*X*/ -int proxy_rewrite_invitation_body(osip_message_t *m, rtp_direction d); /*X*/ +int proxy_rewrite_invitation_body(osip_message_t *m, int direction); /*X*/ int proxy_rewrite_request_uri(osip_message_t *mymsg, int idx); /*X*/ /* utils.c */ @@ -71,17 +66,18 @@ int sip_gen_response(osip_message_t *request, int code); /*X*/ #define IF_INBOUND 1 int sip_add_myvia (osip_message_t *request, int interface); /*X*/ int sip_del_myvia (osip_message_t *response); /*X*/ +int sip_rewrite_contact (osip_message_t *sip_msg, int direction); /*X*/ /* readconf.c */ int read_config(char *name, int search); /*X*/ /* rtpproxy.c */ int rtpproxy_init( void ); /*X*/ -int rtp_start_fwd (osip_call_id_t *callid, rtp_direction dir, /*X*/ +int rtp_start_fwd (osip_call_id_t *callid, int direction, /*X*/ int media_stream_no, struct in_addr outbound_ipaddr, int *outboundport, struct in_addr lcl_client_ipaddr, int lcl_clientport); -int rtp_stop_fwd (osip_call_id_t *callid, rtp_direction dir); /*X*/ +int rtp_stop_fwd (osip_call_id_t *callid, int direction); /*X*/ void rtpproxy_kill( void ); /*X*/ /* accessctl.c */ @@ -184,17 +180,20 @@ struct siproxd_config { #define GETHOSTBYNAME_BUFLEN 1024 #endif +/* symbols for access control */ #define ACCESSCTL_SIP 1 /* for access control - SIP allowed */ #define ACCESSCTL_REG 2 /* --"-- - registr. allowed */ -/* symbolic return status */ - +/* symbolic return stati */ #define STS_SUCCESS 0 /* SUCCESS */ #define STS_TRUE 0 /* TRUE */ #define STS_FAILURE 1 /* FAILURE */ #define STS_FALSE 1 /* FALSE */ #define STS_NEED_AUTH 1001 /* need authentication */ +/* symbolic direction of data */ +#define DIR_INCOMING 1 +#define DIR_OUTGOING 2 /* * optional hacks diff --git a/src/sock.c b/src/sock.c index cc13302..9f43d37 100644 --- a/src/sock.c +++ b/src/sock.c @@ -82,8 +82,16 @@ int sipsock_wait(void) { FD_SET (listen_socket, &fdset); sts=select (listen_socket+1, &fdset, NULL, NULL, &timeout); + /* WARN on failures */ if (sts<0) { - WARN("select() returned error [%s]",strerror(errno)); + /* WARN on failure, except if it is an "interrupted system call" + as it will result by SIGINT, SIGTERM */ + if (errno != 4) { + WARN("select() returned error [%i:%s]",errno, strerror(errno)); + } else { + DEBUGC(DBCLASS_NET,"select() returned error [%i:%s]", + errno, strerror(errno)); + } } return sts;