Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : #pragma once 3 : 4 : #include <sys/timex.h> 5 : 6 : #include "sd-bus.h" 7 : #include "sd-event.h" 8 : #include "sd-network.h" 9 : #include "sd-resolve.h" 10 : 11 : #include "list.h" 12 : #include "ratelimit.h" 13 : #include "time-util.h" 14 : #include "timesyncd-ntp-message.h" 15 : 16 : typedef struct Manager Manager; 17 : 18 : #include "timesyncd-server.h" 19 : 20 : /* 21 : * "A client MUST NOT under any conditions use a poll interval less 22 : * than 15 seconds." 23 : */ 24 : #define NTP_POLL_INTERVAL_MIN_USEC (32 * USEC_PER_SEC) 25 : #define NTP_POLL_INTERVAL_MAX_USEC (2048 * USEC_PER_SEC) 26 : 27 : struct Manager { 28 : sd_bus *bus; 29 : sd_event *event; 30 : sd_resolve *resolve; 31 : 32 : LIST_HEAD(ServerName, system_servers); 33 : LIST_HEAD(ServerName, link_servers); 34 : LIST_HEAD(ServerName, fallback_servers); 35 : 36 : bool have_fallbacks:1; 37 : 38 : RateLimit ratelimit; 39 : bool exhausted_servers; 40 : 41 : /* network */ 42 : sd_event_source *network_event_source; 43 : sd_network_monitor *network_monitor; 44 : 45 : /* peer */ 46 : sd_resolve_query *resolve_query; 47 : sd_event_source *event_receive; 48 : ServerName *current_server_name; 49 : ServerAddress *current_server_address; 50 : int server_socket; 51 : int missed_replies; 52 : uint64_t packet_count; 53 : sd_event_source *event_timeout; 54 : bool good; 55 : 56 : /* last sent packet */ 57 : struct timespec trans_time_mon; 58 : struct timespec trans_time; 59 : usec_t retry_interval; 60 : bool pending; 61 : 62 : /* poll timer */ 63 : sd_event_source *event_timer; 64 : usec_t poll_interval_usec; 65 : usec_t poll_interval_min_usec; 66 : usec_t poll_interval_max_usec; 67 : bool poll_resync; 68 : 69 : /* history data */ 70 : struct { 71 : double offset; 72 : double delay; 73 : } samples[8]; 74 : unsigned samples_idx; 75 : double samples_jitter; 76 : usec_t max_root_distance_usec; 77 : 78 : /* last change */ 79 : bool jumped; 80 : bool sync; 81 : int64_t drift_freq; 82 : 83 : /* watch for time changes */ 84 : sd_event_source *event_clock_watch; 85 : int clock_watch_fd; 86 : 87 : /* Retry connections */ 88 : sd_event_source *event_retry; 89 : 90 : /* RTC runs in local time, leave it alone */ 91 : bool rtc_local_time; 92 : 93 : /* NTP response */ 94 : struct ntp_msg ntpmsg; 95 : struct timespec origin_time, dest_time; 96 : bool spike; 97 : }; 98 : 99 : int manager_new(Manager **ret); 100 : void manager_free(Manager *m); 101 : 102 2 : DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free); 103 : 104 : void manager_set_server_name(Manager *m, ServerName *n); 105 : void manager_set_server_address(Manager *m, ServerAddress *a); 106 : void manager_flush_server_names(Manager *m, ServerType t); 107 : 108 : int manager_connect(Manager *m); 109 : void manager_disconnect(Manager *m);