AmendHub

Download:

vkoskiv

/

MacNTP

/

amendments

/

5

Dump ntp payload in main() instead of io handler & verify origin_ts

We now just store the received payload into the main request, or an
error otherwise. We can then check for errors and continue in main(),
which makes this a bit easier to read.
We're effectively just faking a synchronous _UDPRcv() here, I still
have no idea why doing it synchronously gives me an empty rcvBuff from
MacTCP

vkoskiv made amendment 5 about 1 year ago
--- main.c Tue Aug 22 19:04:33 2023 +++ main.c Tue Aug 22 19:57:28 2023 @@ -12,6 +12,7 @@ u_int8_t version_number(struct ntp_packet pkt); u_int8_t mode(struct ntp_packet pkt); static global_received = 0; +static global_err = 0; // We already have this from dnr.h above, but Think C ignores it. OSErr ResolveName(char *name, unsigned long *ipAddress); @@ -114,13 +115,14 @@ void dump_ntp_packet(struct ntp_packet *packet) { printf("mode: %u\n", mode(*received_ntp)); } -void on_udp_receive(struct UDPiopb *iopb) { - printf("on_udp_receive:\n"); +void on_udp_receive(struct UDPiopb *iopb) { + struct ntp_request *req; if (iopb->csParam.receive.rcvBuffLen != sizeof(struct ntp_packet)) { printf("Unexpected payload size received!\n"); - //printf("%u != 48\n", iopb->csParam.receive.rcvBuffLen); + global_err = 1; } else { - dump_ntp_packet((struct ntp_packet *)iopb->csParam.receive.rcvBuff); + req = (struct ntp_request *)iopb->csParam.receive.userDataPtr; + req->payload = *(struct ntp_packet *)iopb->csParam.receive.rcvBuff; } global_received = 1; @@ -131,6 +133,7 @@ int main(int argc, char **argv) { char ip_s[16]; short err; UDPIOCompletionProc completion; + size_t i; completion = NewUDPIOCompletionProc(on_udp_receive); //TODO: Might need to call util_init() here, it sets up UI alert things @@ -191,7 +194,7 @@ int main(int argc, char **argv) { nil, req->host_ip, 123, - nil, + (Ptr)req, completion, true); @@ -203,7 +206,17 @@ int main(int argc, char **argv) { // 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, goodbye\n"); + printf("global_received set\n"); + if (global_err) goto error; + + // 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) { + printf("origin_timestamp byte %lu != 0x41!\n", i); + } + } + + dump_ntp_packet(&req->payload); error: _UDPRelease(&req->udp_iopb, req->udp_stream, nil, nil, false);