From: Mark D. <the...@us...> - 2006-02-06 05:14:42
|
Update of /cvsroot/gaim/gaim/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21125/src Modified Files: network.c Log Message: Attempt to avoid picking 127.0.0.1 as our public IP address if there are other options available. However, I still think this just picks the first entry in /etc/hosts. Is there a function call that returns all IPs listed in /etc/hosts? I couldn't find one, and I really don't want to have to parse that file myself. Index: network.c =================================================================== RCS file: /cvsroot/gaim/gaim/src/network.c,v retrieving revision 1.39 retrieving revision 1.40 diff -u -d -p -r1.39 -r1.40 --- network.c 6 Feb 2006 04:20:30 -0000 1.39 +++ network.c 6 Feb 2006 05:14:33 -0000 1.40 @@ -106,7 +106,7 @@ gaim_network_get_local_system_ip(int fd) { struct hostent *host; char localhost[129]; - long unsigned add; + int i; static char ip[46]; const char *tmp = NULL; @@ -116,23 +116,28 @@ gaim_network_get_local_system_ip(int fd) if (tmp) return tmp; - /* TODO: Make this avoid using localhost/127.0.0.1 */ if (gethostname(localhost, 128) < 0) return NULL; if ((host = gethostbyname(localhost)) == NULL) return NULL; - memcpy(&add, host->h_addr_list[0], 4); - add = htonl(add); - - g_snprintf(ip, 16, "%lu.%lu.%lu.%lu", - ((add >> 24) & 255), - ((add >> 16) & 255), - ((add >> 8) & 255), - add & 255); + /* Avoid using 127.0.0.1 */ + for (i = 0; (host->h_addr_list[i] != NULL); i++) + { + if ((host->h_addr_list[i][0] != 127) || + (host->h_addr_list[i][1] != 0) || + (host->h_addr_list[i][2] != 0) || + (host->h_addr_list[i][3] != 1)) + { + g_snprintf(ip, 16, "%hhu.%hhu.%hhu.%hhu", + host->h_addr_list[i][0], host->h_addr_list[i][1], + host->h_addr_list[i][2], host->h_addr_list[i][3]); + return ip; + } + } - return ip; + return "127.0.0.1"; } const char * |