AmendHub

Download

vkoskiv

/

MacNTP

/

MacNTP.h

 

(View History)

vkoskiv   Small NTP corrections Latest amendment: 13 on 2023-09-07

1 /*
2 * Copyright (c) 2023 Valtteri Koskivuori <vkoskiv@gmail.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef _MACNTP_H
18 #define _MACNTP_H
19
20 enum MacNTPError {
21 Success = 0,
22 MacTCPInitFailed,
23 InvalidURL,
24 BadNtpStructSize,
25 UDPCreateFailed,
26 UDPSendFailed,
27 UDPRcvFailed,
28 InvalidNTPResponse,
29 OriginTimestampMismatch,
30 DNSResolveFailed,
31 // Below only for SetSystemTime
32 ClockWriteFailed,
33 ClockReadFailed,
34 UDPRcvTimedOut,
35 PacketParamNull
36 };
37
38 struct ntp_ts {
39 unsigned long upper; /* Seconds since 1904-01-01 */
40 unsigned long lower; /* FIXME: These values seem to be wrong */
41 };
42
43 struct ntp_packet {
44 unsigned char li_vn_mode;
45 unsigned char stratum;
46 unsigned char poll;
47 char precision;
48
49 unsigned long root_delay;
50 unsigned long root_dispersion;
51 unsigned long reference_id;
52
53 struct ntp_ts reference_timestamp;
54 struct ntp_ts origin_timestamp;
55 struct ntp_ts receive_timestamp;
56 struct ntp_ts transmit_timestamp;
57 };
58
59 typedef long minutes_t;
60
61 enum MacNTPError MacNTPFetchTime(char *ntp_url, struct ntp_packet *packet, OSErr *supplemental, minutes_t utc_offset);
62
63 // For convenience, you can just pass ntp_packet from above to set the system time
64 // Optionally, you can care about the contents of ntp_packet and do things with it.
65 enum MacNTPError MacNTPSetSystemTime(struct ntp_packet *ntp, minutes_t utc_offset);
66
67 #endif