Index: res_config_mysql.c =================================================================== --- res_config_mysql.c (revision 501) +++ res_config_mysql.c (working copy) @@ -571,12 +571,15 @@ dbport = atoi(s); } - if(dbhost && !(s=ast_variable_retrieve(config, "general", "dbsock"))) { + s = NULL; + if ((ast_strlen_zero(dbhost) || (!strcmp(dbhost, "localhost"))) && !(s = ast_variable_retrieve(config, "general", "dbsock"))) { ast_log(LOG_WARNING, "MySQL RealTime: No database socket found, using '/tmp/mysql.sock' as default.\n"); strncpy(dbsock, "/tmp/mysql.sock", sizeof(dbsock) - 1); - } else { + } else if (s) { strncpy(dbsock, s, sizeof(dbsock) - 1); - } + } else { + dbsock[0] = '\0'; + } } ast_config_destroy(config); Index: asterisk-ooh323c/ooh323c/src/ooGkClient.c =================================================================== --- asterisk-ooh323c/ooh323c/src/ooGkClient.c (revision 501) +++ asterisk-ooh323c/ooh323c/src/ooGkClient.c (working copy) @@ -36,7 +36,21 @@ #include "ooTimer.h" #include "ooSocket.h" #include "ooUtils.h" +#include "asterisk/utils.h" +#include "asterisk/autoconfig.h" +#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__) +#include +#include +#endif + +#if defined(SOLARIS) +#include +#include +#elif defined(HAVE_GETIFADDRS) +#include +#endif + /** Global endpoint structure */ extern OOH323EndPoint gH323ep; @@ -44,6 +58,167 @@ 6, { 0, 0, 8, 2250, 0, 4 } }; +#if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS)) +static int get_local_address(struct in_addr *ourip) +{ + return -1; +} +#else +static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score) +{ + const char *address; + int score; + + address = ast_inet_ntoa(sin->sin_addr); + + /* RFC 1700 alias for the local network */ + if (address[0] == '0') + score = -25; + /* RFC 1700 localnet */ + else if (strncmp(address, "127", 3) == 0) + score = -20; + /* RFC 1918 non-public address space */ + else if (strncmp(address, "10.", 3) == 0) + score = -5; + /* RFC 1918 non-public address space */ + else if (strncmp(address, "172", 3) == 0) { + /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */ + if (address[4] == '1' && address[5] >= '6' && address[6] == '.') + score = -5; + /* 172.20.0.0 - 172.29.255.255, but not 172.200.0.0 - 172.255.255.255 nor 172.2.0.0 - 172.2.255.255 */ + else if (address[4] == '2' && address[6] == '.') + score = -5; + /* 172.30.0.0 - 172.31.255.255 */ + else if (address[4] == '3' && address[5] <= '1') + score = -5; + /* All other 172 addresses are public */ + else + score = 0; + /* RFC 2544 Benchmark test range */ + } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.') + score = -10; + /* RFC 1918 non-public address space */ + else if (strncmp(address, "192.168", 7) == 0) + score = -5; + /* RFC 3330 Zeroconf network */ + else if (strncmp(address, "169.254", 7) == 0) + /*!\note Better score than a test network, but not quite as good as RFC 1918 + * address space. The reason is that some Linux distributions automatically + * configure a Zeroconf address before trying DHCP, so we want to prefer a + * DHCP lease to a Zeroconf address. + */ + score = -10; + /* RFC 3330 Test network */ + else if (strncmp(address, "192.0.2.", 8) == 0) + score = -15; + /* Every other address should be publically routable */ + else + score = 0; + + if (score > *best_score) { + *best_score = score; + memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr)); + } +} + +static int get_local_address(struct in_addr *ourip) +{ + int s, res = -1; +#ifdef SOLARIS + struct lifreq *ifr = NULL; + struct lifnum ifn; + struct lifconf ifc; + struct sockaddr_in *sa; + char *buf = NULL; + int bufsz, x; +#endif /* SOLARIS */ +#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) + struct ifaddrs *ifap, *ifaphead; + int rtnerr; + const struct sockaddr_in *sin; +#endif /* BSD_OR_LINUX */ + struct in_addr best_addr; + int best_score = -100; + memset(&best_addr, 0, sizeof(best_addr)); + +#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) + rtnerr = getifaddrs(&ifaphead); + if (rtnerr) { + perror(NULL); + return -1; + } +#endif /* BSD_OR_LINUX */ + + s = socket(AF_INET, SOCK_STREAM, 0); + + if (s > 0) { +#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) + for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) { + + if (ifap->ifa_addr->sa_family == AF_INET) { + sin = (const struct sockaddr_in *) ifap->ifa_addr; + score_address(sin, &best_addr, &best_score); + res = 0; + + if (best_score == 0) + break; + } + } +#endif /* BSD_OR_LINUX */ + + /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */ +#ifdef SOLARIS + /* Get a count of interfaces on the machine */ + ifn.lifn_family = AF_INET; + ifn.lifn_flags = 0; + ifn.lifn_count = 0; + if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) { + close(s); + return -1; + } + + bufsz = ifn.lifn_count * sizeof(struct lifreq); + if (!(buf = malloc(bufsz))) { + close(s); + return -1; + } + memset(buf, 0, bufsz); + + /* Get a list of interfaces on the machine */ + ifc.lifc_len = bufsz; + ifc.lifc_buf = buf; + ifc.lifc_family = AF_INET; + ifc.lifc_flags = 0; + if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) { + close(s); + free(buf); + return -1; + } + + for (ifr = (struct lifreq *)buf, x = 0; x < ifn.lifn_count; ifr++, x++) { + sa = (struct sockaddr_in *)&(ifr->lifr_addr); + score_address(sa, &best_addr, &best_score); + res = 0; + + if (best_score == 0) + break; + } + + free(buf); +#endif /* SOLARIS */ + + close(s); + } +#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) + freeifaddrs(ifaphead); +#endif /* BSD_OR_LINUX */ + + if (res == 0 && ourip) + memcpy(ourip, &best_addr, sizeof(*ourip)); + return res; +} +#endif /* HAVE_GETIFADDRS */ + int ooGkClientInit(enum RasGatekeeperMode eGkMode, char *szGkAddr, int iGkPort ) { @@ -69,6 +244,12 @@ if(!strcmp(pGkClient->localRASIP, "0.0.0.0") || !strcmp(pGkClient->localRASIP, "127.0.0.1")) { +#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) + struct in_addr our_ip; + + get_local_address(&our_ip); + strcpy(pGkClient->localRASIP, ast_inet_ntoa(our_ip)); +#else if(!gH323ep.ifList) { if(ooSocketGetInterfaceList(&gH323ep.ctxt, &gH323ep.ifList)!= ASN_OK) @@ -92,6 +273,7 @@ OOTRACEERR1("Error:Failed to assign a local RAS IP address\n"); return OO_FAILED; } +#endif } #endif if(OO_OK != ooGkClientSetGkMode(pGkClient, eGkMode, szGkAddr, iGkPort))