aboutsummaryrefslogtreecommitdiff
path: root/src/receive_respond.c
blob: a3b1e8e97c7db4606f2316f6bf6a7ee52e6927ae (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * Copyright (c) 2009 - 2012 Marco Peereboom <marco@peereboom.us>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* for strdup() */
#define _POSIX_C_SOURCE 200809L
#include <string.h>

#include <stdio.h>
#include <stdlib.h>
#include <err.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <ldns/ldns.h>

#define MAXLINE 256
#define INBUF_SIZE 4096

struct sockaddr		paddr;
socklen_t		plen = (socklen_t)sizeof(paddr);

int
udp_bind(int sock, uint16_t port)
{
	struct sockaddr_in		addr;
	in_addr_t			maddr = INADDR_ANY;

	addr.sin_family = AF_INET;
	addr.sin_port = (in_port_t) htons((uint16_t)port);
	addr.sin_addr.s_addr = maddr;
	return (bind(sock, (struct sockaddr*)&addr, (socklen_t) sizeof(addr)));
}

int
socket_create(uint16_t port) {
	int so = socket(AF_INET, SOCK_DGRAM, 0);
	if (so == -1)
		err(1, "can't open socket");
	if (udp_bind(so, port))
		err(1, "can't udp bind");
	return so;
}

void
printpacket(ldns_pkt *pkt)
{
	char			*str = ldns_pkt2str(pkt);

	if (str) {
		printf("%s", str);
		LDNS_FREE(str);
	} else
		warnx("could not convert packet to string");
}

ldns_pkt *
receive_and_print(int so) {
	uint8_t			inbuf[INBUF_SIZE];
	ssize_t			nb;
	ldns_status		status;
	ldns_pkt		*query_pkt;

	nb = recvfrom(so, inbuf, INBUF_SIZE, 0, &paddr, &plen);
	if (nb == -1) {
		if (errno == EINTR || errno == EAGAIN)
			return NULL;
		else
			err(EXIT_FAILURE, "recvfrom");
	}

	status = ldns_wire2pkt(&query_pkt, inbuf, (size_t)nb);
	if (status != LDNS_STATUS_OK) {
		warnx("bad packet: %s", ldns_get_errorstr_by_id(status));
		return NULL;
	} else {
		puts("received packet:");
		printpacket(query_pkt);
	}

	return query_pkt;
}

int
send_response(int so, const char *hostname, ldns_pkt *respkt, uint16_t id)
{
	size_t			answer_size;
	ldns_status		status;
	uint8_t			*outbuf = NULL;
	int			rv = 1;

	if (hostname == NULL || respkt == NULL) {
		warnx("send_response: invalid parameters");
		return (0);
	}

	ldns_pkt_set_id(respkt, id);
	status = ldns_pkt2wire(&outbuf, respkt, &answer_size);
	if (status != LDNS_STATUS_OK)
		warnx("can't create answer: %s",
		      ldns_get_errorstr_by_id(status));
	else {
		puts("response packet:");
		printpacket(respkt);

		if (sendto(so, outbuf, answer_size, 0, &paddr, plen) == -1)
			warn("send_response: sendto");
		else {
			rv = 0;

			printf("send_response: resolved %s", hostname);
		}
	}

	if (outbuf)
		LDNS_FREE(outbuf);

	return (rv);
}

char *
hostnamefrompkt(ldns_pkt *pkt, ldns_rr **qrr)
{
	ldns_rr			*query_rr;
	ldns_buffer		*out = NULL;
	ldns_rdf		*rdf;
	char			*ret = NULL;

	if (pkt == NULL)
		return (NULL);

	query_rr = ldns_rr_list_rr(ldns_pkt_question(pkt), 0);
	if (query_rr == NULL) {
		warnx("hostnamefrompkt invalid parameters");
		goto done;
	}

	out = ldns_buffer_new(LDNS_MAX_DOMAINLEN);
	if (out == NULL) {
		warnx("no memory for out buffer");
		goto done;
	}

	rdf = ldns_rr_owner(query_rr);
	if (ldns_rdf2buffer_str_dname(out, rdf) != LDNS_STATUS_OK) {
		warnx("can't get hostname");
		goto done;
	}

	ret = strdup((char *)ldns_buffer_begin(out));
	if (ret == NULL) {
		warn("no memory for hostname");
		goto done;
	}

	if (qrr)
		*qrr = query_rr;
done:
	if (out)
		ldns_buffer_free(out);

	return (ret);
}

int
respond_query(int so, const char *hostname, const char *ipaddr,
	      ldns_rr *query_rr, uint16_t id)
{
	ldns_status		status;
	ldns_rr_list		*answer_an = NULL;
	ldns_rr_list		*answer_ns = NULL;
	ldns_rr_list		*answer_ad = NULL;
	ldns_rr_list		*answer_qr = NULL;
	ldns_pkt		*answer_pkt = NULL;
	ldns_rr			*myrr = NULL, *myaurr = NULL, *myasrr = NULL;
	ldns_rdf		*prev = NULL;
	char			buf[MAXLINE * 2];
	uint8_t			*outbuf = NULL;
	int			rv = 1;

	/* answer section */
	answer_an = ldns_rr_list_new();
	if (answer_an == NULL)
		goto unwind;

	/* authority section */
	answer_ns = ldns_rr_list_new();
	if (answer_ns == NULL)
		goto unwind;

	/* if we have an ip spoof it there */
	if (ipaddr) {
		/* an */
		snprintf(buf, sizeof buf, "%s\t%d\tIN\tA\t%s",
			 hostname, 259200, ipaddr);
		status = ldns_rr_new_frm_str(&myrr, buf, 0, NULL, &prev);
		if (status != LDNS_STATUS_OK) {
			fprintf(stderr, "can't create answer section: %s\n",
				ldns_get_errorstr_by_id(status));
			goto unwind;
		}
		ldns_rr_list_push_rr(answer_an, myrr);
		ldns_rdf_deep_free(prev);
		prev = NULL;

		/* ns */
		snprintf(buf, sizeof buf, "%s\t%d\tIN\tNS\t%s.",
			 hostname, 259200, "localhost");
		status = ldns_rr_new_frm_str(&myaurr, buf, 0, NULL, &prev);
		if (status != LDNS_STATUS_OK) {
			fprintf(stderr, "can't create authority section: %s\n",
				ldns_get_errorstr_by_id(status));
			goto unwind;
		}
		ldns_rr_list_push_rr(answer_ns, myaurr);
		ldns_rdf_deep_free(prev);
		prev = NULL;
	} else {
		snprintf(buf, sizeof buf, "%s\t3600\tIN\tSOA\t%s root.%s %s",
			 hostname,
			 hostname,
			 hostname,
			 "2 3600 900 3600000 3600");
		status = ldns_rr_new_frm_str(&myaurr, buf, 0, NULL, &prev);
		if (status != LDNS_STATUS_OK) {
			fprintf(stderr, "can't create authority section: %s\n",
				ldns_get_errorstr_by_id(status));
			goto unwind;
		}
		ldns_rr_list_push_rr(answer_ns, myaurr);
		ldns_rdf_deep_free(prev);
		prev = NULL;
	}

	/* question section */
	answer_qr = ldns_rr_list_new();
	if (answer_qr == NULL)
		goto unwind;
	ldns_rr_list_push_rr(answer_qr, ldns_rr_clone(query_rr));

	/* additional section */
	answer_ad = ldns_rr_list_new();
	if (answer_ad == NULL)
		goto unwind;
	if (ipaddr) {
		snprintf(buf, sizeof buf, "%s\t%d\tIN\tA\t%s",
			 "localhost",
			 259200,
			 "127.0.0.1");
		status = ldns_rr_new_frm_str(&myasrr, buf, 0, NULL, &prev);
		if (status != LDNS_STATUS_OK) {
			fprintf(stderr, "can't create additional section: %s\n",
				ldns_get_errorstr_by_id(status));
			goto unwind;
		}
		ldns_rr_list_push_rr(answer_ad, myasrr);
		ldns_rdf_deep_free(prev);
		prev = NULL;

		/* V6 */
		snprintf(buf, sizeof buf, "%s\t%d\tIN\tAAAA\t%s",
			 "localhost",
			 259200,
			 "::1");
		status = ldns_rr_new_frm_str(&myasrr, buf, 0, NULL, &prev);
		if (status != LDNS_STATUS_OK) {
			fprintf(stderr, "can't create additional section: %s\n",
				ldns_get_errorstr_by_id(status));
			goto unwind;
		}
		ldns_rr_list_push_rr(answer_ad, myasrr);
		ldns_rdf_deep_free(prev);
		prev = NULL;
	}

	/* actual packet */
	answer_pkt = ldns_pkt_new();
	if (answer_pkt == NULL)
		goto unwind;

	ldns_pkt_set_qr(answer_pkt, 1);
	ldns_pkt_set_aa(answer_pkt, 1);
	if (ipaddr == NULL)
		ldns_pkt_set_rcode(answer_pkt, LDNS_RCODE_NXDOMAIN);

	ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_QUESTION, answer_qr);
	ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_ANSWER, answer_an);
	ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_AUTHORITY, answer_ns);
	ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_ADDITIONAL, answer_ad);

	/* reply to caller */
	if (send_response(so, hostname, answer_pkt, id))
		warnx("send_response failed");

unwind:
	if (answer_pkt)
		ldns_pkt_free(answer_pkt);
	if (outbuf)
		LDNS_FREE(outbuf);
	if (answer_qr)
		ldns_rr_list_free(answer_qr);
	if (answer_an)
		ldns_rr_list_free(answer_an);
	if (answer_ns)
		ldns_rr_list_free(answer_ns);
	if (answer_ad)
		ldns_rr_list_free(answer_ad);

	return (rv);
}