bin/date/netdate.c
int
netsettime(time_t tval)
{
	struct timeval tout;
	struct servent *sp;
	struct tsp msg;
	struct sockaddr_in tmpsin, dest, from;
	fd_set ready;
	long waittime;
	int s, port, timed_ack, found, errn;
	socklen_t length;
	char hostname[MAXHOSTNAMELEN];

	if ((sp = getservbyname("timed", "udp")) == NULL) {
		warnx("timed/udp: unknown service");
		return (retval = 2);
	}

	dest.sin_port = sp->s_port;
	dest.sin_family = AF_INET;
	dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s < 0) {
		if (errno != EPROTONOSUPPORT)
			warn("timed");
		return (retval = 2);
	}

	memset(&tmpsin, 0, sizeof(tmpsin));
	tmpsin.sin_family = AF_INET;
	for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
		tmpsin.sin_port = htons((u_short)port);
		if (bind(s, (struct sockaddr *)&tmpsin, sizeof(tmpsin)) >= 0)
			break;
		if (errno == EADDRINUSE)
			continue;
		if (errno != EADDRNOTAVAIL)
			warn("bind");
		goto bad;
	}
	if (port == IPPORT_RESERVED / 2) {
		warnx("all ports in use");
		goto bad;
	}
	msg.tsp_type = TSP_SETDATE;
	msg.tsp_vers = TSPVERSION;
	if (gethostname(hostname, sizeof(hostname))) {
		warn("gethostname");
		goto bad;
	}
	strncpy(msg.tsp_name, hostname, sizeof(msg.tsp_name) - 1);
	msg.tsp_name[sizeof(msg.tsp_name) - 1] = '\0';
	msg.tsp_seq = htons((u_short)0);
	msg.tsp_time.tv_sec = htonl((u_long)tval);
	msg.tsp_time.tv_usec = htonl((u_long)0);
	length = sizeof(struct sockaddr_in);
	if (connect(s, (struct sockaddr *)&dest, length) < 0) {
		warn("connect");
		goto bad;
	}
	if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
		if (errno != ECONNREFUSED)
			warn("send");
		goto bad;
	}

	timed_ack = -1;
	waittime = WAITACK;
loop:
	tout.tv_sec = waittime;
	tout.tv_usec = 0;

	FD_ZERO(&ready);
	FD_SET(s, &ready);
	found = select(FD_SETSIZE, &ready, NULL, NULL, &tout);

	length = sizeof(errn);
	if (!getsockopt(s,
	    SOL_SOCKET, SO_ERROR, (char *)&errn, &length) && errn) {
		if (errn != ECONNREFUSED)
			warnc(errn, "send (delayed error)");
		goto bad;
	}

	if (found > 0 && FD_ISSET(s, &ready)) {
		length = sizeof(struct sockaddr_in);
		if (recvfrom(s, &msg, sizeof(struct tsp), 0,
		    (struct sockaddr *)&from, &length) < 0) {
			if (errno != ECONNREFUSED)
				warn("recvfrom");
			goto bad;
		}
		msg.tsp_seq = ntohs(msg.tsp_seq);
		msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
		msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
		switch (msg.tsp_type) {
		case TSP_ACK:
			timed_ack = TSP_ACK;
			waittime = WAITDATEACK;
			goto loop;
		case TSP_DATEACK:
			close(s);
			return (0);
		default:
			warnx("wrong ack received from timed: %s",
			    tsptype[msg.tsp_type]);
			timed_ack = -1;
			break;
		}
	}
	if (timed_ack == -1)
		warnx("can't reach time daemon, time set locally");

bad:
	close(s);
	return (retval = 2);
}
/*
 * Set the date in the machines controlled by timedaemons by communicating the
 * new date to the local timedaemon.  If the timedaemon is in the master state,
 * it performs the correction on all slaves.  If it is in the slave state, it
 * notifies the master that a correction is needed.
 * Returns 0 on success.  Returns > 0 on failure, setting retval to 2;
 */
