vkoskiv
/MacNTP
/amendments
/7
Yeet calls to xmalloc() and xfree()
I only had two buffers I was allocating anyway, so I just stuck them
on the stack instead. No idea what the stack size is on a machine
this old, and if that changes in different contexts. We'll see.
vkoskiv made amendment 7 about 1 year ago
--- main.c Tue Aug 22 20:45:17 2023
+++ main.c Tue Aug 22 20:55:58 2023
@@ -4,7 +4,6 @@
#include "tcp.h"
#include "dnr.h"
-void ntp_req_free(void *);
void on_udp_receive(struct UDPiopb *iopb);
void dump_ntp_packet(struct ntp_packet *packet);
u_int8_t leap_information(struct ntp_packet pkt);
@@ -62,18 +61,6 @@ struct ntp_request {
struct ntp_packet payload;
};
-void ntp_req_free(void *reqptr) {
- unsigned long *addr = (unsigned long *)reqptr;
- void *ptr = (void *)*addr;
- struct ntp_request *req = (struct ntp_request *)ptr;
- if (req == NULL)
- return;
- _UDPRelease(&req->udp_iopb, req->udp_stream, nil, nil, false);
- //FIXME: These crash the program, no idea why
- //if (req->udp_buf != NULL) xfree(req->udp_buf);
- //xfree(ptr);
-}
-
#define HOST_URL "time.google.com";
void dump_ntp_packet(struct ntp_packet *packet) {
@@ -130,11 +117,14 @@ void on_udp_receive(struct UDPiopb *iopb) {
}
int main(int argc, char **argv) {
- struct ntp_request *req;
+ struct ntp_request req;
char ip_s[16];
short err;
UDPIOCompletionProc completion;
size_t i;
+ // I tried other values, but they all seem to make UDPCreate fail with
+ // the undocumented error code -23006
+ unsigned char udp_buf[3000];
completion = NewUDPIOCompletionProc(on_udp_receive);
//TODO: Might need to call util_init() here, it sets up UI alert things
@@ -148,54 +138,53 @@ int main(int argc, char **argv) {
return 1;
}
- req = xmalloczero(sizeof(struct ntp_request), "ntp_request");
- req->url = HOST_URL;
+ memset(&req, 0, sizeof(req));
+ req.url = HOST_URL;
- // 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);
+ memset(&udp_buf, 0, sizeof(udp_buf));
+ req.udp_buf_size = sizeof(udp_buf);
+ req.udp_buf = (unsigned char *)&udp_buf;
+ err = _UDPCreate(&req.udp_iopb, &req.udp_stream, (Ptr)req.udp_buf,
+ req.udp_buf_size, nil, nil, completion, false);
if (err) {
printf("UDPCreate failed: %d\n", err);
goto error;
}
- err = ResolveName(req->url, &req->host_ip);
+ err = ResolveName(req.url, &req.host_ip);
if (err) {
- printf("Couldn't resolve host %s (%d)\n", req->url, err);
+ printf("Couldn't resolve host %s (%d)\n", req.url, err);
goto error;
}
- long2ip(req->host_ip, (char *)&ip_s);
+ long2ip(req.host_ip, (char *)&ip_s);
// Set up NTP payload
- memset(&req->payload, 0, sizeof(req->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;
+ 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));
+ 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(&req.udp_wds, 0, sizeof(req.udp_wds));
+ req.udp_wds[0].ptr = (void *)&req.payload;
+ req.udp_wds[0].length = sizeof(req.payload);
- 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("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: Only works asynchronously
- err = _UDPRcv(&req->udp_iopb,
- req->udp_stream,
+ err = _UDPRcv(&req.udp_iopb,
+ req.udp_stream,
nil,
nil,
- req->host_ip,
+ req.host_ip,
123,
- (Ptr)req,
+ (Ptr)&req,
completion,
true);
@@ -212,14 +201,14 @@ int main(int argc, char **argv) {
// 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) {
+ if (((unsigned char *)&req.payload.origin_timestamp)[i] != 0x41) {
printf("origin_timestamp byte %lu != 0x41!\n", i);
}
}
- dump_ntp_packet(&req->payload);
+ dump_ntp_packet(&req.payload);
error:
- ntp_req_free(&req);
+ _UDPRelease(&req.udp_iopb, req.udp_stream, nil, nil, false);
return 0;
}