2 * Copyright (c) 2013 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
38 #include <fibril_synch.h>
42 #include <net/socket.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
;
67 fibril_condvar_t done_cv
;
68 fibril_mutex_t done_lock
;
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
;
90 laddr
.sin_family
= AF_INET
;
91 laddr
.sin_port
= htons(12345);
92 laddr
.sin_addr
.s_addr
= INADDR_ANY
;
96 fd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
102 rc
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
108 fid
= fibril_create(transport_recv_fibril
, NULL
);
112 fibril_add_ready(fid
);
116 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed initializing network socket.");
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
)
132 treq
= calloc(1, sizeof(trans_req_t
));
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
);
149 static void treq_destroy(trans_req_t
*treq
)
151 if (link_in_use(&treq
->lreq
))
152 list_remove(&treq
->lreq
);
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
) {
172 static void treq_complete(trans_req_t
*treq
, dns_message_t
*resp
)
174 fibril_mutex_lock(&treq
->done_lock
);
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
)
188 struct sockaddr_in addr
;
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
);
205 while (ntry
< REQ_RETRY_MAX
) {
206 rc
= sendto(transport_fd
, req_data
, req_size
, 0,
207 (struct sockaddr
*) &addr
, sizeof(addr
));
211 treq
= treq_create(req
);
218 fibril_mutex_lock(&treq
->done_lock
);
219 while (treq
->done
!= true) {
220 rc
= fibril_condvar_wait_timeout(&treq
->done_cv
, &treq
->done_lock
,
222 if (rc
== ETIMEOUT
) {
228 fibril_mutex_unlock(&treq
->done_lock
);
234 if (ntry
>= REQ_RETRY_MAX
) {
239 if (treq
->status
!= EOK
) {
255 static int transport_recv_msg(dns_message_t
**rresp
)
257 struct sockaddr_in src_addr
;
258 socklen_t src_addr_size
;
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
);
267 log_msg(LOG_DEFAULT
, LVL_ERROR
, "recvfrom returns error - %d", rc
);
271 recv_size
= (size_t)rc
;
273 rc
= dns_message_decode(recv_buf
, recv_size
, &resp
);
286 static int transport_recv_fibril(void *arg
)
293 rc
= transport_recv_msg(&resp
);
297 fibril_mutex_lock(&treq_lock
);
298 treq
= treq_match_resp(resp
);
300 fibril_mutex_unlock(&treq_lock
);
304 list_remove(&treq
->lreq
);
305 fibril_mutex_unlock(&treq_lock
);
307 treq_complete(treq
, resp
);