[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>
|
---|
[adae30d] | 41 |
|
---|
[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 |
|
---|
[dc95342] | 50 | int dns_name2host(const char *name, dns_host_info_t **rinfo)
|
---|
[adae30d] | 51 | {
|
---|
[48171fc4] | 52 | dns_message_t *msg;
|
---|
[f85ed4b] | 53 | dns_message_t *amsg;
|
---|
[a0d97f83] | 54 | dns_question_t *question;
|
---|
[dc95342] | 55 | dns_host_info_t *info;
|
---|
[d531bd6] | 56 | char *sname, *cname;
|
---|
| 57 | size_t eoff;
|
---|
[f85ed4b] | 58 | int rc;
|
---|
[adae30d] | 59 |
|
---|
[a0d97f83] | 60 | question = calloc(1, sizeof(dns_question_t));
|
---|
| 61 | if (question == NULL)
|
---|
| 62 | return ENOMEM;
|
---|
| 63 |
|
---|
| 64 | question->qname = (char *)name;
|
---|
| 65 | question->qtype = DTYPE_A;
|
---|
| 66 | question->qclass = DC_IN;
|
---|
[adae30d] | 67 |
|
---|
[a0d97f83] | 68 | msg = dns_message_new();
|
---|
[48171fc4] | 69 | if (msg == NULL)
|
---|
| 70 | return ENOMEM;
|
---|
[08a6382] | 71 |
|
---|
[a0d97f83] | 72 | list_append(&question->msg, &msg->question);
|
---|
[adae30d] | 73 |
|
---|
[48171fc4] | 74 | msg->id = msg_id++;
|
---|
| 75 | msg->qr = QR_QUERY;
|
---|
| 76 | msg->opcode = OPC_QUERY;
|
---|
| 77 | msg->aa = false;
|
---|
| 78 | msg->tc = false;
|
---|
| 79 | msg->rd = true;
|
---|
| 80 | msg->ra = false;
|
---|
[adae30d] | 81 |
|
---|
[48171fc4] | 82 | rc = dns_request(msg, &amsg);
|
---|
| 83 | if (rc != EOK) {
|
---|
[f85ed4b] | 84 | return rc;
|
---|
[48171fc4] | 85 | }
|
---|
[f85ed4b] | 86 |
|
---|
[d531bd6] | 87 | /* Start with the caller-provided name */
|
---|
| 88 | sname = str_dup(name);
|
---|
| 89 |
|
---|
[7262f89] | 90 | list_foreach(amsg->answer, link) {
|
---|
| 91 | dns_rr_t *rr = list_get_instance(link, dns_rr_t, msg);
|
---|
| 92 |
|
---|
[716357f] | 93 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - '%s' %u/%u, dsize %zu",
|
---|
[7262f89] | 94 | rr->name, rr->rtype, rr->rclass, rr->rdata_size);
|
---|
| 95 |
|
---|
[d531bd6] | 96 | if (rr->rtype == DTYPE_CNAME && rr->rclass == DC_IN &&
|
---|
| 97 | str_cmp(rr->name, sname) == 0) {
|
---|
| 98 | log_msg(LOG_DEFAULT, LVL_DEBUG, "decode cname (%p, %zu, %zu)",
|
---|
[9f029aa] | 99 | amsg->pdu.data, amsg->pdu.size, rr->roff);
|
---|
| 100 | rc = dns_name_decode(&amsg->pdu, rr->roff, &cname, &eoff);
|
---|
[d531bd6] | 101 | if (rc != EOK) {
|
---|
| 102 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
| 103 | "error decoding cname");
|
---|
| 104 | assert(rc == EINVAL || rc == ENOMEM);
|
---|
| 105 | dns_message_destroy(msg);
|
---|
| 106 | dns_message_destroy(amsg);
|
---|
| 107 | return rc;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | log_msg(LOG_DEFAULT, LVL_DEBUG, "name = '%s' "
|
---|
| 111 | "cname = '%s'", sname, cname);
|
---|
| 112 |
|
---|
| 113 | free(sname);
|
---|
| 114 | /* Continue looking for the more canonical name */
|
---|
| 115 | sname = cname;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[dc95342] | 118 | if (rr->rtype == DTYPE_A && rr->rclass == DC_IN &&
|
---|
[d531bd6] | 119 | rr->rdata_size == sizeof(uint32_t) &&
|
---|
| 120 | str_cmp(rr->name, sname) == 0) {
|
---|
[dc95342] | 121 |
|
---|
| 122 | info = calloc(1, sizeof(dns_host_info_t));
|
---|
| 123 | if (info == NULL) {
|
---|
[d531bd6] | 124 | dns_message_destroy(msg);
|
---|
[dc95342] | 125 | dns_message_destroy(amsg);
|
---|
| 126 | return ENOMEM;
|
---|
[7262f89] | 127 | }
|
---|
| 128 |
|
---|
[959d2ec] | 129 | info->cname = str_dup(rr->name);
|
---|
[02a09ed] | 130 | inet_addr_set(dns_uint32_t_decode(rr->rdata, rr->rdata_size),
|
---|
[3e66428] | 131 | &info->addr);
|
---|
| 132 |
|
---|
[a0d97f83] | 133 | dns_message_destroy(msg);
|
---|
[dc95342] | 134 | dns_message_destroy(amsg);
|
---|
| 135 | *rinfo = info;
|
---|
[7262f89] | 136 | return EOK;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[48171fc4] | 140 | dns_message_destroy(msg);
|
---|
[dc95342] | 141 | dns_message_destroy(amsg);
|
---|
[d531bd6] | 142 | log_msg(LOG_DEFAULT, LVL_DEBUG, "'%s' not resolved, fail", sname);
|
---|
[7262f89] | 143 |
|
---|
| 144 | return EIO;
|
---|
[adae30d] | 145 | }
|
---|
| 146 |
|
---|
[dc95342] | 147 | void dns_hostinfo_destroy(dns_host_info_t *info)
|
---|
| 148 | {
|
---|
[959d2ec] | 149 | free(info->cname);
|
---|
[dc95342] | 150 | free(info);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[adae30d] | 153 | /** @}
|
---|
| 154 | */
|
---|