From 2d25da2fda8c3b47e3c9321cd828cf27dc908685 Mon Sep 17 00:00:00 2001 From: Thomas Ries Date: Sat, 23 Nov 2002 13:33:28 +0000 Subject: [PATCH] BSD compatibility --- src/accessctl.c | 1 + src/auth.c | 14 ++++----- src/log.c | 22 ++++++++++++- src/log.h | 28 +++++++++-------- src/proxy.c | 13 +++++--- src/readconf.c | 14 +++++++-- src/register.c | 1 + src/rtpproxy.c | 43 ++++++++++++++++--------- src/siproxd.c | 6 +++- src/siproxd.h | 78 +++++++++++++++++++++++----------------------- src/sock.c | 8 +++-- src/utils.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 225 insertions(+), 86 deletions(-) diff --git a/src/accessctl.c b/src/accessctl.c index 584f6aa..15aae9f 100644 --- a/src/accessctl.c +++ b/src/accessctl.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include diff --git a/src/auth.c b/src/auth.c index ec5703d..d8c4315 100644 --- a/src/auth.c +++ b/src/auth.c @@ -348,7 +348,7 @@ void CvtHex( Hex[i*2+1] = (j + 'a' - 10); }; Hex[HASHHEXLEN] = '\0'; -}; +} /* calculate H(A1) as per spec */ void DigestCalcHA1( @@ -382,7 +382,7 @@ void DigestCalcHA1( MD5Final(HA1, &Md5Ctx); }; CvtHex(HA1, SessionKey); -}; +} /* calculate request-digest/response-digest as per HTTP Digest spec */ void DigestCalcResponse( @@ -402,7 +402,7 @@ void DigestCalcResponse( HASH RespHash; HASHHEX HA2Hex; - // calculate H(A2) + /* calculate H(A2) */ MD5Init(&Md5Ctx); MD5Update(&Md5Ctx, pszMethod, strlen(pszMethod)); MD5Update(&Md5Ctx, ":", 1); @@ -412,11 +412,11 @@ void DigestCalcResponse( goto auth_withqop; }; -// auth_withoutqop: + /* auth_withoutqop: */ MD5Final(HA2, &Md5Ctx); CvtHex(HA2, HA2Hex); - // calculate response + /* calculate response */ MD5Init(&Md5Ctx); MD5Update(&Md5Ctx, HA1, HASHHEXLEN); MD5Update(&Md5Ctx, ":", 1); @@ -432,7 +432,7 @@ void DigestCalcResponse( MD5Final(HA2, &Md5Ctx); CvtHex(HA2, HA2Hex); - // calculate response + /* calculate response */ MD5Init(&Md5Ctx); MD5Update(&Md5Ctx, HA1, HASHHEXLEN); MD5Update(&Md5Ctx, ":", 1); @@ -449,6 +449,6 @@ void DigestCalcResponse( MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN); MD5Final(RespHash, &Md5Ctx); CvtHex(RespHash, Response); -}; +} diff --git a/src/log.c b/src/log.c index 734e7f8..d533859 100644 --- a/src/log.c +++ b/src/log.c @@ -24,6 +24,8 @@ #include #include +#include +#include #include #include @@ -40,6 +42,10 @@ void log_set_pattern(int pattern) { debug_pattern=pattern; } +int log_get_pattern(void) { + return debug_pattern; +} + void log_set_tosyslog(int tosyslog) { log_to_syslog=tosyslog; } @@ -142,15 +148,29 @@ void log_warn(char *file, int line, const char *format, ...) { void log_dump_buffer(int class, char *file, int line, char *buffer, int length) { - int i; + int i, j; + char tmp[8], tmplin1[80], tmplin2[80]; if ((debug_pattern & class) == 0) return; if (log_to_syslog) return; fprintf(stderr,"---BUFFER DUMP follows---\n"); +/* for (i=0;i -#define DBCLASS_BABBLE 0x00000001 // babble (like entering/leaving fnc) -#define DBCLASS_NET 0x00000002 // network -#define DBCLASS_SIP 0x00000004 // SIP manipulations -#define DBCLASS_REG 0x00000008 // Client registration -#define DBCLASS_NOSPEC 0x00000010 // non specified class -#define DBCLASS_PROXY 0x00000020 // proxy -#define DBCLASS_DNS 0x00000040 // DNS stuff -#define DBCLASS_NETTRAF 0x00000080 // network traffic -#define DBCLASS_CONFIG 0x00000100 // configuration -#define DBCLASS_RTP 0x00000200 // RTP proxy -#define DBCLASS_ACCESS 0x00000400 // Access list evaluation -#define DBCLASS_AUTH 0x00000800 // Authentication +#define DBCLASS_BABBLE 0x00000001 /* babble (like entering/leaving fnc)*/ +#define DBCLASS_NET 0x00000002 /* network */ +#define DBCLASS_SIP 0x00000004 /* SIP manipulations */ +#define DBCLASS_REG 0x00000008 /* Client registration */ +#define DBCLASS_NOSPEC 0x00000010 /* non specified class */ +#define DBCLASS_PROXY 0x00000020 /* proxy */ +#define DBCLASS_DNS 0x00000040 /* DNS stuff */ +#define DBCLASS_NETTRAF 0x00000080 /* network traffic */ +#define DBCLASS_CONFIG 0x00000100 /* configuration */ +#define DBCLASS_RTP 0x00000200 /* RTP proxy */ +#define DBCLASS_ACCESS 0x00000400 /* Access list evaluation */ +#define DBCLASS_AUTH 0x00000800 /* Authentication */ void log_set_pattern(int pattern); +int log_get_pattern(void); void log_set_tosyslog(int tosyslog); +#undef DEBUG #define DEBUG(F...) log_debug(1,__FILE__, __LINE__,F) #define DEBUGC(C,F...) log_debug(C,__FILE__, __LINE__,F) @@ -49,6 +51,6 @@ void log_error(char *file, int line, const char *format, ...); void log_warn(char *file, int line, const char *format, ...); /* tobedone: dump a buffer */ -#define DUMP_BUFFER(C,F...) log_dump_buffer(C,__FILE__, __LINE__,F) +#define DUMP_BUFFER(C,F,L) log_dump_buffer(C,__FILE__, __LINE__,F,L) void log_dump_buffer(int class, char *file, int line, char *buffer, int length); diff --git a/src/proxy.c b/src/proxy.c index ef12102..9240d0f 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -144,7 +145,7 @@ int proxy_request (sip_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 */ - rtp_stop_fwd(msg_getcall_id(request)); + rtp_stop_fwd(msg_getcall_id(request), 0); } break; @@ -190,7 +191,7 @@ int proxy_request (sip_t *request) { /* if this is CANCEL/BYE request, stop RTP proxying */ if (MSG_IS_BYE(request) || MSG_IS_CANCEL(request)) { - rtp_stop_fwd(msg_getcall_id(request)); + rtp_stop_fwd(msg_getcall_id(request), 0); } break; @@ -203,7 +204,9 @@ int proxy_request (sip_t *request) { /* some clients seem to run amok when passing back a negative response * so we simply drop the request silently */ -// proxy_gen_response(request, 403 /*forbidden*/); +#if 0 + proxy_gen_response(request, 403 /*forbidden*/); +#endif return STS_FAILURE; } @@ -383,7 +386,9 @@ int proxy_response (sip_t *response) { response->from->url->username, response->from->url->host); /* some clients seem to run amok when passing back a negative response */ -// proxy_gen_response(response, 403 /*forbidden*/); +#if 0 + proxy_gen_response(response, 403 /*forbidden*/); +#endif return STS_FAILURE; } diff --git a/src/readconf.c b/src/readconf.c index 94136e2..1dd7365 100644 --- a/src/readconf.c +++ b/src/readconf.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -188,9 +189,16 @@ static int parse_config (FILE *configfile) { break; case TYP_STRING: -// num=sscanf(ptr,"%a[^#]",(char**)configoptions[j].dest); - num=sscanf(ptr,"%as",(char**)configoptions[j].dest); - DEBUGC(DBCLASS_BABBLE,"STRING=%s",*(char**)configoptions[j].dest); + /* the %as within sscanf seems to be not too portable. + * it is supposed to allocate the memory + * num=sscanf(ptr,"%as",(char**)configoptions[j].dest); + */ + + /* figure out the amount of space we need */ + num=strlen(ptr); + configoptions[j].dest=(char*)malloc(num); + num=sscanf(ptr,"%s",(char*)configoptions[j].dest); + DEBUGC(DBCLASS_BABBLE,"STRING=%s",(char*)configoptions[j].dest); break; default: diff --git a/src/register.c b/src/register.c index ba3ee00..ea02bda 100644 --- a/src/register.c +++ b/src/register.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include diff --git a/src/rtpproxy.c b/src/rtpproxy.c index efdd576..8aeb1cd 100644 --- a/src/rtpproxy.c +++ b/src/rtpproxy.c @@ -39,8 +39,8 @@ /* configuration storage */ extern struct siproxd_config configuration; -/* use a 'recursive mutex' for synchronizing */ -pthread_mutex_t rtp_proxytable_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; +/* use a 'fast' mutex for synchronizing - as these are portable... */ +pthread_mutex_t rtp_proxytable_mutex = PTHREAD_MUTEX_INITIALIZER; /* * table to remember all active rtp proxy streams @@ -178,7 +178,8 @@ DEBUGC(DBCLASS_RTP,"got data on sock=%i",rtp_proxytable[i].sock); callid.host=rtp_proxytable[i].callid_host; DEBUGC(DBCLASS_RTP,"RTP stream %s@%s (idx=%i) has expired", callid.number, callid.host, i); - rtp_stop_fwd(&callid); + rtp_stop_fwd(&callid, 1); /* don't lock the mutex, as we own + the lock already here */ } } } /* if (t>...) */ @@ -374,7 +375,7 @@ unlock_and_exit: * STS_SUCCESS on success * STS_FAILURE on error */ -int rtp_stop_fwd (call_id_t *callid) { +int rtp_stop_fwd (call_id_t *callid, int nolock) { int i; int sts=STS_SUCCESS; @@ -388,17 +389,23 @@ int rtp_stop_fwd (call_id_t *callid) { DEBUGC(DBCLASS_RTP,"stopping RTP proxy stream for: %s@%s", callid->number, callid->host); -/* lock mutex */ - #define return is_forbidden_in_this_code_section - pthread_mutex_lock(&rtp_proxytable_mutex); /* - * !! We now have a locked MUTEX! It is forbidden to return() from - * !! here up to the end of this funtion where the MUTEX is - * !! unlocked again. - * !! Per design, a mutex is locked (for one purpose) at *exactly one* - * !! place in the code and unlocked also at *exactly one* place. - * !! this minimizes the risk of deadlocks. + * lock mutex - only if not requested to skip the lock. + * this is needed as we are also called from within + * the RTP thread itself - and there we already own the lock. */ + #define return is_forbidden_in_this_code_section + if (nolock == 0) { + pthread_mutex_lock(&rtp_proxytable_mutex); + /* + * !! We now have a locked MUTEX! It is forbidden to return() from + * !! here up to the end of this funtion where the MUTEX is + * !! unlocked again. + * !! Per design, a mutex is locked (for one purpose) at *exactly one* + * !! place in the code and unlocked also at *exactly one* place. + * !! this minimizes the risk of deadlocks. + */ + } /* find the proper entry in rtp_proxytable */ for (i=0; i #include #include +#include #include #include #include +#include +#include #include #include @@ -361,3 +364,83 @@ void secure_enviroment (void) { } } } + + +/* + * get_ip_by_ifname: + * fetches own IP address by its interface name + */ +#define MAX_IF 32 +struct in_addr* get_ip_by_ifname(char *ifname) { + static struct in_addr addr; + int sock, err, i, found; + struct ifconf netconf; + char buffer[sizeof(struct ifreq)*MAX_IF]; + struct ifreq *ifr; + int len, af; + + if (ifname == NULL) { + ERROR("get_ip_by_ifname: got NULL ifname passed"); + return NULL; + } + + netconf.ifc_len=sizeof(struct ifreq)*MAX_IF; + netconf.ifc_buf=buffer; + + sock=socket(PF_INET, SOCK_DGRAM,0); + err=ioctl(sock,SIOCGIFCONF,&netconf); + if (err<0) ERROR("Error in ioctl: %s\n",strerror(errno)); + close(sock); + + + if (log_get_pattern() && DBCLASS_BABBLE) { + for (i=0, ifr=(struct ifreq*)&buffer[i]; + iifr_addr)->sa_len; +#else + len=SA_LEN((struct sockaddr *)&ifr->ifr_addr); +#endif + af=((struct sockaddr *)&ifr->ifr_addr)->sa_family; + i+=len+IFNAMSIZ; + if(af != AF_INET) continue; + + DEBUG("[i=%i] IF %s, l=%i af=%i -> IP:%s", i, + ifr->ifr_name, len, af, inet_ntoa(((struct sockaddr_in *) + (&ifr->ifr_addr))->sin_addr)); + } + } + + found=0; + memset(&addr, 0, sizeof(addr)); + for (i=0, ifr=(struct ifreq*)&buffer[i]; + iifr_addr)->sa_len; +#else + len=SA_LEN((struct sockaddr *)&ifr->ifr_addr); +#endif + af=((struct sockaddr *)&ifr->ifr_addr)->sa_family; + i+=len+IFNAMSIZ; + if(af != AF_INET) continue; + + if (strcmp(ifr->ifr_name, ifname)==0) { + memcpy(&addr, &((struct sockaddr_in *) + (&ifr->ifr_addr))->sin_addr, + sizeof(addr)); + found=1; + break; + } + } + + if (found==1) { + DEBUGC(DBCLASS_DNS, "get_ip_by_ifname: interface %s has IP: %s", + ifname, inet_ntoa(addr)); + return &addr; + } else { + ERROR("get_ip_by_ifname: interface %s does not exist", ifname); + return NULL; + } +}