FreeBSD/contrib/ntp/ntpdate/ntpdate.c
/*
 * init_io - initialize I/O data and open socket
 */
initialize I/O data and open socket
initialize I / O data and open socket
static void
init_io(void)
{
	struct addrinfo *res, *ressave;
	struct addrinfo hints;
	sockaddr_u addr;
	char service[5];
	int rc;
	int optval = 1;
	int check_ntp_port_in_use = !debug && !simple_query && !unpriv_port;
	init_recvbuff(sys_numservers + 2);
	strlcpy(service, "ntp", sizeof(service));
	ZERO(hints);
	hints.ai_family = ai_fam_templ;
	hints.ai_flags = AI_PASSIVE;
	hints.ai_socktype = SOCK_DGRAM;
	if (getaddrinfo(NULL, service, &hints, &res) != 0) {
		msyslog(LOG_ERR, "getaddrinfo() failed: %m");
		exit(1);
	}
#ifdef SYS_WINNT
	if (check_ntp_port_in_use && ntp_port_inuse(AF_INET, NTP_PORT)){
		msyslog(LOG_ERR, "the NTP socket is in use, exiting: %m");
		exit(1);
	}
#endif
	ressave = res;
	for(nbsock = 0; (nbsock < MAX_AF) && res ; res = res->ai_next) {
		fd[nbsock] = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
		if (fd[nbsock] == SOCKET_ERROR) {
#ifndef SYS_WINNT
		if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT ||
		    errno == EPFNOSUPPORT)
#else
		int err = WSAGetLastError();
		if (err == WSAEPROTONOSUPPORT || err == WSAEAFNOSUPPORT ||
		    err == WSAEPFNOSUPPORT)
#endif
			continue;
		msyslog(LOG_ERR, "socket() failed: %m");
		exit(1);
		}
		if (setsockopt(fd[nbsock], SOL_SOCKET, SO_REUSEADDR, (void*) &optval, sizeof(optval)) < 0) {
				msyslog(LOG_ERR, "setsockopt() SO_REUSEADDR failed: %m");
				exit(1);
		}
#ifdef IPV6_V6ONLY
		if (res->ai_family == AF_INET6)
			if (setsockopt(fd[nbsock], IPPROTO_IPV6, IPV6_V6ONLY, (void*) &optval, sizeof(optval)) < 0) {
				   msyslog(LOG_ERR, "setsockopt() IPV6_V6ONLY failed: %m");
					exit(1);
		}
#endif
		fd_family[nbsock] = res->ai_family;
		if (check_ntp_port_in_use) {
			ZERO(addr);
			INSIST(res->ai_addrlen <= sizeof(addr));
			memcpy(&addr, res->ai_addr, res->ai_addrlen);
			rc = bind(fd[nbsock], &addr.sa, SOCKLEN(&addr));
			if (rc < 0) {
				if (EADDRINUSE == socket_errno())
					msyslog(LOG_ERR, "the NTP socket is in use, exiting");
				else
					msyslog(LOG_ERR, "bind() fails: %m");
				exit(1);
			}
		}
#ifdef HAVE_POLL_H
		fdmask[nbsock].fd = fd[nbsock];
		fdmask[nbsock].events = POLLIN;
#else
		FD_SET(fd[nbsock], &fdmask);
		if (maxfd < fd[nbsock]+1) {
			maxfd = fd[nbsock]+1;
		}
#endif
#ifndef SYS_WINNT
# ifdef SYS_VXWORKS
		{
			int on = TRUE;
			if (ioctl(fd[nbsock],FIONBIO, &on) == ERROR) {
				msyslog(LOG_ERR, "ioctl(FIONBIO) fails: %m");
				exit(1);
			}
		}
# else 
#  if defined(O_NONBLOCK)
		if (fcntl(fd[nbsock], F_SETFL, O_NONBLOCK) < 0) {
			msyslog(LOG_ERR, "fcntl(FNDELAY|FASYNC) fails: %m");
			exit(1);
		}
#  else 
#	if defined(FNDELAY)
		if (fcntl(fd[nbsock], F_SETFL, FNDELAY) < 0) {
			msyslog(LOG_ERR, "fcntl(FNDELAY|FASYNC) fails: %m");
			exit(1);
		}
#	else 
#	 include "Bletch: Need non blocking I/O"
#	endif 
#  endif 
# endif 
#else 
		if (ioctlsocket(fd[nbsock], FIONBIO, (u_long *) &on) == SOCKET_ERROR) {
			msyslog(LOG_ERR, "ioctlsocket(FIONBIO) fails: %m");
			exit(1);
		}
#endif 
		nbsock++;
	}
	freeaddrinfo(ressave);
}
static void init_io ( void ) { struct addrinfo * res , * ressave ; struct addrinfo hints ; sockaddr_u addr ; char service [ 5 ] ; int rc ; int optval = 1 ; int check_ntp_port_in_use = ! debug && ! simple_query && ! unpriv_port ; init_recvbuff ( sys_numservers + 2 ) ; strlcpy ( service , " " , sizeof ( service ) ) ; ZERO ( hints ) ; hints . ai_family = ai_fam_templ ; hints . ai_flags = AI_PASSIVE ; hints . ai_socktype = SOCK_DGRAM ; if ( getaddrinfo ( NULL , service , & hints , & res ) != 0 ) { msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } #ifdef SYS_WINNT if ( check_ntp_port_in_use && ntp_port_inuse ( AF_INET , NTP_PORT ) ) { msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } #endif ressave = res ; for ( nbsock = 0 ; ( nbsock < MAX_AF ) && res ; res = res -> ai_next ) { fd [ nbsock ] = socket ( res -> ai_family , res -> ai_socktype , res -> ai_protocol ) ; if ( fd [ nbsock ] == SOCKET_ERROR ) { #ifndef SYS_WINNT if ( errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT || errno == EPFNOSUPPORT ) #else int err = WSAGetLastError ( ) ; if ( err == WSAEPROTONOSUPPORT || err == WSAEAFNOSUPPORT || err == WSAEPFNOSUPPORT ) #endif continue ; msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } if ( setsockopt ( fd [ nbsock ] , SOL_SOCKET , SO_REUSEADDR , ( void * ) & optval , sizeof ( optval ) ) < 0 ) { msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } #ifdef IPV6_V6ONLY if ( res -> ai_family == AF_INET6 ) if ( setsockopt ( fd [ nbsock ] , IPPROTO_IPV6 , IPV6_V6ONLY , ( void * ) & optval , sizeof ( optval ) ) < 0 ) { msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } #endif fd_family [ nbsock ] = res -> ai_family ; if ( check_ntp_port_in_use ) { ZERO ( addr ) ; INSIST ( res -> ai_addrlen <= sizeof ( addr ) ) ; memcpy ( & addr , res -> ai_addr , res -> ai_addrlen ) ; rc = bind ( fd [ nbsock ] , & addr . sa , SOCKLEN ( & addr ) ) ; if ( rc < 0 ) { if ( EADDRINUSE == socket_errno ( ) ) msyslog ( LOG_ERR , " " ) ; else msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } } #ifdef HAVE_POLL_H fdmask [ nbsock ] . fd = fd [ nbsock ] ; fdmask [ nbsock ] . events = POLLIN ; #else FD_SET ( fd [ nbsock ] , & fdmask ) ; if ( maxfd < fd [ nbsock ] + 1 ) { maxfd = fd [ nbsock ] + 1 ; } #endif #ifndef SYS_WINNT # ifdef SYS_VXWORKS { int on = TRUE ; if ( ioctl ( fd [ nbsock ] , FIONBIO , & on ) == ERROR ) { msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } } # else #  if defined ( O_NONBLOCK ) if ( fcntl ( fd [ nbsock ] , F_SETFL , O_NONBLOCK ) < 0 ) { msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } #  else #	if defined ( FNDELAY ) if ( fcntl ( fd [ nbsock ] , F_SETFL , FNDELAY ) < 0 ) { msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } #	else #	 include " " #	endif #  endif # endif #else if ( ioctlsocket ( fd [ nbsock ] , FIONBIO , ( u_long * ) & on ) == SOCKET_ERROR ) { msyslog ( LOG_ERR , " " ) ; exit ( 1 ) ; } #endif nbsock ++ ; } freeaddrinfo ( ressave ) ; }
init_io - initialize I/O data and open socket
init_io - initialize I / O data and open socket
/*
	 * Init buffer free list and stat counters
	 */ /*
	 * Open the socket
	 */ /*
	 * Init hints addrinfo structure
	 */ /*NOTREACHED*/ /* Remember the address of the addrinfo structure chain */ /*
	 * For each structure returned, open and bind socket
	 */ /* create a datagram (UDP) socket */ /*NOTREACHED*/ /* set socket to reuse address */ /*NOTREACHED*/ /* Restricts AF_INET6 socket to IPv6 communications (see RFC 2553bis-03) */ /*NOTREACHED*/ /* Remember the socket family in fd_family structure */ /*
		 * bind the socket to the NTP port
		 */ /*
		 * set non-blocking,
		 */ /* not SYS_VXWORKS */ /*NOTREACHED*/ /* not O_NONBLOCK */ /*NOTREACHED*/ /* FNDELAY */ /* FNDELAY */ /* not O_NONBLOCK */ /* SYS_VXWORKS */ /* SYS_WINNT */ /* SYS_WINNT */