1 | /* |
---|
2 | * $Source: /afs/dev.mit.edu/source/repository/athena/etc/gettime/gettime.c,v $ |
---|
3 | * $Author: tytso $ |
---|
4 | * $Locker: $ |
---|
5 | * $Header: /afs/dev.mit.edu/source/repository/athena/etc/gettime/gettime.c,v 1.4 1987-12-14 18:03:02 tytso 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.4 1987-12-14 18:03:02 tytso 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 | |
---|
22 | #define DEFAULT_TIME_SERVER "dcn1.arpa" |
---|
23 | |
---|
24 | /* On the RT, we need to explicitly make this an unsigned long. Neither the |
---|
25 | VAX or RT versions of pcc accept this syntax, however. |
---|
26 | - Win Treese, 2/21/86 |
---|
27 | */ |
---|
28 | |
---|
29 | #if defined(ibm032) && !defined(_pcc_) |
---|
30 | #define TM_OFFSET 2208988800UL |
---|
31 | #else |
---|
32 | #define TM_OFFSET 2208988800 |
---|
33 | #endif rtpc |
---|
34 | |
---|
35 | char buffer[512]; |
---|
36 | char *ctime(); |
---|
37 | struct timeval tv; |
---|
38 | struct timezone tz; |
---|
39 | jmp_buf top_level; |
---|
40 | int hiccup(); |
---|
41 | main(argc, argv) |
---|
42 | int argc; |
---|
43 | char *argv[]; |
---|
44 | { |
---|
45 | struct sockaddr_in sin; |
---|
46 | struct servent *sp; |
---|
47 | struct hostent *host; |
---|
48 | int setflg = 0; |
---|
49 | register int i; |
---|
50 | register int s; |
---|
51 | long hosttime; |
---|
52 | register int *nettime; |
---|
53 | char hostname[64]; |
---|
54 | int attempts = 0, cc; |
---|
55 | |
---|
56 | strcpy (hostname, DEFAULT_TIME_SERVER); |
---|
57 | for (i = 1;i < argc;i++) { |
---|
58 | if (*argv[i] == '-') { |
---|
59 | if (argv[i][1] == 's') setflg++; |
---|
60 | else { |
---|
61 | fprintf (stderr, "%s: Invalid argument %s\n", |
---|
62 | argv[0], argv[i]); |
---|
63 | exit (11); |
---|
64 | } |
---|
65 | } |
---|
66 | else strcpy (hostname, argv[i]); |
---|
67 | } |
---|
68 | sp = getservbyname("time", "udp"); |
---|
69 | if (sp == 0) { |
---|
70 | fprintf (stderr, "%s: time/udp: unknown service.\n", |
---|
71 | argv[0]); |
---|
72 | exit (1); |
---|
73 | } |
---|
74 | host = gethostbyname(hostname); |
---|
75 | if (host == NULL) { |
---|
76 | fprintf (stderr, "%s: The timeserver host %s is unknown\n", |
---|
77 | argv[0], hostname); |
---|
78 | exit (2); |
---|
79 | } |
---|
80 | sin.sin_family = host->h_addrtype; |
---|
81 | bcopy (host->h_addr, (caddr_t)&sin.sin_addr, |
---|
82 | host->h_length); |
---|
83 | sin.sin_port = sp->s_port; |
---|
84 | s = socket(AF_INET, SOCK_DGRAM, 0); |
---|
85 | if (s < 0) { |
---|
86 | perror ("gettime: socket"); |
---|
87 | exit (3); |
---|
88 | } |
---|
89 | if (connect (s, (caddr_t)&sin, sizeof (sin)) < 0) { |
---|
90 | perror ("gettime: connect"); |
---|
91 | exit (4); |
---|
92 | } |
---|
93 | setjmp(top_level); |
---|
94 | if (attempts++ > 5) { |
---|
95 | close (s); |
---|
96 | fprintf (stderr, "Failed to get time from %s\n", |
---|
97 | hostname); |
---|
98 | exit (10); |
---|
99 | } |
---|
100 | alarm(0); |
---|
101 | signal(SIGALRM, hiccup); |
---|
102 | alarm(5); |
---|
103 | send (s, buffer, 40, 0); /* Send an empty packet */ |
---|
104 | if (gettimeofday (&tv, &tz) < 0) { |
---|
105 | perror ("gettime: gettimeofday"); |
---|
106 | exit (5); |
---|
107 | } |
---|
108 | cc = recv (s, buffer, 512, 0); |
---|
109 | if (cc < 0) { |
---|
110 | perror("recv"); |
---|
111 | close(s); |
---|
112 | fprintf (stderr, "Failed to get time from %s\n", |
---|
113 | hostname); |
---|
114 | exit (7); |
---|
115 | } |
---|
116 | if (cc != 4) { |
---|
117 | close(s); |
---|
118 | fprintf(stderr, |
---|
119 | "Protocol error -- received %d bytes; expected 4.\n", |
---|
120 | cc); |
---|
121 | exit(8); |
---|
122 | } |
---|
123 | nettime = (int *)buffer; |
---|
124 | hosttime = (long) ntohl (*nettime) - TM_OFFSET; |
---|
125 | fprintf (stdout, "%s", ctime(&hosttime)); |
---|
126 | (&tv)->tv_sec = hosttime; |
---|
127 | if (setflg) { |
---|
128 | if (settimeofday (&tv, &tz) < 0) { |
---|
129 | perror ("gettime: settimeofday"); |
---|
130 | exit (6); |
---|
131 | } |
---|
132 | } |
---|
133 | close (s); |
---|
134 | exit (0); |
---|
135 | } |
---|
136 | hiccup() { |
---|
137 | longjmp (top_level); |
---|
138 | } |
---|