BSD compatibility

This commit is contained in:
Thomas Ries 2002-11-23 13:33:28 +00:00
parent f44185f0b3
commit 2d25da2fda
12 changed files with 225 additions and 86 deletions

View File

@ -23,6 +23,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <osip/smsg.h> #include <osip/smsg.h>

View File

@ -348,7 +348,7 @@ void CvtHex(
Hex[i*2+1] = (j + 'a' - 10); Hex[i*2+1] = (j + 'a' - 10);
}; };
Hex[HASHHEXLEN] = '\0'; Hex[HASHHEXLEN] = '\0';
}; }
/* calculate H(A1) as per spec */ /* calculate H(A1) as per spec */
void DigestCalcHA1( void DigestCalcHA1(
@ -382,7 +382,7 @@ void DigestCalcHA1(
MD5Final(HA1, &Md5Ctx); MD5Final(HA1, &Md5Ctx);
}; };
CvtHex(HA1, SessionKey); CvtHex(HA1, SessionKey);
}; }
/* calculate request-digest/response-digest as per HTTP Digest spec */ /* calculate request-digest/response-digest as per HTTP Digest spec */
void DigestCalcResponse( void DigestCalcResponse(
@ -402,7 +402,7 @@ void DigestCalcResponse(
HASH RespHash; HASH RespHash;
HASHHEX HA2Hex; HASHHEX HA2Hex;
// calculate H(A2) /* calculate H(A2) */
MD5Init(&Md5Ctx); MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, pszMethod, strlen(pszMethod)); MD5Update(&Md5Ctx, pszMethod, strlen(pszMethod));
MD5Update(&Md5Ctx, ":", 1); MD5Update(&Md5Ctx, ":", 1);
@ -412,11 +412,11 @@ void DigestCalcResponse(
goto auth_withqop; goto auth_withqop;
}; };
// auth_withoutqop: /* auth_withoutqop: */
MD5Final(HA2, &Md5Ctx); MD5Final(HA2, &Md5Ctx);
CvtHex(HA2, HA2Hex); CvtHex(HA2, HA2Hex);
// calculate response /* calculate response */
MD5Init(&Md5Ctx); MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, HA1, HASHHEXLEN); MD5Update(&Md5Ctx, HA1, HASHHEXLEN);
MD5Update(&Md5Ctx, ":", 1); MD5Update(&Md5Ctx, ":", 1);
@ -432,7 +432,7 @@ void DigestCalcResponse(
MD5Final(HA2, &Md5Ctx); MD5Final(HA2, &Md5Ctx);
CvtHex(HA2, HA2Hex); CvtHex(HA2, HA2Hex);
// calculate response /* calculate response */
MD5Init(&Md5Ctx); MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, HA1, HASHHEXLEN); MD5Update(&Md5Ctx, HA1, HASHHEXLEN);
MD5Update(&Md5Ctx, ":", 1); MD5Update(&Md5Ctx, ":", 1);
@ -449,6 +449,6 @@ void DigestCalcResponse(
MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN); MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
MD5Final(RespHash, &Md5Ctx); MD5Final(RespHash, &Md5Ctx);
CvtHex(RespHash, Response); CvtHex(RespHash, Response);
}; }

View File

