# HG changeset patch # User Mark Doliner # Date 1139202873 0 # Node ID 66268ae5b94a66fccc651dae7cf123b18ca55ebc # Parent d0ae6489a0fb0b4c011661f5013bfa85df45cadb [gaim-migrate @ 15492] 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. committer: Tailor Script diff -r d0ae6489a0fb -r 66268ae5b94a src/network.c --- a/src/network.c Mon Feb 06 04:20:30 2006 +0000 +++ b/src/network.c Mon Feb 06 05:14:33 2006 +0000 @@ -106,7 +106,7 @@ { struct hostent *host; char localhost[129]; - long unsigned add; + int i; static char ip[46]; const char *tmp = NULL; @@ -116,23 +116,28 @@ 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); + /* 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; + } + } - g_snprintf(ip, 16, "%lu.%lu.%lu.%lu", - ((add >> 24) & 255), - ((add >> 16) & 255), - ((add >> 8) & 255), - add & 255); - - return ip; + return "127.0.0.1"; } const char *