-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.cpp
99 lines (85 loc) · 2.9 KB
/
time.cpp
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
/* -*- mode: c; indent-width: 8; -*- */
/*
* time client - RFC 868
* windows inetd service.
*
* Copyright (c) 2020 - 2022, Adam Young.
* All rights reserved.
*
* The applications are free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, version 3.
*
* Redistributions of source code must retain the above copyright
* notice, and must be distributed with the license document above.
*
* Redistributions in binary form must reproduce the above copyright
* notice, and must include the license document above in
* the documentation and/or other materials provided with the
* distribution.
*
* This project is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* license for more details.
* ==end==
*/
#if !defined(_WINSOCK_DEPRECATED_NO_WARNINGS)
#define _WINSOCK_DEPRECATED_NO_WARNINGS /* gethostbyname */
#endif
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#if !defined(WINDOWS_MEAN_AND_LEAN)
#define WINDOWS_MEAN_AND_LEAN
#include <Winsock2.h>
#include <ws2tcpip.h> // getaddrinfo
#include <Windows.h>
#endif
#pragma comment(lib, "Ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* for fgets */
#include <time.h>
#include <errno.h>
#include <io.h>
#include "client.h"
#define OFFSET ((DWORD)25567 * 24*60*60) /* 1/1/1970 */
int
main(int argc, char **argv)
{
int port = 37; /* default port */
const char *host;
int n;
if (argc < 2 || argc > 3) {
fprintf(stderr, "usage: %s <host> [<port>]\n", argv[0]);
exit(0);
}
Client client;
host = argv[1];
if (3 == argc) {
port = atoi(argv[2]);
} else {
if (0 == (port = client.getservport("time"))) {
fputs("unknown service: time tcp\n", stderr);
exit(1);
}
}
if (! client.connect(host, port)) {
printf("connection failure: %u\n", (unsigned)::WSAGetLastError());
} else {
DWORD u32time;
n = client.read((void *)&u32time, sizeof(u32time));
if (n == sizeof(u32time)) {
u32time = ntohl(u32time);
if (u32time >= OFFSET) {
const time_t machtime = (u32time - OFFSET);
printf("machine time: %s", ctime(&machtime));
} else {
printf("machine time: <1970 or >7/Feb/2037");
}
}
client.close();
}
}
/*end*/