changeset 13130:66268ae5b94a

[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 <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Mon, 06 Feb 2006 05:14:33 +0000
parents d0ae6489a0fb
children 47ecef83e2e1
files src/network.c
diffstat 1 files changed, 16 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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 *