This commit is contained in:
Murphy 2023-04-11 09:27:16 -04:00
parent 2a75d25632
commit b22811eca2
10 changed files with 75 additions and 16 deletions

View file

@ -349,6 +349,11 @@ static bool config_read_caa_record(char* data, CAARecord* record) {
return true;
}
static bool config_read_cmd_record(char* data, CMDRecord* record) {
record->command = data;
return true;
}
static bool config_read_record_data(char* data, Record* record) {
switch (record->type) {
case UNKOWN:
@ -374,6 +379,8 @@ static bool config_read_record_data(char* data, Record* record) {
return config_read_srv_record(data, &record->data.srv);
case CAA:
return config_read_caa_record(data, &record->data.caa);
case CMD:
return config_read_cmd_record(data, &record->data.cmd);
}
return false;
}

View file

@ -43,6 +43,8 @@ ResultCode rescode_from_id(uint8_t id) {
}
}
#define MAX(var, max) var = var > max ? max : var;
void read_header(PacketBuffer* buffer, Header* header) {
// memset(header, 0, sizeof(Header));
header->id = buffer_read_short(buffer);

View file

@ -1,34 +1,49 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "packet.h"
#include "buffer.h"
#include "header.h"
#include "question.h"
#include "record.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void read_packet(PacketBuffer* buffer, Packet* packet) {
read_header(buffer, &packet->header);
packet->questions = malloc(sizeof(Question) * packet->header.questions);
for(uint16_t i = 0; i < packet->header.questions; i++) {
read_question(buffer, &packet->questions[i]);
if (!read_question(buffer, &packet->questions[i])) {
i--;
packet->header.questions--;
}
}
packet->answers = malloc(sizeof(Record) * packet->header.answers);
for(uint16_t i = 0; i < packet->header.answers; i++) {
read_record(buffer, &packet->answers[i]);
if (!read_record(buffer, &packet->answers[i])) {
i--;
packet->header.answers--;
}
}
packet->authorities = malloc(sizeof(Record) * packet->header.authoritative_entries);
for(uint16_t i = 0; i < packet->header.authoritative_entries; i++) {
read_record(buffer, &packet->authorities[i]);
if (!read_record(buffer, &packet->authorities[i])) {
i--;
packet->header.authoritative_entries--;
}
}
packet->resources = malloc(sizeof(Record) * packet->header.resource_entries);
for(uint16_t i = 0; i < packet->header.resource_entries; i++) {
read_record(buffer, &packet->resources[i]);
if (!read_record(buffer, &packet->resources[i])) {
i--;
packet->header.resource_entries--;
}
}
}

View file

@ -6,6 +6,8 @@
#include "record.h"
#include "../server/addr.h"
#include <stdbool.h>
typedef struct {
Header header;
Question* questions;

View file

@ -1,21 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "question.h"
#include "buffer.h"
#include "record.h"
#include "../io/log.h"
void read_question(PacketBuffer* buffer, Question* question) {
bool read_question(PacketBuffer* buffer, Question* question) {
buffer_read_qname(buffer, &question->domain);
uint16_t qtype_num = buffer_read_short(buffer);
record_from_id(qtype_num, &question->qtype);
question->cls = buffer_read_short(buffer);
if (question->qtype == UNKOWN) {
free(question->domain);
return false;
}
INIT_LOG_BUFFER(log)
LOGONLY(print_question(question, log);)
TRACE("Reading question: %s", log);
TRACE("Reading question: %s", log);
return true;
}
void write_question(PacketBuffer* buffer, Question* question) {
@ -85,6 +92,8 @@ void print_question(Question* question, char* buffer) {
case CAA:
APPEND(buffer, "CAA ");
break;
case CMD:
APPEND(buffer, "CMD ");
break;
}
APPEND(buffer, "%.*s",

View file

@ -9,7 +9,7 @@ typedef struct {
uint16_t cls;
} Question;
void read_question(PacketBuffer* buffer, Question* question);
bool read_question(PacketBuffer* buffer, Question* question);
void write_question(PacketBuffer* buffer, Question* question);
void free_question(Question* question);
void print_question(Question* question, char* buffer);

View file

@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -28,6 +29,8 @@ uint16_t record_to_id(RecordType type) {
return 33;
case CAA:
return 257;
case CMD:
return 1000;
default:
return 0;
}
@ -65,6 +68,9 @@ void record_from_id(uint16_t i, RecordType* type) {
case 257:
*type = CAA;
break;
case 1000:
*type = CMD;
break;
default:
*type = UNKOWN;
}
@ -172,7 +178,7 @@ static void read_caa_record(PacketBuffer* buffer, Record* record, int header_pos
record->data.caa = data;
}
void read_record(PacketBuffer* buffer, Record* record) {
bool read_record(PacketBuffer* buffer, Record* record) {
buffer_read_qname(buffer, &record->domain);
uint16_t qtype_num = buffer_read_short(buffer);
@ -217,12 +223,14 @@ void read_record(PacketBuffer* buffer, Record* record) {
break;
default:
buffer_step(buffer, record->len);
return;
free(record->domain);
return false;
}
INIT_LOG_BUFFER(log)
LOGONLY(print_record(record, log);)
TRACE("Reading record: %s", log);
return true;
}
static void write_a_record(PacketBuffer* buffer, Record* record) {
@ -536,5 +544,10 @@ void print_record(Record* record, char* buffer) {
record->data.caa.value + 1
);
break;
case CMD:
APPEND(buffer, "CMD (%s)",
record->data.cmd.command
);
break;
}
}

View file

@ -3,6 +3,7 @@
#include "buffer.h"
#include <string.h>
#include <stdbool.h>
typedef enum {
UNKOWN,
@ -15,7 +16,8 @@ typedef enum {
TXT, // 16
AAAA, // 28
SRV, // 33
CAA // 257
CAA, // 257
CMD // 1000
} RecordType;
uint16_t record_to_id(RecordType type);
@ -75,6 +77,10 @@ typedef struct {
uint8_t* value;
} CAARecord;
typedef struct {
char* command;
} CMDRecord;
typedef struct {
uint32_t ttl;
uint16_t cls;
@ -92,10 +98,11 @@ typedef struct {
AAAARecord aaaa;
SRVRecord srv;
CAARecord caa;
CMDRecord cmd;
} data;
} Record;
void read_record(PacketBuffer* buffer, Record* record);
bool read_record(PacketBuffer* buffer, Record* record);
void write_record(PacketBuffer* buffer, Record* record);
void free_record(Record* record);
void print_record(Record* record, char* buffer);

View file

@ -117,16 +117,17 @@ static bool read_tcp(Connection* connection, Packet* packet) {
&connection->sock.tcp,
&len,
sizeof(uint16_t)
) < 0) {
) < 2) {
return false;
}
len = ntohs(len);
uint8_t buffer[len];
if ( read_tcp_stream(
if (read_tcp_stream(
&connection->sock.tcp,
buffer,
len
) < 0) {
) < len) {
return false;
}

View file

@ -91,6 +91,7 @@ static void* server_listen(void* arg) {
free_connection(&connection);
continue;
}
pthread_detach(thread);
}
return NULL;
@ -120,6 +121,8 @@ void server_run(Server* server) {
pthread_join(udp, NULL);
pthread_join(tcp, NULL);
pthread_exit(0);
}
void server_free(Server* server) {