forked from snarfed/libmsntp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
93 lines (77 loc) · 2.07 KB
/
example.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
/**
* libmsntp
* http://snarfed.org/libmsntp
*
* Copyright 2005, Ryan Barrett <[email protected]>
*
* This is an example program that uses the libmsntp library.
*
* libmsntp is a shared library that provides SNTP client and server
* functionality. It allows you to embed an SNTP client and/or server in your
* own programs. It's implemented on top of msntp, a command-line SNTP tool
* from the HPCF group at Cambridge University.
*
* To use libmsntp in your own programs, include libmsntp.h and link with
* libmsntp.a. For more information on building libmsntp, see the README file.
*/
#include "libmsntp.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define USAGE "Usage: example [HOSTNAME] PORT\n" \
"If hostname is specified, connects to an SNTP server and prints the \n" \
"server's time and the difference from the local clock. Otherwise, runs \n" \
"an SNTP server on the specified port.\n\n"
struct tm to_struct_tm(struct timeval tv) {
return *gmtime((time_t *) &tv.tv_sec);
}
int serve(int port) {
int ret;
ret = msntp_start_server(port);
if (ret)
return ret;
printf("Listening for SNTP clients on port %d...", port);
while (1) {
ret = msntp_serve();
if (ret > 0 || ret < -1)
return ret;
}
msntp_stop_server();
}
int get(char *hostname, int port) {
struct timeval tv;
struct tm tm;
int err;
err = msntp_get_time(hostname, port, &tv);
if (err)
return err;
tm = to_struct_tm(tv);
printf(asctime(&tm));
return 0;
}
int main(int argc, char **argv) {
int port, ret;
char *portstr;
if (argc == 1 || argc > 3 ||
!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
fprintf(stderr, USAGE);
exit(1);
}
if (sscanf(argv[argc - 1], "%d", &port) != 1) {
fprintf(stderr, "Invalid port: %s\n", argv[argc - 1]);
exit(-1);
}
assert(port <= 65535);
if (argc == 2) {
ret = serve(port);
} else {
assert(argc == 3);
ret = get(argv[1], port);
}
if (ret > 0 || ret < -1) {
fprintf(stderr, msntp_strerror());
}
return ret;
}