[asterisk-commits] file: branch file/netsock2 r97617 - /team/file/netsock2/main/netsock2.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jan 9 14:01:51 CST 2008


Author: file
Date: Wed Jan  9 14:01:50 2008
New Revision: 97617

URL: http://svn.digium.com/view/asterisk?view=rev&rev=97617
Log:
Ensure the loopbacks are binded last so that they are not considered the default socket under normal circumstances.

Modified:
    team/file/netsock2/main/netsock2.c

Modified: team/file/netsock2/main/netsock2.c
URL: http://svn.digium.com/view/asterisk/team/file/netsock2/main/netsock2.c?view=diff&rev=97617&r1=97616&r2=97617
==============================================================================
--- team/file/netsock2/main/netsock2.c (original)
+++ team/file/netsock2/main/netsock2.c Wed Jan  9 14:01:50 2008
@@ -40,6 +40,7 @@
 #include <sys/sockio.h>
 #endif
 #include <ifaddrs.h>
+#include <net/if.h>
 
 #ifdef HAVE_FIONREAD_SYS_IOCTL
 #include <sys/ioctl.h>
@@ -329,6 +330,7 @@
 	struct ifaddrs *ifstart = NULL, *ifcurrent = NULL;
 	int family = (network_layer == AST_NETSOCK2_NETWORK_LAYER_IPV6 ? AF_INET6 : AF_INET), res = 0;
 	socklen_t salen = (network_layer == AST_NETSOCK2_NETWORK_LAYER_IPV6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
+	char ifloopback[NI_MAXHOST] = "";
 
 	/* Grab a list of interfaces so we can the IP addresses currently on the system */
 	if (getifaddrs(&ifstart) < 0)
@@ -346,11 +348,19 @@
 		if (getnameinfo(ifcurrent->ifa_addr, salen, address, sizeof(address), NULL, 0, NI_NUMERICHOST) < 0)
 			continue;
 
-		res = ast_netsock2_bind(binder, socket_list, transport, address, port, tos, cos, connect, read, disconnect, periodic, certificate, cipher);
+		/* If this interface is not a loopback bind it now, otherwise defer it until the end so that the default socket chosen doesn't end up being the loopback */
+		if (!(ifcurrent->ifa_flags & IFF_LOOPBACK))
+			res = ast_netsock2_bind(binder, socket_list, transport, address, port, tos, cos, connect, read, disconnect, periodic, certificate, cipher);
+		else
+			ast_copy_string(ifloopback, address, sizeof(ifloopback));
 	}
 
 	/* Now that we are all done we have to free the list, otherwise it would leak memory and that is bad, mmmk? */
 	freeifaddrs(ifstart);
+
+	/* If a loopback interface was found bind it at the end */
+	if (!ast_strlen_zero(ifloopback))
+		res = ast_netsock2_bind(binder, socket_list, transport, ifloopback, port, tos, cos, connect, read, disconnect, periodic, certificate, cipher);
 
 	return 0;
 }




More information about the asterisk-commits mailing list