1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include "addr.h"
#include "server.h"
#include "resolver.h"
#include "../io/log.h"
#include "../io/map.h"
#include "../io/config.h"
static pthread_t udp, tcp;
static RecordMap map;
void server_init(uint16_t port, Server* server) {
INFO("Server port set to %hu", port);
create_binding(UDP, port, &server->udp);
create_binding(TCP, port, &server->tcp);
if (load_config("/etc/wrapper.conf", &map)) {
return;
}
if (load_config("config", &map)) {
return;
}
WARN("No dns config files were found");
}
struct DnsRequest {
Connection connection;
Packet request;
};
static void* server_respond(void* arg) {
struct DnsRequest req = *(struct DnsRequest*) arg;
INFO("Recieved packet request ID %hu", req.request.header.id);
Packet response;
handle_query(&req.request, &response, req.connection.type, &map);
if (!write_connection(&req.connection, &response)) {
ERROR("Failed to respond to connection ID %hu: %s",
req.request.header.id,
strerror(errno)
);
}
free_packet(&req.request);
free_connection(&req.connection);
if (response.header.z == false) {
free_packet(&response);
} else {
// Dont free from config
free(response.questions);
free(response.answers);
free(response.authorities);
free(response.resources);
}
return NULL;
}
static void* server_listen(void* arg) {
Binding* binding = (Binding*) arg;
while(1) {
Connection connection;
if (!accept_connection(binding, &connection)) {
ERROR("Failed to accept connection");
continue;
}
Packet request;
if (!read_connection(&connection, &request)) {
ERROR("Failed to read connection");
free_connection(&connection);
continue;
}
struct DnsRequest req;
req.connection = connection;
req.request = request;
pthread_t thread;
if(pthread_create(&thread, NULL, &server_respond, &req)) {
ERROR("Failed to create thread for dns request ID %hu: %s",
request.header.id,
strerror(errno)
);
free_packet(&request);
free_connection(&connection);
continue;
}
pthread_detach(thread);
}
pthread_detach(pthread_self());
return NULL;
}
static void signal_handler() {
printf("\n");
pthread_kill(udp, SIGTERM);
pthread_kill(tcp, SIGTERM);
}
void server_run(Server* server) {
if (!pthread_create(&udp, NULL, &server_listen, &server->udp)) {
INFO("Listening for connections on UDP");
} else {
ERROR("Failed to start UDP thread");
exit(EXIT_FAILURE);
}
if (!pthread_create(&tcp, NULL, &server_listen, &server->tcp)) {
INFO("Listening for connections on TCP");
} else {
ERROR("Failed to start TCP thread");
exit(EXIT_FAILURE);
}
signal(SIGINT, signal_handler);
pthread_join(udp, NULL);
pthread_join(tcp, NULL);
pthread_exit(0);
}
void server_free(Server* server) {
free_binding(&server->udp);
free_binding(&server->tcp);
record_map_free(&map);
}
|