1 | /*
|
---|
2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup dnsres
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <adt/list.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <fibril_synch.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <net/in.h>
|
---|
41 | #include <net/inet.h>
|
---|
42 | #include <net/socket.h>
|
---|
43 | #include <stdbool.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 |
|
---|
46 | #include "dns_msg.h"
|
---|
47 | #include "dns_type.h"
|
---|
48 | #include "transport.h"
|
---|
49 |
|
---|
50 | #define RECV_BUF_SIZE 4096
|
---|
51 | #define DNS_SERVER_PORT 53
|
---|
52 |
|
---|
53 | /** Request timeout (microseconds) */
|
---|
54 | #define REQ_TIMEOUT (5 * 1000 * 1000)
|
---|
55 |
|
---|
56 | /** Maximum number of retries */
|
---|
57 | #define REQ_RETRY_MAX 3
|
---|
58 |
|
---|
59 | inet_addr_t dns_server_addr;
|
---|
60 |
|
---|
61 | typedef struct {
|
---|
62 | link_t lreq;
|
---|
63 | dns_message_t *req;
|
---|
64 | dns_message_t *resp;
|
---|
65 |
|
---|
66 | bool done;
|
---|
67 | fibril_condvar_t done_cv;
|
---|
68 | fibril_mutex_t done_lock;
|
---|
69 |
|
---|
70 | int status;
|
---|
71 | } trans_req_t;
|
---|
72 |
|
---|
73 | static uint8_t recv_buf[RECV_BUF_SIZE];
|
---|
74 | static fid_t recv_fid;
|
---|
75 | static int transport_fd = -1;
|
---|
76 |
|
---|
77 | /** Outstanding requests */
|
---|
78 | static LIST_INITIALIZE(treq_list);
|
---|
79 | static FIBRIL_MUTEX_INITIALIZE(treq_lock);
|
---|
80 |
|
---|
81 | static int transport_recv_fibril(void *arg);
|
---|
82 |
|
---|
83 | int transport_init(void)
|
---|
84 | {
|
---|
85 | struct sockaddr_in laddr;
|
---|
86 | int fd;
|
---|
87 | fid_t fid;
|
---|
88 | int rc;
|
---|
89 |
|
---|
90 | laddr.sin_family = AF_INET;
|
---|
91 | laddr.sin_port = htons(12345);
|
---|
92 | laddr.sin_addr.s_addr = INADDR_ANY;
|
---|
93 |
|
---|
94 | fd = -1;
|
---|
95 |
|
---|
96 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
---|
97 | if (fd < 0) {
|
---|
98 | rc = EIO;
|
---|
99 | goto error;
|
---|
100 | }
|
---|
101 |
|
---|
102 | rc = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
|
---|
103 | if (rc != EOK)
|
---|
104 | goto error;
|
---|
105 |
|
---|
106 | transport_fd = fd;
|
---|
107 |
|
---|
108 | fid = fibril_create(transport_recv_fibril, NULL);
|
---|
109 | if (fid == 0)
|
---|
110 | goto error;
|
---|
111 |
|
---|
112 | fibril_add_ready(fid);
|
---|
113 | recv_fid = fid;
|
---|
114 | return EOK;
|
---|
115 | error:
|
---|
116 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing network socket.");
|
---|
117 | if (fd >= 0)
|
---|
118 | closesocket(fd);
|
---|
119 | return rc;
|
---|
120 | }
|
---|
121 |
|
---|
122 | void transport_fini(void)
|
---|
123 | {
|
---|
124 | if (transport_fd >= 0)
|
---|
125 | closesocket(transport_fd);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static trans_req_t *treq_create(dns_message_t *req)
|
---|
129 | {
|
---|
130 | trans_req_t *treq;
|
---|
131 |
|
---|
132 | treq = calloc(1, sizeof(trans_req_t));
|
---|
133 | if (treq == NULL)
|
---|
134 | return NULL;
|
---|
135 |
|
---|
136 | treq->req = req;
|
---|
137 | treq->resp = NULL;
|
---|
138 | treq->done = false;
|
---|
139 | fibril_condvar_initialize(&treq->done_cv);
|
---|
140 | fibril_mutex_initialize(&treq->done_lock);
|
---|
141 |
|
---|
142 | fibril_mutex_lock(&treq_lock);
|
---|
143 | list_append(&treq->lreq, &treq_list);
|
---|
144 | fibril_mutex_unlock(&treq_lock);
|
---|
145 |
|
---|
146 | return treq;
|
---|
147 | }
|
---|
148 |
|
---|
149 | static void treq_destroy(trans_req_t *treq)
|
---|
150 | {
|
---|
151 | if (link_in_use(&treq->lreq))
|
---|
152 | list_remove(&treq->lreq);
|
---|
153 | free(treq);
|
---|
154 | }
|
---|
155 |
|
---|
156 | static trans_req_t *treq_match_resp(dns_message_t *resp)
|
---|
157 | {
|
---|
158 | assert(fibril_mutex_is_locked(&treq_lock));
|
---|
159 |
|
---|
160 | list_foreach(treq_list, lreq, trans_req_t, treq) {
|
---|
161 | if (treq->req->id == resp->id) {
|
---|
162 | /* Match */
|
---|
163 | return treq;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | return NULL;
|
---|
168 | }
|
---|
169 |
|
---|
170 | static void treq_complete(trans_req_t *treq, dns_message_t *resp)
|
---|
171 | {
|
---|
172 | fibril_mutex_lock(&treq->done_lock);
|
---|
173 | treq->done = true;
|
---|
174 | treq->status = EOK;
|
---|
175 | treq->resp = resp;
|
---|
176 | fibril_mutex_unlock(&treq->done_lock);
|
---|
177 |
|
---|
178 | fibril_condvar_broadcast(&treq->done_cv);
|
---|
179 | }
|
---|
180 |
|
---|
181 | int dns_request(dns_message_t *req, dns_message_t **rresp)
|
---|
182 | {
|
---|
183 | trans_req_t *treq = NULL;
|
---|
184 |
|
---|
185 | void *req_data;
|
---|
186 | size_t req_size;
|
---|
187 | int rc = dns_message_encode(req, &req_data, &req_size);
|
---|
188 | if (rc != EOK)
|
---|
189 | goto error;
|
---|
190 |
|
---|
191 | struct sockaddr_in addr;
|
---|
192 | struct sockaddr_in6 addr6;
|
---|
193 | uint16_t af =
|
---|
194 | inet_addr_sockaddr_in(&dns_server_addr, &addr, &addr6);
|
---|
195 |
|
---|
196 | struct sockaddr *address;
|
---|
197 | socklen_t addrlen;
|
---|
198 |
|
---|
199 | switch (af) {
|
---|
200 | case AF_INET:
|
---|
201 | addr.sin_port = htons(DNS_SERVER_PORT);
|
---|
202 | address = (struct sockaddr *) &addr;
|
---|
203 | addrlen = sizeof(addr);
|
---|
204 | break;
|
---|
205 | case AF_INET6:
|
---|
206 | addr6.sin6_port = htons(DNS_SERVER_PORT);
|
---|
207 | address = (struct sockaddr *) &addr6;
|
---|
208 | addrlen = sizeof(addr6);
|
---|
209 | break;
|
---|
210 | default:
|
---|
211 | rc = EAFNOSUPPORT;
|
---|
212 | goto error;
|
---|
213 | }
|
---|
214 |
|
---|
215 | size_t ntry = 0;
|
---|
216 |
|
---|
217 | while (ntry < REQ_RETRY_MAX) {
|
---|
218 | rc = sendto(transport_fd, req_data, req_size, 0,
|
---|
219 | (struct sockaddr *) address, addrlen);
|
---|
220 | if (rc != EOK)
|
---|
221 | goto error;
|
---|
222 |
|
---|
223 | treq = treq_create(req);
|
---|
224 | if (treq == NULL) {
|
---|
225 | rc = ENOMEM;
|
---|
226 | goto error;
|
---|
227 | }
|
---|
228 |
|
---|
229 | fibril_mutex_lock(&treq->done_lock);
|
---|
230 | while (treq->done != true) {
|
---|
231 | rc = fibril_condvar_wait_timeout(&treq->done_cv, &treq->done_lock,
|
---|
232 | REQ_TIMEOUT);
|
---|
233 | if (rc == ETIMEOUT) {
|
---|
234 | ++ntry;
|
---|
235 | break;
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | fibril_mutex_unlock(&treq->done_lock);
|
---|
240 |
|
---|
241 | if (rc != ETIMEOUT)
|
---|
242 | break;
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (ntry >= REQ_RETRY_MAX) {
|
---|
246 | rc = EIO;
|
---|
247 | goto error;
|
---|
248 | }
|
---|
249 |
|
---|
250 | if (treq->status != EOK) {
|
---|
251 | rc = treq->status;
|
---|
252 | goto error;
|
---|
253 | }
|
---|
254 |
|
---|
255 | *rresp = treq->resp;
|
---|
256 | treq_destroy(treq);
|
---|
257 | free(req_data);
|
---|
258 | return EOK;
|
---|
259 |
|
---|
260 | error:
|
---|
261 | if (treq != NULL)
|
---|
262 | treq_destroy(treq);
|
---|
263 |
|
---|
264 | free(req_data);
|
---|
265 | return rc;
|
---|
266 | }
|
---|
267 |
|
---|
268 | static int transport_recv_msg(dns_message_t **rresp)
|
---|
269 | {
|
---|
270 | struct sockaddr_in src_addr;
|
---|
271 | socklen_t src_addr_size;
|
---|
272 | size_t recv_size;
|
---|
273 | dns_message_t *resp;
|
---|
274 | int rc;
|
---|
275 |
|
---|
276 | src_addr_size = sizeof(src_addr);
|
---|
277 | rc = recvfrom(transport_fd, recv_buf, RECV_BUF_SIZE, 0,
|
---|
278 | (struct sockaddr *)&src_addr, &src_addr_size);
|
---|
279 | if (rc < 0) {
|
---|
280 | log_msg(LOG_DEFAULT, LVL_ERROR, "recvfrom returns error - %d", rc);
|
---|
281 | goto error;
|
---|
282 | }
|
---|
283 |
|
---|
284 | recv_size = (size_t)rc;
|
---|
285 |
|
---|
286 | rc = dns_message_decode(recv_buf, recv_size, &resp);
|
---|
287 | if (rc != EOK) {
|
---|
288 | rc = EIO;
|
---|
289 | goto error;
|
---|
290 | }
|
---|
291 |
|
---|
292 | *rresp = resp;
|
---|
293 | return EOK;
|
---|
294 |
|
---|
295 | error:
|
---|
296 | return rc;
|
---|
297 | }
|
---|
298 |
|
---|
299 | static int transport_recv_fibril(void *arg)
|
---|
300 | {
|
---|
301 | dns_message_t *resp;
|
---|
302 | trans_req_t *treq;
|
---|
303 | int rc;
|
---|
304 |
|
---|
305 | while (true) {
|
---|
306 | rc = transport_recv_msg(&resp);
|
---|
307 | if (rc != EOK)
|
---|
308 | continue;
|
---|
309 |
|
---|
310 | fibril_mutex_lock(&treq_lock);
|
---|
311 | treq = treq_match_resp(resp);
|
---|
312 | if (treq == NULL) {
|
---|
313 | fibril_mutex_unlock(&treq_lock);
|
---|
314 | continue;
|
---|
315 | }
|
---|
316 |
|
---|
317 | list_remove(&treq->lreq);
|
---|
318 | fibril_mutex_unlock(&treq_lock);
|
---|
319 |
|
---|
320 | treq_complete(treq, resp);
|
---|
321 | }
|
---|
322 |
|
---|
323 | return 0;
|
---|
324 | }
|
---|
325 |
|
---|
326 | /** @}
|
---|
327 | */
|
---|