@ -24,6 +24,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <time.h> #include <time.h>
#include <syslog.h> #include <syslog.h>
@ -40,6 +42,10 @@ void log_set_pattern(int pattern) {
debug_pattern=pattern; debug_pattern=pattern;
} }
int log_get_pattern(void) {
return debug_pattern;
}
void log_set_tosyslog(int tosyslog) { void log_set_tosyslog(int tosyslog) {
log_to_syslog=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, void log_dump_buffer(int class, char *file, int line,
char *buffer, int length) { char *buffer, int length) {
int i; int i, j;
char tmp[8], tmplin1[80], tmplin2[80];
if ((debug_pattern & class) == 0) return; if ((debug_pattern & class) == 0) return;
if (log_to_syslog) return; if (log_to_syslog) return;
fprintf(stderr,"---BUFFER DUMP follows---\n"); fprintf(stderr,"---BUFFER DUMP follows---\n");
/*
for (i=0;i<length;i++) { for (i=0;i<length;i++) {
fprintf(stderr,"%c",buffer[i]); fprintf(stderr,"%c",buffer[i]);
} }
*/
for (i=0; i<length; i+=16) {
strcpy(tmplin1,"");
strcpy(tmplin2,"");
for (j=0;(j<16) && (i+j)<length ;j++) {
sprintf(tmp,"%2.2x ",(unsigned char)buffer[i+j]);
strcat(tmplin1, tmp);
sprintf(tmp, "%c",(isprint(buffer[i+j]))? buffer[i+j]: '.');
strcat(tmplin2, tmp);
}
fprintf(stderr, " %-47.47s %-16.16s\n",tmplin1, tmplin2);
}
fprintf(stderr,"\n---end of BUFFER DUMP---\n"); fprintf(stderr,"\n---end of BUFFER DUMP---\n");
fflush(stderr); fflush(stderr);

View File

@ -20,23 +20,25 @@
#include <stdarg.h> #include <stdarg.h>
#define DBCLASS_BABBLE 0x00000001 // babble (like entering/leaving fnc) #define DBCLASS_BABBLE 0x00000001 /* babble (like entering/leaving fnc)*/
#define DBCLASS_NET 0x00000002 // network #define DBCLASS_NET 0x00000002 /* network */
#define DBCLASS_SIP 0x00000004 // SIP manipulations #define DBCLASS_SIP 0x00000004 /* SIP manipulations */
#define DBCLASS_REG 0x00000008 // Client registration #define DBCLASS_REG 0x00000008 /* Client registration */
#define DBCLASS_NOSPEC 0x00000010 // non specified class #define DBCLASS_NOSPEC 0x00000010 /* non specified class */
#define DBCLASS_PROXY 0x00000020 // proxy #define DBCLASS_PROXY 0x00000020 /* proxy */
#define DBCLASS_DNS 0x00000040 // DNS stuff #define DBCLASS_DNS 0x00000040 /* DNS stuff */
#define DBCLASS_NETTRAF 0x00000080 // network traffic #define DBCLASS_NETTRAF 0x00000080 /* network traffic */
#define DBCLASS_CONFIG 0x00000100 // configuration #define DBCLASS_CONFIG 0x00000100 /* configuration */
#define DBCLASS_RTP 0x00000200 // RTP proxy #define DBCLASS_RTP 0x00000200 /* RTP proxy */
#define DBCLASS_ACCESS 0x00000400 // Access list evaluation #define DBCLASS_ACCESS 0x00000400 /* Access list evaluation */
#define DBCLASS_AUTH 0x00000800 // Authentication #define DBCLASS_AUTH 0x00000800 /* Authentication */
void log_set_pattern(int pattern); void log_set_pattern(int pattern);
int log_get_pattern(void);
void log_set_tosyslog(int tosyslog); void log_set_tosyslog(int tosyslog);
#undef DEBUG
#define DEBUG(F...) log_debug(1,__FILE__, __LINE__,F) #define DEBUG(F...) log_debug(1,__FILE__, __LINE__,F)
#define DEBUGC(C,F...) log_debug(C,__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, ...); void log_warn(char *file, int line, const char *format, ...);
/* tobedone: dump a buffer */ /* 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, void log_dump_buffer(int class, char *file, int line,
char *buffer, int length); char *buffer, int length);

View File

@ -24,6 +24,7 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
@ -144,7 +145,7 @@ int proxy_request (sip_t *request) {
/* if this is CANCEL/BYE request, stop RTP proxying */ /* if this is CANCEL/BYE request, stop RTP proxying */
if (MSG_IS_BYE(request) || MSG_IS_CANCEL(request)) { if (MSG_IS_BYE(request) || MSG_IS_CANCEL(request)) {
/* stop the RTP proxying stream */ /* stop the RTP proxying stream */
rtp_stop_fwd(msg_getcall_id(request)); rtp_stop_fwd(msg_getcall_id(request), 0);
} }
break; break;
@ -190,7 +191,7 @@ int proxy_request (sip_t *request) {
/* if this is CANCEL/BYE request, stop RTP proxying */ /* if this is CANCEL/BYE request, stop RTP proxying */
if (MSG_IS_BYE(request) || MSG_IS_CANCEL(request)) { 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; break;
@ -203,7 +204,9 @@ int proxy_request (sip_t *request) {
/* some clients seem to run amok when passing back a negative response /* some clients seem to run amok when passing back a negative response
* so we simply drop the request silently * 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; return STS_FAILURE;
} }
@ -383,7 +386,9 @@ int proxy_response (sip_t *response) {
response->from->url->username, response->from->url->username,
response->from->url->host); response->from->url->host);
/* some clients seem to run amok when passing back a negative response */ /* 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; return STS_FAILURE;
} }

View File

@ -24,6 +24,7 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <osip/smsg.h> #include <osip/smsg.h>
@ -188,9 +189,16 @@ static int parse_config (FILE *configfile) {
break; break;
case TYP_STRING: case TYP_STRING:
// num=sscanf(ptr,"%a[^#]",(char**)configoptions[j].dest); /* the %as within sscanf seems to be not too portable.
num=sscanf(ptr,"%as",(char**)configoptions[j].dest); * it is supposed to allocate the memory
DEBUGC(DBCLASS_BABBLE,"STRING=%s",*(char**)configoptions[j].dest); * 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; break;
default: default:

View File

@ -24,6 +24,7 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>

View File

@ -39,8 +39,8 @@
/* configuration storage */ /* configuration storage */
extern struct siproxd_config configuration; extern struct siproxd_config configuration;
/* use a 'recursive mutex' for synchronizing */ /* use a 'fast' mutex for synchronizing - as these are portable... */
pthread_mutex_t rtp_proxytable_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; pthread_mutex_t rtp_proxytable_mutex = PTHREAD_MUTEX_INITIALIZER;
/* /*
* table to remember all active rtp proxy streams * 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; callid.host=rtp_proxytable[i].callid_host;
DEBUGC(DBCLASS_RTP,"RTP stream %s@%s (idx=%i) has expired", DEBUGC(DBCLASS_RTP,"RTP stream %s@%s (idx=%i) has expired",
callid.number, callid.host, i); 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>...) */ } /* if (t>...) */
@ -374,7 +375,7 @@ unlock_and_exit:
* STS_SUCCESS on success * STS_SUCCESS on success
* STS_FAILURE on error * 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 i;
int sts=STS_SUCCESS; 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", DEBUGC(DBCLASS_RTP,"stopping RTP proxy stream for: %s@%s",
callid->number, callid->host); 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 * lock mutex - only if not requested to skip the lock.
* !! here up to the end of this funtion where the MUTEX is * this is needed as we are also called from within
* !! unlocked again. * the RTP thread itself - and there we already own the lock.
* !! 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.
*/ */
#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 */ /* find the proper entry in rtp_proxytable */
for (i=0; i<RTPPROXY_SIZE; i++) { for (i=0; i<RTPPROXY_SIZE; i++) {
@ -432,8 +439,14 @@ int rtp_stop_fwd (call_id_t *callid) {
pthread_kill(rtpproxy_tid, SIGALRM); pthread_kill(rtpproxy_tid, SIGALRM);
unlock_and_exit: unlock_and_exit:
/* unlock mutex */ /*
pthread_mutex_unlock(&rtp_proxytable_mutex); * unlock 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.
*/
if (nolock == 0) {
pthread_mutex_unlock(&rtp_proxytable_mutex);
}
#undef return #undef return
return sts; return sts;

View File

@ -93,7 +93,7 @@ int main (int argc, char *argv[])
{0,0,0,0} {0,0,0,0}
}; };
while ((ch1 = getopt_long_only(argc, argv, "hc:d:n", while ((ch1 = getopt_long(argc, argv, "hc:d:n",
long_options, &option_index)) != -1) { long_options, &option_index)) != -1) {
switch (ch1) { switch (ch1) {
case 'h': /* help */ case 'h': /* help */
@ -146,6 +146,10 @@ int main (int argc, char *argv[])
/* initialize the registration facility */ /* initialize the registration facility */
register_init(); register_init();
/*
*TEST
*/
get_ip_by_ifname("ppp0");
/* listen for incomming messages */ /* listen for incomming messages */
sts=sipsock_listen(); sts=sipsock_listen();
if (sts == STS_FAILURE) { if (sts == STS_FAILURE) {

View File

@ -21,54 +21,54 @@
/* function returns STS_* status values vvv */ /* function returns STS_* status values vvv */
/* sock.c */ /* sock.c */
int sipsock_listen (void); // * int sipsock_listen (void); /*X*/
int sipsock_wait(void); int sipsock_wait(void);
int sipsock_read(void *buf, size_t bufsize, struct sockaddr_in *from); int sipsock_read(void *buf, size_t bufsize, struct sockaddr_in *from);
int sipsock_send_udp(int *sock, struct in_addr addr, int port, // * int sipsock_send_udp(int *sock, struct in_addr addr, int port, /*X*/
char *buffer, int size, int allowdump); char *buffer, int size, int allowdump);
int sockbind(struct in_addr ipaddr, int localport); int sockbind(struct in_addr ipaddr, int localport);
/* register.c */ /* register.c */
void register_init(void); void register_init(void);
int register_client(sip_t *request); // * int register_client(sip_t *request); /*X*/
void register_agemap(void); void register_agemap(void);
int register_response(sip_t *request, int flag); // * int register_response(sip_t *request, int flag); /*X*/
/* proxy.c */ /* proxy.c */
int proxy_request (sip_t *request); // * int proxy_request (sip_t *request); /*X*/
int proxy_response (sip_t *response); // * int proxy_response (sip_t *response); /*X*/
int proxy_gen_response(sip_t *request, int code); // * int proxy_gen_response(sip_t *request, int code); /*X*/
int proxy_add_myvia (sip_t *request, int interface); // * int proxy_add_myvia (sip_t *request, int interface); /*X*/
int proxy_del_myvia (sip_t *response); // * int proxy_del_myvia (sip_t *response); /*X*/
int proxy_rewrite_invitation_body(sip_t *mymsg); // * int proxy_rewrite_invitation_body(sip_t *mymsg); /*X*/
/* utils.c */ /* utils.c */
sip_t * msg_make_template_reply (sip_t * request, int code); sip_t * msg_make_template_reply (sip_t * request, int code);
int check_vialoop (sip_t *my_msg); // * int check_vialoop (sip_t *my_msg); /*X*/
int is_via_local (via_t *via); // * int is_via_local (via_t *via); /*X*/
int get_ip_by_host(char *hostname, struct in_addr *addr); // * int get_ip_by_host(char *hostname, struct in_addr *addr); /*X*/
int compare_url(url_t *url1, url_t *url2); // * int compare_url(url_t *url1, url_t *url2); /*X*/
void secure_enviroment (void); void secure_enviroment (void);
/* readconf.c */ /* readconf.c */
int read_config(char *name, int search); // * int read_config(char *name, int search); /*X*/
/* rtpproxy.c */ /* rtpproxy.c */
int rtpproxy_init( void ); // * int rtpproxy_init( void ); /*X*/
int rtp_start_fwd (call_id_t *callid, // * int rtp_start_fwd (call_id_t *callid, /*X*/
struct in_addr outbound_ipaddr, int *outboundport, struct in_addr outbound_ipaddr, int *outboundport,
struct in_addr lcl_client_ipaddr, int lcl_clientport); struct in_addr lcl_client_ipaddr, int lcl_clientport);
int rtp_stop_fwd (call_id_t *callid); // * int rtp_stop_fwd (call_id_t *callid, int nolock); /*X*/
/* accessctl.c */ /* accessctl.c */
int accesslist_check(struct sockaddr_in from); int accesslist_check(struct sockaddr_in from);
/* security.c */ /* security.c */
int security_check(char *sip_buffer, int size); // * int security_check(char *sip_buffer, int size); /*X*/
/* auth.c */ /* auth.c */
int authenticate_proxy(sip_t *request); // * int authenticate_proxy(sip_t *request); /*X*/
int auth_include_authrq(sip_t *response); // * int auth_include_authrq(sip_t *response); /*X*/
@ -113,30 +113,30 @@ struct siproxd_config {
*/ */
#define SIP_PORT 5060 #define SIP_PORT 5060
#define URLMAP_SIZE 8 // number of URL mapping table entries #define URLMAP_SIZE 8 /* number of URL mapping table entries */
#define RTPPROXY_SIZE 8 // number of rtp proxy entries #define RTPPROXY_SIZE 8 /* number of rtp proxy entries */
#define BUFFER_SIZE 1024 // input buffer for read from socket #define BUFFER_SIZE 1024 /* input buffer for read from socket */
#define RTP_BUFFER_SIZE 512 // max size of an RTP frame #define RTP_BUFFER_SIZE 512 /* max size of an RTP frame */
#define URL_STRING_SIZE 128 // max size of an URL/URI string #define URL_STRING_SIZE 128 /* max size of an URL/URI string */
#define STATUSCODE_SIZE 5 // size of string representation of status #define STATUSCODE_SIZE 5 /* size of string representation of status */
#define DNS_CACHE_SIZE 32 // number of entries in internal DNS cache #define DNS_CACHE_SIZE 32 /* number of entries in internal DNS cache */
#define DNS_MAX_AGE 60 // maximum age of an cache entry (sec) #define DNS_MAX_AGE 60 /* maximum age of an cache entry (sec) */
#define HOSTNAME_SIZE 32 // max string length of a hostname #define HOSTNAME_SIZE 32 /* max string length of a hostname */
#define USERNAME_SIZE 32 // max string length of a username (auth) #define USERNAME_SIZE 32 /* max string length of a username (auth) */
#define PASSWORD_SIZE 32 // max string length of a password (auth) #define PASSWORD_SIZE 32 /* max string length of a password (auth) */
#define ACCESSCTL_SIP 1 // for access control - SIP allowed #define ACCESSCTL_SIP 1 /* for access control - SIP allowed */
#define ACCESSCTL_REG 2 // --"-- - registrations allowed #define ACCESSCTL_REG 2 /* --"-- - registr. allowed */
/* symbolic return status */ /* symbolic return status */
#define STS_SUCCESS 0 // SUCCESS #define STS_SUCCESS 0 /* SUCCESS */
#define STS_TRUE 0 // TRUE #define STS_TRUE 0 /* TRUE */
#define STS_FAILURE 1 // FAILURE #define STS_FAILURE 1 /* FAILURE */
#define STS_FALSE 1 // FALSE #define STS_FALSE 1 /* FALSE */
#define STS_NEED_AUTH 1001 // need authentication #define STS_NEED_AUTH 1001 /* need authentication */
/* /*
* optional hacks * optional hacks

View File

@ -95,8 +95,9 @@ int sipsock_read(void *buf, size_t bufsize, struct sockaddr_in *from) {
int count; int count;
socklen_t fromlen; socklen_t fromlen;
fromlen=sizeof(struct sockaddr); fromlen=sizeof(struct sockaddr_in);
count=recvfrom(listen_socket, buf, bufsize, 0, from, &fromlen); count=recvfrom(listen_socket, buf, bufsize, 0,
(struct sockaddr *)from, &fromlen);
DEBUGC(DBCLASS_NET,"received UDP packet, count=%i", count); DEBUGC(DBCLASS_NET,"received UDP packet, count=%i", count);
DUMP_BUFFER(DBCLASS_NETTRAF, buf, count); DUMP_BUFFER(DBCLASS_NETTRAF, buf, count);
@ -138,7 +139,8 @@ int sipsock_send_udp(int *sock, struct in_addr addr, int port,
DUMP_BUFFER(DBCLASS_NETTRAF, buffer, size); DUMP_BUFFER(DBCLASS_NETTRAF, buffer, size);
} }
sts = sendto(*sock, buffer, size, 0, &dst_addr, sizeof(dst_addr)); sts = sendto(*sock, buffer, size, 0, (const struct sockaddr *)&dst_addr,
(socklen_t)sizeof(dst_addr));
if (sts == -1) { if (sts == -1) {
if (errno != ECONNREFUSED) { if (errno != ECONNREFUSED) {

View File

@ -25,9 +25,12 @@
#include <time.h> #include <time.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/types.h> #include <sys/types.h>
#include <pwd.h> #include <pwd.h>
@ -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];
i<netconf.ifc_len;
ifr=(struct ifreq*)&buffer[i]) {
#if defined(_BSD)
len=((struct sockaddr *)&ifr->ifr_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];
i<netconf.ifc_len;
ifr=(struct ifreq*)&buffer[i]) {
#if defined(_BSD)
len=((struct sockaddr *)&ifr->ifr_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;
}
}