[adae30d] | 1 | /*
|
---|
[dc95342] | 2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
[adae30d] | 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 |
|
---|
[a63966d] | 29 | /** @addtogroup dnsrsrv
|
---|
[adae30d] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[48171fc4] | 36 | #include <adt/list.h>
|
---|
[adae30d] | 37 | #include <errno.h>
|
---|
[c1694b6b] | 38 | #include <str_error.h>
|
---|
[48171fc4] | 39 | #include <fibril_synch.h>
|
---|
[fab2746] | 40 | #include <inet/addr.h>
|
---|
| 41 | #include <inet/endpoint.h>
|
---|
| 42 | #include <inet/udp.h>
|
---|
[48171fc4] | 43 | #include <io/log.h>
|
---|
| 44 | #include <stdbool.h>
|
---|
[08a6382] | 45 | #include <stdlib.h>
|
---|
[adae30d] | 46 |
|
---|
| 47 | #include "dns_msg.h"
|
---|
| 48 | #include "dns_type.h"
|
---|
| 49 | #include "transport.h"
|
---|
| 50 |
|
---|
[ccb5165] | 51 | #define RECV_BUF_SIZE 4096
|
---|
[5d1cb8a] | 52 | #define DNS_SERVER_PORT 53
|
---|
[ccb5165] | 53 |
|
---|
[48171fc4] | 54 | /** Request timeout (microseconds) */
|
---|
[3e66428] | 55 | #define REQ_TIMEOUT (5 * 1000 * 1000)
|
---|
[48171fc4] | 56 |
|
---|
[06fe3b6] | 57 | /** Maximum number of retries */
|
---|
| 58 | #define REQ_RETRY_MAX 3
|
---|
| 59 |
|
---|
[a2e3ee6] | 60 | inet_addr_t dns_server_addr;
|
---|
[3e66428] | 61 |
|
---|
[48171fc4] | 62 | typedef struct {
|
---|
| 63 | link_t lreq;
|
---|
| 64 | dns_message_t *req;
|
---|
| 65 | dns_message_t *resp;
|
---|
| 66 |
|
---|
| 67 | bool done;
|
---|
| 68 | fibril_condvar_t done_cv;
|
---|
| 69 | fibril_mutex_t done_lock;
|
---|
| 70 |
|
---|
[b7fd2a0] | 71 | errno_t status;
|
---|
[48171fc4] | 72 | } trans_req_t;
|
---|
| 73 |
|
---|
[ccb5165] | 74 | static uint8_t recv_buf[RECV_BUF_SIZE];
|
---|
[fab2746] | 75 | static udp_t *transport_udp;
|
---|
| 76 | static udp_assoc_t *transport_assoc;
|
---|
[ccb5165] | 77 |
|
---|
[48171fc4] | 78 | /** Outstanding requests */
|
---|
| 79 | static LIST_INITIALIZE(treq_list);
|
---|
| 80 | static FIBRIL_MUTEX_INITIALIZE(treq_lock);
|
---|
| 81 |
|
---|
[fab2746] | 82 | static void transport_recv_msg(udp_assoc_t *, udp_rmsg_t *);
|
---|
| 83 | static void transport_recv_err(udp_assoc_t *, udp_rerr_t *);
|
---|
| 84 | static void transport_link_state(udp_assoc_t *, udp_link_state_t);
|
---|
| 85 |
|
---|
| 86 | static udp_cb_t transport_cb = {
|
---|
| 87 | .recv_msg = transport_recv_msg,
|
---|
| 88 | .recv_err = transport_recv_err,
|
---|
| 89 | .link_state = transport_link_state
|
---|
| 90 | };
|
---|
[48171fc4] | 91 |
|
---|
[b7fd2a0] | 92 | errno_t transport_init(void)
|
---|
[adae30d] | 93 | {
|
---|
[fab2746] | 94 | inet_ep2_t epp;
|
---|
[b7fd2a0] | 95 | errno_t rc;
|
---|
[141a20d] | 96 |
|
---|
[fab2746] | 97 | inet_ep2_init(&epp);
|
---|
[adae30d] | 98 |
|
---|
[fab2746] | 99 | rc = udp_create(&transport_udp);
|
---|
| 100 | if (rc != EOK) {
|
---|
[08a6382] | 101 | rc = EIO;
|
---|
| 102 | goto error;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[fab2746] | 105 | rc = udp_assoc_create(transport_udp, &epp, &transport_cb, NULL,
|
---|
| 106 | &transport_assoc);
|
---|
| 107 | if (rc != EOK) {
|
---|
| 108 | rc = EIO;
|
---|
[48171fc4] | 109 | goto error;
|
---|
[fab2746] | 110 | }
|
---|
[48171fc4] | 111 |
|
---|
| 112 | return EOK;
|
---|
| 113 | error:
|
---|
[fab2746] | 114 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing network.");
|
---|
| 115 | udp_assoc_destroy(transport_assoc);
|
---|
| 116 | udp_destroy(transport_udp);
|
---|
[48171fc4] | 117 | return rc;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | void transport_fini(void)
|
---|
| 121 | {
|
---|
[fab2746] | 122 | udp_assoc_destroy(transport_assoc);
|
---|
| 123 | udp_destroy(transport_udp);
|
---|
[48171fc4] | 124 | }
|
---|
| 125 |
|
---|
| 126 | static trans_req_t *treq_create(dns_message_t *req)
|
---|
| 127 | {
|
---|
| 128 | trans_req_t *treq;
|
---|
| 129 |
|
---|
| 130 | treq = calloc(1, sizeof(trans_req_t));
|
---|
| 131 | if (treq == NULL)
|
---|
| 132 | return NULL;
|
---|
| 133 |
|
---|
| 134 | treq->req = req;
|
---|
| 135 | treq->resp = NULL;
|
---|
| 136 | treq->done = false;
|
---|
| 137 | fibril_condvar_initialize(&treq->done_cv);
|
---|
| 138 | fibril_mutex_initialize(&treq->done_lock);
|
---|
| 139 |
|
---|
| 140 | fibril_mutex_lock(&treq_lock);
|
---|
| 141 | list_append(&treq->lreq, &treq_list);
|
---|
| 142 | fibril_mutex_unlock(&treq_lock);
|
---|
| 143 |
|
---|
| 144 | return treq;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | static void treq_destroy(trans_req_t *treq)
|
---|
| 148 | {
|
---|
| 149 | if (link_in_use(&treq->lreq))
|
---|
| 150 | list_remove(&treq->lreq);
|
---|
| 151 | free(treq);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | static trans_req_t *treq_match_resp(dns_message_t *resp)
|
---|
| 155 | {
|
---|
| 156 | assert(fibril_mutex_is_locked(&treq_lock));
|
---|
| 157 |
|
---|
[feeac0d] | 158 | list_foreach(treq_list, lreq, trans_req_t, treq) {
|
---|
[48171fc4] | 159 | if (treq->req->id == resp->id) {
|
---|
| 160 | /* Match */
|
---|
| 161 | return treq;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | return NULL;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | static void treq_complete(trans_req_t *treq, dns_message_t *resp)
|
---|
| 169 | {
|
---|
| 170 | fibril_mutex_lock(&treq->done_lock);
|
---|
| 171 | treq->done = true;
|
---|
| 172 | treq->status = EOK;
|
---|
| 173 | treq->resp = resp;
|
---|
| 174 | fibril_mutex_unlock(&treq->done_lock);
|
---|
| 175 |
|
---|
| 176 | fibril_condvar_broadcast(&treq->done_cv);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[b7fd2a0] | 179 | errno_t dns_request(dns_message_t *req, dns_message_t **rresp)
|
---|
[48171fc4] | 180 | {
|
---|
[02a09ed] | 181 | trans_req_t *treq = NULL;
|
---|
[fab2746] | 182 | inet_ep_t ep;
|
---|
| 183 |
|
---|
[48171fc4] | 184 | void *req_data;
|
---|
| 185 | size_t req_size;
|
---|
[58e8646] | 186 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dns_request: Encode dns message");
|
---|
[b7fd2a0] | 187 | errno_t rc = dns_message_encode(req, &req_data, &req_size);
|
---|
[48171fc4] | 188 | if (rc != EOK)
|
---|
| 189 | goto error;
|
---|
[fab2746] | 190 |
|
---|
| 191 | inet_ep_init(&ep);
|
---|
| 192 | ep.addr = dns_server_addr;
|
---|
| 193 | ep.port = DNS_SERVER_PORT;
|
---|
| 194 |
|
---|
[02a09ed] | 195 | size_t ntry = 0;
|
---|
[fab2746] | 196 |
|
---|
[06fe3b6] | 197 | while (ntry < REQ_RETRY_MAX) {
|
---|
[58e8646] | 198 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dns_request: Send DNS message");
|
---|
[fab2746] | 199 | rc = udp_assoc_send_msg(transport_assoc, &ep, req_data,
|
---|
| 200 | req_size);
|
---|
[58e8646] | 201 | if (rc != EOK) {
|
---|
[c1694b6b] | 202 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Error sending message: %s", str_error(rc));
|
---|
[06fe3b6] | 203 | goto error;
|
---|
[58e8646] | 204 | }
|
---|
[fab2746] | 205 |
|
---|
[06fe3b6] | 206 | treq = treq_create(req);
|
---|
| 207 | if (treq == NULL) {
|
---|
| 208 | rc = ENOMEM;
|
---|
[48171fc4] | 209 | goto error;
|
---|
| 210 | }
|
---|
[fab2746] | 211 |
|
---|
[06fe3b6] | 212 | fibril_mutex_lock(&treq->done_lock);
|
---|
| 213 | while (treq->done != true) {
|
---|
| 214 | rc = fibril_condvar_wait_timeout(&treq->done_cv, &treq->done_lock,
|
---|
| 215 | REQ_TIMEOUT);
|
---|
| 216 | if (rc == ETIMEOUT) {
|
---|
| 217 | ++ntry;
|
---|
| 218 | break;
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
[fab2746] | 221 |
|
---|
[06fe3b6] | 222 | fibril_mutex_unlock(&treq->done_lock);
|
---|
[fab2746] | 223 |
|
---|
[06fe3b6] | 224 | if (rc != ETIMEOUT)
|
---|
| 225 | break;
|
---|
[48171fc4] | 226 | }
|
---|
[fab2746] | 227 |
|
---|
[06fe3b6] | 228 | if (ntry >= REQ_RETRY_MAX) {
|
---|
| 229 | rc = EIO;
|
---|
| 230 | goto error;
|
---|
| 231 | }
|
---|
[fab2746] | 232 |
|
---|
[48171fc4] | 233 | if (treq->status != EOK) {
|
---|
| 234 | rc = treq->status;
|
---|
| 235 | goto error;
|
---|
[0041cd6d] | 236 | }
|
---|
[fab2746] | 237 |
|
---|
[48171fc4] | 238 | *rresp = treq->resp;
|
---|
| 239 | treq_destroy(treq);
|
---|
[141a20d] | 240 | free(req_data);
|
---|
[48171fc4] | 241 | return EOK;
|
---|
[fab2746] | 242 |
|
---|
[48171fc4] | 243 | error:
|
---|
| 244 | if (treq != NULL)
|
---|
| 245 | treq_destroy(treq);
|
---|
[fab2746] | 246 |
|
---|
[48171fc4] | 247 | free(req_data);
|
---|
| 248 | return rc;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[fab2746] | 251 | static void transport_recv_msg(udp_assoc_t *assoc, udp_rmsg_t *rmsg)
|
---|
[48171fc4] | 252 | {
|
---|
[fab2746] | 253 | dns_message_t *resp = NULL;
|
---|
| 254 | trans_req_t *treq;
|
---|
| 255 | size_t size;
|
---|
| 256 | inet_ep_t remote_ep;
|
---|
[b7fd2a0] | 257 | errno_t rc;
|
---|
[48171fc4] | 258 |
|
---|
[fab2746] | 259 | size = udp_rmsg_size(rmsg);
|
---|
| 260 | if (size > RECV_BUF_SIZE)
|
---|
| 261 | size = RECV_BUF_SIZE; /* XXX */
|
---|
[141a20d] | 262 |
|
---|
[fab2746] | 263 | rc = udp_rmsg_read(rmsg, 0, recv_buf, size);
|
---|
[dc95342] | 264 | if (rc != EOK) {
|
---|
[fab2746] | 265 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error reading message.");
|
---|
| 266 | return;
|
---|
[dc95342] | 267 | }
|
---|
[0041cd6d] | 268 |
|
---|
[fab2746] | 269 | udp_rmsg_remote_ep(rmsg, &remote_ep);
|
---|
| 270 | /* XXX */
|
---|
[08a6382] | 271 |
|
---|
[fab2746] | 272 | rc = dns_message_decode(recv_buf, size, &resp);
|
---|
| 273 | if (rc != EOK) {
|
---|
| 274 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error decoding message.");
|
---|
| 275 | return;
|
---|
| 276 | }
|
---|
[48171fc4] | 277 |
|
---|
[fab2746] | 278 | assert(resp != NULL);
|
---|
[48171fc4] | 279 |
|
---|
[fab2746] | 280 | fibril_mutex_lock(&treq_lock);
|
---|
| 281 | treq = treq_match_resp(resp);
|
---|
| 282 | if (treq == NULL) {
|
---|
| 283 | fibril_mutex_unlock(&treq_lock);
|
---|
| 284 | return;
|
---|
| 285 | }
|
---|
[b5f716b] | 286 |
|
---|
[fab2746] | 287 | list_remove(&treq->lreq);
|
---|
| 288 | fibril_mutex_unlock(&treq_lock);
|
---|
[48171fc4] | 289 |
|
---|
[fab2746] | 290 | treq_complete(treq, resp);
|
---|
| 291 | }
|
---|
[48171fc4] | 292 |
|
---|
[fab2746] | 293 | static void transport_recv_err(udp_assoc_t *assoc, udp_rerr_t *rerr)
|
---|
| 294 | {
|
---|
| 295 | log_msg(LOG_DEFAULT, LVL_WARN, "Ignoring ICMP error");
|
---|
| 296 | }
|
---|
[48171fc4] | 297 |
|
---|
[fab2746] | 298 | static void transport_link_state(udp_assoc_t *assoc, udp_link_state_t ls)
|
---|
| 299 | {
|
---|
| 300 | log_msg(LOG_DEFAULT, LVL_NOTE, "Link state change");
|
---|
[48171fc4] | 301 | }
|
---|
| 302 |
|
---|
[adae30d] | 303 | /** @}
|
---|
| 304 | */
|
---|