AmendHub

Download:

vkoskiv

/

MacNTP

/

amendments

/

4

More tidying up around main

- Removed a bunch of unneeded variables
- Add a few more comments to remind later me

vkoskiv made amendment 4 about 1 year ago
--- main.c Tue Aug 22 18:04:21 2023 +++ main.c Tue Aug 22 19:04:33 2023 @@ -57,7 +57,6 @@ struct ntp_request { unsigned char *udp_buf; unsigned short udp_buf_size; wdsEntry udp_wds[2]; - //TCPStatusPB tcp_status_pb; struct ntp_packet payload; }; @@ -130,12 +129,7 @@ void on_udp_receive(struct UDPiopb *iopb) { int main(int argc, char **argv) { struct ntp_request *req; char ip_s[16]; - ip_addr local_ip; - tcp_port local_port; short err; - size_t len; - char rcvbuf[64]; - unsigned short rcvbuflen; UDPIOCompletionProc completion; completion = NewUDPIOCompletionProc(on_udp_receive); @@ -170,31 +164,36 @@ int main(int argc, char **argv) { } long2ip(req->host_ip, (char *)&ip_s); - printf("%s => %s\n", req->url, ip_s); // Set up NTP payload memset(&req->payload, 0, sizeof(req->payload)); + // Set the mode to tell the server we're a client req->payload.li_vn_mode = (4 << 3) | 3; // TODO: This should be fine, our time is always in 1904 anyway // Just verify when receiving memset(&req->payload.transmit_timestamp, 0x41, sizeof(req->payload.transmit_timestamp)); + // And prepare for sending memset(&req->udp_wds, 0, sizeof(req->udp_wds)); req->udp_wds[0].ptr = (void *)&req->payload; req->udp_wds[0].length = sizeof(req->payload); - memset(&rcvbuf, 0, sizeof(rcvbuf)); - rcvbuflen = 0; - - printf("Sending NTP query to %s...\n", ip_s); + printf("Sending NTP query to %s (%s)...\n", ip_s, req->url); err = _UDPSend(&req->udp_iopb, req->udp_stream, req->udp_wds, req->host_ip, 123, nil, nil, false); printf("Waiting for response...\n"); - //FIXME: Probably broken, I wrote UDPRcv - err = _UDPRcv(&req->udp_iopb, req->udp_stream, (Ptr)&rcvbuf, - &rcvbuflen, req->host_ip, 123, nil, completion, true); + //FIXME: Only works asynchronously + err = _UDPRcv(&req->udp_iopb, + req->udp_stream, + nil, + nil, + req->host_ip, + 123, + nil, + completion, + true); if (err) { printf("UDPRcv failed: %d\n", err); @@ -202,11 +201,13 @@ 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"); error: _UDPRelease(&req->udp_iopb, req->udp_stream, nil, nil, false); - //ntp_req_free(&req); + //ntp_req_free(req); + //xfree(req); return 0; }