From b22811eca246c9805cd1972981bc289e224181e6 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Tue, 11 Apr 2023 09:27:16 -0400 Subject: [PATCH] config --- src/io/config.c | 7 +++++++ src/packet/header.c | 2 ++ src/packet/packet.c | 27 +++++++++++++++++++++------ src/packet/packet.h | 2 ++ src/packet/question.c | 13 +++++++++++-- src/packet/question.h | 2 +- src/packet/record.c | 17 +++++++++++++++-- src/packet/record.h | 11 +++++++++-- src/server/binding.c | 7 ++++--- src/server/server.c | 3 +++ 10 files changed, 75 insertions(+), 16 deletions(-) diff --git a/src/io/config.c b/src/io/config.c index 7bd4522..be6e5b1 100644 --- a/src/io/config.c +++ b/src/io/config.c @@ -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; } diff --git a/src/packet/header.c b/src/packet/header.c index fd601ce..f7be5f0 100644 --- a/src/packet/header.c +++ b/src/packet/header.c @@ -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); diff --git a/src/packet/packet.c b/src/packet/packet.c index 1d96e38..07bfccc 100644 --- a/src/packet/packet.c +++ b/src/packet/packet.c @@ -1,34 +1,49 @@ +#include +#include +#include +#include + #include "packet.h" #include "buffer.h" #include "header.h" #include "question.h" #include "record.h" -#include -#include -#include - 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--; + } } } diff --git a/src/packet/packet.h b/src/packet/packet.h index aa1c35e..48b0902 100644 --- a/src/packet/packet.h +++ b/src/packet/packet.h @@ -6,6 +6,8 @@ #include "record.h" #include "../server/addr.h" +#include + typedef struct { Header header; Question* questions; diff --git a/src/packet/question.c b/src/packet/question.c index 5a08fd6..b138121 100644 --- a/src/packet/question.c +++ b/src/packet/question.c @@ -1,21 +1,28 @@ #include #include +#include #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", diff --git a/src/packet/question.h b/src/packet/question.h index e8c385a..43e03b3 100644 --- a/src/packet/question.h +++ b/src/packet/question.h @@ -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); diff --git a/src/packet/record.c b/src/packet/record.c index e3e9077..07a8c62 100644 --- a/src/packet/record.c +++ b/src/packet/record.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -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; } } diff --git a/src/packet/record.h b/src/packet/record.h index 479ce40..6afd078 100644 --- a/src/packet/record.h +++ b/src/packet/record.h @@ -3,6 +3,7 @@ #include "buffer.h" #include +#include 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); diff --git a/src/server/binding.c b/src/server/binding.c index 157d1d4..c643ee5 100644 --- a/src/server/binding.c +++ b/src/server/binding.c @@ -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; } diff --git a/src/server/server.c b/src/server/server.c index 27cc9d9..f96fac0 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -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) {