source: mainline/uspace/srv/net/dnsrsrv/query.c@ 850fd32

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 850fd32 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 5.5 KB
Line 
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
47static uint16_t msg_id;
48
49static errno_t 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 log_msg(LOG_DEFAULT, LVL_DEBUG, "dns_name_query: send DNS request");
93 dns_message_t *amsg;
94 errno_t rc = dns_request(msg, &amsg);
95 if (rc != EOK) {
96 dns_message_destroy(msg);
97 free(sname);
98 return rc;
99 }
100
101 list_foreach(amsg->answer, msg, dns_rr_t, rr) {
102 log_msg(LOG_DEFAULT, LVL_DEBUG, " - '%s' %u/%u, dsize %zu",
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
108 log_msg(LOG_DEFAULT, LVL_DEBUG, "decode cname (%p, %zu, %zu)",
109 amsg->pdu.data, amsg->pdu.size, rr->roff);
110
111 char *cname;
112 size_t eoff;
113 rc = dns_name_decode(&amsg->pdu, rr->roff, &cname, &eoff);
114 if (rc != EOK) {
115 assert((rc == EINVAL) || (rc == ENOMEM));
116
117 log_msg(LOG_DEFAULT, LVL_DEBUG, "error decoding cname");
118
119 dns_message_destroy(msg);
120 dns_message_destroy(amsg);
121 free(sname);
122
123 return rc;
124 }
125
126 log_msg(LOG_DEFAULT, LVL_DEBUG, "name = '%s' "
127 "cname = '%s'", sname, cname);
128
129 /* Continue looking for the more canonical name */
130 free(sname);
131 sname = cname;
132 }
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) {
140 dns_message_destroy(msg);
141 dns_message_destroy(amsg);
142 free(sname);
143
144 return ENOMEM;
145 }
146
147 inet_addr_set(dns_uint32_t_decode(rr->rdata, rr->rdata_size),
148 &info->addr);
149
150 dns_message_destroy(msg);
151 dns_message_destroy(amsg);
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
179 return EOK;
180 }
181 }
182
183 log_msg(LOG_DEFAULT, LVL_DEBUG, "'%s' not resolved, fail", sname);
184
185 dns_message_destroy(msg);
186 dns_message_destroy(amsg);
187 free(sname);
188
189 return EIO;
190}
191
192errno_t dns_name2host(const char *name, dns_host_info_t **rinfo, ip_ver_t ver)
193{
194 dns_host_info_t *info = calloc(1, sizeof(dns_host_info_t));
195 if (info == NULL)
196 return ENOMEM;
197
198 errno_t rc;
199
200 switch (ver) {
201 case ip_any:
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 ip_v4:
209 rc = dns_name_query(name, DTYPE_A, info);
210 break;
211 case ip_v6:
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
226void dns_hostinfo_destroy(dns_host_info_t *info)
227{
228 free(info->cname);
229 free(info);
230}
231
232/** @}
233 */
Note: See TracBrowser for help on using the repository browser.