summaryrefslogtreecommitdiff
path: root/src/worker.c
blob: 82d4ce665cd4458cc74bc36eb2f5116dd203c786 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
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"
#include <netdb.h> // reverse DNS

static job_t *job_queue;
static pthread_cond_t job_queue_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
static int job_quit_flag = 0;

job_t *job_create(job_type_t type, db_entry *entry)
{
	job_t *job;

	job = calloc(1, sizeof(job_t));
	if (!job) {
		fprintf(stderr, "job_create: calloc failed\n");
		abort();
	}

	job->type = type;
	job->entry = entry;

	return job;
}

void job_destroy(job_t *job)
{
	free(job);
}

void job_enqueue(job_t *job)
{
	pthread_mutex_lock(&job_queue_mutex);
	eli_append(&job_queue, job, queue_list);
	pthread_mutex_unlock(&job_queue_mutex);
	pthread_cond_signal(&job_queue_cond);
}

// FIXME: bad name
void job_quit(void)
{
	pthread_mutex_lock(&job_queue_mutex);
	job_quit_flag = 1;
	pthread_mutex_unlock(&job_queue_mutex);
	pthread_cond_broadcast(&job_queue_cond);
}

// FIXME: refactor with vstrs
static int read_everything(FILE *fp, char **buffer)
{
	size_t read = 0, alloc = 128, partial;

	alloc = 128;
	*buffer = malloc(alloc);
	if (!*buffer)
		goto oom;

	while (1) {
		size_t left = alloc - read;

		if (left == 0) {
			alloc *= 2;
			*buffer = realloc(*buffer, alloc);
			if (!buffer)
				goto oom;

			continue;
		}

		partial = fread((*buffer) + read, 1, left, fp);
		if (partial == 0) {
			if (ferror(fp))
				return 1;

			break;
		}

		read += partial;
	}


	*buffer = realloc(*buffer, read + 1);
	(*buffer)[read] = '\0';
	return 0;

oom:
	fprintf(stderr, "read_everything: out of memory\n");
	abort();
}

// Check if the database entry is complete (all fields are DB_VALID).
// If it is then enqueue all of its waiting jobs.
static void check_waiting_jobs(db_entry *entry)
{
	job_t *job;

	if (entry->revdns.state != DB_VALID
	    || entry->whois.state != DB_VALID)
		return;

	eli_for(job, entry->waiting_jobs, waiting_list)
		job_enqueue(job);

	entry->waiting_jobs = NULL;
}

// Update an entry's field and call check_waiting_jobs.
static void update_entry(db_entry *entry, db_entry_field *field, char *data)
{
	pthread_mutex_lock(&entry->mutex);

	field->state = DB_VALID;
	free(field->data);
	field->data = data;
	field->exp_time = get_time() + 24 * TIME_HOUR;

	// invalidate the result cache
	entry->cached_result_valid = false;

	check_waiting_jobs(entry);

	pthread_mutex_unlock(&entry->mutex);
}

// This job is queued when both WHOIS and revDNS data is available.
// It calls lists_test and sends back the result. 
static void worker_job_reply(job_t *job)
{
	int result;
	char reply[1000];

	pthread_mutex_lock(&job->entry->mutex);

	if (!job->entry->cached_result_valid) {
		DEBUG("result cache miss for %08X\n", job->ipv4);
		result = lists_test(job->entry->revdns.data,
		                    job->entry->whois.data);

		job->entry->cached_result_valid = true;
		job->entry->cached_result = result;
	} else
		result = job->entry->cached_result;

	pthread_mutex_unlock(&job->entry->mutex);

	snprintf(reply, sizeof(reply), "%s%hhu.%hhu.%hhu.%hhu %i\n",
	         RESPONSE_HEADER,
	         (job->ipv4 >> 24) & 0xFF, (job->ipv4 >> 16) & 0xFF,
	         (job->ipv4 >> 8) & 0xFF, job->ipv4 & 0xFF, result);
	// FIXME: handle sendto errors
	sendto(server_sockfd, reply, strlen(reply), 0, (void*)&job->ret_addr,
	       sizeof(job->ret_addr));

	DEBUG("reply %08X %i\n", job->ipv4, result);
}

// This job makes a revDNS query and stores it in the database.
static void worker_job_revdns(job_t *job)
{
	struct sockaddr_in addr;
	char buffer[NI_MAXHOST], *revdns;
	int rv;

	DEBUG("getnameinfo(%08X, ...)\n", job->ipv4);

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(job->ipv4);
	addr.sin_port = 0;

	rv = getnameinfo((void*)&addr, sizeof(addr), buffer, sizeof(buffer),
	                 NULL, 0, NI_NAMEREQD);
	switch (rv) {
	case 0:
		revdns = strdup(buffer);
		break;

	case EAI_NONAME:
		// FIXME: handle errors and missing revDNS entries better
		revdns = strdup("<none>");
		break;

	case EAI_SYSTEM:
		revdns = strdup("<error>");
		perror("getnameinfo");
		break;

	default:
		revdns = strdup("<error>");
		fprintf(stderr, "getnameinfo: %s\n", gai_strerror(rv));
		break;
	}

	update_entry(job->entry, &job->entry->revdns, revdns);
}

// This job makes a WHOIS query and stores it in the database.
static void worker_job_whois(job_t *job)
{
	char command[80];
	FILE *fp;
	char *out;

	snprintf(command, sizeof(command), "whois \"%hhu.%hhu.%hhu.%hhu\"",
	         (job->ipv4 >> 24) & 0xFF, (job->ipv4 >> 16) & 0xFF,
	         (job->ipv4 >> 8) & 0xFF, job->ipv4 & 0xFF);
	DEBUG("%s\n", command);

	fp = popen(command, "r");
	read_everything(fp, &out); // FIXME: handle errors
	fclose(fp);

	update_entry(job->entry, &job->entry->whois, out);
}

void *worker_main(void *unused)
{
	for (;;) {
		job_t *job;

		pthread_mutex_lock(&job_queue_mutex);
		while (!job_queue && !job_quit_flag)
			pthread_cond_wait(&job_queue_cond, &job_queue_mutex);

		if (job_quit_flag) {
			pthread_mutex_unlock(&job_queue_mutex);
			DEBUG("quitting\n");
			return NULL;
		}

		job = job_queue;
		eli_unlink(&job_queue, job, queue_list);
		pthread_mutex_unlock(&job_queue_mutex);

		switch (job->type) {
		case JOB_REPLY:
			worker_job_reply(job);
			break;

		case JOB_REVDNS:
			worker_job_revdns(job);
			break;

		case JOB_WHOIS:
			worker_job_whois(job);
			break;
		}

		job_destroy(job);
	}
}