chiark / gitweb /
timedated: update spike handling
[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 int main(int argc, char *argv[]) {
78         _cleanup_manager_free_ Manager *m = NULL;
79         const char *server;
80         int r;
81
82         if (argv[1])
83                 log_set_max_level(LOG_DEBUG);
84
85         r = manager_new(&m);
86         if (r < 0)
87                 goto out;
88
89         r = sntp_new(&m->sntp, m->event);
90         if (r < 0)
91                 goto out;
92
93         //server = "216.239.32.15";       /* time1.google.com */
94         //server = "192.53.103.108";      /* ntp1.ptb.de */
95         server = "130.102.2.123";       /* 0.au.pool.ntp.org */
96         r = sntp_server_connect(m->sntp, server);
97
98         if (r < 0)
99                 goto out;
100
101         r = sd_event_loop(m->event);
102         if (r < 0)
103                 goto out;
104
105 out:
106         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
107 }