Non-deterministic behavior of the INADDR_ANY destination IP address

Ruslan Ermilov ru at FreeBSD.org
Thu Sep 11 16:37:54 PDT 2003


Hi there!

I always thought (until today) that an empty /etc/resolv.conf
is equivalent to specifying the ``nameserver 127.0.0.1'' line
in it, but as it turns out, it's not: the resolver(3) library
just uses an uninitialized 0.0.0.0 address which is mapped to
the first local IP address, not necessarily the one of the
lo0 interface).  The actual mapping depends on the order you
have configured IP addresses.

What the attached patch does is to ensure that if the loopback
interface is configured for IPv4, its address is used for
mapping of the 0.0.0.0 address.  If not, then the first local
IP address is used, as before.

Let me know what do you think about it.


Cheers,
-- 
Ruslan Ermilov		Sysadmin and DBA,
ru at sunbay.com		Sunbay Software Ltd,
ru at FreeBSD.org		FreeBSD committer
-------------- next part --------------
Index: in_pcb.c
===================================================================
RCS file: /home/ncvs/src/sys/netinet/in_pcb.c,v
retrieving revision 1.122
diff -u -p -r1.122 in_pcb.c
--- in_pcb.c	7 Aug 2003 15:04:27 -0000	1.122
+++ in_pcb.c	11 Sep 2003 23:06:35 -0000
@@ -542,14 +542,22 @@ in_pcbconnect_setup(inp, nam, laddrp, lp
 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
 		/*
 		 * If the destination address is INADDR_ANY,
+		 * use the address of a loopback interface, or
 		 * use the primary local address.
 		 * If the supplied address is INADDR_BROADCAST,
 		 * and the primary interface supports broadcast,
 		 * choose the broadcast address for that interface.
 		 */
-		if (faddr.s_addr == INADDR_ANY)
-			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
-		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
+		if (faddr.s_addr == INADDR_ANY) {
+			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
+				if (ia->ia_ifa.ifa_ifp->if_flags & IFF_LOOPBACK) {
+					faddr = ia->ia_addr.sin_addr;
+					break;
+				}
+			}
+			if (faddr.s_addr == INADDR_ANY)
+				faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
+		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
 		    IFF_BROADCAST))
 			faddr = satosin(&TAILQ_FIRST(
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-net/attachments/20030912/62c69103/attachment.bin


More information about the freebsd-net mailing list