FreeBSD/contrib/ntp/ntpd/ntp_io.c
/*
 * findlocalinterface - find local interface corresponding to addr,
 * which does not have any of flags set.  If bast is nonzero, addr is
 * a broadcast address.
 *
 * This code attempts to find the local sending address for an outgoing
 * address by connecting a new socket to destinationaddress:NTP_PORT
 * and reading the sockname of the resulting connect.
 * the complicated sequence simulates the routing table lookup
 * for to first hop without duplicating any of the routing logic into
 * ntpd. preferably we would have used an API call - but its not there -
 * so this is the best we can do here short of duplicating to entire routing
 * logic in ntpd which would be a silly and really unportable thing to do.
 *
 */
find local interface corresponding to addr,
which does not have any of flags set.  If bast is nonzero, addr is
a broadcast address.

This code attempts to find the local sending address for an outgoing
address by connecting a new socket to destinationaddress:NTP_PORT
and reading the sockname of the resulting connect.
the complicated sequence simulates the routing table lookup
for to first hop without duplicating any of the routing logic into
ntpd. preferably we would have used an API call - but its not there
so this is the best we can do here short of duplicating to entire routing
logic in ntpd which would be a silly and really unportable thing to do.
find local interface corresponding to addr which does not have any of flags set . If bast is nonzero addr is a broadcast address . This code attempts to find the local sending address for an outgoing address by connecting a new socket to destinationaddress : NTP_PORT and reading the sockname of the resulting connect . the complicated sequence simulates the routing table lookup for to first hop without duplicating any of the routing logic into ntpd . preferably we would have used an API call - but its not there so this is the best we can do here short of duplicating to entire routing logic in ntpd which would be a silly and really unportable thing to do .
static endpt *
findlocalinterface(
	sockaddr_u *	addr,
	int		flags,
	int		bcast
	)
{
	GETSOCKNAME_SOCKLEN_TYPE	sockaddrlen;
	endpt *				iface;
	sockaddr_u			saddr;
	SOCKET				s;
	int				rtn;
	int				on;
	DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
		    stoa(addr)));
	s = socket(AF(addr), SOCK_DGRAM, 0);
	if (INVALID_SOCKET == s)
		return NULL;
	if (bcast) {
		on = 1;
		if (SOCKET_ERROR == setsockopt(s, SOL_SOCKET,
						SO_BROADCAST,
						(char *)&on,
						sizeof(on))) {
			closesocket(s);
			return NULL;
		}
	}
	rtn = connect(s, &addr->sa, SOCKLEN(addr));
	if (SOCKET_ERROR == rtn) {
		closesocket(s);
		return NULL;
	}
	sockaddrlen = sizeof(saddr);
	rtn = getsockname(s, &saddr.sa, &sockaddrlen);
	closesocket(s);
	if (SOCKET_ERROR == rtn)
		return NULL;
	DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n",
		    stoa(addr), stoa(&saddr)));
	iface = getinterface(&saddr, flags);
	if (NULL == iface || iface->ignore_packets)
		iface = findclosestinterface(&saddr,
					     flags | INT_LOOPBACK);
	if (iface != NULL && iface->ignore_packets)
		iface = NULL;
	return iface;
}
static endpt * findlocalinterface ( sockaddr_u * addr , int flags , int bcast ) { GETSOCKNAME_SOCKLEN_TYPE sockaddrlen ; endpt * iface ; sockaddr_u saddr ; SOCKET s ; int rtn ; int on ; DPRINTF ( 4 , ( " \n " , stoa ( addr ) ) ) ; s = socket ( AF ( addr ) , SOCK_DGRAM , 0 ) ; if ( INVALID_SOCKET == s ) return NULL ; if ( bcast ) { on = 1 ; if ( SOCKET_ERROR == setsockopt ( s , SOL_SOCKET , SO_BROADCAST , ( char * ) & on , sizeof ( on ) ) ) { closesocket ( s ) ; return NULL ; } } rtn = connect ( s , & addr -> sa , SOCKLEN ( addr ) ) ; if ( SOCKET_ERROR == rtn ) { closesocket ( s ) ; return NULL ; } sockaddrlen = sizeof ( saddr ) ; rtn = getsockname ( s , & saddr . sa , & sockaddrlen ) ; closesocket ( s ) ; if ( SOCKET_ERROR == rtn ) return NULL ; DPRINTF ( 4 , ( " \n " , stoa ( addr ) , stoa ( & saddr ) ) ) ; iface = getinterface ( & saddr , flags ) ; if ( NULL == iface || iface -> ignore_packets ) iface = findclosestinterface ( & saddr , flags | INT_LOOPBACK ) ; if ( iface != NULL && iface -> ignore_packets ) iface = NULL ; return iface ; }
findlocalinterface - find local interface corresponding to addr,
 which does not have any of flags set.
findlocalinterface - find local interface corresponding to addr which does not have any of flags set .
/*
	 * If we are looking for broadcast interface we need to set this
	 * socket to allow broadcast
	 */ /*
	 * if we didn't find an exact match on saddr, find the closest
	 * available local address.  This handles the case of the
	 * address suggested by the kernel being excluded by nic rules
	 * or the user's -I and -L options to ntpd.
	 * See http://bugs.ntp.org/1184 and http://bugs.ntp.org/1683
	 * for more background.
	 */ /* Don't use an interface which will ignore replies */