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