source: mainline/uspace/srv/net/dnsrsrv/query.c@ 996df189

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 996df189 was 5a324d99, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Remove include where not needed anymore.

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[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>
41#include "dns_msg.h"
[adae30d]42#include "dns_std.h"
43#include "dns_type.h"
44#include "query.h"
[f85ed4b]45#include "transport.h"
[adae30d]46
47static uint16_t msg_id;
48
[0aa70f4]49static int dns_name_query(const char *name, dns_qtype_t qtype,
50 dns_host_info_t *info)
[adae30d]51{
[0aa70f4]52 /* Start with the caller-provided name */
53 char *sname = str_dup(name);
54 if (sname == NULL)
[a0d97f83]55 return ENOMEM;
[0aa70f4]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;
[a0d97f83]72 question->qclass = DC_IN;
[0aa70f4]73
74 dns_message_t *msg = dns_message_new();
75 if (msg == NULL) {
76 free(question);
77 free(qname);
78 free(sname);
[48171fc4]79 return ENOMEM;
[0aa70f4]80 }
81
[48171fc4]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;
[0aa70f4]89
90 list_append(&question->msg, &msg->question);
91
92 dns_message_t *amsg;
93 int rc = dns_request(msg, &amsg);
[48171fc4]94 if (rc != EOK) {
[0aa70f4]95 dns_message_destroy(msg);
96 free(sname);
[f85ed4b]97 return rc;
[48171fc4]98 }
[0aa70f4]99
[feeac0d]100 list_foreach(amsg->answer, msg, dns_rr_t, rr) {
[716357f]101 log_msg(LOG_DEFAULT, LVL_DEBUG, " - '%s' %u/%u, dsize %zu",
[0aa70f4]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
[d531bd6]107 log_msg(LOG_DEFAULT, LVL_DEBUG, "decode cname (%p, %zu, %zu)",
[9f029aa]108 amsg->pdu.data, amsg->pdu.size, rr->roff);
[0aa70f4]109
110 char *cname;
111 size_t eoff;
[9f029aa]112 rc = dns_name_decode(&amsg->pdu, rr->roff, &cname, &eoff);
[d531bd6]113 if (rc != EOK) {
[0aa70f4]114 assert((rc == EINVAL) || (rc == ENOMEM));
115
116 log_msg(LOG_DEFAULT, LVL_DEBUG, "error decoding cname");
117
[d531bd6]118 dns_message_destroy(msg);
119 dns_message_destroy(amsg);
[0aa70f4]120 free(sname);
121
[d531bd6]122 return rc;
123 }
[0aa70f4]124
[d531bd6]125 log_msg(LOG_DEFAULT, LVL_DEBUG, "name = '%s' "
126 "cname = '%s'", sname, cname);
[0aa70f4]127
[d531bd6]128 /* Continue looking for the more canonical name */
[0aa70f4]129 free(sname);
[d531bd6]130 sname = cname;
131 }
[0aa70f4]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) {
[d531bd6]139 dns_message_destroy(msg);
[dc95342]140 dns_message_destroy(amsg);
[0aa70f4]141 free(sname);
142
[dc95342]143 return ENOMEM;
[7262f89]144 }
[0aa70f4]145
[02a09ed]146 inet_addr_set(dns_uint32_t_decode(rr->rdata, rr->rdata_size),
[3e66428]147 &info->addr);
148
[a0d97f83]149 dns_message_destroy(msg);
[dc95342]150 dns_message_destroy(amsg);
[0aa70f4]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
[7262f89]178 return EOK;
179 }
180 }
[0aa70f4]181
182 log_msg(LOG_DEFAULT, LVL_DEBUG, "'%s' not resolved, fail", sname);
183
[48171fc4]184 dns_message_destroy(msg);
[dc95342]185 dns_message_destroy(amsg);
[0aa70f4]186 free(sname);
187
[7262f89]188 return EIO;
[adae30d]189}
190
[e948fde]191int dns_name2host(const char *name, dns_host_info_t **rinfo, ip_ver_t ver)
[0aa70f4]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
[e948fde]199 switch (ver) {
200 case ip_any:
[0aa70f4]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;
[e948fde]207 case ip_v4:
[0aa70f4]208 rc = dns_name_query(name, DTYPE_A, info);
209 break;
[e948fde]210 case ip_v6:
[0aa70f4]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
[dc95342]225void dns_hostinfo_destroy(dns_host_info_t *info)
226{
[959d2ec]227 free(info->cname);
[dc95342]228 free(info);
229}
230
[adae30d]231/** @}
232 */
Note: See TracBrowser for help on using the repository browser.