You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
2.4 KiB
53 lines
2.4 KiB
/* ntp_time — асинхронные UDP NTP-запросы с DNS и периодическим повтором в uasync-потоке ядра. |
|
* Не меняет часы ОС: хранит local_time - ntp_time в instance->ntp.offset_us. |
|
* get_us/get_seconds возвращают скорректированное wall-clock время; до синхронизации — локальное. |
|
* init читает config и планирует первый цикл, destroy отменяет DNS/сокет/таймеры. |
|
* synced означает наличие коррекции (в том числе полученной от пира), reachable — результат интернет-NTP. */ |
|
#ifndef NTP_TIME_H |
|
#define NTP_TIME_H |
|
|
|
#include <stdint.h> |
|
#include <time.h> |
|
#include "../lib/platform_compat.h" |
|
|
|
#ifdef __cplusplus |
|
extern "C" { |
|
#endif |
|
|
|
struct UTUN_INSTANCE; |
|
|
|
#define NTP_DEFAULT_INTERVAL_SEC 3600 |
|
#define NTP_FIRST_SYNC_DELAY_SEC 0 |
|
|
|
struct NTP_TIME { |
|
int enabled; |
|
int synced; |
|
int reachable; // доступность интернет-NTP по циклам (1=есть интернет, 0=нет); для классификации сокетов |
|
int64_t offset_us; // local_time - ntp_time (positive = local clock ahead) |
|
uint64_t last_sync_tb; // monotonic time of last successful sync (0.1ms units) |
|
void* timer; // uasync timer handle |
|
char **servers; // dynamic array, allocated at init |
|
int server_count; |
|
int server_current; |
|
int resync_interval_sec; |
|
struct sockaddr_in test_addr; // test mode: direct address when sin_port != 0 |
|
struct sockaddr_in test_dns; // test mode: DNS server for hostname resolution (sin_port != 0) |
|
uint16_t test_port; // test mode: NTP query port override (0 = 123) |
|
void* sync; // активный контекст синхронизации (struct ntp_sync*, opaque) |
|
}; |
|
|
|
int ntp_time_init(struct UTUN_INSTANCE* instance); |
|
void ntp_time_destroy(struct UTUN_INSTANCE* instance); |
|
|
|
void ntp_time_set_test_addr(struct NTP_TIME* ntp, const char* ip, uint16_t port); |
|
void ntp_time_set_test_dns(struct NTP_TIME* ntp, const char* ip, uint16_t dns_port, uint16_t ntp_port); |
|
|
|
int64_t ntp_time_get_us(struct UTUN_INSTANCE* instance); |
|
time_t ntp_time_get_seconds(struct UTUN_INSTANCE* instance); |
|
int ntp_time_is_synced(struct UTUN_INSTANCE* instance); |
|
|
|
#ifdef __cplusplus |
|
} |
|
#endif |
|
|
|
#endif // NTP_TIME_H
|
|
|