From 8cc053a6110e6038f2eb581b7201b6c66e4d5dd1 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 3 Jun 2009 22:30:09 +0000 Subject: [PATCH] FSBUILD-168 now he has to come to cluecon git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@13600 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/switch_utils.c | 74 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 3 deletions(-) diff --git a/src/switch_utils.c b/src/switch_utils.c index 2ac36a7ad4..825a020146 100644 --- a/src/switch_utils.c +++ b/src/switch_utils.c @@ -740,9 +740,8 @@ SWITCH_DECLARE(const char *) switch_stristr(const char *instr, const char *str) return NULL; } -#ifndef WIN32 +#ifdef HAVE_GETIFADDRS #include - static int get_netmask(struct sockaddr_in *me, int *mask) { struct ifaddrs *ifaddrs, *i = NULL; @@ -761,9 +760,78 @@ static int get_netmask(struct sockaddr_in *me, int *mask) } } - + freeifaddrs(ifaddrs); + return -2; } +#elif defined(__linux__) + +#include +#include +static int get_netmask(struct sockaddr_in *me, int *mask) +{ + + static struct ifreq ifreqs[20] = { {{{0}}} }; + struct ifconf ifconf; + int nifaces, i; + int sock; + int r = -1; + + memset(&ifconf,0,sizeof(ifconf)); + ifconf.ifc_buf = (char*) (ifreqs); + ifconf.ifc_len = sizeof(ifreqs); + + + if ((sock = socket(AF_INET,SOCK_STREAM, 0)) < 0) { + goto end; + } + + if (ioctl(sock, SIOCGIFCONF, (char *) &ifconf) < 0) { + goto end; + } + + nifaces = ifconf.ifc_len / sizeof(struct ifreq); + + for(i = 0; i < nifaces; i++) { + struct sockaddr_in *sin = NULL; + struct in_addr ip; + + ioctl(sock, SIOCGIFADDR, &ifreqs[i]); + sin = (struct sockaddr_in *)&ifreqs[i].ifr_addr; + ip = sin->sin_addr; + + if (ip.s_addr == me->sin_addr.s_addr) { + ioctl(sock, SIOCGIFNETMASK, &ifreqs[i]); + sin = (struct sockaddr_in *)&ifreqs[i].ifr_addr; + //mask = sin->sin_addr; + *mask = sin->sin_addr.s_addr; + r = 0; + break; + } + + } + + end: + + close(sock); + return r; + +} + +#elif defined(WIN32) + +static int get_netmask(struct sockaddr_in *me, int *mask) +{ + return -1; +} + +#else + +static int get_netmask(struct sockaddr_in *me, int *mask) +{ + return -1; +} + #endif SWITCH_DECLARE(switch_status_t) switch_find_local_ip(char *buf, int len, int *mask, int family)