use new network address infrastructure (towards IPv6 support)
[helenos.git] / uspace / srv / net / dnsrsrv / transport.c
blob5517879c645b504708362a3c473b20cd4095a58b
1 /*
2 * Copyright (c) 2013 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup dnsres
30 * @{
32 /**
33 * @file
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>
46 #include "dns_msg.h"
47 #include "dns_type.h"
48 #include "transport.h"
50 #define RECV_BUF_SIZE 4096
51 #define DNS_SERVER_PORT 53
53 /** Request timeout (microseconds) */
54 #define REQ_TIMEOUT (5 * 1000 * 1000)
56 /** Maximum number of retries */
57 #define REQ_RETRY_MAX 3
59 inet_addr_t dns_server_addr;
61 typedef struct {
62 link_t lreq;
63 dns_message_t *req;
64 dns_message_t *resp;
66 bool done;
67 fibril_condvar_t done_cv;
68 fibril_mutex_t done_lock;
70 int status;
71 } trans_req_t;
73 static uint8_t recv_buf[RECV_BUF_SIZE];
74 static fid_t recv_fid;
75 static int transport_fd = -1;
77 /** Outstanding requests */
78 static LIST_INITIALIZE(treq_list);
79 static FIBRIL_MUTEX_INITIALIZE(treq_lock);
81 static int transport_recv_fibril(void *arg);
83 int transport_init(void)
85 struct sockaddr_in laddr;
86 int fd;
87 fid_t fid;
88 int rc;
90 laddr.sin_family = AF_INET;
91 laddr.sin_port = htons(12345);
92 laddr.sin_addr.s_addr = INADDR_ANY;
94 fd = -1;
96 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
97 if (fd < 0) {
98 rc = EIO;
99 goto error;
102 rc = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
103 if (rc != EOK)
104 goto error;
106 transport_fd = fd;
108 fid = fibril_create(transport_recv_fibril, NULL);
109 if (fid == 0)
110 goto error;
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;
122 void transport_fini(void)
124 if (transport_fd >= 0)
125 closesocket(transport_fd);
128 static trans_req_t *treq_create(dns_message_t *req)
130 trans_req_t *treq;
132 treq = calloc(1, sizeof(trans_req_t));
133 if (treq == NULL)
134 return NULL;
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);
142 fibril_mutex_lock(&treq_lock);
143 list_append(&treq->lreq, &treq_list);
144 fibril_mutex_unlock(&treq_lock);
146 return treq;
149 static void treq_destroy(trans_req_t *treq)
151 if (link_in_use(&treq->lreq))
152 list_remove(&treq->lreq);
153 free(treq);
156 static trans_req_t *treq_match_resp(dns_message_t *resp)
158 assert(fibril_mutex_is_locked(&treq_lock));
160 list_foreach(treq_list, link) {
161 trans_req_t *treq = list_get_instance(link, trans_req_t, lreq);
163 if (treq->req->id == resp->id) {
164 /* Match */
165 return treq;
169 return NULL;
172 static void treq_complete(trans_req_t *treq, dns_message_t *resp)
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);
180 fibril_condvar_broadcast(&treq->done_cv);
183 int dns_request(dns_message_t *req, dns_message_t **rresp)
185 int rc;
186 void *req_data;
187 size_t req_size;
188 struct sockaddr_in addr;
189 trans_req_t *treq;
190 int ntry;
192 req_data = NULL;
193 treq = NULL;
195 addr.sin_family = AF_INET;
196 addr.sin_port = htons(DNS_SERVER_PORT);
197 inet_addr_sockaddr_in(&dns_server_addr, &addr);
199 rc = dns_message_encode(req, &req_data, &req_size);
200 if (rc != EOK)
201 goto error;
203 ntry = 0;
205 while (ntry < REQ_RETRY_MAX) {
206 rc = sendto(transport_fd, req_data, req_size, 0,
207 (struct sockaddr *) &addr, sizeof(addr));
208 if (rc != EOK)
209 goto error;
211 treq = treq_create(req);
212 if (treq == NULL) {
213 rc = ENOMEM;
214 goto error;
218 fibril_mutex_lock(&treq->done_lock);
219 while (treq->done != true) {
220 rc = fibril_condvar_wait_timeout(&treq->done_cv, &treq->done_lock,
221 REQ_TIMEOUT);
222 if (rc == ETIMEOUT) {
223 ++ntry;
224 break;
228 fibril_mutex_unlock(&treq->done_lock);
230 if (rc != ETIMEOUT)
231 break;
234 if (ntry >= REQ_RETRY_MAX) {
235 rc = EIO;
236 goto error;
239 if (treq->status != EOK) {
240 rc = treq->status;
241 goto error;
244 *rresp = treq->resp;
245 treq_destroy(treq);
246 free(req_data);
247 return EOK;
248 error:
249 if (treq != NULL)
250 treq_destroy(treq);
251 free(req_data);
252 return rc;
255 static int transport_recv_msg(dns_message_t **rresp)
257 struct sockaddr_in src_addr;
258 socklen_t src_addr_size;
259 size_t recv_size;
260 dns_message_t *resp;
261 int rc;
263 src_addr_size = sizeof(src_addr);
264 rc = recvfrom(transport_fd, recv_buf, RECV_BUF_SIZE, 0,
265 (struct sockaddr *)&src_addr, &src_addr_size);
266 if (rc < 0) {
267 log_msg(LOG_DEFAULT, LVL_ERROR, "recvfrom returns error - %d", rc);
268 goto error;
271 recv_size = (size_t)rc;
273 rc = dns_message_decode(recv_buf, recv_size, &resp);
274 if (rc != EOK) {
275 rc = EIO;
276 goto error;
279 *rresp = resp;
280 return EOK;
282 error:
283 return rc;
286 static int transport_recv_fibril(void *arg)
288 dns_message_t *resp;
289 trans_req_t *treq;
290 int rc;
292 while (true) {
293 rc = transport_recv_msg(&resp);
294 if (rc != EOK)
295 continue;
297 fibril_mutex_lock(&treq_lock);
298 treq = treq_match_resp(resp);
299 if (treq == NULL) {
300 fibril_mutex_unlock(&treq_lock);
301 continue;
304 list_remove(&treq->lreq);
305 fibril_mutex_unlock(&treq_lock);
307 treq_complete(treq, resp);
310 return 0;
313 /** @}