FreeBSD/contrib/ntp/ntpdate/ntpdate.c
/* XXX ELIMINATE: merge BIG slew into adj_systime in lib/systime.c */
/*
 * addserver - determine a server's address and allocate a new structure
 *		for it.
 */
XXX ELIMINATE: merge BIG slew into adj_systime in lib/systime.c
addserver - determine a server's address and allocate a new structure
for it.
XXX ELIMINATE : merge BIG slew into adj_systime in lib / systime . c addserver - determine a server ' s address and allocate a new structure for it .
static void
addserver(
	char *serv
	)
{
	register struct server *server;
	struct addrinfo *addrResult, *ptr;
	struct addrinfo hints;
	int error;
	char service[5];
	sockaddr_u addr;
	strlcpy(service, "ntp", sizeof(service));
	ZERO(hints);
	hints.ai_family = ai_fam_templ;
	hints.ai_socktype = SOCK_DGRAM;
#ifdef DEBUG
	if (debug)
		printf("Looking for host %s and service %s\n", serv, service);
#endif
	error = getaddrinfo(serv, service, &hints, &addrResult);
	if (error != 0) {
		if (error == EAI_FAIL || error == EAI_AGAIN){
			fprintf(stderr, "Exiting, name server cannot be used: %s (%d)",
				gai_strerror(error), error);
			msyslog(LOG_ERR, "name server cannot be used: %s (%d)",
				gai_strerror(error), error);
			exit(1);
		}
		fprintf(stderr, "Error resolving %s: %s (%d)\n", serv,
			gai_strerror(error), error);
		msyslog(LOG_ERR, "Can't find host %s: %s (%d)", serv,
			gai_strerror(error), error);
		return;
	}
#ifdef DEBUG
	if (debug) {
		ZERO(addr);
		INSIST(addrResult->ai_addrlen <= sizeof(addr));
		memcpy(&addr, addrResult->ai_addr, addrResult->ai_addrlen);
		fprintf(stderr, "host found : %s\n", stohost(&addr));
	}
#endif
	for (ptr = addrResult; ptr != NULL; ptr = ptr->ai_next) {
		ZERO(addr);
		INSIST(ptr->ai_addrlen <= sizeof(addr));
		memcpy(&addr, ptr->ai_addr, ptr->ai_addrlen);
		if (is_reachable(&addr)) {
			server = emalloc_zero(sizeof(*server));
			memcpy(&server->srcadr, ptr->ai_addr, ptr->ai_addrlen);
			server->event_time = ++sys_numservers;
			if (sys_servers == NULL)
				sys_servers = server;
			else {
				struct server *sp;
				for (sp = sys_servers; sp->next_server != NULL;
				     sp = sp->next_server)
					;
				sp->next_server = server;
			}
		}
	}
	freeaddrinfo(addrResult);
}
static void addserver ( char * serv ) { register struct server * server ; struct addrinfo * addrResult , * ptr ; struct addrinfo hints ; int error ; char service [ 5 ] ; sockaddr_u addr ; strlcpy ( service , " " , sizeof ( service ) ) ; ZERO ( hints ) ; hints . ai_family = ai_fam_templ ; hints . ai_socktype = SOCK_DGRAM ; #ifdef DEBUG if ( debug ) printf ( " \n " , serv , service ) ; #endif error = getaddrinfo ( serv , service , & hints , & addrResult ) ; if ( error != 0 ) { if ( error == EAI_FAIL || error == EAI_AGAIN ) { fprintf ( stderr , " " , gai_strerror ( error ) , error ) ; msyslog ( LOG_ERR , " " , gai_strerror ( error ) , error ) ; exit ( 1 ) ; } fprintf ( stderr , " \n " , serv , gai_strerror ( error ) , error ) ; msyslog ( LOG_ERR , " " , serv , gai_strerror ( error ) , error ) ; return ; } #ifdef DEBUG if ( debug ) { ZERO ( addr ) ; INSIST ( addrResult -> ai_addrlen <= sizeof ( addr ) ) ; memcpy ( & addr , addrResult -> ai_addr , addrResult -> ai_addrlen ) ; fprintf ( stderr , " \n " , stohost ( & addr ) ) ; } #endif for ( ptr = addrResult ; ptr != NULL ; ptr = ptr -> ai_next ) { ZERO ( addr ) ; INSIST ( ptr -> ai_addrlen <= sizeof ( addr ) ) ; memcpy ( & addr , ptr -> ai_addr , ptr -> ai_addrlen ) ; if ( is_reachable ( & addr ) ) { server = emalloc_zero ( sizeof ( * server ) ) ; memcpy ( & server -> srcadr , ptr -> ai_addr , ptr -> ai_addrlen ) ; server -> event_time = ++ sys_numservers ; if ( sys_servers == NULL ) sys_servers = server ; else { struct server * sp ; for ( sp = sys_servers ; sp -> next_server != NULL ; sp = sp -> next_server ) ; sp -> next_server = server ; } } } freeaddrinfo ( addrResult ) ; }
XXX ELIMINATE: merge BIG slew into adj_systime in lib/systime.c
 addserver - determine a server's address and allocate a new structure
              for it.
XXX ELIMINATE : merge BIG slew into adj_systime in lib / systime . c addserver - determine a server ' s address and allocate a new structure for it .
/* Address infos structure to store result of getaddrinfo */ /* Address infos structure to store hints for getaddrinfo */ /* Error variable for getaddrinfo */ /* Service name */ /* Get host address. Looking for UDP datagram connection. */ /* Conduct more refined error analysis */ /* Name server is unusable. Exit after failing on the
			   first server, in order to shorten the timeout caused
			   by waiting for resolution of several servers */ /* We must get all returned server in case the first one fails */ /* empty */