forked from OS2World/APP-INTERNET-DayTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daytime.c
526 lines (444 loc) · 12.8 KB
/
daytime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* daytime.c - set local time from remote daytime or time server
*
* Author: Kai Uwe Rommel <[email protected]>
* Created: Sun Apr 10 1994
*
* This code is in the public domain.
* Let the author know if you make improvements or fixes, though.
*/
static char *rcsid =
"$Id: daytime.c,v 1.17 1999/06/13 12:00:58 rommel Exp rommel $";
/* static char *rcsrev = "$Revision: 1.17 $"; */
static char *rcsrev = "revision: 1.21";
/*
* $Log: daytime.c,v $
* Revision 1.17 1999/06/13 12:00:58 rommel
* fixed SNTP bitfield alignment error
*
* Revision 1.16 1999/03/05 15:47:06 rommel
* added logging
*
* Revision 1.15 1999/02/27 14:15:41 rommel
* changed NT service code
*
* Revision 1.14 1998/10/19 05:58:55 rommel
* minor changes/fixes
*
* Revision 1.13 1998/10/18 19:36:26 rommel
* make code time_t independent
*
* Revision 1.12 1998/10/18 17:42:34 rommel
* fix incompatibility with time_t being defined as double
*
* Revision 1.11 1998/10/01 10:06:10 rommel
* fixes from [email protected]
*
* Revision 1.10 1998/07/31 14:11:58 rommel
* fixed small bug
*
* Revision 1.9 1998/07/30 06:46:09 rommel
* added Win32 port
* added SNTP support
* fixed many bugs
* prepared for modem support
*
* Revision 1.8 1996/12/08 11:18:04 rommel
* added get_date prototype
*
* Revision 1.7 1996/12/08 11:15:07 rommel
* don't change time within 1 second margin
* from Ewen McNeill <[email protected]>
*
* Revision 1.6 1996/11/29 15:45:00 rommel
* changes (maxadj) from Ewen McNeill <[email protected]>
*
* Revision 1.5 1995/08/20 08:15:10 rommel
* updated for new emx socket library, IBM compiler
* fixed minor bugs
*
* Revision 1.4 1994/07/17 21:13:26 rommel
* fixed usage() display again
*
* Revision 1.3 1994/07/17 21:10:23 rommel
* fixed usage() display
*
* Revision 1.2 1994/07/17 21:07:57 rommel
* added daemon mode, continuously updating local time
*
* Revision 1.1 1994/07/17 20:45:55 rommel
* Initial revision
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "daytime.h"
#include "os.h"
struct options
{
int interval;
int dont;
int maxadj;
int maxwait;
int retries;
int rtryint;
long offset;
service serv;
protocol proto;
int portnum;
char host[64];
// char port[64]; // 2010-09-27 SHL
char logfile[256];
};
static struct options opts;
int get_modem_line(int port, char *node, char *buffer, int bytes)
{
/* get time over modem 0531512038 ? */
return -1;
}
int get_and_set_time(int port, char *node, service serv, protocol proto,
int dont, long offs, long maxadj, int maxwait)
{
int sock, bytes, size, rc;
struct sockaddr_in server;
struct hostent *host;
struct linger linger;
struct timeval tv;
fd_set fds;
time_t remote, here;
long newtime, ourtime;
char buffer[128];
char *request;
char *timestr;
char *p;
sntp *message;
switch (serv)
{
case DAYTIME:
case TIME:
bytes = 1;
buffer[0] = 0;
break;
case SNTP:
bytes = sizeof(sntp);
memset(buffer, 0, bytes);
message = (sntp *) buffer;
message->flags.version = 1;
message->flags.mode = 3;
break;
}
if (proto != MODEM)
{
server.sin_family = AF_INET;
server.sin_port = port;
if (isdigit(node[0]))
server.sin_addr.s_addr = inet_addr(node);
else
{
if ((host = gethostbyname(node)) == NULL)
return print_h_errno("gethostbyname()"), 2;
server.sin_addr = * (struct in_addr *) (host -> h_addr);
}
}
switch (proto)
{
case TCP:
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
return print_sock_errno("socket(tcp)"), 2;
if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0)
return print_sock_errno("connect()"), 2;
if (serv == SNTP)
{
if (send(sock, buffer, bytes, 0) < 0)
return print_sock_errno("send()"), 2;
}
FD_ZERO(&fds);
FD_SET(sock, &fds);
tv.tv_sec = maxwait;
tv.tv_usec = 0;
if ((rc = select(FD_SETSIZE, &fds, 0, 0, &tv)) < 0)
return print_sock_errno("select()"), 2;
else if (rc == 0)
{
soclose(sock);
lprintf("timeout waiting for answer from time server");
return 0;
}
if ((bytes = recv(sock, buffer, sizeof(buffer), 0)) <= 0)
return print_sock_errno("recv()"), 2;
soclose(sock);
break;
case UDP:
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 1)
return print_sock_errno("socket(udp)"), 2;
if (sendto(sock, buffer, bytes, 0,
(struct sockaddr *) &server, sizeof(server)) < 0)
return print_sock_errno("sendto()"), 2;
FD_ZERO(&fds);
FD_SET(sock, &fds);
tv.tv_sec = maxwait;
tv.tv_usec = 0;
if ((rc = select(FD_SETSIZE, &fds, 0, 0, &tv)) < 0)
return print_sock_errno("select()"), 2;
else if (rc == 0)
{
soclose(sock);
lprintf("timeout waiting for answer from time server");
return 0;
}
size = sizeof(server);
if ((bytes = recvfrom(sock, buffer, sizeof(buffer), 0,
(struct sockaddr *) &server, &size)) < 0)
print_sock_errno("recvfrom()");
soclose(sock);
break;
case MODEM:
if ((bytes = get_modem_line(port, node, buffer, sizeof(buffer))) == -1)
return perror("modem"), 2;
break;
}
switch (serv)
{
case DAYTIME:
buffer[bytes] = 0;
remote = get_date(buffer, NULL);
newtime = (long) remote;
if (newtime <= 0)
lprintf("unexpected response \"%s\"", buffer);
break;
case TIME:
if (bytes != sizeof(long))
return lprintf("invalid time value received"), 1;
newtime = * (long *) buffer;
newtime = ntohl(newtime);
/*
* The time service returns seconds since 1900/01/01 00:00:00 GMT, not
* since 1970 as assumed by Unix time_t values. We need to subtract
* 70 years' worth of seconds. Fortunately, RFC 868 tells us that the
* time value 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT.
*/
newtime -= 2208988800UL;
break;
case SNTP:
if (message->flags.version < 1 || 3 < message->flags.version)
return lprintf("incompatible protocol version on server"), 1;
if (message->flags.leap_ind == 3)
return lprintf("server not synchronized"), 1;
if (message->stratum == 0)
return lprintf("server not synchronized"), 1;
newtime = ntohl(message->transmit_time.integer);
if (newtime == 0)
return lprintf("server not synchronized"), 1;
/* same here */
newtime -= 2208988800UL;
break;
}
if (newtime <= 0)
return lprintf("invalid time (%ld)", newtime), 1;
newtime += offs;
remote = (time_t) newtime;
time(&here);
ourtime = (long) here;
if (maxadj)
if (newtime < (ourtime - maxadj) || (ourtime + maxadj) < newtime)
dont = 1; /* too far off: just report it */
if (abs(newtime - ourtime) <= 1)
dont = 1;
if (!dont && stime(&remote))
return lprintf("invalid time set request"), 1;
timestr = ctime(&remote);
timestr[strlen(timestr) - 1] = 0;
lprintf("time %s %s: (%+ld) %s", dont ? "at" : "set from",
node, newtime - ourtime, timestr);
fflush(stdout);
return 0;
}
int usage(void)
{
printf("\nSet time from remote host, %s"
"\n(C) 1997 Kai-Uwe Rommel\n", rcsrev);
printf("\nUsage: daytime [options] host\n"
"\nOptions:"
#ifdef WIN32
"\n -I install as a service"
"\n -U uninstall service\n"
#endif
"\n -l file write messages to logfile"
"\n -c int run continuously, perform action every 'int' seconds"
"\n -n do not set the local clock, display the remote time only"
"\n -o offs adjust the retrieved time by offs seconds before"
"\n setting the local clock"
"\n -m secs maximum number of seconds to adjust by"
"\n -w secs maximum number of seconds to wait for the time server's answer"
"\n -r rtry number of retries if a network connection error occurs (default 0)"
"\n -i secs number of seconds to wait between retries (default 15)"
"\n -d use the DAYTIME service (port 13)"
"\n -t use the TIME service (port 37, this is the default)"
"\n -s use the SNTP service (port 123), usually requires -u too"
"\n -u use the UDP protocol (default is to use TCP)"
"\n -p port use nonstandard port number\n"
#if 0
"\n -x use serial modem to access time server"
"\n (in this case, the 'host' is the phone number to dial"
"\n and 'port' is the number of the serial port)\n");
#else
"\n");
#endif
return 1;
}
int endless(void)
{
int rc = 0;
int rtry = 0;
for(;;)
{
rc = get_and_set_time(opts.portnum, opts.host, opts.serv, opts.proto,
opts.dont, opts.offset, opts.maxadj, opts.maxwait);
if (rc == 2 && opts.retries && rtry < opts.retries) {
sleep(opts.rtryint);
rtry++;
}
else
sleep(opts.interval);
}
return rc;
}
int main(int argc, char **argv)
{
int opt, rc, isrv = 0;
struct servent *port;
tzset();
if (sock_init())
return print_sock_errno("sock_init()"), 1;
#ifdef WIN32
#define SERVICE_NAME "daytime"
#define SERVICE_TITLE "Daytime Client"
if (started_as_service() && argc == 1)
{
restore_options(SERVICE_NAME, &opts, sizeof(opts));
logfile = opts.logfile;
run_as_service(SERVICE_NAME, endless);
return 0;
}
#endif
opts.serv = TIME;
opts.proto = TCP;
opts.portnum = -1;
opts.maxwait = 60;
opts.retries = 0;
opts.rtryint = 10;
while ((opt = getopt(argc, argv, "?l:IUndtsuo:p:c:m:w:xr:i:")) != EOF)
switch (opt)
{
case 'l':
strcpy(opts.logfile, optarg);
logfile = opts.logfile;
break;
case 'n':
opts.dont = 1;
break;
case 'd':
opts.serv = DAYTIME;
break;
case 't':
opts.serv = TIME;
break;
case 's':
opts.serv = SNTP;
break;
case 'u':
opts.proto = UDP;
break;
case 'o':
opts.offset = atol(optarg);
break;
case 'p':
opts.portnum = htons(atoi(optarg));
break;
case 'c':
opts.interval = atol(optarg);
break;
case 'm':
opts.maxadj = atol(optarg);
break;
case 'w':
opts.maxwait = atol(optarg);
break;
case 'r':
opts.retries = atol(optarg);
break;
case 'i':
opts.rtryint = atol(optarg);
break;
case 'x':
opts.proto = MODEM;
opts.serv = DAYTIME;
break;
#ifdef WIN32
case 'I':
isrv = 1;
break;
case 'U':
if ((rc = install_as_service(SERVICE_NAME, 0, 0)) != 0)
printf("deinstallation of service failed\n");
return rc;
#endif
default:
return usage();
}
if (optind == argc)
return usage();
else
strcpy(opts.host, argv[optind]);
if (opts.portnum == -1)
{
char *s_service = opts.serv == SNTP ? "ntp" :
opts.serv == DAYTIME ? "daytime" : "time";
char *s_proto = opts.proto == TCP ? "tcp" : "udp";
#ifndef OS2
if ((port = getservbyname(s_service, s_proto)) == NULL)
return print_sock_errno("getservbyname()"), 1;
#else
// 2010-11-05 SHL Some versions of TCP/IP stack appear to have broken getservbyname
port = getservbyname(s_service, s_proto);
if (port == NULL) {
// 2010-11-05 SHL Fallback and do our own search
endservent();
while ((port = getservent()) != NULL) {
if (strcmp(port->s_name, s_service) == 0 &&
strcmp(port->s_proto, s_proto) == 0) {
break;
}
}
endservent();
}
if (port == NULL)
return print_sock_errno("getservbyname()"), 1;
#endif
opts.portnum = port -> s_port;
}
#ifdef WIN32
if (isrv)
{
if ((rc = install_as_service(SERVICE_NAME, SERVICE_TITLE, 1)) != 0)
return printf("installation as service failed\n"), rc;
else
return save_options(SERVICE_NAME, &opts, sizeof(opts));
}
#endif
if (opts.interval == 0) {
int tries = 0;
for (rc = 2; rc == 2 && tries <= opts.retries; tries++) {
rc = get_and_set_time(opts.portnum, opts.host, opts.serv, opts.proto,
opts.dont, opts.offset, opts.maxadj, opts.maxwait);
if (rc == 2 && opts.retries)
sleep(opts.rtryint);
}
return rc;
}
else
return endless();
}
/* end of daytime.c */