summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 69653c701af9f2bf7bcf3a1d5e26b54a317e25d0 (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
/*
Copyright (C) 2017 Paweł Redman

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "shared.h"

int server_sockfd;

static int parse_req_addr(char *begin, char *end, uint32_t *out)
{
	char *p;
	size_t i = 0, parts[4] = {0, 0, 0, 0};

	for (p = begin; p < end; p++) {
		if (*p >= '0' && *p <= '9') {
			parts[i] *= 10;
			parts[i] += *p - '0';

			if (parts[i] > 255)
				return 1;
		} else if (*p == '.') {
			i++;

			if (i > 3)
				return 1;
		} else
			break;
	}

	if (i != 3)
		return 1;

	*out = (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3];
	return 0;
}

#define NUM_WORKERS 8 // FIXME: shouldn't be hardcoded

int main(void)
{
	int error = 0; // return value
	struct sockaddr_in sockaddr;
	pthread_t workers[NUM_WORKERS];
	size_t i;

	if (lists_load("schachts.list", 0)) {
		eprintf("fatal error: couldn't load the lists\n");
		goto error_lists;
	}

	server_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (server_sockfd == -1) {
		perror("socket");
		error = 1;
		goto error_socket;
	}

	sockaddr.sin_family = AF_INET;
	sockaddr.sin_addr.s_addr = INADDR_ANY;
	sockaddr.sin_port = htons(1337);

	if (bind(server_sockfd, (void*)&sockaddr, sizeof(sockaddr)) == -1) {
		perror("bind");
		error = 1;
		goto error_bind;
	}

	for (i = 0; i < NUM_WORKERS; i++)
		pthread_create(workers + i, NULL, worker_main, NULL);

	while (1) {
		char buffer[256];
		struct sockaddr_in addr;
		socklen_t addrlen = sizeof(addr);
		ssize_t size;
		uint32_t query;
		job_t *job;

		size = recvfrom(server_sockfd, buffer, sizeof(buffer), 0,
		                (void*)&addr, &addrlen);
		if (size == -1) {
			perror("recvfrom");
			goto error_recvfrom;
		}

		if ((ntohl(addr.sin_addr.s_addr) & LOCALHOST_MASK)
		    != LOCALHOST_NETWORK)
			continue;

		if (size < REQUEST_HEADER_LEN)
			continue;

		if (memcmp(buffer, REQUEST_HEADER, REQUEST_HEADER_LEN))
			continue;

		if (parse_req_addr(buffer + REQUEST_HEADER_LEN, buffer + size,
		                   &query))
			continue;

		job = job_create();
		job->type = JOB_TEST;
		job->query = query;
		memcpy(&job->reply_to, &addr, sizeof(addr));
		job_enqueue(job);
	}

	job_quit();

	for (i = 0; i < NUM_WORKERS; i++)
		pthread_join(workers[i], NULL);

	cache_destroy();
	// todo: clear all jobs

error_recvfrom:
error_bind:
	shutdown(server_sockfd, SHUT_RDWR);
	close(server_sockfd);
error_socket:
	lists_destroy();
error_lists:
	return error;
}