[net.sources] rdate - get time and date via TCP/IP

lee@rochester.UUCP (Lee Moore) (06/07/85)

This is just a quick hack to request the time from another machine.
Since network time is transmitted in gmt, it doesn't matter where
you are relative to the machine that you are querying.

-=lee

#!/bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #!/bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	Makefile
#	rdate.1
#	rdate.c
# This archive created: Thu Jun  6 20:18:48 1985
export PATH; PATH=/bin:$PATH
if test -f 'Makefile'
then
	echo shar: over-writing existing file "'Makefile'"
fi
cat << \SHAR_EOF > 'Makefile'
#
# TCP/IP time client
#

BINDIR = /usr/local/bin

CFLAGS = -O

rdate: rdate.o
	cc rdate.o -o rdate

clean:
	rm -f *.o .,* a.out ERRS rdate

install: all
	install -s rdate $(DESTDIR)/$(BINDIR)
SHAR_EOF
if test -f 'rdate.1'
then
	echo shar: over-writing existing file "'rdate.1'"
fi
cat << \SHAR_EOF > 'rdate.1'
.TH RDATE 1 3/24/85
.CM 1
.SH "NAME"
rdate \- get the date and time via the network
.SH "SYNOPSIS"
.B rdate
[
host ...
]
.SH "DESCRIPTION"
.B Rdate
uses TCP to retrieve the current time of another machine using
using the protocol described in RFC 868.
The time for each system is returned in
ctime(3) format. The following is an example:
.nf
.IP ""
% rdate uci mc
[uci-icsa.arpa]	Sun Mar 24 20:35:41 1985
[mit-mc.arpa]	Sun Mar 24 20:36:19 1985
.fi
.SH FILES
.nf
/etc/services	map service name to socket number
/etc/hosts	map host name to internet address
.fi
.SH HISTORY
.TP
24-Mar-85  Lee Moore at University of Rochester
Created.
SHAR_EOF
if test -f 'rdate.c'
then
	echo shar: over-writing existing file "'rdate.c'"
fi
cat << \SHAR_EOF > 'rdate.c'
#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>      
#include <netdb.h>

#include <stdio.h>

#define BASE1970	2208988800L	/* difference between Unix time and net time */


main (argc,argv)
int argc;
char *argv[];
{
	int i;

	if (argc == 1) {
		printf("usage: rdate <host1> <host2> <host...>\n");
		exit(1);
	}

	for( i = 1; i < argc; i++ )
		RemoteData(argv[i]);
}

RemoteData(host)
char *host;
{
	struct	hostent *him;		/* host table entry */
	struct	servent *timeServ;	/* sevice file entry */
	struct	sockaddr_in sin;	/* socket address */
	int	fd;			/* network file descriptor */
	long	unixTime;		/* time in Unix format */
	u_char  netTime[4];		/* time in network format */
	int	i;			/* loop variable */
	char	*ctime();

	if ((him = gethostbyname(host)) == NULL) {
		fprintf(stderr, "rdate: Unknown host %s\n", host);
		return(-1);
	}

        if ((timeServ = getservbyname("time","tcp")) == NULL) {
                fprintf(stderr, "rdate: time/tcp: unknown service\n");
                return(-1);
        }

	if ((fd = socket(AF_INET, SOCK_STREAM, 0, 0)) < 0) {
		perror("rdate");
		return(-1);
	}

        sin.sin_family = him->h_addrtype;      
        bcopy(him->h_addr, (caddr_t)&sin.sin_addr, him->h_length);
        sin.sin_port = timeServ->s_port;

        if (connect(fd, (caddr_t)&sin, sizeof(sin), 0) < 0) {
		perror("rdate");
		close(fd);
		return(-1);
	}

	printf("[%s]\t", him->h_name);

	/* read in the response */
	for (i = 0; i < 4; i++)
		if (read(fd, &netTime[i], 1) != 1) {
			perror("rdate");
			close(fd);
			return(-1);
		}

	close(fd);
	unixTime = ntohl(* (long *) netTime) - BASE1970;
	printf("%s", ctime(&unixTime));
}
SHAR_EOF
#	End of shell archive
exit 0
-- 
TCP/IP:		lee@rochester.arpa
UUCP:		{decvax, allegra, seismo, cmcl2}!rochester!lee
XNS:		Lee Moore:CS:Univ Rochester
Phone:		+1 (716) 275-7747, -5671
Physical:	43 01' 40'' N, 77 37' 49'' W