AmendHub

Download:

vkoskiv

/

MacNTP

/

amendments

/

10

Gate debug prints behind #define and add more prints

These prints are by far the slowest part of the running program, but
quite useful for debugging.
There is now a SLOW_DEBUG_PRINTF define that can be commented out to
disable these prints. In that case, no window opens, MacNTP just does
its thing and exits.
I also added printouts to show the old and new system time, if it was
updated.

vkoskiv made amendment 10 about 1 year ago
--- main.c Tue Aug 22 23:26:24 2023 +++ main.c Thu Aug 24 16:56:44 2023 @@ -1,10 +1,13 @@ #include <stdio.h> #include <string.h> #include <OSUtils.h> +#include <Packages.h> #include "util.h" #include "tcp.h" #include "dnr.h" +#define SLOW_DEBUG_PRINTF + void on_udp_receive(struct UDPiopb *iopb); void dump_ntp_packet(struct ntp_packet *packet); u_int8_t leap_information(struct ntp_packet pkt); @@ -61,20 +64,45 @@ void set_system_time(struct ntp_ts ts) { 4 * 365 * 24 * 60 * 60 = 126144000 */ //FIXME: Find out how timezone offsets are done on Sys6, if at all. + //FIXME: We seem to be behind about 10 seconds after update. OSErr err; long mac_seconds; + long old_time; + Str255 date_string; + Str255 time_string; short utc_offset = 3; long ntp_seconds = ts.upper; ntp_seconds += utc_offset * 60 * 60; mac_seconds = ntp_seconds - 126144000; + GetDateTime(&old_time); err = SetDateTime(mac_seconds); if (err) { if (err == -86) { // clkWrErr + #ifdef SLOW_DEBUG_PRINTF printf("clkWrErr encountered\n"); + #endif + return; } else if (err == -85) { // clkRdErr + #ifdef SLOW_DEBUG_PRINTF printf("clkRdErr encountered\n"); + #endif + return; } } + #ifdef SLOW_DEBUG_PRINTF + printf("System clock set successfully!\n"); + IUDateString(old_time, abbrevDate, date_string); + IUTimeString(old_time, 1, time_string); + PtoCstr(date_string); + PtoCstr(time_string); + printf("Old: %s %s\n", date_string, time_string); + + IUDateString(mac_seconds, abbrevDate, date_string); + IUTimeString(mac_seconds, 1, time_string); + PtoCstr(date_string); + PtoCstr(time_string); + printf("New: %s %s\n", date_string, time_string); + #endif } struct ntp_request { @@ -89,7 +117,8 @@ struct ntp_request { struct ntp_packet payload; }; -#define HOST_URL "time.google.com"; +//#define HOST_URL "time.google.com"; +#define HOST_URL "time.apple.com"; void dump_ntp_packet(struct ntp_packet *packet) { struct ntp_packet *received_ntp; @@ -103,38 +132,38 @@ void dump_ntp_packet(struct ntp_packet *packet) { ref_id[3] = (t & 0x000000FF) >> 0; ref_id[4] = 0; - printf("struct ntp_packet received = {\n"); - 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: %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); + printf("li_vn_mode: %u\n", received_ntp->li_vn_mode); + printf("stratum: %u\n", received_ntp->stratum); + printf("poll: %u\n", received_ntp->poll); + printf("precision: %i\n", received_ntp->precision); + printf("root_delay: %lu\n", received_ntp->root_delay); + printf("root_dispersion: %lu\n", received_ntp->root_dispersion); + printf("reference_id: %s\n", ref_id); - printf("\treference_ts(hi): %lu\n", received_ntp->reference_timestamp.upper); - printf("\treference_ts(lo): %lu\n", received_ntp->reference_timestamp.lower); + printf("reference_ts(hi): %lu\n", received_ntp->reference_timestamp.upper); + printf("reference_ts(lo): %lu\n", received_ntp->reference_timestamp.lower); - printf("\torigin_ts(hi): %lu\n", received_ntp->origin_timestamp.upper); - printf("\torigin_ts(lo): %lu\n", received_ntp->origin_timestamp.lower); + printf("origin_ts(hi): %lu\n", received_ntp->origin_timestamp.upper); + printf("origin_ts(lo): %lu\n", received_ntp->origin_timestamp.lower); - printf("\treceive_ts(hi): %lu\n", received_ntp->receive_timestamp.upper); - printf("\treceive_ts(lo): %lu\n", received_ntp->receive_timestamp.lower); + printf("receive_ts(hi): %lu\n", received_ntp->receive_timestamp.upper); + printf("receive_ts(lo): %lu\n", received_ntp->receive_timestamp.lower); - printf("\ttransmit_ts(hi): %lu\n", received_ntp->transmit_timestamp.upper); - printf("\ttransmit_ts(lo): %lu\n", received_ntp->transmit_timestamp.lower); + printf("transmit_ts(hi): %lu\n", received_ntp->transmit_timestamp.upper); + printf("transmit_ts(lo): %lu\n", received_ntp->transmit_timestamp.lower); - printf("}\nWoo, success(?)\n"); - - printf("leap_information: %u\n", leap_information(*received_ntp)); - printf("version_number: %u\n", version_number(*received_ntp)); - printf("mode: %u\n", mode(*received_ntp)); + printf("leap_information: %u, version_number: %u, mode: %u\n", + leap_information(*received_ntp), + version_number(*received_ntp), + mode(*received_ntp)); } void on_udp_receive(struct UDPiopb *iopb) { struct ntp_request *req; if (iopb->csParam.receive.rcvBuffLen != sizeof(struct ntp_packet)) { + #ifdef SLOW_DEBUG_PRINTF printf("Unexpected payload size received!\n"); + #endif global_err = 1; } else { req = (struct ntp_request *)iopb->csParam.receive.userDataPtr; @@ -158,11 +187,15 @@ int main(int argc, char **argv) { //TODO: Might need to call util_init() here, it sets up UI alert things MaxApplZone(); if (sizeof(struct ntp_packet) != 48lu) { + #ifdef SLOW_DEBUG_PRINTF printf("sizeof(struct ntp_packet) != 48, exiting\n"); + #endif return 1; } if (_TCPInit() != 0) { + #ifdef SLOW_DEBUG_PRINTF printf("Failed to init MacTCP, exiting.\n"); + #endif return 1; } @@ -175,12 +208,16 @@ int main(int argc, char **argv) { err = _UDPCreate(&req.udp_iopb, &req.udp_stream, (Ptr)req.udp_buf, req.udp_buf_size, nil, nil, completion, false); if (err) { + #ifdef SLOW_DEBUG_PRINTF printf("UDPCreate failed: %d\n", err); + #endif goto error; } err = ResolveName(req.url, &req.host_ip); if (err) { + #ifdef SLOW_DEBUG_PRINTF printf("Couldn't resolve host %s (%d)\n", req.url, err); + #endif goto error; } @@ -200,11 +237,12 @@ int main(int argc, char **argv) { req.udp_wds[0].ptr = (void *)&req.payload; req.udp_wds[0].length = sizeof(req.payload); + #ifdef SLOW_DEBUG_PRINTF printf("Sending NTP query to %s (%s)...\n", ip_s, req.url); + #endif err = _UDPSend(&req.udp_iopb, req.udp_stream, req.udp_wds, req.host_ip, 123, nil, nil, false); - printf("Waiting for response...\n"); //FIXME: Only works asynchronously err = _UDPRcv(&req.udp_iopb, req.udp_stream, @@ -217,26 +255,39 @@ int main(int argc, char **argv) { true); if (err) { + #ifdef SLOW_DEBUG_PRINTF printf("UDPRcv failed: %d\n", err); + #endif goto error; } // Wait for event handler to fire (not sure if needed) //FIXME: This needs a timeout, or we might be able to set it for udp while (!global_received) {} - printf("global_received set\n"); if (global_err) goto error; + #ifdef SLOW_DEBUG_PRINTF + printf("Got response:\n"); + #endif + // Verify received origin_ts is all 0x41s for (i = 0; i < sizeof(struct ntp_ts); ++i) { if (((unsigned char *)&req.payload.origin_timestamp)[i] != 0x41) { + #ifdef SLOW_DEBUG_PRINTF printf("origin_timestamp byte %lu != 0x41!\n", i); + #endif + goto error; } } +#ifdef SLOW_DEBUG_PRINTF dump_ntp_packet(&req.payload); +#endif set_system_time(req.payload.reference_timestamp); +#ifdef SLOW_DEBUG_PRINTF + printf("Goodbye.\n"); +#endif error: _UDPRelease(&req.udp_iopb, req.udp_stream, nil, nil, false); return 0;