| 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 <errno.h>
|
|---|
| 37 | #include <io/log.h>
|
|---|
| 38 | #include <mem.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <str.h>
|
|---|
| 41 | #include "dns_msg.h"
|
|---|
| 42 | #include "dns_std.h"
|
|---|
| 43 | #include "dns_type.h"
|
|---|
| 44 | #include "query.h"
|
|---|
| 45 | #include "transport.h"
|
|---|
| 46 |
|
|---|
| 47 | static uint16_t msg_id;
|
|---|
| 48 |
|
|---|
| 49 | static int dns_name_query(const char *name, dns_qtype_t qtype,
|
|---|
| 50 | dns_host_info_t *info)
|
|---|
| 51 | {
|
|---|
| 52 | /* Start with the caller-provided name */
|
|---|
| 53 | char *sname = str_dup(name);
|
|---|
| 54 | if (sname == NULL)
|
|---|
| 55 | return ENOMEM;
|
|---|
| 56 |
|
|---|
| 57 | char *qname = str_dup(name);
|
|---|
| 58 | if (qname == NULL) {
|
|---|
| 59 | free(sname);
|
|---|
| 60 | return ENOMEM;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | dns_question_t *question = calloc(1, sizeof(dns_question_t));
|
|---|
| 64 | if (question == NULL) {
|
|---|
| 65 | free(qname);
|
|---|
| 66 | free(sname);
|
|---|
| 67 | return ENOMEM;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | question->qname = qname;
|
|---|
| 71 | question->qtype = qtype;
|
|---|
| 72 | question->qclass = DC_IN;
|
|---|
| 73 |
|
|---|
| 74 | dns_message_t *msg = dns_message_new();
|
|---|
| 75 | if (msg == NULL) {
|
|---|
| 76 | free(question);
|
|---|
| 77 | free(qname);
|
|---|
| 78 | free(sname);
|
|---|
| 79 | return ENOMEM;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | msg->id = msg_id++;
|
|---|
| 83 | msg->qr = QR_QUERY;
|
|---|
| 84 | msg->opcode = OPC_QUERY;
|
|---|
| 85 | msg->aa = false;
|
|---|
| 86 | msg->tc = false;
|
|---|
| 87 | msg->rd = true;
|
|---|
| 88 | msg->ra = false;
|
|---|
| 89 |
|
|---|
| 90 | list_append(&question->msg, &msg->question);
|
|---|
| 91 |
|
|---|
| 92 | dns_message_t *amsg;
|
|---|
| 93 | int rc = dns_request(msg, &amsg);
|
|---|
| 94 | if (rc != EOK) {
|
|---|
| 95 | dns_message_destroy(msg);
|
|---|
| 96 | free(sname);
|
|---|
| 97 | return rc;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | list_foreach(amsg->answer, msg, dns_rr_t, rr) {
|
|---|
| 101 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - '%s' %u/%u, dsize %zu",
|
|---|
| 102 | rr->name, rr->rtype, rr->rclass, rr->rdata_size);
|
|---|
| 103 |
|
|---|
| 104 | if ((rr->rtype == DTYPE_CNAME) && (rr->rclass == DC_IN) &&
|
|---|
| 105 | (str_cmp(rr->name, sname) == 0)) {
|
|---|
| 106 |
|
|---|
| 107 | log_msg(LOG_DEFAULT, LVL_DEBUG, "decode cname (%p, %zu, %zu)",
|
|---|
| 108 | amsg->pdu.data, amsg->pdu.size, rr->roff);
|
|---|
| 109 |
|
|---|
| 110 | char *cname;
|
|---|
| 111 | size_t eoff;
|
|---|
| 112 | rc = dns_name_decode(&amsg->pdu, rr->roff, &cname, &eoff);
|
|---|
| 113 | if (rc != EOK) {
|
|---|
| 114 | assert((rc == EINVAL) || (rc == ENOMEM));
|
|---|
| 115 |
|
|---|
| 116 | log_msg(LOG_DEFAULT, LVL_DEBUG, "error decoding cname");
|
|---|
| 117 |
|
|---|
| 118 | dns_message_destroy(msg);
|
|---|
| 119 | dns_message_destroy(amsg);
|
|---|
| 120 | free(sname);
|
|---|
| 121 |
|
|---|
| 122 | return rc;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | log_msg(LOG_DEFAULT, LVL_DEBUG, "name = '%s' "
|
|---|
| 126 | "cname = '%s'", sname, cname);
|
|---|
| 127 |
|
|---|
| 128 | /* Continue looking for the more canonical name */
|
|---|
| 129 | free(sname);
|
|---|
| 130 | sname = cname;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | if ((qtype == DTYPE_A) && (rr->rtype == DTYPE_A) &&
|
|---|
| 134 | (rr->rclass == DC_IN) && (rr->rdata_size == sizeof(addr32_t)) &&
|
|---|
| 135 | (str_cmp(rr->name, sname) == 0)) {
|
|---|
| 136 |
|
|---|
| 137 | info->cname = str_dup(rr->name);
|
|---|
| 138 | if (info->cname == NULL) {
|
|---|
| 139 | dns_message_destroy(msg);
|
|---|
| 140 | dns_message_destroy(amsg);
|
|---|
| 141 | free(sname);
|
|---|
| 142 |
|
|---|
| 143 | return ENOMEM;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | inet_addr_set(dns_uint32_t_decode(rr->rdata, rr->rdata_size),
|
|---|
| 147 | &info->addr);
|
|---|
| 148 |
|
|---|
| 149 | dns_message_destroy(msg);
|
|---|
| 150 | dns_message_destroy(amsg);
|
|---|
| 151 | free(sname);
|
|---|
| 152 |
|
|---|
| 153 | return EOK;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | if ((qtype == DTYPE_AAAA) && (rr->rtype == DTYPE_AAAA) &&
|
|---|
| 157 | (rr->rclass == DC_IN) && (rr->rdata_size == sizeof(addr128_t)) &&
|
|---|
| 158 | (str_cmp(rr->name, sname) == 0)) {
|
|---|
| 159 |
|
|---|
| 160 | info->cname = str_dup(rr->name);
|
|---|
| 161 | if (info->cname == NULL) {
|
|---|
| 162 | dns_message_destroy(msg);
|
|---|
| 163 | dns_message_destroy(amsg);
|
|---|
| 164 | free(sname);
|
|---|
| 165 |
|
|---|
| 166 | return ENOMEM;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | addr128_t addr;
|
|---|
| 170 | dns_addr128_t_decode(rr->rdata, rr->rdata_size, addr);
|
|---|
| 171 |
|
|---|
| 172 | inet_addr_set6(addr, &info->addr);
|
|---|
| 173 |
|
|---|
| 174 | dns_message_destroy(msg);
|
|---|
| 175 | dns_message_destroy(amsg);
|
|---|
| 176 | free(sname);
|
|---|
| 177 |
|
|---|
| 178 | return EOK;
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | log_msg(LOG_DEFAULT, LVL_DEBUG, "'%s' not resolved, fail", sname);
|
|---|
| 183 |
|
|---|
| 184 | dns_message_destroy(msg);
|
|---|
| 185 | dns_message_destroy(amsg);
|
|---|
| 186 | free(sname);
|
|---|
| 187 |
|
|---|
| 188 | return EIO;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | int dns_name2host(const char *name, dns_host_info_t **rinfo, ip_ver_t ver)
|
|---|
| 192 | {
|
|---|
| 193 | dns_host_info_t *info = calloc(1, sizeof(dns_host_info_t));
|
|---|
| 194 | if (info == NULL)
|
|---|
| 195 | return ENOMEM;
|
|---|
| 196 |
|
|---|
| 197 | int rc;
|
|---|
| 198 |
|
|---|
| 199 | switch (ver) {
|
|---|
| 200 | case ip_any:
|
|---|
| 201 | rc = dns_name_query(name, DTYPE_AAAA, info);
|
|---|
| 202 |
|
|---|
| 203 | if (rc != EOK)
|
|---|
| 204 | rc = dns_name_query(name, DTYPE_A, info);
|
|---|
| 205 |
|
|---|
| 206 | break;
|
|---|
| 207 | case ip_v4:
|
|---|
| 208 | rc = dns_name_query(name, DTYPE_A, info);
|
|---|
| 209 | break;
|
|---|
| 210 | case ip_v6:
|
|---|
| 211 | rc = dns_name_query(name, DTYPE_AAAA, info);
|
|---|
| 212 | break;
|
|---|
| 213 | default:
|
|---|
| 214 | rc = EINVAL;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | if (rc == EOK)
|
|---|
| 218 | *rinfo = info;
|
|---|
| 219 | else
|
|---|
| 220 | free(info);
|
|---|
| 221 |
|
|---|
| 222 | return rc;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | void dns_hostinfo_destroy(dns_host_info_t *info)
|
|---|
| 226 | {
|
|---|
| 227 | free(info->cname);
|
|---|
| 228 | free(info);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /** @}
|
|---|
| 232 | */
|
|---|