aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 9d3e214f0ad91c06de235a71569a0401080abd83 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>

#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>

#include <unistd.h>

#include <signal.h>

#include "dns.h"
#include "log.h"

#define PRINT_ERRNO() {printf("%s:%i %i:%s\n", __FILE__, __LINE__, errno, strerror(errno));}

#define UDP_BUFFER_LEN 512

struct dns_message {
	//Header
	uint16_t id;

	uint8_t QR;	//Query:0 Reply:1
	uint8_t OPCODE;	//Query:0 Iquery:1 Status:2
	uint8_t AA;	//Authorative answer
	uint8_t TC;	//Truncation
	uint8_t RD;	//Recursion Desired
	uint8_t RA;	//Recursion Available
	uint8_t RCODE;	//Response Code

	uint16_t question_count;
	uint16_t answer_count;
	uint16_t authorative_count;
	uint16_t additional_count;

	//Question
	//Answer
 };

int sock_server;

int dns_parse_packet ( 	int _socket,
			struct sockaddr_in *sockaddr_client,
			socklen_t sockaddr_client_len,
			char* buffer,
			int bufflen );

void signal_term ( );

void signal_term_child ( );

int test_main(	int argc,
		char* argv[] )
{
	printf("TEST MODE. NOT FUNCTIONAL\n");

	char in[128];
	char out[128];

	strncpy ( in, "aaa.aaaaa.aa\0", 127);

	printf("%s\n", in);

	int written = fqdn_to_qname (in,128,out,128);

	if (written < 0) {
		printf("invallid fqdn\n");
		return 1;
	}

	for(int i = 0; i < written; i++)
		printf(" %x ", out[i]);

	printf("\n\n");
	return 0;
}

int main(	int argc,
		char* argv[] )
{
	int 		ret;
       	struct		sockaddr_in sock_server_addr;
	
	char recv_buffer[ UDP_BUFFER_LEN ];

	signal ( SIGTERM, signal_term );
	signal ( SIGINT,  signal_term );

	log_init_stdout ( _LOG_DEBUG );

	sock_server = socket ( AF_INET, SOCK_DGRAM, 0 );
	if ( sock_server == -1 ) {
		PRINT_ERRNO();
		return errno;
	}

	memset( &sock_server_addr, '\0', sizeof(struct sockaddr_in) );
	sock_server_addr.sin_family = AF_INET;
	sock_server_addr.sin_port   = htons( 53 );
	ret = inet_aton ( "0.0.0.0", & sock_server_addr.sin_addr );
	if( ret == 0 ) { //Error on 0, no errno!
		LOGPRINTF(_LOG_NOTE, "inet_aton(): Invalid IP\n" );
		return 1;
	}

	ret = bind (	sock_server,
			(struct sockaddr*) &sock_server_addr,
			sizeof(struct sockaddr_in) );
	if ( ret == -1 ) {
		PRINT_ERRNO();
		return errno;
	}

	while( 1 )
	{
		struct 		sockaddr_in sock_client_addr;
		socklen_t	sock_client_addr_len;

		sock_client_addr_len = sizeof ( struct sockaddr_in );
		memset ( &sock_client_addr, '\0', sock_client_addr_len );

		ret = recvfrom (sock_server,
				recv_buffer,
				UDP_BUFFER_LEN,
				0,
				(struct sockaddr*) &sock_client_addr,
				&sock_client_addr_len );
		if ( ret == -1 ) {
			LOGPRINTF( _LOG_ERROR, "recvfrom()");
			return errno;
		}

		LOGPRINTF(_LOG_NOTE, "Connection");

		if ( dns_parse_packet ( sock_server,
					&sock_client_addr,
					sock_client_addr_len,
					recv_buffer,
					ret ) ) {
			LOGPRINTF(_LOG_ERROR, "dns_parse_packet()");
			return errno;
		}
		else {
			LOGPRINTF ( _LOG_DEBUG, "forked\n" );
		}
	}

	close( sock_server );

	return 0;
}

int dns_parse_packet (	int _socket,
			struct sockaddr_in *sockaddr_client,
			socklen_t sockaddr_client_len,
			char* buffer,
			int bufflen )
{
	pid_t pid = fork();
	
	if ( pid > 0)
		return 0;
	else if ( pid < 0 )
		return errno;

	signal ( SIGTERM, signal_term_child);

	struct dns_message msg;

	msg.id = *( (uint16_t*) buffer );
	msg.QR =  0x80 & *( (uint8_t*) (buffer + 2));

	exit ( 0 );
}

void signal_term ( ) {
	printf( "Recieved Signal. Terminating active connections and closing socket\n" );
	
	kill ( 0, SIGTERM ); //terminate all child processes

	shutdown ( sock_server, SHUT_RDWR );
	close ( sock_server );

	printf( "done\n" );

	exit( 0 );
}

void signal_term_child ( ) {
	close ( sock_server );
	printf ( "%i: Active connection terminated\n", getpid() );
	exit( 0 );
}