| This page
is an addendum to the Time Synchronisation page
which you should have already read.
A number of people have mailed me looking for the source code to rdate.
As I also had some trouble locating it I took the time to setup this page
to include it. Please note that I had nothing do with writing this code
and merely include it here as a convenience. As of February 2004 I know
that it compiles and runs on Linux2.4(x86) and Solaris2.9(sparc)
This software is supplied as is with no warranty or statement of
fitness of purpose. You use it at your own risk.
The tarball contains the following files;
- README
- rdate-linux2.4-i386 Linux(x86) executable
- rdate-solaris2.9-sparc Solaris (sparc) executable
- rdate.1 manual page
- rdate.c source
The source is shown below;
/* $Header: /home/hyperion/mu/christos/src/mine/sys/rdate/RCS/rdate.c,v 1.3 1991 /05/30 22:13:23 christos Exp $ */ /* * rdate.c: Set the date from the specified host * * Uses the rfc868 time protocol at socket 37. * Time is returned as the number of seconds since * midnight January 1st 1900. * * A.Gray (andrew@argray.org) 16/2/04 * Few small mods to get it compiled on Solaris; * Replaced 'err(' with fprintf(stderr. * replaced __progname with argv[0] */ #ifndef lint static char rcsid[] = "$Id: rdate.c,v 1.3 1991/05/30 22:13:23 christos Exp $"; #endif /* lint */
#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
/* seconds from midnight Jan 1900 - 1970 */
#if __STDC__
# define DIFFERENCE 2208988800UL
#else
# define DIFFERENCE 2208988800
#endif
int
main(argc, argv)
int argc;
char *argv[];
{
int pr = 0, silent = 0, s;
time_t tim;
char *hname;
struct hostent *hp;
struct protoent *pp, ppp;
struct servent *sp, ssp;
struct sockaddr_in sa;
extern int optind;
int c;
while ((c = getopt(argc, argv, "ps")) != -1)
switch (c) {
case 'p':
pr++;
break;
case 's':
silent++;
break;
default:
goto usage;
}
if (argc - 1 != optind) {
usage:
(void) fprintf(stderr, "Usage: %s [-ps] .\n", argv[0]);
return(1);
}
hname = argv[optind];
if (isdigit(hname[0])) {
if ((hp = gethostbyaddr(hname, sizeof(struct sockaddr_in),
AF_INET)) == NULL)
(void) fprintf(stderr, "Unknown host address %s\n", hname);
}
else {
if ((hp = gethostbyname(hname)) == NULL)
(void) fprintf(stderr, "Unknown host name %s\n", hname);
}
if ((sp = getservbyname("time", "tcp")) == NULL) {
sp = &ssp;
sp->s_port = 37;
sp->s_proto = "tcp";
}
if ((pp = getprotobyname(sp->s_proto)) == NULL) {
pp = &ppp;
pp->p_proto = 6;
}
if ((s = socket(AF_INET, SOCK_STREAM, pp->p_proto)) == -1)
(void) fprintf(stderr, "Could not create socket\n");
sa.sin_family = AF_INET;
sa.sin_port = sp->s_port;
memcpy(&(sa.sin_addr.s_addr), hp->h_addr, hp->h_length);
memset(sa.sin_zero, 0, sizeof(sa.sin_zero));
if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) == -1)
(void) fprintf(stderr, "Could not connect socket\n");
if (read(s, &tim, sizeof(time_t)) != sizeof(time_t))
(void) fprintf(stderr, "Could not read data\n");
(void) close(s);
tim = ntohl(tim) - DIFFERENCE;
if (!pr) {
struct timeval tv;
tv.tv_sec = tim;
tv.tv_usec = 0;
if (settimeofday(&tv, NULL) == -1)
(void) fprintf(stderr, "Could not set time of day\n");
}
if (!silent)
(void) fputs(ctime(&tim), stdout);
return 0;
}
|