[15] | 1 | /* |
---|
| 2 | * $Source: /afs/dev.mit.edu/source/repository/athena/etc/gettime/gettime.c,v $ |
---|
| 3 | * $Author: builder $ |
---|
| 4 | * $Locker: $ |
---|
| 5 | * $Header: /afs/dev.mit.edu/source/repository/athena/etc/gettime/gettime.c,v 1.1 1985-05-27 16:17:18 builder Exp $ |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #ifndef lint |
---|
| 9 | static char *rcsid_gettime_c = "$Header: /afs/dev.mit.edu/source/repository/athena/etc/gettime/gettime.c,v 1.1 1985-05-27 16:17:18 builder Exp $"; |
---|
| 10 | #endif lint |
---|
| 11 | |
---|
| 12 | #include <sys/types.h> |
---|
| 13 | #include <sys/time.h> |
---|
| 14 | #include <sys/socket.h> |
---|
| 15 | #include <netinet/in.h> |
---|
| 16 | #include <stdio.h> |
---|
| 17 | #include <errno.h> |
---|
| 18 | #include <netdb.h> |
---|
| 19 | #include <signal.h> |
---|
| 20 | #include <setjmp.h> |
---|
| 21 | char buffer[512]; |
---|
| 22 | char *ctime(); |
---|
| 23 | struct timeval tv; |
---|
| 24 | struct timezone tz; |
---|
| 25 | jmp_buf top_level; |
---|
| 26 | int hiccup(); |
---|
| 27 | main(argc, argv) |
---|
| 28 | int argc; |
---|
| 29 | char *argv[]; |
---|
| 30 | { |
---|
| 31 | struct sockaddr_in sin; |
---|
| 32 | struct servent *sp; |
---|
| 33 | struct hostent *host; |
---|
| 34 | int setflg = 0; |
---|
| 35 | register int i; |
---|
| 36 | register int s; |
---|
| 37 | long hosttime; |
---|
| 38 | register int *nettime; |
---|
| 39 | char hostname[64]; |
---|
| 40 | int attempts = 0; |
---|
| 41 | strcpy (hostname, "dcn1"); |
---|
| 42 | for (i = 1;i < argc;i++) { |
---|
| 43 | if (*argv[i] == '-') { |
---|
| 44 | if (argv[i][1] == 's') setflg++; |
---|
| 45 | else { |
---|
| 46 | fprintf (stderr, "%s: Invalid argument %s\n", |
---|
| 47 | argv[0], argv[i]); |
---|
| 48 | exit (11); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | else strcpy (hostname, argv[i]); |
---|
| 52 | } |
---|
| 53 | sp = getservbyname("time", "udp"); |
---|
| 54 | if (sp == 0) { |
---|
| 55 | fprintf (stderr, "%s: time/udp: unknown service.\n", |
---|
| 56 | argv[0]); |
---|
| 57 | exit (1); |
---|
| 58 | } |
---|
| 59 | host = gethostbyname(hostname); |
---|
| 60 | if (host == NULL) { |
---|
| 61 | fprintf (stderr, "%s: The timeserver host %s is unknown\n", |
---|
| 62 | argv[0], hostname); |
---|
| 63 | exit (2); |
---|
| 64 | } |
---|
| 65 | sin.sin_family = host->h_addrtype; |
---|
| 66 | bcopy (host->h_addr, (caddr_t)&sin.sin_addr, |
---|
| 67 | host->h_length); |
---|
| 68 | sin.sin_port = sp->s_port; |
---|
| 69 | s = socket(AF_INET, SOCK_DGRAM, 0); |
---|
| 70 | if (s < 0) { |
---|
| 71 | perror ("gettime: socket"); |
---|
| 72 | exit (3); |
---|
| 73 | } |
---|
| 74 | if (connect (s, (caddr_t)&sin, sizeof (sin)) < 0) { |
---|
| 75 | perror ("gettime: connect"); |
---|
| 76 | exit (4); |
---|
| 77 | } |
---|
| 78 | setjmp(top_level); |
---|
| 79 | if (attempts++ > 5) { |
---|
| 80 | close (s); |
---|
| 81 | fprintf (stderr, "Failed to get time from %s\n", |
---|
| 82 | hostname); |
---|
| 83 | exit (10); |
---|
| 84 | } |
---|
| 85 | alarm(0); |
---|
| 86 | signal(SIGALRM, hiccup); |
---|
| 87 | alarm(5); |
---|
| 88 | send (s, buffer, 40, 0); /* Send an empty packet */ |
---|
| 89 | if (gettimeofday (&tv, &tz) < 0) { |
---|
| 90 | perror ("gettime: gettimeofday"); |
---|
| 91 | exit (5); |
---|
| 92 | } |
---|
| 93 | recv (s, buffer, 512, 0); /* Wait for the reply */ |
---|
| 94 | nettime = (int *)buffer; |
---|
| 95 | hosttime = (long) ntohl (*nettime) - 2208988800; |
---|
| 96 | fprintf (stdout, "%s", ctime(&hosttime)); |
---|
| 97 | (&tv)->tv_sec = hosttime; |
---|
| 98 | if (setflg) { |
---|
| 99 | if (settimeofday (&tv, &tz) < 0) { |
---|
| 100 | perror ("gettime: settimeofday"); |
---|
| 101 | exit (6); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | close (s); |
---|
| 105 | exit (0); |
---|
| 106 | } |
---|
| 107 | hiccup() { |
---|
| 108 | longjmp (top_level); |
---|
| 109 | } |
---|