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; 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) { static bool config_read_record_data(char* data, Record* record) {
switch (record->type) { switch (record->type) {
case UNKOWN: 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); return config_read_srv_record(data, &record->data.srv);
case CAA: case CAA:
return config_read_caa_record(data, &record->data.caa); return config_read_caa_record(data, &record->data.caa);
case CMD:
return config_read_cmd_record(data, &record->data.cmd);
} }
return false; 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) { void read_header(PacketBuffer* buffer, Header* header) {
// memset(header, 0, sizeof(Header)); // memset(header, 0, sizeof(Header));
header->id = buffer_read_short(buffer); 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 "packet.h"
#include "buffer.h" #include "buffer.h"
#include "header.h" #include "header.h"
#include "question.h" #include "question.h"
#include "record.h" #include "record.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void read_packet(PacketBuffer* buffer, Packet* packet) { void read_packet(PacketBuffer* buffer, Packet* packet) {
read_header(buffer, &packet->header); read_header(buffer, &packet->header);
packet->questions = malloc(sizeof(Question) * packet->header.questions); packet->questions = malloc(sizeof(Question) * packet->header.questions);
for(uint16_t i = 0; i < packet->header.questions; i++) { 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); packet->answers = malloc(sizeof(Record) * packet->header.answers);
for(uint16_t i = 0; i < packet->header.answers; i++) { 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); packet->authorities = malloc(sizeof(Record) * packet->header.authoritative_entries);
for(uint16_t i = 0; i < packet->header.authoritative_entries; i++) { for(uint16_t i = 0; i < packet->header.authoritative_entries; i++) {
read_record(buffer, &packet->authorities[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); packet->resources = malloc(sizeof(Record) * packet->header.resource_entries);
for(uint16_t i = 0; i < packet->header.resource_entries; i++) { for(uint16_t i = 0; i < packet->header.resource_entries; i++) {
read_record(buffer, &packet->resources[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 "record.h"
#include "../server/addr.h" #include "../server/addr.h"
#include <stdbool.h>
typedef struct { typedef struct {
Header header; Header header;
Question* questions; Question* questions;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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