From 68400a25b7bcd817ff2a7c1f80fc7e76c26a606c Mon Sep 17 00:00:00 2001 From: Piper Pentagram Date: Tue, 8 Jul 2025 15:36:27 -0700 Subject: [PATCH] read the packets into packet structs --- include/dns_packet_buffer.h | 7 +++++-- include/dns_request.h | 5 ++++- include/udp_server.h | 2 +- src/dns_packet_buffer.c | 32 ++++++++++++++++++++++++++++++++ src/dns_request.c | 11 +++++++++++ 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/include/dns_packet_buffer.h b/include/dns_packet_buffer.h index f23287d..4539e02 100644 --- a/include/dns_packet_buffer.h +++ b/include/dns_packet_buffer.h @@ -1,13 +1,16 @@ #pragma once #include -#define PACKET_BUFFER_SIZE 512 +#define PB_SIZE 512 +#define PB_OUT_OF_BOUNDS 9999; struct DNSPacketBuffer { - char buf[PACKET_BUFFER_SIZE]; + char buf[PB_SIZE]; size_t pos; }; struct DNSPacketBuffer *new_dns_packet_buffer(); struct DNSPacketHeader *dns_header(struct DNSPacketBuffer); + +char pb_read_byte(struct DNSPacketBuffer *); diff --git a/include/dns_request.h b/include/dns_request.h index aeecf54..e5705ff 100644 --- a/include/dns_request.h +++ b/include/dns_request.h @@ -1,7 +1,10 @@ #pragma once #include "dns_packets.h" +#include "dns_packet_buffer.h" struct DNSRequest { - struct DNSPacketHeader header; + struct DNSPacketHeader *header; }; + +struct DNSRequest *new_dns_request(struct DNSPacketBuffer *pb); diff --git a/include/udp_server.h b/include/udp_server.h index cd63cfc..40276be 100644 --- a/include/udp_server.h +++ b/include/udp_server.h @@ -1,5 +1,5 @@ #pragma once -#define UDP_PORT 53 +#define UDP_PORT 1053 int start_server(); diff --git a/src/dns_packet_buffer.c b/src/dns_packet_buffer.c index 55fe986..7d7cd69 100644 --- a/src/dns_packet_buffer.c +++ b/src/dns_packet_buffer.c @@ -1,4 +1,5 @@ #include "dns_packet_buffer.h" +#include #include struct DNSPacketBuffer *new_dns_packet_buffer() @@ -12,3 +13,34 @@ struct DNSPacketHeader *dns_header(struct DNSPacketBuffer) { } + + +// read a single byte and move the cursor one byte forward +char pb_read_byte(struct DNSPacketBuffer *pb) +{ + char b = pb->buf[pb->pos]; + pb->pos++; + + if (pb->pos >= PB_SIZE) { + printf( + "WARN: pb_read_byte: advanced packetbuffer cursor pos to %lu, which is greater than the max %d", + pb->pos, + PB_SIZE + ); + } + + return b; +} + +char pb_get_byte(struct DNSPacketBuffer *pb) +{ + if (pb->pos >= PB_SIZE) { + printf( + "WARN: pb_get_byte: accessing packetbuffer cursor pos %lu, which is greater than the max %d", + pb->pos, + PB_SIZE + ); + } + + return pb->buf[pb->pos]; +} diff --git a/src/dns_request.c b/src/dns_request.c index 243502c..b960d9d 100644 --- a/src/dns_request.c +++ b/src/dns_request.c @@ -1,3 +1,14 @@ +#include + #include "dns_request.h" + +struct DNSRequest *new_dns_request(struct DNSPacketBuffer *pb) +{ + struct DNSRequest *req; + req = malloc(sizeof(struct DNSRequest)); + + req->header = (struct DNSPacketHeader *) pb->buf; + return req; +}