source: mainline/uspace/srv/net/dnsrsrv/transport.c@ 12df1f1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 12df1f1 was 02a09ed, checked in by Martin Decky <martin@…>, 12 years ago

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 6.7 KB
Line 
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
59inet_addr_t dns_server_addr;
60
61typedef 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
73static uint8_t recv_buf[RECV_BUF_SIZE];
74static fid_t recv_fid;
75static int transport_fd = -1;
76
77/** Outstanding requests */
78static LIST_INITIALIZE(treq_list);
79static FIBRIL_MUTEX_INITIALIZE(treq_lock);
80
81static int transport_recv_fibril(void *arg);
82
83int 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;
115error:
116 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing network socket.");
117 if (fd >= 0)
118 closesocket(fd);
119 return rc;
120}
121
122void transport_fini(void)
123{
124 if (transport_fd >= 0)
125 closesocket(transport_fd);
126}
127
128static 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
149static 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
156static 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, link) {
161 trans_req_t *treq = list_get_instance(link, trans_req_t, lreq);
162
163 if (treq->req->id == resp->id) {
164 /* Match */
165 return treq;
166 }
167 }
168
169 return NULL;
170}
171
172static void treq_complete(trans_req_t *treq, dns_message_t *resp)
173{
174 fibril_mutex_lock(&treq->done_lock);
175 treq->done = true;
176 treq->status = EOK;
177 treq->resp = resp;
178 fibril_mutex_unlock(&treq->done_lock);
179
180 fibril_condvar_broadcast(&treq->done_cv);
181}
182
183int dns_request(dns_message_t *req, dns_message_t **rresp)
184{
185 trans_req_t *treq = NULL;
186
187 void *req_data;
188 size_t req_size;
189 int rc = dns_message_encode(req, &req_data, &req_size);
190 if (rc != EOK)
191 goto error;
192
193 struct sockaddr_in addr;
194 struct sockaddr_in6 addr6;
195 uint16_t af =
196 inet_addr_sockaddr_in(&dns_server_addr, &addr, &addr6);
197
198 struct sockaddr *address;
199 socklen_t addrlen;
200
201 switch (af) {
202 case AF_INET:
203 addr.sin_port = htons(DNS_SERVER_PORT);
204 address = (struct sockaddr *) &addr;
205 addrlen = sizeof(addr);
206 break;
207 case AF_INET6:
208 addr6.sin6_port = htons(DNS_SERVER_PORT);
209 address = (struct sockaddr *) &addr6;
210 addrlen = sizeof(addr6);
211 break;
212 default:
213 rc = EAFNOSUPPORT;
214 goto error;
215 }
216
217 size_t ntry = 0;
218
219 while (ntry < REQ_RETRY_MAX) {
220 rc = sendto(transport_fd, req_data, req_size, 0,
221 (struct sockaddr *) address, addrlen);
222 if (rc != EOK)
223 goto error;
224
225 treq = treq_create(req);
226 if (treq == NULL) {
227 rc = ENOMEM;
228 goto error;
229 }
230
231 fibril_mutex_lock(&treq->done_lock);
232 while (treq->done != true) {
233 rc = fibril_condvar_wait_timeout(&treq->done_cv, &treq->done_lock,
234 REQ_TIMEOUT);
235 if (rc == ETIMEOUT) {
236 ++ntry;
237 break;
238 }
239 }
240
241 fibril_mutex_unlock(&treq->done_lock);
242
243 if (rc != ETIMEOUT)
244 break;
245 }
246
247 if (ntry >= REQ_RETRY_MAX) {
248 rc = EIO;
249 goto error;
250 }
251
252 if (treq->status != EOK) {
253 rc = treq->status;
254 goto error;
255 }
256
257 *rresp = treq->resp;
258 treq_destroy(treq);
259 free(req_data);
260 return EOK;
261
262error:
263 if (treq != NULL)
264 treq_destroy(treq);
265
266 free(req_data);
267 return rc;
268}
269
270static int transport_recv_msg(dns_message_t **rresp)
271{
272 struct sockaddr_in src_addr;
273 socklen_t src_addr_size;
274 size_t recv_size;
275 dns_message_t *resp;
276 int rc;
277
278 src_addr_size = sizeof(src_addr);
279 rc = recvfrom(transport_fd, recv_buf, RECV_BUF_SIZE, 0,
280 (struct sockaddr *)&src_addr, &src_addr_size);
281 if (rc < 0) {
282 log_msg(LOG_DEFAULT, LVL_ERROR, "recvfrom returns error - %d", rc);
283 goto error;
284 }
285
286 recv_size = (size_t)rc;
287
288 rc = dns_message_decode(recv_buf, recv_size, &resp);
289 if (rc != EOK) {
290 rc = EIO;
291 goto error;
292 }
293
294 *rresp = resp;
295 return EOK;
296
297error:
298 return rc;
299}
300
301static int transport_recv_fibril(void *arg)
302{
303 dns_message_t *resp;
304 trans_req_t *treq;
305 int rc;
306
307 while (true) {
308 rc = transport_recv_msg(&resp);
309 if (rc != EOK)
310 continue;
311
312 fibril_mutex_lock(&treq_lock);
313 treq = treq_match_resp(resp);
314 if (treq == NULL) {
315 fibril_mutex_unlock(&treq_lock);
316 continue;
317 }
318
319 list_remove(&treq->lreq);
320 fibril_mutex_unlock(&treq_lock);
321
322 treq_complete(treq, resp);
323 }
324
325 return 0;
326}
327
328/** @}
329 */
Note: See TracBrowser for help on using the repository browser.