From d9aec2834bc8dfd7d6ba268cec837b9026add432 Mon Sep 17 00:00:00 2001 From: Thomas Ries Date: Sun, 25 Apr 2004 22:50:00 +0000 Subject: [PATCH] - simplified SIP RX & TX routines --- ChangeLog | 1 + src/proxy.c | 6 +++--- src/register.c | 4 +--- src/rtpproxy_relay.c | 42 +++++++++++++++++++++++-------------- src/sip_utils.c | 4 +--- src/siproxd.c | 11 ++++------ src/siproxd.h | 10 ++++----- src/sock.c | 50 ++++++++++++++++++++------------------------ 8 files changed, 64 insertions(+), 64 deletions(-) diff --git a/ChangeLog b/ChangeLog index c63195f..a0400e4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ 0.5.6 ===== 11-Apr-2004: - on termination, stop all active RTP streams + 24-Apr-2004: - simplified SIP RX & TX routines 0.5.5 ===== diff --git a/src/proxy.c b/src/proxy.c index e0491a9..3c90769 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -43,7 +43,6 @@ extern struct siproxd_config configuration; extern int errno; extern struct urlmap_s urlmap[]; /* URL mapping table */ extern struct lcl_if_s local_addresses; -extern int sip_socket; /* sending SIP datagrams */ /* @@ -547,7 +546,7 @@ int proxy_request (osip_message_t *request, struct sockaddr_in *from) { return STS_FAILURE; } - sipsock_send_udp(&sip_socket, sendto_addr, port, buffer, strlen(buffer), 1); + sipsock_send(sendto_addr, port, buffer, strlen(buffer)); osip_free (buffer); /* @@ -730,6 +729,7 @@ int proxy_response (osip_message_t *response, struct sockaddr_in *from) { (osip_strncasecmp(ua_hdr->hvalue,"grandstream", 11)==0) && (MSG_IS_RESPONSE_FOR(response,"SUBSCRIBE")) && (MSG_TEST_CODE(response, 202))) { + DEBUGC(DBCLASS_PROXY, "proxy_request: Grandstream hack 202->404"); response->status_code=404; } } @@ -811,7 +811,7 @@ int proxy_response (osip_message_t *response, struct sockaddr_in *from) { return STS_FAILURE; } - sipsock_send_udp(&sip_socket, sendto_addr, port, buffer, strlen(buffer), 1); + sipsock_send(sendto_addr, port, buffer, strlen(buffer)); osip_free (buffer); return STS_SUCCESS; } diff --git a/src/register.c b/src/register.c index ebd0271..239d5c1 100644 --- a/src/register.c +++ b/src/register.c @@ -42,8 +42,6 @@ extern struct siproxd_config configuration; struct urlmap_s urlmap[URLMAP_SIZE]; /* URL mapping table */ -extern int sip_socket; /* sending SIP datagrams */ - extern int errno; /* * initialize the URL mapping table @@ -536,7 +534,7 @@ int register_response(osip_message_t *request, int flag) { port=configuration.sip_listen_port; } - sipsock_send_udp(&sip_socket, addr, port, buffer, strlen(buffer), 1); + sipsock_send(addr, port, buffer, strlen(buffer)); /* free the resources */ osip_message_free(response); diff --git a/src/rtpproxy_relay.c b/src/rtpproxy_relay.c index 66ebbcb..61644aa 100644 --- a/src/rtpproxy_relay.c +++ b/src/rtpproxy_relay.c @@ -299,24 +299,34 @@ static void *rtpproxy_main(void *arg) { } /* rtp_tx_sock == 0 */ if (rtp_proxytable[i].rtp_tx_sock != 0) { - /* write to dest via socket rtp_tx_sock */ - sts = sipsock_send_udp(&rtp_proxytable[i].rtp_tx_sock, - rtp_proxytable[i].remote_ipaddr, - rtp_proxytable[i].remote_port, - rtp_buff, count, 0); /* don't dump it */ + /* write to dest via socket rtp_tx_sock */ + struct sockaddr_in dst_addr; + dst_addr.sin_family = AF_INET; + memcpy(&dst_addr.sin_addr.s_addr, + &rtp_proxytable[i].remote_ipaddr, + sizeof(struct in_addr)); + dst_addr.sin_port= htons(rtp_proxytable[i].remote_port); - if (sts != STS_SUCCESS) { - /* if sendto() fails with bad filedescriptor, - * this means that the opposite stream has been - * canceled or timed out. - * we should then cancel this stream as well.*/ + sts = sendto(rtp_proxytable[i].rtp_tx_sock, rtp_buff, + count, 0, (const struct sockaddr *)&dst_addr, + (socklen_t)sizeof(dst_addr)); + if (sts == -1) { + if (errno != ECONNREFUSED) { + ERROR("sendto() [%s:%i size=%i] call failed: %s", + utils_inet_ntoa(rtp_proxytable[i].remote_ipaddr), + rtp_proxytable[i].remote_port, count, strerror(errno)); - WARN("stopping opposite stream"); - /* don't lock the mutex, as we own the lock */ - callid.number=rtp_proxytable[i].callid_number; - callid.host=rtp_proxytable[i].callid_host; - rtp_relay_stop_fwd(&callid, - rtp_proxytable[i].direction, 1); + /* if sendto() fails with bad filedescriptor, + * this means that the opposite stream has been + * canceled or timed out. + * we should then cancel this stream as well.*/ + + WARN("stopping opposite stream"); + /* don't lock the mutex, as we own the lock */ + callid.number=rtp_proxytable[i].callid_number; + callid.host=rtp_proxytable[i].callid_host; + rtp_relay_stop_fwd(&callid, rtp_proxytable[i].direction, 1); + } } } } /* count > 0 */ diff --git a/src/sip_utils.c b/src/sip_utils.c index edc3449..0665e92 100644 --- a/src/sip_utils.c +++ b/src/sip_utils.c @@ -50,7 +50,6 @@ static char const ident[]="$Id$"; extern struct siproxd_config configuration; extern int h_errno; -extern int sip_socket; /* sending SIP datagrams */ extern struct urlmap_s urlmap[]; /* URL mapping table */ @@ -571,8 +570,7 @@ int sip_gen_response(osip_message_t *request, int code) { } /* send to destination */ - sipsock_send_udp(&sip_socket, addr, port, - buffer, strlen(buffer), 1); + sipsock_send(addr, port, buffer, strlen(buffer)); /* free the resources */ osip_message_free(response); diff --git a/src/siproxd.c b/src/siproxd.c index e480f84..f86f40a 100644 --- a/src/siproxd.c +++ b/src/siproxd.c @@ -43,9 +43,6 @@ static char const ident[]="$Id$"; /* configuration storage */ struct siproxd_config configuration; -/* socket used for sending SIP datagrams */ -int sip_socket=0; - /* -h help option text */ static const char str_helpmsg[] = PACKAGE "-" VERSION "-" BUILDSTR " (c) 2002-2004 Thomas Ries\n" @@ -216,7 +213,7 @@ int main (int argc, char *argv[]) parser_init(); /* listen for incoming messages */ - sts=sipsock_listen(&sip_socket); + sts=sipsock_listen(); if (sts == STS_FAILURE) { /* failure to allocate SIP socket... */ ERROR("unable to bind to SIP listening socket - aborting"); @@ -238,7 +235,7 @@ int main (int argc, char *argv[]) while (!exit_program) { DEBUGC(DBCLASS_BABBLE,"going into sip_wait\n"); - while (sipsock_wait(sip_socket)<=0) { + while (sipsock_wait()<=0) { /* got no input, here by timeout. do aging */ register_agemap(); @@ -260,7 +257,7 @@ int main (int argc, char *argv[]) /* got input, process */ DEBUGC(DBCLASS_BABBLE,"back from sip_wait"); - i=sipsock_read(sip_socket, &buff, sizeof(buff)-1, &from); + i=sipsock_read(&buff, sizeof(buff)-1, &from); buff[i]='\0'; /* evaluate the access lists (IP based filter)*/ @@ -456,7 +453,7 @@ int main (int argc, char *argv[]) } /* while TRUE */ exit_prg: - /* dump current knwon SIP registrations */ + /* dump current known SIP registrations */ register_shut(); INFO("properly terminating siproxd"); diff --git a/src/siproxd.h b/src/siproxd.h index b5a4e3b..3a0d398 100644 --- a/src/siproxd.h +++ b/src/siproxd.h @@ -28,12 +28,12 @@ /* function returns STS_* status values vvv */ /* sock.c */ -int sipsock_listen (int *sock); /*X*/ -int sipsock_wait(int sock); -int sipsock_read(int sock, void *buf, size_t bufsize, +int sipsock_listen (void); /*X*/ +int sipsock_wait(); +int sipsock_read(void *buf, size_t bufsize, struct sockaddr_in *from); -int sipsock_send_udp(int *sock, struct in_addr addr, int port, /*X*/ - char *buffer, int size, int allowdump); +int sipsock_send(struct in_addr addr, int port, /*X*/ + char *buffer, int size); int sockbind(struct in_addr ipaddr, int localport, int errflg); /* register.c */ diff --git a/src/sock.c b/src/sock.c index f207c42..32ca255 100644 --- a/src/sock.c +++ b/src/sock.c @@ -44,6 +44,10 @@ static char const ident[]="$Id$"; /* configuration storage */ extern struct siproxd_config configuration; +/* socket used for sending SIP datagrams */ +int sip_udp_socket=0; + + /* * binds to SIP UDP socket for listening to incoming packets * @@ -51,17 +55,15 @@ extern struct siproxd_config configuration; * STS_SUCCESS on success * STS_FAILURE on error */ -int sipsock_listen (int *sock) { +int sipsock_listen (void) { struct in_addr ipaddr; - if (sock == NULL) return STS_FAILURE; - memset(&ipaddr, 0, sizeof(ipaddr)); - *sock=sockbind(ipaddr, configuration.sip_listen_port, 1); - if (*sock == 0) return STS_FAILURE; /* failure*/ + sip_udp_socket=sockbind(ipaddr, configuration.sip_listen_port, 1); + if (sip_udp_socket == 0) return STS_FAILURE; /* failure*/ INFO("bound to port %i", configuration.sip_listen_port); - DEBUGC(DBCLASS_NET,"bound socket %i",*sock); + DEBUGC(DBCLASS_NET,"bound socket %i",sip_udp_socket); return STS_SUCCESS; } @@ -71,7 +73,7 @@ int sipsock_listen (int *sock) { * * RETURNS >0 if data received, =0 if nothing received /T/O), -1 on error */ -int sipsock_wait(int sock) { +int sipsock_wait(void) { int sts; fd_set fdset; struct timeval timeout; @@ -80,8 +82,8 @@ int sipsock_wait(int sock) { timeout.tv_usec=0; FD_ZERO(&fdset); - FD_SET (sock, &fdset); - sts=select (sock+1, &fdset, NULL, NULL, &timeout); + FD_SET (sip_udp_socket, &fdset); + sts=select (sip_udp_socket+1, &fdset, NULL, NULL, &timeout); /* WARN on failures */ if (sts<0) { @@ -104,13 +106,13 @@ int sipsock_wait(int sock) { * RETURNS number of bytes read * from is modified to return the sockaddr_in of the sender */ -int sipsock_read(int sock, void *buf, size_t bufsize, +int sipsock_read(void *buf, size_t bufsize, struct sockaddr_in *from) { int count; socklen_t fromlen; fromlen=sizeof(struct sockaddr_in); - count=recvfrom(sock, buf, bufsize, 0, + count=recvfrom(sip_udp_socket, buf, bufsize, 0, (struct sockaddr *)from, &fromlen); if (count<0) { @@ -132,23 +134,19 @@ int sipsock_read(int sock, void *buf, size_t bufsize, * STS_SUCCESS on success * STS_FAILURE on error */ -int sipsock_send_udp(int *sock, struct in_addr addr, int port, - char *buffer, int size, int allowdump) { +int sipsock_send(struct in_addr addr, int port, + char *buffer, int size) { struct sockaddr_in dst_addr; int sts; /* first time: allocate a socket for sending */ - if (*sock == 0) { - *sock=socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (*sock < 0) { - ERROR("socket() call failed:%s",strerror(errno)); - return STS_FAILURE; - } - DEBUGC(DBCLASS_NET,"allocated send socket %i",*sock); + if (sip_udp_socket == 0) { + ERROR("SIP socket not allocated"); + return STS_FAILURE; } if (buffer == NULL) { - ERROR("sipsock_send_udp got NULL buffer"); + ERROR("sipsock_send got NULL buffer"); return STS_FAILURE; } @@ -156,13 +154,11 @@ int sipsock_send_udp(int *sock, struct in_addr addr, int port, memcpy(&dst_addr.sin_addr.s_addr, &addr, sizeof(struct in_addr)); dst_addr.sin_port= htons(port); - if (allowdump) { - DEBUGC(DBCLASS_NET,"send UDP packet to %s: %i", - utils_inet_ntoa(addr),port); - DUMP_BUFFER(DBCLASS_NETTRAF, buffer, size); - } + DEBUGC(DBCLASS_NET,"send UDP packet to %s: %i", utils_inet_ntoa(addr),port); + DUMP_BUFFER(DBCLASS_NETTRAF, buffer, size); - sts = sendto(*sock, buffer, size, 0, (const struct sockaddr *)&dst_addr, + sts = sendto(sip_udp_socket, buffer, size, 0, + (const struct sockaddr *)&dst_addr, (socklen_t)sizeof(dst_addr)); if (sts == -1) {