[Vm-dev] [commit][3485] update resolver local address discovery; it seem gethostbyaddr has been compromised by OS changes recently.

commits at squeakvm.org commits at squeakvm.org
Thu Nov 5 20:53:46 UTC 2015


Revision: 3485
Author:   rowledge
Date:     2015-11-05 12:53:44 -0800 (Thu, 05 Nov 2015)
Log Message:
-----------
update resolver local address discovery; it seem gethostbyaddr has been compromised by OS changes recently. Change to using ifgetaddrs and scanning for eth0 or wlan0 addresses, which looks like a good change on Raspbian at least.

Modified Paths:
--------------
    branches/Cog/platforms/unix/plugins/SocketPlugin/sqUnixSocket.c

Modified: branches/Cog/platforms/unix/plugins/SocketPlugin/sqUnixSocket.c
===================================================================
--- branches/Cog/platforms/unix/plugins/SocketPlugin/sqUnixSocket.c	2015-11-05 18:16:38 UTC (rev 3484)
+++ branches/Cog/platforms/unix/plugins/SocketPlugin/sqUnixSocket.c	2015-11-05 20:53:44 UTC (rev 3485)
@@ -83,6 +83,7 @@
 # include <netinet/tcp.h>
 # include <arpa/inet.h>
 # include <netdb.h>
+#include <ifaddrs.h>
 # include <errno.h>
 # include <unistd.h>
   
@@ -1504,11 +1505,55 @@
 sqInt sqResolverAddrLookupResultSize(void)	{ return strlen(lastName); }
 sqInt sqResolverError(void)			{ return lastError; }
 sqInt sqResolverLocalAddress(void)
+#if 0 
+/* old code */
 {	sqInt localaddr = nameToAddr(localHostName);
 	if (!localaddr)
 		localaddr = nameToAddr("localhost");
 	return localaddr;
 }
+#else
+/* experimental new code */
+{
+    struct ifaddrs *ifaddr, *ifa;
+    int family, s;
+    char host[NI_MAXHOST];
+    sqInt localAddr = 0;
+
+    if (getifaddrs(&ifaddr) == -1) {
+        interpreterProxy->success(false);
+        return 0;
+    }
+
+
+    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) 
+    {
+        if (ifa->ifa_addr == NULL)
+            continue;  
+
+        s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
+
+        if(((strcmp(ifa->ifa_name,"eth0")==0)||(strcmp(ifa->ifa_name,"wlan0")==0))&&(ifa->ifa_addr->sa_family==AF_INET))
+        {
+            if (s != 0)
+            {
+                interpreterProxy->success(false);
+                return 0;
+            }
+            FPRINTF((stderr, "\tInterface : <%s>\n",ifa->ifa_name ));
+            FPRINTF((stderr, "\t IP       : <%s>\n", inet_ntoa(((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr)));
+            if(localAddr == 0) { /* take the first plausible answer */
+                localAddr = ((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr;
+            }
+           
+        }
+    }
+
+    freeifaddrs(ifaddr);
+    return ntohl(localAddr);
+
+}
+#endif
 sqInt sqResolverNameLookupResult(void)		{ return lastAddr; }
 
 void sqResolverAddrLookupResult(char *nameForAddress, sqInt nameSize)



More information about the Vm-dev mailing list