Set the date in the machines controlled by timedaemons by communicating the
new date to the local timedaemon.  If the timedaemon is in the master state,
it performs the correction on all slaves.  If it is in the slave state, it
notifies the master that a correction is needed.
Returns 0 on success.  Returns > 0 on failure, setting retval to 2.
Set the date in the machines controlled by timedaemons by communicating the new date to the local timedaemon . If the timedaemon is in the master state it performs the correction on all slaves . If it is in the slave state it notifies the master that a correction is needed . Returns 0 on success . Returns > 0 on failure setting retval to 2 .
int
netsettime(time_t tval)
{
	struct timeval tout;
	struct servent *sp;
	struct tsp msg;
	struct sockaddr_in tmpsin, dest, from;
	fd_set ready;
	long waittime;
	int s, port, timed_ack, found, errn;
	socklen_t length;
	char hostname[MAXHOSTNAMELEN];
	if ((sp = getservbyname("timed", "udp")) == NULL) {
		warnx("timed/udp: unknown service");
		return (retval = 2);
	}
	dest.sin_port = sp->s_port;
	dest.sin_family = AF_INET;
	dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s < 0) {
		if (errno != EPROTONOSUPPORT)
			warn("timed");
		return (retval = 2);
	}
	memset(&tmpsin, 0, sizeof(tmpsin));
	tmpsin.sin_family = AF_INET;
	for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
		tmpsin.sin_port = htons((u_short)port);
		if (bind(s, (struct sockaddr *)&tmpsin, sizeof(tmpsin)) >= 0)
			break;
		if (errno == EADDRINUSE)
			continue;
		if (errno != EADDRNOTAVAIL)
			warn("bind");
		goto bad;
	}
	if (port == IPPORT_RESERVED / 2) {
		warnx("all ports in use");
		goto bad;
	}
	msg.tsp_type = TSP_SETDATE;
	msg.tsp_vers = TSPVERSION;
	if (gethostname(hostname, sizeof(hostname))) {
		warn("gethostname");
		goto bad;
	}
	strncpy(msg.tsp_name, hostname, sizeof(msg.tsp_name) - 1);
	msg.tsp_name[sizeof(msg.tsp_name) - 1] = '\0';
	msg.tsp_seq = htons((u_short)0);
	msg.tsp_time.tv_sec = htonl((u_long)tval);
	msg.tsp_time.tv_usec = htonl((u_long)0);
	length = sizeof(struct sockaddr_in);
	if (connect(s, (struct sockaddr *)&dest, length) < 0) {
		warn("connect");
		goto bad;
	}
	if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
		if (errno != ECONNREFUSED)
			warn("send");
		goto bad;
	}
	timed_ack = -1;
	waittime = WAITACK;
loop:
	tout.tv_sec = waittime;
	tout.tv_usec = 0;
	FD_ZERO(&ready);
	FD_SET(s, &ready);
	found = select(FD_SETSIZE, &ready, NULL, NULL, &tout);
	length = sizeof(errn);
	if (!getsockopt(s,
	    SOL_SOCKET, SO_ERROR, (char *)&errn, &length) && errn) {
		if (errn != ECONNREFUSED)
			warnc(errn, "send (delayed error)");
		goto bad;
	}
	if (found > 0 && FD_ISSET(s, &ready)) {
		length = sizeof(struct sockaddr_in);
		if (recvfrom(s, &msg, sizeof(struct tsp), 0,
		    (struct sockaddr *)&from, &length) < 0) {
			if (errno != ECONNREFUSED)
				warn("recvfrom");
			goto bad;
		}
		msg.tsp_seq = ntohs(msg.tsp_seq);
		msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
		msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
		switch (msg.tsp_type) {
		case TSP_ACK:
			timed_ack = TSP_ACK;
			waittime = WAITDATEACK;
			goto loop;
		case TSP_DATEACK:
			close(s);
			return (0);
		default:
			warnx("wrong ack received from timed: %s",
			    tsptype[msg.tsp_type]);
			timed_ack = -1;
			break;
		}
	}
	if (timed_ack == -1)
		warnx("can't reach time daemon, time set locally");
