read the packets into packet structs
This commit is contained in:
parent
f1e00117d9
commit
68400a25b7
5 changed files with 53 additions and 4 deletions
|
|
@ -1,13 +1,16 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#define PACKET_BUFFER_SIZE 512
|
#define PB_SIZE 512
|
||||||
|
#define PB_OUT_OF_BOUNDS 9999;
|
||||||
|
|
||||||
struct DNSPacketBuffer {
|
struct DNSPacketBuffer {
|
||||||
char buf[PACKET_BUFFER_SIZE];
|
char buf[PB_SIZE];
|
||||||
size_t pos;
|
size_t pos;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DNSPacketBuffer *new_dns_packet_buffer();
|
struct DNSPacketBuffer *new_dns_packet_buffer();
|
||||||
|
|
||||||
struct DNSPacketHeader *dns_header(struct DNSPacketBuffer);
|
struct DNSPacketHeader *dns_header(struct DNSPacketBuffer);
|
||||||
|
|
||||||
|
char pb_read_byte(struct DNSPacketBuffer *);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "dns_packets.h"
|
#include "dns_packets.h"
|
||||||
|
#include "dns_packet_buffer.h"
|
||||||
|
|
||||||
struct DNSRequest {
|
struct DNSRequest {
|
||||||
struct DNSPacketHeader header;
|
struct DNSPacketHeader *header;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct DNSRequest *new_dns_request(struct DNSPacketBuffer *pb);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define UDP_PORT 53
|
#define UDP_PORT 1053
|
||||||
|
|
||||||
int start_server();
|
int start_server();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include "dns_packet_buffer.h"
|
#include "dns_packet_buffer.h"
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct DNSPacketBuffer *new_dns_packet_buffer()
|
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];
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,14 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "dns_request.h"
|
#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;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue