File: event_udp.c

package info (click to toggle)
curvedns 0.87-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,964 kB
  • sloc: asm: 19,669; ansic: 11,907; cpp: 1,066; sh: 954; makefile: 57
file content (226 lines) | stat: -rw-r--r-- 7,204 bytes parent folder | download | duplicates (2)
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
/* 
 * Copyright 2010 CurveDNS Project. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this list of
 *      conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY CurveDNS Project ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CurveDNS Project OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of CurveDNS Project.
 * 
 */

/*
 * $Id$ 
 * $Author$
 * $Date$
 * $Revision$
 */

#include "event.h"
#include "dns.h"

void event_cleanup_udp_entry(struct ev_loop *loop, struct event_udp_entry *entry) {
	if (entry) {
		if (ev_is_active(&entry->read_int_watcher))
			ev_io_stop(loop, &entry->read_int_watcher);
		if (entry->read_int_watcher.fd >= 0)
			close(entry->read_int_watcher.fd);
		if (ev_is_active(&entry->timeout_int_watcher))
			ev_timer_stop(loop, &entry->timeout_int_watcher);
		free(entry);
	}
}

void event_udp_timeout_cb(struct ev_loop *loop, ev_timer *w, int revent) {
	event_entry_t *general_entry = (event_entry_t *) w->data;
	struct event_udp_entry *entry = (struct event_udp_entry *) &general_entry->udp;

	if (!(revent & EV_TIMEOUT))
		goto wrong;
	if (entry->state != EVENT_UDP_INT_WRITING)
		goto wrong;

	// Check if we reached maximum number of retries:
	if (entry->retries >= global_ip_udp_retries) {
		debug_log(DEBUG_INFO, "event_udp_timeout_cb(): reached maximum number of UDP retries\n");
		goto wrong;
	}

	// If not, close down the socket, i/o watcher and timeout, and try to send it again:
	ev_io_stop(loop, &entry->read_int_watcher);
	ev_timer_stop(loop, &entry->timeout_int_watcher);

	if (entry->read_int_watcher.fd >= 0) {
		close(entry->read_int_watcher.fd);
		entry->read_int_watcher.fd = -1;
	}

	if (!dns_forward_query_udp(general_entry)) {
		debug_log(DEBUG_WARN, "event_udp_timeout_cb(): unable to resend query to authoritative server\n");
		goto wrong;
	}

	return;

/*
nxdomain:
	ev_io_stop(loop, &entry->read_int_watcher);
	ev_timer_stop(loop, &entry->timeout_int_watcher);

	if (!dns_reply_nxdomain_query_udp(general_entry)) {
		debug_log(DEBUG_WARN, "event_udp_timeout_cb(): unable to send NXDOMAIN response\n");
	}
*/

wrong:
	event_cleanup_entry(loop, general_entry);
	return;
}

void event_udp_int_cb(struct ev_loop *loop, ev_io *w, int revent) {
	event_entry_t *general_entry = (event_entry_t *) w->data;
	struct event_udp_entry *entry = (struct event_udp_entry *) &general_entry->udp;
	int n;
	anysin_t address;
	socklen_t addresslen = sizeof(anysin_t);

	if (!(revent & EV_READ))
		goto wrong;
	if (general_entry->general.protocol != IP_PROTOCOL_UDP)
		goto wrong;
	if (entry->state != EVENT_UDP_INT_WRITING)
		goto wrong;

	// We will only receive one UDP packet, and stop the (timeout) watcher:
	ev_timer_stop(loop, &entry->timeout_int_watcher);
	ev_io_stop(loop, &entry->read_int_watcher);

	entry->state = EVENT_UDP_INT_READING;

	n = recvfrom(w->fd, entry->buffer, global_ip_udp_buffersize, MSG_DONTWAIT,
			(struct sockaddr *) &address.sa, &addresslen);
	if (n == -1) {
		// The ready for reading event will again be triggered...
		return;
	}

	entry->packetsize = n;

	// We can also close the socket towards the authoritative name server, as we are done:
	if (entry->read_int_watcher.fd >= 0) {
		close(entry->read_int_watcher.fd);
		entry->read_int_watcher.fd = -1;
	}

	// Check if the response really came from our target server:
	if (ip_compare_address(&address, &global_target_address) != 0) {
		char s[52];
		ip_address_total_string(&address, s, sizeof(s));
		debug_log(DEBUG_WARN, "event_udp_int_cb(): response is not coming from target address, but from %s\n", s);
		goto wrong;
	}

	// And the same goes for the port:
	if (ip_compare_port(&address, &global_target_address) != 0) {
		debug_log(DEBUG_WARN, "event_udp_int_cb(): response is not coming from target address port\n");
		goto wrong;
	}

	// Now analyze the query (i.e. is it the right one?):
	if (!dns_analyze_reply_query(general_entry)) {
		debug_log(DEBUG_WARN, "event_udp_int_cb(): failed to analyze the reply\n");
		goto wrong;
	}

	// Send the reply through UDP:
	if (!dns_reply_query_udp(general_entry)) {
		debug_log(DEBUG_WARN, "event_udp_int_cb(): failed to send the reply\n");
		goto wrong;
	}

wrong:

	// And since we're now done, clear the memory:
	event_cleanup_entry(loop, general_entry);

	return;
}

void event_udp_ext_cb(struct ev_loop *loop, ev_io *w, int revent) {
	struct ip_socket_t *sock = (struct ip_socket_t *) w->data;
	event_entry_t *general_entry = NULL;
	struct event_udp_entry *entry = NULL;
	ssize_t n;
	socklen_t addresslen = sizeof(anysin_t);

	if (!(revent & EV_READ))
		return;

	general_entry = (event_entry_t *) malloc(sizeof(event_entry_t));
	if (!general_entry)
		goto wrong;
	memset(general_entry, 0, sizeof(event_entry_t));

	entry = &general_entry->udp;
	entry->protocol = IP_PROTOCOL_UDP;
	entry->buffer = (uint8_t *) malloc(global_ip_udp_buffersize);
	if (!entry->buffer)
		goto wrong;
	entry->bufferlen = global_ip_udp_buffersize;
	memset(entry->buffer, 0, entry->bufferlen);

	entry->retries = 0;
	entry->sock = sock;
	entry->state = EVENT_UDP_EXT_READING;

	n = recvfrom(w->fd, entry->buffer, entry->bufferlen, MSG_DONTWAIT,
			(struct sockaddr *) &entry->address.sa, &addresslen);
	if (n == -1) {
		// YYY: maybe an overlap
		goto wrong;
	}

	entry->packetsize = n;

	if (debug_level >= DEBUG_INFO) {
		char s[52];
		ip_address_total_string(&entry->address, s, sizeof(s));
		debug_log(DEBUG_INFO, "event_udp_ext_cb(): received UDP query from %s\n", s);
	}

	// Start analyzing the query (is it malformed, or not?):
	if (!dns_analyze_query(general_entry)) {
		debug_log(DEBUG_WARN, "event_udp_ext_cb(): analyzing of query failed\n");
		goto wrong;
	}

	// Now forward the query (through UDP) towards the authoritative name server:
	if (!dns_forward_query_udp(general_entry)) {
		debug_log(DEBUG_WARN, "event_udp_ext_cb(): failed to forward query to authoritative name server\n");
		goto wrong;
	}

	return;

wrong:
	event_cleanup_entry(loop, general_entry);
	return;
}