chiark / gitweb /
1e39a6acf545faeb7941469aace8260f025c90e2
[elogind.git] / src / timedate / test-timedate-sntp.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Kay Sievers
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <time.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <sys/timex.h>
32 #include <sys/socket.h>
33
34 #include "sd-event.h"
35 #include "util.h"
36 #include "log.h"
37 #include "timedate-sntp.h"
38
39 typedef struct Manager Manager;
40 struct Manager {
41         sd_event *event;
42         SNTPContext *sntp;
43 };
44
45 static void manager_free(Manager *m);
46 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
47 #define _cleanup_manager_free_ _cleanup_(manager_freep)
48
49 static int manager_new(Manager **ret) {
50         _cleanup_manager_free_ Manager *m = NULL;
51         int r;
52
53         m = new0(Manager, 1);
54         if (!m)
55                 return -ENOMEM;
56
57         r = sd_event_default(&m->event);
58         if (r < 0)
59                 return r;
60
61         *ret = m;
62         m = NULL;
63
64         return 0;
65 }
66
67 static void manager_free(Manager *m) {
68
69         if (!m)
70                 return;
71
72         m->sntp = sntp_unref(m->sntp);
73         sd_event_unref(m->event);
74         free(m);
75 }
76
77 static void manager_report(usec_t poll_usec, double offset, double delay, double jitter, bool spike) {
78         log_info("%4llu %+10f %10f %10f%s",
79                  poll_usec / USEC_PER_SEC, offset, delay, jitter, spike ? " spike" : "");
80 }
81
82 int main(int argc, char *argv[]) {
83         _cleanup_manager_free_ Manager *m = NULL;
84         const char *server;
85         int r;
86
87         r = manager_new(&m);
88         if (r < 0)
89                 goto out;
90
91         r = sntp_new(&m->sntp, m->event);
92         if (r < 0)
93                 goto out;
94
95         if (argv[1])
96                 log_set_max_level(LOG_DEBUG);
97         else
98                 sntp_report_register(m->sntp, manager_report);
99
100         //server = "216.239.32.15";       /* time1.google.com */
101         //server = "192.53.103.108";      /* ntp1.ptb.de */
102         server = "27.54.95.11";         /* au.pool.ntp.org */
103         r = sntp_server_connect(m->sntp, server);
104
105         if (r < 0)
106                 goto out;
107
108         r = sd_event_loop(m->event);
109         if (r < 0)
110                 goto out;
111
112 out:
113         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
114 }