vkoskiv
/MacNTP
/amendments
/3
Few fixes:
- Clean up comments
- Switch precision to signed integer type
- Check ntp_packet size before doing TCPInit()
- Comment udp_buf_size weirdness
vkoskiv made amendment 3 about 1 year ago
--- main.c Tue Aug 22 00:00:40 2023
+++ main.c Tue Aug 22 18:04:21 2023
@@ -17,16 +17,15 @@ static global_received = 0;
OSErr ResolveName(char *name, unsigned long *ipAddress);
struct ntp_ts {
- /* FIXME: These may be swapped*/
- u_int32_t upper; /* Maybe seconds since 1900-01-01 */
- u_int32_t lower; /* Maybe fractional bits? Probably not needed */
+ u_int32_t upper; /* Seconds since 1900-01-01 */
+ u_int32_t lower; /* FIXME: These values seem to be wrong */
};
struct ntp_packet {
u_int8_t li_vn_mode;
u_int8_t stratum;
u_int8_t poll;
- u_int8_t precision;
+ int8_t precision;
u_int32_t root_delay;
u_int32_t root_dispersion;
@@ -92,7 +91,7 @@ void dump_ntp_packet(struct ntp_packet *packet) {
printf("\tli_vn_mode: %u\n", received_ntp->li_vn_mode);
printf("\tstratum: %u\n", received_ntp->stratum);
printf("\tpoll: %u\n", received_ntp->poll);
- printf("\tprecision: %u\n", received_ntp->precision);
+ printf("\tprecision: %i\n", received_ntp->precision);
printf("\troot_delay: %lu\n", received_ntp->root_delay);
printf("\troot_dispersion: %lu\n", received_ntp->root_dispersion);
printf("\treference_id: %s\n", ref_id);
@@ -142,18 +141,21 @@ int main(int argc, char **argv) {
//TODO: Might need to call util_init() here, it sets up UI alert things
MaxApplZone();
- if (_TCPInit() != 0) {
- printf("Failed to init MacTCP, exiting.\n");
- return 1;
- }
if (sizeof(struct ntp_packet) != 48lu) {
printf("sizeof(struct ntp_packet) != 48, exiting\n");
return 1;
}
+ if (_TCPInit() != 0) {
+ printf("Failed to init MacTCP, exiting.\n");
+ return 1;
+ }
req = xmalloczero(sizeof(struct ntp_request), "ntp_request");
req->url = HOST_URL;
- req->udp_buf_size = (2 * 1500);
+
+ // I tried other values, but they all seem to make UDPCreate fail with
+ // the undocumented error code -23006
+ req->udp_buf_size = 2 * 1500;
req->udp_buf = xmalloc(req->udp_buf_size, "ntp_req buf");
err = _UDPCreate(&req->udp_iopb, &req->udp_stream, (Ptr)req->udp_buf,
req->udp_buf_size, nil, nil, completion, false);