bad:
	close(s);
	return (retval = 2);
}
int netsettime ( time_t tval ) { struct timeval tout ; struct servent * sp ; struct tsp msg ; struct sockaddr_in tmpsin , dest , from ; fd_set ready ; long waittime ; int s , port , timed_ack , found , errn ; socklen_t length ; char hostname [ MAXHOSTNAMELEN ] ; if ( ( sp = getservbyname ( " " , " " ) ) == NULL ) { warnx ( " " ) ; return ( retval = 2 ) ; } dest . sin_port = sp -> s_port ; dest . sin_family = AF_INET ; dest . sin_addr . s_addr = htonl ( ( u_long ) INADDR_ANY ) ; s = socket ( AF_INET , SOCK_DGRAM , 0 ) ; if ( s < 0 ) { if ( errno != EPROTONOSUPPORT ) warn ( " " ) ; return ( retval = 2 ) ; } memset ( & tmpsin , 0 , sizeof ( tmpsin ) ) ; tmpsin . sin_family = AF_INET ; for ( port = IPPORT_RESERVED - 1 ; port > IPPORT_RESERVED / 2 ; port -- ) { tmpsin . sin_port = htons ( ( u_short ) port ) ; if ( bind ( s , ( struct sockaddr * ) & tmpsin , sizeof ( tmpsin ) ) >= 0 ) break ; if ( errno == EADDRINUSE ) continue ; if ( errno != EADDRNOTAVAIL ) warn ( " " ) ; goto bad ; } if ( port == IPPORT_RESERVED / 2 ) { warnx ( " " ) ; goto bad ; } msg . tsp_type = TSP_SETDATE ; msg . tsp_vers = TSPVERSION ; if ( gethostname ( hostname , sizeof ( hostname ) ) ) { warn ( " " ) ; goto bad ; } strncpy ( msg . tsp_name , hostname , sizeof ( msg . tsp_name ) - 1 ) ; msg . tsp_name [ sizeof ( msg . tsp_name ) - 1 ] = ' \0 ' ; msg . tsp_seq = htons ( ( u_short ) 0 ) ; msg . tsp_time . tv_sec = htonl ( ( u_long ) tval ) ; msg . tsp_time . tv_usec = htonl ( ( u_long ) 0 ) ; length = sizeof ( struct sockaddr_in ) ; if ( connect ( s , ( struct sockaddr * ) & dest , length ) < 0 ) { warn ( " " ) ; goto bad ; } if ( send ( s , ( char * ) & msg , sizeof ( struct tsp ) , 0 ) < 0 ) { if ( errno != ECONNREFUSED ) warn ( " " ) ; goto bad ; } timed_ack = -1 ; waittime = WAITACK ; loop : tout . tv_sec = waittime ; tout . tv_usec = 0 ; FD_ZERO ( & ready ) ; FD_SET ( s , & ready ) ; found = select ( FD_SETSIZE , & ready , NULL , NULL , & tout ) ; length = sizeof ( errn ) ; if ( ! getsockopt ( s , SOL_SOCKET , SO_ERROR , ( char * ) & errn , & length ) && errn ) { if ( errn != ECONNREFUSED ) warnc ( errn , " " ) ; goto bad ; } if ( found > 0 && FD_ISSET ( s , & ready ) ) { length = sizeof ( struct sockaddr_in ) ; if ( recvfrom ( s , & msg , sizeof ( struct tsp ) , 0 , ( struct sockaddr * ) & from , & length ) < 0 ) { if ( errno != ECONNREFUSED ) warn ( " " ) ; goto bad ; } msg . tsp_seq = ntohs ( msg . tsp_seq ) ; msg . tsp_time . tv_sec = ntohl ( msg . tsp_time . tv_sec ) ; msg . tsp_time . tv_usec = ntohl ( msg . tsp_time . tv_usec ) ; switch ( msg . tsp_type ) { case TSP_ACK : timed_ack = TSP_ACK ; waittime = WAITDATEACK ; goto loop ; case TSP_DATEACK : close ( s ) ; return ( 0 ) ; default : warnx ( " " , tsptype [ msg . tsp_type ] ) ; timed_ack = -1 ; break ; } } if ( timed_ack == -1 ) warnx ( " " ) ; bad : close ( s ) ; return ( retval = 2 ) ; }
Set the date in the machines controlled by timedaemons by communicating the
 new date to the local timedaemon.
Set the date in the machines controlled by timedaemons by communicating the new date to the local timedaemon .