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
143
144
145
|
/* main.c
* (c) Jonas Gunz, 2020
* License: MIT
* */
#include "server.h"
void server_start ( server_config_t* _config )
{
char recv_buffer[ UDP_BUFFER_LEN ];
database_t zone_db;
signal ( SIGTERM, signal_term );
signal ( SIGINT, signal_term );
if ( database_populate( &zone_db, _config->zonefile ) ) {
LOGPRINTF(_LOG_ERRNO, "Failed to populate database from zonefile");
exit(1);
}
sock_server = server_get_socket( _config->bind_ip, _config->bind_port );
LOGPRINTF(_LOG_NOTE, "Done!");
while( 1 ) {
struct sockaddr_in sock_client_addr;
socklen_t sock_client_addr_len = sizeof( struct sockaddr_in );
int recv_len = 0;
memset ( &sock_client_addr, 0, sock_client_addr_len );
recv_len = recvfrom (sock_server,
recv_buffer,
UDP_BUFFER_LEN,
0,
(struct sockaddr*) &sock_client_addr,
&sock_client_addr_len );
if ( recv_len == -1 ) {
LOGPRINTF( _LOG_ERRNO, "recvfrom()");
exit ( errno );
}
DEBUG("Packet size %i from %s:%i", recv_len, inet_ntoa(sock_client_addr.sin_addr), sock_client_addr.sin_port );
handle_connection ( sock_server,
&sock_client_addr,
sock_client_addr_len,
recv_buffer,
recv_len,
&zone_db );
}
close( sock_server );
exit(0);
}
int handle_connection ( int _socket,
struct sockaddr_in *sockaddr_client,
socklen_t sockaddr_client_len,
char* _buffer,
int _bufflen,
database_t* _zone_db ) {
dns_message_t msg;
if ( dns_parse_packet (_buffer, _bufflen, &msg) ) {
DEBUG("Malformed packet recieved. parsing failed");
return 1;
}
if ( ! msg.question_count ) {
DEBUG("No questions in request.");
return 1;
}
if (msg.question_count > 0) {
char out[128];
qname_to_fqdn( (char*) msg.question[0].qname, msg.question[0].qname_len, out, 128);
LOGPRINTF(_LOG_DEBUG, "Request for %s QTYPE %i", out, msg.question[0].qtype);
}
// Only handles first request
// TODO heavy refactoring. major POC vibe
database_rdata_t rdata;
dns_question_t* quest = & msg.question[0];
int db_ret = database_query( &rdata, _zone_db, quest->qname, quest->qname_len, quest->qtype, quest->qclass );
if (db_ret) {
LOGPRINTF(_LOG_DEBUG, "DB Query exited with code %i", db_ret);
dns_destroy_struct ( &msg );
return 1;
}
dns_header_t head = {msg.header.id,1,OP_QUERY,0,0,0,0,0,RCODE_NOERR,0,1,0,0};
dns_answer_t answ = {quest->qname, quest->qname_len, RR_A, CL_IN, rdata.ttl, rdata.rdlen, rdata.rdata };
char ret[512];
int hlen = dns_construct_header ( ret, 512, &head );
int alen = dns_construct_answer ( ret + hlen, 512-hlen, &answ );
sendto( _socket, ret, hlen + alen, 0, (struct sockaddr*) sockaddr_client, sockaddr_client_len );
dns_destroy_struct ( &msg );
return 0;
}
int server_get_socket ( char* _bind_ip, uint16_t _bind_port ) {
struct sockaddr_in socket_addr;
int server_socket;
LOGPRINTF(_LOG_NOTE, "Binding on %s:%i", _bind_ip, _bind_port);
server_socket = socket ( AF_INET, SOCK_DGRAM, 0 );
if ( server_socket == -1 ) {
LOGPRINTF(_LOG_ERRNO, "failed to create socket");
exit ( errno );
}
memset( &socket_addr, '\0', sizeof(struct sockaddr_in) );
socket_addr.sin_family = AF_INET;
socket_addr.sin_port = htons( _bind_port );
if ( ! inet_aton ( _bind_ip, & socket_addr.sin_addr ) ) {
LOGPRINTF(_LOG_ERROR, "inet_aton(): Invalid bind IP\n" );
exit ( 1 );
}
if ( bind( server_socket, (struct sockaddr*) &socket_addr, sizeof(struct sockaddr_in) ) ) {
LOGPRINTF(_LOG_ERRNO, "bind() failed");
exit ( errno );
}
return server_socket;
}
void signal_term ( ) {
LOGPRINTF(_LOG_NOTE, "Server shutting down" );
shutdown ( sock_server, SHUT_RDWR );
close ( sock_server );
LOGPRINTF(_LOG_NOTE, "Done!" );
exit( 0 );
}
|