1 | /*
|
---|
2 | * TINET (TCP/IP Protocol Stack)
|
---|
3 | *
|
---|
4 | * Copyright (C) 2001-2017 by Dep. of Computer Science and Engineering
|
---|
5 | * Tomakomai National College of Technology, JAPAN
|
---|
6 | *
|
---|
7 | * ä¸è¨èä½æ¨©è
|
---|
8 | ã¯ï¼ä»¥ä¸ã® (1)ï½(4) ã®æ¡ä»¶ãï¼Free Software Foundation
|
---|
9 | * ã«ãã£ã¦å
|
---|
10 | ¬è¡¨ããã¦ãã GNU General Public License ã® Version 2 ã«è¨
|
---|
11 | * è¿°ããã¦ããæ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§ã¢ï¼æ¬ã½ããã¦ã§ã¢
|
---|
12 | * ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹å¤ã»åé
|
---|
13 | å¸ï¼ä»¥ä¸ï¼
|
---|
14 | * å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
15 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
16 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
17 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
18 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
19 | * ç¨ã§ããå½¢ã§åé
|
---|
20 | å¸ããå ´åã«ã¯ï¼åé
|
---|
21 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
22 | * è
|
---|
23 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
24 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
25 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
26 | * ç¨ã§ããªãå½¢ã§åé
|
---|
27 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
28 | * ã¨ï¼
|
---|
29 | * (a) åé
|
---|
30 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
31 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
32 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
33 | * (b) åé
|
---|
34 | å¸ã®å½¢æ
|
---|
35 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
36 | * å ±åãããã¨ï¼
|
---|
37 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
38 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
39 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
40 | 責ãããã¨ï¼
|
---|
41 | *
|
---|
42 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
43 | ã
|
---|
44 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ãã®é©ç¨å¯è½æ§ã
|
---|
45 | * å«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´
|
---|
46 | * æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ãã®è²¬ä»»ãè² ããªãï¼
|
---|
47 | *
|
---|
48 | * @(#) $Id$
|
---|
49 | */
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * ãã¹ãåââIP ã¢ãã¬ã¹å¤æ
|
---|
53 | */
|
---|
54 |
|
---|
55 | #include <string.h>
|
---|
56 |
|
---|
57 | #ifdef TARGET_KERNEL_ASP
|
---|
58 |
|
---|
59 | #include <kernel.h>
|
---|
60 | #include <t_syslog.h>
|
---|
61 | #include <target_syssvc.h>
|
---|
62 | #define LOGTASK_PORTID SIO_PORTID
|
---|
63 | #include "kernel_cfg.h"
|
---|
64 | #include "tinet_cfg.h"
|
---|
65 |
|
---|
66 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
67 |
|
---|
68 | #ifdef TARGET_KERNEL_JSP
|
---|
69 |
|
---|
70 | #include <s_services.h>
|
---|
71 | #include <t_services.h>
|
---|
72 | #include "kernel_id.h"
|
---|
73 | #include "tinet_id.h"
|
---|
74 |
|
---|
75 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
76 |
|
---|
77 | #include <tinet_config.h>
|
---|
78 |
|
---|
79 | #include <netinet/in.h>
|
---|
80 | #include <netinet/in_itron.h>
|
---|
81 |
|
---|
82 | #include <netapp/netapp.h>
|
---|
83 | #include <netapp/netapp_var.h>
|
---|
84 | #include <netapp/resolver.h>
|
---|
85 |
|
---|
86 | #if defined(USE_RESOLVER)
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * å±æå¤æ°
|
---|
90 | */
|
---|
91 |
|
---|
92 | static uint8_t dns_domain_name[DNS_NAME_LENGTH + 1] = RSLV_CFG_DNS_DOMAIN_NAME_STR;
|
---|
93 |
|
---|
94 | #if defined(SUPPORT_INET6)
|
---|
95 | static T_IN6_ADDR in6_addr_dns = IPV6_ADDR_DNS_INIT;
|
---|
96 | static T_IN6_ADDR in6_addr_dns_init = IPV6_ADDR_DNS_INIT;
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | #if defined(SUPPORT_INET4)
|
---|
100 | static T_IN4_ADDR in4_addr_dns = IPV4_ADDR_DNS;
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | static uint16_t dns_msg_id = 0;
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * get_dns_header -- DNS ã¡ãã»ã¼ã¸ãã DNS ããããåãåºãã
|
---|
107 | */
|
---|
108 |
|
---|
109 | static ER_UINT
|
---|
110 | get_dns_header (uint8_t *msg, uint_t length, T_RSLV_DNS_MSG *rslv)
|
---|
111 | {
|
---|
112 | T_DNS_HDR *dns_hdr;
|
---|
113 |
|
---|
114 | /* DNS ããããåãåºãã*/
|
---|
115 | if (length < sizeof(rslv->dns_hdr))
|
---|
116 | return EV_RSLV_ESIZE;
|
---|
117 | dns_hdr = (T_DNS_HDR*)msg;
|
---|
118 | rslv->dns_hdr.id = MSG2HOST16(dns_hdr->id);
|
---|
119 | rslv->dns_hdr.code = MSG2HOST16(dns_hdr->code);
|
---|
120 | rslv->dns_hdr.qdcount = MSG2HOST16(dns_hdr->qdcount);
|
---|
121 | rslv->dns_hdr.ancount = MSG2HOST16(dns_hdr->ancount);
|
---|
122 | rslv->dns_hdr.nscount = MSG2HOST16(dns_hdr->nscount);
|
---|
123 | rslv->dns_hdr.arcount = MSG2HOST16(dns_hdr->arcount);
|
---|
124 | return sizeof(rslv->dns_hdr);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * dns_strtype -- DNS ã® type ã®æååãè¿ãã
|
---|
129 | */
|
---|
130 |
|
---|
131 | static const char *
|
---|
132 | dns_flag_qtype (uint16_t flags)
|
---|
133 | {
|
---|
134 | if (flags & DNS_LUP_FLAGS_QTYPE_AAAA)
|
---|
135 | return "AAAA";
|
---|
136 | else if (flags & DNS_LUP_FLAGS_QTYPE_A)
|
---|
137 | return " A";
|
---|
138 | else if (flags & DNS_LUP_FLAGS_QTYPE_PTR)
|
---|
139 | return " PTR";
|
---|
140 | else
|
---|
141 | return "unknown type";
|
---|
142 | }
|
---|
143 |
|
---|
144 | #if defined(SUPPORT_INET6)
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * udp6_dns -- UDP/IPv6 ã«ãã DNS ãµã¼ãããã¡ãã»ã¼ã¸ãåä¿¡ããã
|
---|
148 | */
|
---|
149 |
|
---|
150 | static ER
|
---|
151 | udp6_dns (uint16_t flags, T_IN6_ADDR *addr, uint8_t *msg, uint_t msg_size, uint_t length)
|
---|
152 | {
|
---|
153 | T_IPV6EP ep_dns;
|
---|
154 | ER_UINT len = E_OK;
|
---|
155 | int retry;
|
---|
156 |
|
---|
157 | /* DNS ãµã¼ãã® IPv6 ã¢ãã¬ã¹ãæªå®ç¾©ã®æã¯ãã©ã¡ã¼ã¿ã¨ã©ã¼ */
|
---|
158 | if (IN6_IS_ADDR_UNSPECIFIED(addr) || (in6_is_addr_ipv4mapped(addr) && addr->s6_addr32[3] == IPV4_ADDRANY)) {
|
---|
159 | syslog(LOG_NOTICE, "[RSLV] undefined IPv6 DNS server address.");
|
---|
160 | return E_PAR;
|
---|
161 | }
|
---|
162 |
|
---|
163 | memcpy(&ep_dns.ipaddr, addr, sizeof(*addr));
|
---|
164 | ep_dns.portno = RSLV_CFG_DNS_PORTNO;
|
---|
165 | for (retry = NUM_DNS_UDP_RETRY + 1; retry -- > 0; ) {
|
---|
166 | if ((len = udp6_snd_dat(UDP6_RESOLVER_CEPID, &ep_dns, msg, length, TMO_DNS_UDP_SND)) >= 0) {
|
---|
167 | if ((len = udp6_rcv_dat(UDP6_RESOLVER_CEPID, &ep_dns, msg, msg_size, TMO_DNS_UDP_RCV)) >= 0)
|
---|
168 | return len;
|
---|
169 | }
|
---|
170 | if (flags & DNS_LUP_FLAGS_MSG) {
|
---|
171 | if (len == E_TMOUT)
|
---|
172 | syslog(LOG_NOTICE, "[RSLV] TMOUT: %d, type: %s, server: %s.%d.",
|
---|
173 | NUM_DNS_UDP_RETRY + 1 - retry,
|
---|
174 | dns_flag_qtype(flags),
|
---|
175 | ipv62str(NULL, &ep_dns.ipaddr), ep_dns.portno);
|
---|
176 | else {
|
---|
177 | syslog(LOG_NOTICE, "[RSLV] error: %s, type: %s,\n server: %s.%d.",
|
---|
178 | itron_strerror(len),
|
---|
179 | dns_flag_qtype(flags),
|
---|
180 | ipv62str(NULL, &ep_dns.ipaddr), ep_dns.portno);
|
---|
181 | return len;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 | return E_TMOUT;
|
---|
186 | }
|
---|
187 |
|
---|
188 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
189 |
|
---|
190 | #if defined(SUPPORT_INET4)
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * udp4_dns -- UDP/IPv4 ã«ãã DNS ãµã¼ãããã¡ãã»ã¼ã¸ãåä¿¡ããã
|
---|
194 | */
|
---|
195 |
|
---|
196 | static ER
|
---|
197 | udp4_dns (uint16_t flags, T_IN4_ADDR *addr, uint8_t *msg, uint_t msg_size, uint_t length)
|
---|
198 | {
|
---|
199 | T_IPV4EP ep_dns;
|
---|
200 | ER_UINT len = E_OK;
|
---|
201 | int retry;
|
---|
202 |
|
---|
203 | /* DNS ãµã¼ãã® IPv4 ã¢ãã¬ã¹ãæªå®ç¾©ã®æã¯ãã©ã¡ã¼ã¿ã¨ã©ã¼ */
|
---|
204 | if (*addr == IPV4_ADDRANY) {
|
---|
205 | syslog(LOG_NOTICE, "[RSLV] undefined IPv4 DNS server address.");
|
---|
206 | return E_PAR;
|
---|
207 | }
|
---|
208 |
|
---|
209 | memcpy(&ep_dns.ipaddr, addr, sizeof(*addr));
|
---|
210 | ep_dns.portno = RSLV_CFG_DNS_PORTNO;
|
---|
211 | for (retry = NUM_DNS_UDP_RETRY + 1; retry -- > 0; ) {
|
---|
212 | if ((len = udp_snd_dat(UDP4_RESOLVER_CEPID, &ep_dns, msg, length, TMO_DNS_UDP_SND)) >= 0) {
|
---|
213 | if ((len = udp_rcv_dat(UDP4_RESOLVER_CEPID, &ep_dns, msg, msg_size, TMO_DNS_UDP_RCV)) >= 0)
|
---|
214 | return len;
|
---|
215 | }
|
---|
216 | if (flags & DNS_LUP_FLAGS_MSG) {
|
---|
217 | if (len == E_TMOUT)
|
---|
218 | syslog(LOG_NOTICE, "[RSLV] TMOUT: %d, type: %s, server: %s.%d.",
|
---|
219 | NUM_DNS_UDP_RETRY + 1 - retry,
|
---|
220 | dns_flag_qtype(flags),
|
---|
221 | ip2str(NULL, &ep_dns.ipaddr), ep_dns.portno);
|
---|
222 | else {
|
---|
223 | syslog(LOG_NOTICE, "[RSLV] error: %s, type: %s, server: %s.%d.",
|
---|
224 | itron_strerror(len),
|
---|
225 | dns_flag_qtype(flags),
|
---|
226 | ip2str(NULL, &ep_dns.ipaddr), ep_dns.portno);
|
---|
227 | return len;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | return E_TMOUT;
|
---|
232 | }
|
---|
233 |
|
---|
234 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * skip_label -- ã©ãã«ãèªã¿é£ã°ãã
|
---|
238 | */
|
---|
239 |
|
---|
240 | static char *
|
---|
241 | skip_label (char *name)
|
---|
242 | {
|
---|
243 | while (!(*name == '\0' || *name == '.'))
|
---|
244 | name ++;
|
---|
245 | return name;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * add_name -- ã¡ãã»ã¼ã¸ã«ååã追å ããã
|
---|
250 | */
|
---|
251 |
|
---|
252 | static ER_UINT
|
---|
253 | add_name (uint8_t *msg, uint_t size, uint_t offset, char *name)
|
---|
254 | {
|
---|
255 | uint_t label_len;
|
---|
256 | char *tail;
|
---|
257 |
|
---|
258 | while (*name != '\0') {
|
---|
259 |
|
---|
260 | /* ã¡ãã»ã¼ã¸ãµã¤ãºã«éãã¦ãããã¨ã©ã¼ */
|
---|
261 | if (offset + 1 > size)
|
---|
262 | return E_BOVR;
|
---|
263 |
|
---|
264 | /* ã©ãã«ã®çµãããè¦ã¤ããã*/
|
---|
265 | tail = skip_label(name);
|
---|
266 |
|
---|
267 | /* ã©ãã«é·ãè¨å®ããã*/
|
---|
268 | label_len = tail - name;
|
---|
269 | if (label_len > DNS_LABEL_LENGTH) { /* 63 ãªã¯ããããè¶
|
---|
270 | ããã¨ã¨ã©ã¼ */
|
---|
271 | syslog(LOG_NOTICE, "[RSLV] label length(%d) too long > %d.",
|
---|
272 | label_len, DNS_LABEL_LENGTH);
|
---|
273 | return E_PAR;
|
---|
274 | }
|
---|
275 |
|
---|
276 | *(msg + offset) = (uint8_t)label_len;
|
---|
277 | offset ++;
|
---|
278 |
|
---|
279 | /* ã©ãã«åãè¨å®ããã*/
|
---|
280 | if (offset + label_len > size) { /* ã¡ãã»ã¼ã¸ãµã¤ãºãè¶
|
---|
281 | ããã¨ã¨ã©ã¼ */
|
---|
282 | syslog(LOG_NOTICE, "[RSLV] message length(%d) too long > %d.",
|
---|
283 | offset + label_len, size);
|
---|
284 | return E_BOVR;
|
---|
285 | }
|
---|
286 |
|
---|
287 | memcpy((void*)(msg + offset), (void*)name, label_len);
|
---|
288 | offset += label_len;
|
---|
289 |
|
---|
290 | /* 次ã®ã©ãã«ã«ç§»åããã*/
|
---|
291 | if (*tail == '\0')
|
---|
292 | name = tail;
|
---|
293 | else
|
---|
294 | name = tail + 1; /* '.' ãã¹ãããããã*/
|
---|
295 | }
|
---|
296 |
|
---|
297 | return offset;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * add_ipv6addr -- ã¡ãã»ã¼ã¸ã« IPv6 ã¢ãã¬ã¹ã追å ããã
|
---|
302 | */
|
---|
303 |
|
---|
304 | static ER_UINT
|
---|
305 | add_ipv6addr (uint8_t *msg, uint_t size, uint_t offset, char *name)
|
---|
306 | {
|
---|
307 | T_IN6_ADDR addr;
|
---|
308 | ER_UINT error;
|
---|
309 | uint_t len;
|
---|
310 | int col;
|
---|
311 | char digit[2];
|
---|
312 |
|
---|
313 | len = strlen(name);
|
---|
314 | if (len == 0)
|
---|
315 | return E_PAR;
|
---|
316 |
|
---|
317 | digit[1] = '\0';
|
---|
318 | get_ipv6addr(&addr, name);
|
---|
319 | for (col = sizeof(T_IN6_ADDR) * 2; -- col >= 0; ) {
|
---|
320 |
|
---|
321 | /* 16é²æ° 1æ¡ã®å¤ããååã¨ãã¦è¿½å ããã*/
|
---|
322 | digit[0] = (addr.s6_addr8[col / 2] >> ((1 - (col % 2)) << 2)) & 0x0f;
|
---|
323 | if (digit[0] >= 10)
|
---|
324 | digit[0] += 'a' - 10;
|
---|
325 | else
|
---|
326 | digit[0] += '0';
|
---|
327 | if ((error = add_name(msg, size, offset, digit)) < 0)
|
---|
328 | return error;
|
---|
329 | offset = error;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /* "ip6" ã追å ããã*/
|
---|
333 | if ((error = add_name(msg, size, offset, "ip6")) < 0)
|
---|
334 | return error;
|
---|
335 |
|
---|
336 | /* "arpa" ã追å ããã*/
|
---|
337 | return add_name(msg, size, error, "arpa");
|
---|
338 | }
|
---|
339 |
|
---|
340 | /*
|
---|
341 | * add_ipv4addr -- ã¡ãã»ã¼ã¸ã« IPv4 ã¢ãã¬ã¹ã追å ããã
|
---|
342 | */
|
---|
343 |
|
---|
344 | static ER_UINT
|
---|
345 | add_ipv4addr (uint8_t *msg, uint_t size, uint_t offset, char *name)
|
---|
346 | {
|
---|
347 | T_IN4_ADDR addr;
|
---|
348 | ER_UINT error;
|
---|
349 | uint_t len, octet;
|
---|
350 | int col;
|
---|
351 | char *op, ostr[4];
|
---|
352 |
|
---|
353 | len = strlen(name);
|
---|
354 | if (len == 0)
|
---|
355 | return E_PAR;
|
---|
356 |
|
---|
357 | ostr[3] = '\0';
|
---|
358 | get_ipv4addr(&addr, name);
|
---|
359 | for (col = 0; col < sizeof(T_IN4_ADDR); col ++) {
|
---|
360 |
|
---|
361 | /* 1ãªã¯ãããã®å¤ããæ´æ°å¤ãããªãã©ã«ã«å¤æããã*/
|
---|
362 | octet = (addr >> (col << 3)) & 0xff;
|
---|
363 | op = &ostr[3];
|
---|
364 | while (octet > 0) {
|
---|
365 | *(-- op) = octet % 10 + '0';
|
---|
366 | octet /= 10;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /* 1ãªã¯ãããã®å¤ããååã¨ãã¦è¿½å ããã*/
|
---|
370 | if ((error = add_name(msg, size, offset, op)) < 0)
|
---|
371 | return error;
|
---|
372 | offset = error;
|
---|
373 | }
|
---|
374 |
|
---|
375 | /* "in-addr" ã追å ããã*/
|
---|
376 | if ((error = add_name(msg, size, offset, "in-addr")) < 0)
|
---|
377 | return error;
|
---|
378 |
|
---|
379 | /* "arpa" ã追å ããã*/
|
---|
380 | return add_name(msg, size, error, "arpa");
|
---|
381 | }
|
---|
382 |
|
---|
383 | /*
|
---|
384 | * setup_dns_msg -- DNS ã¡ãã»ã¼ã¸ãä½æããã
|
---|
385 | */
|
---|
386 |
|
---|
387 | static ER
|
---|
388 | setup_dns_msg (uint16_t flags, char *name, uint8_t *msg, uint_t msg_size)
|
---|
389 | {
|
---|
390 | T_DNS_HDR dns_hdr;
|
---|
391 | ER_UINT offset;
|
---|
392 |
|
---|
393 | memset((void*)&dns_hdr, 0, sizeof(dns_hdr));
|
---|
394 |
|
---|
395 | dns_msg_id++;
|
---|
396 | HOST2MSG16(dns_hdr.id, dns_msg_id);
|
---|
397 | HOST2MSG16(dns_hdr.code, (flags & DNS_OPCODE_MASK) | DNS_RECURSION_DESIRED);
|
---|
398 | HOST2MSG16(dns_hdr.qdcount, 1);
|
---|
399 |
|
---|
400 | memcpy((void*)msg, (void*)&dns_hdr, sizeof(dns_hdr));
|
---|
401 | offset = sizeof(dns_hdr);
|
---|
402 |
|
---|
403 | switch (flags & DNS_LUP_FLAGS_NAME_MASK) {
|
---|
404 |
|
---|
405 | case DNS_LUP_FLAGS_NAME_HOST:
|
---|
406 | case DNS_LUP_FLAGS_NAME_FQDN:
|
---|
407 |
|
---|
408 | /* ãã¹ãåãã¾ã㯠FQDN ã®æã®å¦çã*/
|
---|
409 | /* ãã¹ãåã追å ããã*/
|
---|
410 | if ((offset = add_name(msg, msg_size, offset, name)) < 0)
|
---|
411 | return offset;
|
---|
412 |
|
---|
413 | if (offset - sizeof(dns_hdr) > DNS_NAME_LENGTH) { /* ååã 255 ãªã¯ããããè¶
|
---|
414 | ããã¨ã¨ã©ã¼ */
|
---|
415 | syslog(LOG_NOTICE, "[RSLV] name length(%d) too long > %d.",
|
---|
416 | offset - sizeof(dns_hdr), DNS_NAME_LENGTH);
|
---|
417 | return E_PAR;
|
---|
418 | }
|
---|
419 |
|
---|
420 | /* ãã¹ãåã ãã®æã¯ããã¡ã¤ã³åã追å ããã*/
|
---|
421 | if ((flags & DNS_LUP_FLAGS_NAME_MASK) == DNS_LUP_FLAGS_NAME_HOST) {
|
---|
422 |
|
---|
423 | /* ãã¡ã¤ã³åã追å ããã*/
|
---|
424 | if ((offset = add_name(msg, msg_size, offset, (char *)dns_domain_name)) < 0)
|
---|
425 | return offset;
|
---|
426 |
|
---|
427 | if (offset - sizeof(dns_hdr) > DNS_NAME_LENGTH) { /* ååã 255 ãªã¯ããããè¶
|
---|
428 | ããã¨ã¨ã©ã¼ */
|
---|
429 | syslog(LOG_NOTICE, "[RSLV] name length(%d) too long > %d.",
|
---|
430 | offset - sizeof(dns_hdr), DNS_NAME_LENGTH);
|
---|
431 | return E_PAR;
|
---|
432 | }
|
---|
433 | }
|
---|
434 | break;
|
---|
435 |
|
---|
436 | case DNS_LUP_FLAGS_NAME_IPV4:
|
---|
437 | if ((offset = add_ipv4addr(msg, msg_size, offset, name)) < 0)
|
---|
438 | return offset;
|
---|
439 | break;
|
---|
440 |
|
---|
441 | case DNS_LUP_FLAGS_NAME_IPV6:
|
---|
442 | if ((offset = add_ipv6addr(msg, msg_size, offset, name)) < 0)
|
---|
443 | return offset;
|
---|
444 | break;
|
---|
445 |
|
---|
446 | default:
|
---|
447 | syslog(LOG_NOTICE, "[RSLV] unknown name type: %04x.", flags & DNS_LUP_FLAGS_NAME_MASK);
|
---|
448 | return E_PAR;
|
---|
449 | break;
|
---|
450 |
|
---|
451 | }
|
---|
452 |
|
---|
453 | /* ååã®çµäºã³ã¼ããTYPEãCLASS ã追å ãã¦ãã¡ãã»ã¼ã¸é·ãè¶
|
---|
454 | ããªããã¨ã確èªããã*/
|
---|
455 | if (offset + sizeof(uint16_t) * 2 + 1 > msg_size) {
|
---|
456 | syslog(LOG_NOTICE, "[RSLV] message length(%d) too long > %d.",
|
---|
457 | offset + sizeof(uint16_t) * 2 + 1, msg_size);
|
---|
458 | return E_BOVR;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /* ååã®çµäºã³ã¼ããè¨å®ããã*/
|
---|
462 | *(msg + offset) = '\0';
|
---|
463 | offset ++;
|
---|
464 |
|
---|
465 | /* DNS ã® TYPE ãè¨å®ããã*/
|
---|
466 | if (flags & DNS_LUP_FLAGS_QTYPE_PTR)
|
---|
467 | host2msg16(msg + offset, DNS_TYPE_PTR);
|
---|
468 | else if (flags & DNS_LUP_FLAGS_QTYPE_AAAA)
|
---|
469 | host2msg16(msg + offset, DNS_TYPE_AAAA);
|
---|
470 | else if (flags & DNS_LUP_FLAGS_QTYPE_A)
|
---|
471 | host2msg16(msg + offset, DNS_TYPE_A);
|
---|
472 | else {
|
---|
473 | syslog(LOG_NOTICE, "[RSLV] unknown query type: %04x.", flags & DNS_LUP_FLAGS_QTYPE_MASK);
|
---|
474 | return E_PAR;
|
---|
475 | }
|
---|
476 | offset += sizeof(uint16_t);
|
---|
477 |
|
---|
478 | /* DNS ã® CLASS ãè¨å®ããã*/
|
---|
479 | host2msg16(msg + offset, DNS_CLASS_IN);
|
---|
480 |
|
---|
481 | return offset + sizeof(uint16_t);
|
---|
482 | }
|
---|
483 |
|
---|
484 | /*
|
---|
485 | * skip_dns_name -- DNS ã¡ãã»ã¼ã¸ã®ååãèªã¿é£ã°ãã
|
---|
486 | *
|
---|
487 | * 注æ: å§ç¸®å½¢å¼ãèæ
|
---|
488 | ®ãããä¸ä½ 2ãããã 11ãªãå§ç¸®å½¢å¼ã
|
---|
489 | */
|
---|
490 |
|
---|
491 | static ER_UINT
|
---|
492 | skip_dns_name (uint8_t *name, uint_t offset, uint_t length)
|
---|
493 | {
|
---|
494 | while (*(name + offset)) {
|
---|
495 | if ((*(name + offset) & 0xc0) == 0xc0) {
|
---|
496 | if (offset + 2 > length)
|
---|
497 | return EV_RSLV_ESIZE;
|
---|
498 | else
|
---|
499 | return offset + 2;
|
---|
500 | }
|
---|
501 | else if (offset + *name + 1 > length)
|
---|
502 | return EV_RSLV_ESIZE;
|
---|
503 | else
|
---|
504 | offset += *name + 1;
|
---|
505 | }
|
---|
506 | return offset + 1;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /*
|
---|
510 | * skip_dns_qd -- DNS Question section ãèªã¿é£ã°ãã
|
---|
511 | */
|
---|
512 |
|
---|
513 | static ER_UINT
|
---|
514 | skip_dns_qd (uint8_t *msg, uint_t offset, uint_t length, uint_t count)
|
---|
515 | {
|
---|
516 | ER_UINT next;
|
---|
517 |
|
---|
518 | while (count != 0) {
|
---|
519 | /* name ãèªã¿é£ã°ãã*/
|
---|
520 | if ((next = skip_dns_name(msg, offset, length)) < 0)
|
---|
521 | return next;
|
---|
522 | offset = next;
|
---|
523 |
|
---|
524 | /* typeãclass ãèªã¿é£ã°ãã*/
|
---|
525 | if ((offset + sizeof(uint16_t) + sizeof(uint16_t)) > length)
|
---|
526 | return EV_RSLV_ESIZE;
|
---|
527 | offset += sizeof(uint16_t) + sizeof(uint16_t);
|
---|
528 |
|
---|
529 | count --;
|
---|
530 | }
|
---|
531 |
|
---|
532 | return offset;
|
---|
533 | }
|
---|
534 |
|
---|
535 | /*
|
---|
536 | * skip_dns_rr -- DNS RR ãèªã¿é£ã°ãã
|
---|
537 | */
|
---|
538 |
|
---|
539 | static ER_UINT
|
---|
540 | skip_dns_rr (uint8_t *msg, uint_t offset, uint_t length, uint_t count)
|
---|
541 | {
|
---|
542 | ER_UINT next;
|
---|
543 | uint16_t len = 0;
|
---|
544 |
|
---|
545 | while (count != 0) {
|
---|
546 | /* name ãèªã¿é£ã°ãã*/
|
---|
547 | if ((next = skip_dns_name(msg, offset, length)) < 0)
|
---|
548 | return next;
|
---|
549 | offset = next;
|
---|
550 |
|
---|
551 | /* typeãclassãTTL ãèªã¿é£ã°ãã*/
|
---|
552 | if (offset + sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t) > length)
|
---|
553 | return EV_RSLV_ESIZE;
|
---|
554 | offset += sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t);
|
---|
555 |
|
---|
556 | /* RD length 㨠RDATA ãèªã¿é£ã°ãã*/
|
---|
557 | if (offset + sizeof(uint16_t) > length)
|
---|
558 | return EV_RSLV_ESIZE;
|
---|
559 | len = msg2host16(msg + offset);
|
---|
560 |
|
---|
561 | if (offset + sizeof(uint16_t) + len > length)
|
---|
562 | return EV_RSLV_ESIZE;
|
---|
563 | offset += sizeof(uint16_t) + len;
|
---|
564 |
|
---|
565 | count --;
|
---|
566 | }
|
---|
567 |
|
---|
568 | return offset;
|
---|
569 | }
|
---|
570 |
|
---|
571 | /*
|
---|
572 | * dns_analyze_qd -- DNS ã¡ãã»ã¼ã¸ã® question section ã解æããã
|
---|
573 | */
|
---|
574 |
|
---|
575 | ER_UINT
|
---|
576 | dns_analyze_qd (T_RSLV_QD *qd, uint_t offset, uint8_t *msg, uint_t length)
|
---|
577 | {
|
---|
578 | ER_UINT error;
|
---|
579 | T_DNS_QD *dns_qd;
|
---|
580 |
|
---|
581 | if ((error = skip_dns_name(msg, offset, length)) < 0)
|
---|
582 | return error;
|
---|
583 | offset = error;
|
---|
584 |
|
---|
585 | /* DNS ã¡ãã»ã¼ã¸å
|
---|
586 | ã®å½¢å¼ã®ãµã¤ãºã§ãã§ãã¯ããã*/
|
---|
587 | if (offset + T_DNS_QD_SIZE > length)
|
---|
588 | return EV_RSLV_ESIZE;
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * DNS ã¡ãã»ã¼ã¸å
|
---|
592 | ã® SOA RDATA ããã¤ããªã¼ãã¼ã調æ´ãã¦
|
---|
593 | * å
|
---|
594 | é¨æ§é ä½ã«ã³ãã¼ããã
|
---|
595 | */
|
---|
596 | dns_qd = (T_DNS_QD*)(msg + offset);
|
---|
597 | qd->type = MSG2HOST16(dns_qd->type);
|
---|
598 | qd->class = MSG2HOST16(dns_qd->class);
|
---|
599 |
|
---|
600 | return offset + T_DNS_QD_SIZE;
|
---|
601 | }
|
---|
602 |
|
---|
603 | /*
|
---|
604 | * dns_analyze_soa -- DNS ã¡ãã»ã¼ã¸ã® SOA RDATA ã解æããã
|
---|
605 | */
|
---|
606 |
|
---|
607 | ER_UINT
|
---|
608 | dns_analyze_soa (T_RSLV_SOA *soa, uint_t offset, uint8_t *msg, uint_t length)
|
---|
609 | {
|
---|
610 | ER_UINT error;
|
---|
611 | T_DNS_SOA *dns_soa;
|
---|
612 |
|
---|
613 | /* MNAME ãã¹ãããããã*/
|
---|
614 | if ((error = skip_dns_name(msg, offset, length)) < 0)
|
---|
615 | return error;
|
---|
616 | offset = error;
|
---|
617 |
|
---|
618 | /* RNAME ãã¹ãããããã*/
|
---|
619 | if ((error = skip_dns_name(msg, offset, length)) < 0)
|
---|
620 | return error;
|
---|
621 | offset = error;
|
---|
622 |
|
---|
623 | /* DNS ã¡ãã»ã¼ã¸å
|
---|
624 | ã®å½¢å¼ã®ãµã¤ãºã§ãã§ãã¯ããã*/
|
---|
625 | if (offset + T_DNS_SOA_SIZE > length)
|
---|
626 | return EV_RSLV_ESIZE;
|
---|
627 |
|
---|
628 | /*
|
---|
629 | * DNS ã¡ãã»ã¼ã¸å
|
---|
630 | ã® SOA RDATA ããã¤ããªã¼ãã¼ã調æ´ãã¦
|
---|
631 | * å
|
---|
632 | é¨æ§é ä½ã«ã³ãã¼ããã
|
---|
633 | */
|
---|
634 | dns_soa = (T_DNS_SOA*)(msg + offset);
|
---|
635 | soa->serial = MSG2HOST32(dns_soa->serial);
|
---|
636 | soa->refresh = MSG2HOST32(dns_soa->refresh);
|
---|
637 | soa->retry = MSG2HOST32(dns_soa->retry);
|
---|
638 | soa->expire = MSG2HOST32(dns_soa->expire);
|
---|
639 | soa->minimum = MSG2HOST32(dns_soa->minimum);
|
---|
640 |
|
---|
641 | return offset + T_DNS_SOA_SIZE;
|
---|
642 | }
|
---|
643 |
|
---|
644 | /*
|
---|
645 | * dns_analyze_rr -- DNS ã¡ãã»ã¼ã¸ã® AN/NS/AR section ã解æããã
|
---|
646 | */
|
---|
647 |
|
---|
648 | ER_UINT
|
---|
649 | dns_analyze_rr (T_RSLV_RR *rr, uint_t offset, uint8_t *msg, uint_t length)
|
---|
650 | {
|
---|
651 | ER_UINT error;
|
---|
652 | T_DNS_RR *dns_rr;
|
---|
653 |
|
---|
654 | if ((error = skip_dns_name(msg, offset, length)) < 0)
|
---|
655 | return error;
|
---|
656 | offset = error;
|
---|
657 |
|
---|
658 | /* DNS ã¡ãã»ã¼ã¸å
|
---|
659 | ã®å½¢å¼ã®ãµã¤ãºã§ãã§ãã¯ããã*/
|
---|
660 | if (offset + T_DNS_RR_SIZE > length)
|
---|
661 | return EV_RSLV_ESIZE;
|
---|
662 |
|
---|
663 | /*
|
---|
664 | * DNS ã¡ãã»ã¼ã¸å
|
---|
665 | ã® SOA RDATA ããã¤ããªã¼ãã¼ã調æ´ãã¦
|
---|
666 | * å
|
---|
667 | é¨æ§é ä½ã«ã³ãã¼ããã
|
---|
668 | */
|
---|
669 | dns_rr = (T_DNS_RR*)(msg + offset);
|
---|
670 | rr->type = MSG2HOST16(dns_rr->type);
|
---|
671 | rr->class = MSG2HOST16(dns_rr->class);
|
---|
672 | rr->ttl = MSG2HOST32(dns_rr->ttl);
|
---|
673 | rr->rdlength = MSG2HOST16(dns_rr->rdlength);
|
---|
674 | rr->rdata_offset= offset + sizeof(rr->type) + sizeof(rr->class)
|
---|
675 | + sizeof(rr->ttl) + sizeof(rr->rdlength);
|
---|
676 |
|
---|
677 | return offset + T_DNS_RR_SIZE + rr->rdlength;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /*
|
---|
681 | * analyze_dns_msg -- DNS ã¡ãã»ã¼ã¸ã解æããã
|
---|
682 | */
|
---|
683 |
|
---|
684 | static ER_UINT
|
---|
685 | analyze_dns_msg (uint8_t *msg, uint_t length, T_RSLV_DNS_MSG *rslv)
|
---|
686 | {
|
---|
687 | ER_UINT offset;
|
---|
688 |
|
---|
689 | /* DNS ããããåãåºãã*/
|
---|
690 | if ((offset = get_dns_header(msg, length, rslv)) < 0)
|
---|
691 | return offset;
|
---|
692 |
|
---|
693 | /* question section ãèªã¿é£ã°ãã*/
|
---|
694 | rslv->qd_offset = offset;
|
---|
695 | if ((offset = skip_dns_qd(msg, offset, length, rslv->dns_hdr.qdcount)) < 0)
|
---|
696 | return offset;
|
---|
697 |
|
---|
698 | /* answer section ãèªã¿é£ã°ãã*/
|
---|
699 | rslv->an_offset = offset;
|
---|
700 | if ((offset = skip_dns_rr(msg, offset, length, rslv->dns_hdr.ancount)) < 0)
|
---|
701 | return offset;
|
---|
702 |
|
---|
703 | /* authority records section ãèªã¿é£ã°ãã*/
|
---|
704 | rslv->ns_offset = offset;
|
---|
705 | if ((offset = skip_dns_rr(msg, offset, length, rslv->dns_hdr.nscount)) < 0)
|
---|
706 | return offset;
|
---|
707 |
|
---|
708 | /* additional records section ãèªã¿é£ã°ãã*/
|
---|
709 | rslv->ar_offset = offset;
|
---|
710 | if ((offset = skip_dns_rr(msg, offset, length, rslv->dns_hdr.arcount)) < 0)
|
---|
711 | return offset;
|
---|
712 |
|
---|
713 | return E_OK;
|
---|
714 | }
|
---|
715 |
|
---|
716 | /*
|
---|
717 | * dns_lookup_host_sub -- DNS ã«ãããã¹ãã®æ
|
---|
718 | å ±ãåå¾ããï¼è£å©é¢æ°ï¼ã
|
---|
719 | */
|
---|
720 |
|
---|
721 | static ER_UINT
|
---|
722 | dns_lookup_host_sub (uint16_t flags, char *hostname, uint8_t *msg,
|
---|
723 | uint_t msg_size, T_RSLV_DNS_MSG *rslv)
|
---|
724 | {
|
---|
725 | #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4)
|
---|
726 | uint16_t proto;
|
---|
727 | #endif
|
---|
728 | ER_UINT length;
|
---|
729 | ER error;
|
---|
730 |
|
---|
731 | if ((length = setup_dns_msg(flags, hostname, msg, msg_size)) < 0)
|
---|
732 | return length;
|
---|
733 |
|
---|
734 | #if defined(SUPPORT_INET6)
|
---|
735 |
|
---|
736 | #if defined(SUPPORT_INET4)
|
---|
737 |
|
---|
738 | if (flags & DNS_LUP_FLAGS_PROTO_IPV6) {
|
---|
739 | proto = DNS_LUP_FLAGS_PROTO_IPV6;
|
---|
740 | if ((length = udp6_dns(flags, &in6_addr_dns, msg, msg_size, length)) < 0)
|
---|
741 | return length;
|
---|
742 | }
|
---|
743 | else if (flags & DNS_LUP_FLAGS_PROTO_IPV4) {
|
---|
744 | proto = DNS_LUP_FLAGS_PROTO_IPV4;
|
---|
745 | if ((length = udp4_dns(flags, &in4_addr_dns, msg, msg_size, length)) < 0)
|
---|
746 | return length;
|
---|
747 | }
|
---|
748 | else {
|
---|
749 | syslog(LOG_NOTICE, "[RSLV] unknown lookup protocol: %04x.", flags & DNS_LUP_FLAGS_PROTO_MASK);
|
---|
750 | return E_PAR;
|
---|
751 | }
|
---|
752 |
|
---|
753 | #else /* of #if defined(SUPPORT_INET4) */
|
---|
754 |
|
---|
755 | if (flags & DNS_LUP_FLAGS_PROTO_IPV6) {
|
---|
756 | if ((length = udp6_dns(flags, &in6_addr_dns, msg, msg_size, length)) < 0)
|
---|
757 | return length;
|
---|
758 | }
|
---|
759 | else {
|
---|
760 | syslog(LOG_NOTICE, "[RSLV] unknown lookup protocol: %04x.", flags & DNS_LUP_FLAGS_PROTO_MASK);
|
---|
761 | return E_PAR;
|
---|
762 | }
|
---|
763 |
|
---|
764 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
765 |
|
---|
766 | #else /* of #if defined(SUPPORT_INET6) */
|
---|
767 |
|
---|
768 | #if defined(SUPPORT_INET4)
|
---|
769 |
|
---|
770 | if (flags & DNS_LUP_FLAGS_PROTO_IPV4) {
|
---|
771 | if ((length = udp4_dns(flags, &in4_addr_dns, msg, msg_size, length)) < 0)
|
---|
772 | return length;
|
---|
773 | }
|
---|
774 | else {
|
---|
775 | syslog(LOG_NOTICE, "[RSLV] unknown lookup protocol: %04x.", flags & DNS_LUP_FLAGS_PROTO_MASK);
|
---|
776 | return E_PAR;
|
---|
777 | }
|
---|
778 |
|
---|
779 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
780 |
|
---|
781 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
782 |
|
---|
783 | if ((error = analyze_dns_msg(msg, length, rslv)) != E_OK)
|
---|
784 | return error;
|
---|
785 | else {
|
---|
786 | if ((rslv->dns_hdr.ancount > 0) && (flags & DNS_LUP_FLAGS_MSG)) {
|
---|
787 |
|
---|
788 | #if defined(SUPPORT_INET6)
|
---|
789 |
|
---|
790 | #if defined(SUPPORT_INET4)
|
---|
791 |
|
---|
792 | if (proto & DNS_LUP_FLAGS_PROTO_IPV6) {
|
---|
793 | syslog(LOG_NOTICE, "[RSLV] reslv: type: %s, server: %s.%d.",
|
---|
794 | dns_flag_qtype(flags),
|
---|
795 | ipv62str(NULL, &in6_addr_dns),
|
---|
796 | RSLV_CFG_DNS_PORTNO);
|
---|
797 | }
|
---|
798 | else {
|
---|
799 | syslog(LOG_NOTICE, "[RSLV] reslv: type: %s, server: %s.%d.",
|
---|
800 | dns_flag_qtype(flags),
|
---|
801 | ip2str(NULL, &in4_addr_dns),
|
---|
802 | RSLV_CFG_DNS_PORTNO);
|
---|
803 | }
|
---|
804 |
|
---|
805 | #else /* of #if defined(SUPPORT_INET4) */
|
---|
806 |
|
---|
807 | syslog(LOG_NOTICE, "[RSLV] reslv: type: %s, server: %s.%d.",
|
---|
808 | dns_flag_qtype(flags),
|
---|
809 | ipv62str(NULL, &in6_addr_dns),
|
---|
810 | RSLV_CFG_DNS_PORTNO);
|
---|
811 |
|
---|
812 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
813 |
|
---|
814 | #else /* of #if defined(SUPPORT_INET6) */
|
---|
815 |
|
---|
816 | #if defined(SUPPORT_INET4)
|
---|
817 |
|
---|
818 | syslog(LOG_NOTICE, "[RSLV] reslv: type: %s, server: %s.%d.",
|
---|
819 | dns_flag_qtype(flags),
|
---|
820 | ip2str(NULL, &in4_addr_dns),
|
---|
821 | RSLV_CFG_DNS_PORTNO);
|
---|
822 |
|
---|
823 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
824 |
|
---|
825 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
826 |
|
---|
827 | }
|
---|
828 | return length;
|
---|
829 | }
|
---|
830 | }
|
---|
831 |
|
---|
832 | #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4)
|
---|
833 |
|
---|
834 | /*
|
---|
835 | * udp64_dns -- DNS/IPv6ãDNS/IPv4 ã«ãã DNS ãµã¼ãããã¡ãã»ã¼ã¸ãåä¿¡ããã
|
---|
836 | */
|
---|
837 |
|
---|
838 | static ER_UINT
|
---|
839 | udp64_dns (uint16_t flags, char *hostname, uint8_t *msg,
|
---|
840 | uint_t msg_size, T_RSLV_DNS_MSG *rslv)
|
---|
841 | {
|
---|
842 | ER_UINT length;
|
---|
843 | ER error;
|
---|
844 | uint16_t proto;
|
---|
845 |
|
---|
846 | /* DNS ã¡ãã»ã¼ã¸ãä½æããã*/
|
---|
847 | if ((length = setup_dns_msg(flags, hostname, msg, msg_size)) < 0)
|
---|
848 | return length;
|
---|
849 |
|
---|
850 | /* UDP/IPv6 ã® DNS ãµã¼ãã«ç
|
---|
851 | §ä¼ããã*/
|
---|
852 | proto = DNS_LUP_FLAGS_PROTO_IPV6;
|
---|
853 | if ((length = udp6_dns(flags, &in6_addr_dns, msg, msg_size, length)) < 0) {
|
---|
854 | /* UDP/IPv6 ã® DNS ãµã¼ãã¸ã®ç
|
---|
855 | §ä¼ãã¨ã©ã¼ã«ãªã£ãæã¯ãUDP/IPv4 ã® DNS ãµã¼ãã«ç
|
---|
856 | §ä¼ããã*/
|
---|
857 | if ((length = setup_dns_msg(flags, hostname, msg, msg_size)) < 0)
|
---|
858 | return length;
|
---|
859 |
|
---|
860 | /* UDP/IPv4 ã® DNS ãµã¼ãã«ç
|
---|
861 | §ä¼ããã*/
|
---|
862 | proto = DNS_LUP_FLAGS_PROTO_IPV4;
|
---|
863 | if ((length = udp4_dns(flags, &in4_addr_dns, msg, msg_size, length)) < 0)
|
---|
864 | return length;
|
---|
865 | }
|
---|
866 |
|
---|
867 | /* DNS ã¡ãã»ã¼ã¸ã解æããã*/
|
---|
868 | if ((error = analyze_dns_msg(msg, length, rslv)) != E_OK)
|
---|
869 | return error;
|
---|
870 | else {
|
---|
871 | if ((rslv->dns_hdr.ancount > 0) && (flags & DNS_LUP_FLAGS_MSG)) {
|
---|
872 | if (proto & DNS_LUP_FLAGS_PROTO_IPV6) {
|
---|
873 | syslog(LOG_NOTICE, "[RSLV] reslv: type: %s, server: %s.%d.",
|
---|
874 | dns_flag_qtype(flags),
|
---|
875 | ipv62str(NULL, &in6_addr_dns),
|
---|
876 | RSLV_CFG_DNS_PORTNO);
|
---|
877 | }
|
---|
878 | else {
|
---|
879 | syslog(LOG_NOTICE, "[RSLV] reslv: type: %s, server: %s.%d.",
|
---|
880 | dns_flag_qtype(flags),
|
---|
881 | ip2str(NULL, &in4_addr_dns),
|
---|
882 | RSLV_CFG_DNS_PORTNO);
|
---|
883 | }
|
---|
884 | }
|
---|
885 | return length;
|
---|
886 | }
|
---|
887 | }
|
---|
888 |
|
---|
889 | #endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
|
---|
890 |
|
---|
891 | /*
|
---|
892 | * dns_lookup_host -- DNS ã«ãããã¹ãã®æ
|
---|
893 | å ±ãåå¾ããã
|
---|
894 | */
|
---|
895 |
|
---|
896 | ER_UINT
|
---|
897 | dns_lookup_host (uint16_t flags, char *hostname, uint8_t *msg,
|
---|
898 | uint_t msg_size, T_RSLV_DNS_MSG *rslv)
|
---|
899 | {
|
---|
900 | ER error;
|
---|
901 |
|
---|
902 | #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4)
|
---|
903 |
|
---|
904 | if ((flags & DNS_LUP_FLAGS_PROTO_MASK) == (DNS_LUP_FLAGS_PROTO_IPV6 | DNS_LUP_FLAGS_PROTO_IPV4)) {
|
---|
905 |
|
---|
906 | /*
|
---|
907 | * IPv6/IPv4 ã®ä¸¡æ¹ãæå®ãããæã®å¦ç
|
---|
908 | * ã¾ã type == AAAA 㧠DNS ãµã¼ãã«ç
|
---|
909 | §ä¼ããã
|
---|
910 | */
|
---|
911 | if (flags & DNS_LUP_FLAGS_QTYPE_PTR) {
|
---|
912 | if ((error = udp64_dns(flags, hostname, msg, msg_size, rslv)) < 0)
|
---|
913 | return error;
|
---|
914 | }
|
---|
915 | else if (flags & DNS_LUP_FLAGS_QTYPE_AAAA) {
|
---|
916 | if ((error = udp64_dns(flags & ~DNS_LUP_FLAGS_QTYPE_A, hostname, msg, msg_size, rslv)) < 0)
|
---|
917 | return error;
|
---|
918 | }
|
---|
919 | else {
|
---|
920 | HOST2MSG16(rslv->dns_hdr.ancount, 0);
|
---|
921 | error = E_OK;
|
---|
922 | }
|
---|
923 |
|
---|
924 | if (rslv->dns_hdr.ancount == 0) {
|
---|
925 |
|
---|
926 | /*
|
---|
927 | * type == AAAA ã§ã¢ãã¬ã¹è§£æ±ºãã§ããªãã£ãæã®å¦ç
|
---|
928 | * type == A 㧠DNS ãµã¼ãã«ç
|
---|
929 | §ä¼ããã
|
---|
930 | */
|
---|
931 | if (flags & DNS_LUP_FLAGS_QTYPE_A)
|
---|
932 | return udp64_dns(flags & ~DNS_LUP_FLAGS_QTYPE_AAAA, hostname, msg, msg_size, rslv);
|
---|
933 | else {
|
---|
934 | syslog(LOG_NOTICE, "[RSLV] unknown query type: %04x.", flags & DNS_LUP_FLAGS_QTYPE_MASK);
|
---|
935 | return E_PAR;
|
---|
936 | }
|
---|
937 | }
|
---|
938 | else
|
---|
939 | return error;
|
---|
940 | }
|
---|
941 |
|
---|
942 | if ((flags & DNS_LUP_FLAGS_QTYPE_MASK) == (DNS_LUP_FLAGS_QTYPE_AAAA | DNS_LUP_FLAGS_QTYPE_A)) {
|
---|
943 |
|
---|
944 | /*
|
---|
945 | * type == AAAA 㨠A ã®ä¸¡æ¹ãæå®ãããæã®å¦ç
|
---|
946 | * ã¾ã type == AAAA 㧠DNS ãµã¼ãã«ç
|
---|
947 | §ä¼ããã
|
---|
948 | */
|
---|
949 | if ((error = dns_lookup_host_sub(flags & ~DNS_LUP_FLAGS_QTYPE_A, hostname, msg, msg_size, rslv)) < 0)
|
---|
950 | return error;
|
---|
951 |
|
---|
952 | if (rslv->dns_hdr.ancount == 0) {
|
---|
953 |
|
---|
954 | /*
|
---|
955 | * type == AAAA ã§ã¢ãã¬ã¹è§£æ±ºãã§ããªãã£ãæã®å¦ç
|
---|
956 | * type == A 㧠DNS ãµã¼ãã«ç
|
---|
957 | §ä¼ããã
|
---|
958 | */
|
---|
959 | return dns_lookup_host_sub(flags & ~DNS_LUP_FLAGS_QTYPE_AAAA, hostname, msg, msg_size, rslv);
|
---|
960 | }
|
---|
961 | #if 0
|
---|
962 | else {
|
---|
963 | syslog(LOG_NOTICE, "[RSLV] unknown query type: %04x.", flags & DNS_LUP_FLAGS_QTYPE_MASK);
|
---|
964 | return E_PAR;
|
---|
965 | }
|
---|
966 | #endif
|
---|
967 | }
|
---|
968 |
|
---|
969 | else {
|
---|
970 | /* IPv6/IPv4 ã®ã©ã¡ããä¸æ¹ãæå®ãããæã®å¦ç */
|
---|
971 | error = dns_lookup_host_sub(flags, hostname, msg, msg_size, rslv);
|
---|
972 | }
|
---|
973 |
|
---|
974 | #else /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
|
---|
975 |
|
---|
976 | error = dns_lookup_host_sub(flags, hostname, msg, msg_size, rslv);
|
---|
977 |
|
---|
978 | #endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
|
---|
979 |
|
---|
980 | return error;
|
---|
981 | }
|
---|
982 |
|
---|
983 | #if defined(SUPPORT_INET6)
|
---|
984 |
|
---|
985 | /*
|
---|
986 | * dns_in6_set_dname -- DNS ã® ãã¡ã¤ã³åãè¨å®ããï¼DHCPv6ç¨ï¼ã
|
---|
987 | *
|
---|
988 | * 注æ: new ã« NULL ãæå®ããã¨ãåæå¤ã«æ»ãã
|
---|
989 | */
|
---|
990 |
|
---|
991 | const uint8_t *
|
---|
992 | dns_in6_set_dname (const uint8_t *new, uint_t len)
|
---|
993 | {
|
---|
994 | uint8_t *dst;
|
---|
995 | int label_len, name_len = DNS_NAME_LENGTH;
|
---|
996 |
|
---|
997 | if (new == NULL)
|
---|
998 | strcpy(dns_domain_name, RSLV_CFG_DNS_DOMAIN_NAME_STR);
|
---|
999 | else {
|
---|
1000 | dst = dns_domain_name;
|
---|
1001 | while ((*new != 0) && (name_len > 0) && (len > 0)) {
|
---|
1002 | label_len = *new ++;
|
---|
1003 | len --;
|
---|
1004 | while ((name_len > 0) && (len > 0) && (label_len > 0)) {
|
---|
1005 | *dst ++ = *new ++;
|
---|
1006 | name_len --;
|
---|
1007 | len --;
|
---|
1008 | label_len --;
|
---|
1009 | }
|
---|
1010 | if ((*new != 0) && (name_len > 0)) {
|
---|
1011 | *dst ++ = '.';
|
---|
1012 | name_len --;
|
---|
1013 | }
|
---|
1014 | }
|
---|
1015 | *dst = '\0';
|
---|
1016 | }
|
---|
1017 | return dns_domain_name;
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | /*
|
---|
1021 | * dns_in6_get_dname -- DNS ã® ãã¡ã¤ã³åãå¾ãã
|
---|
1022 | */
|
---|
1023 |
|
---|
1024 | const uint8_t *
|
---|
1025 | dns_in6_get_dname (void)
|
---|
1026 | {
|
---|
1027 | return dns_domain_name;
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | /*
|
---|
1031 | * dns_in6_set_addr -- DNS ãµã¼ãã® IPv6 ã¢ãã¬ã¹ãè¨å®ããï¼DHCPv6ç¨ï¼ã
|
---|
1032 | *
|
---|
1033 | * 注æ: new ã« NULL ãæå®ããã¨ãåæå¤ã«æ»ãã
|
---|
1034 | */
|
---|
1035 |
|
---|
1036 | T_IN6_ADDR *
|
---|
1037 | dns_in6_set_addr (T_IN6_ADDR *new)
|
---|
1038 | {
|
---|
1039 | if (new == NULL)
|
---|
1040 | in6_addr_dns = in6_addr_dns_init;
|
---|
1041 | else
|
---|
1042 | in6_addr_dns = *new;
|
---|
1043 | return &in6_addr_dns;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | /*
|
---|
1047 | * dns_in6_get_addr -- DNS ãµã¼ãã® IPv6 ã¢ãã¬ã¹ãå¾ãã
|
---|
1048 | */
|
---|
1049 |
|
---|
1050 | T_IN6_ADDR *
|
---|
1051 | dns_in6_get_addr (T_IN6_ADDR *dst)
|
---|
1052 | {
|
---|
1053 | if (dst == NULL)
|
---|
1054 | return NULL;
|
---|
1055 | else {
|
---|
1056 | *dst = in6_addr_dns;
|
---|
1057 | return dst;
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
1062 |
|
---|
1063 | #if defined(SUPPORT_INET4)
|
---|
1064 |
|
---|
1065 | /*
|
---|
1066 | * dns_in4_set_dname -- DNS ã® ãã¡ã¤ã³åãè¨å®ããï¼DHCPv4ç¨ï¼ã
|
---|
1067 | *
|
---|
1068 | * 注æ: new ã« NULL ãæå®ããã¨ãåæå¤ã«æ»ãã
|
---|
1069 | */
|
---|
1070 |
|
---|
1071 | const uint8_t *
|
---|
1072 | dns_in4_set_dname (const uint8_t *new, uint_t len)
|
---|
1073 | {
|
---|
1074 | uint8_t *dst;
|
---|
1075 | int name_len = DNS_NAME_LENGTH;
|
---|
1076 |
|
---|
1077 | if (new == NULL)
|
---|
1078 | strcpy((char *)dns_domain_name, RSLV_CFG_DNS_DOMAIN_NAME_STR);
|
---|
1079 | else {
|
---|
1080 | dst = dns_domain_name;
|
---|
1081 | while ((name_len -- > 0) && (len -- > 0) && *new)
|
---|
1082 | *dst ++ = *new ++;
|
---|
1083 | *dst = '\0';
|
---|
1084 | }
|
---|
1085 | return dns_domain_name;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /*
|
---|
1089 | * dns_in4_get_dname -- DNS ã® ãã¡ã¤ã³åãå¾ãã
|
---|
1090 | */
|
---|
1091 |
|
---|
1092 | const uint8_t *
|
---|
1093 | dns_in4_get_dname (void)
|
---|
1094 | {
|
---|
1095 | return dns_domain_name;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | /*
|
---|
1099 | * dns_in4_set_addr -- DNS ãµã¼ãã® IPv4 ã¢ãã¬ã¹ãè¨å®ããï¼DHCPv4ç¨ï¼ã
|
---|
1100 | *
|
---|
1101 | * 注æ: new ã« NULL ãæå®ããã¨ãåæå¤ã«æ»ãã
|
---|
1102 | */
|
---|
1103 |
|
---|
1104 | T_IN4_ADDR *
|
---|
1105 | dns_in4_set_addr (T_IN4_ADDR *new)
|
---|
1106 | {
|
---|
1107 | if (new == NULL)
|
---|
1108 | in4_addr_dns = IPV4_ADDR_DNS;
|
---|
1109 | else
|
---|
1110 | in4_addr_dns = *new;
|
---|
1111 | return &in4_addr_dns;
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | /*
|
---|
1115 | * dns_in4_get_addr -- DNS ãµã¼ãã® IPv4 ã¢ãã¬ã¹ãå¾ãã
|
---|
1116 | */
|
---|
1117 |
|
---|
1118 | T_IN4_ADDR *
|
---|
1119 | dns_in4_get_addr (T_IN4_ADDR *dst)
|
---|
1120 | {
|
---|
1121 | if (dst == NULL)
|
---|
1122 | return NULL;
|
---|
1123 | else {
|
---|
1124 | *dst = in4_addr_dns;
|
---|
1125 | return dst;
|
---|
1126 | }
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
1130 |
|
---|
1131 | /*
|
---|
1132 | * dns_host_addr -- ãã¹ãåã IP ã¢ãã¬ã¹ã«å¤æããã
|
---|
1133 | */
|
---|
1134 |
|
---|
1135 | ER
|
---|
1136 | dns_host_addr (uint16_t flags, char *hostname, T_IN_ADDR *addr)
|
---|
1137 | {
|
---|
1138 | T_RSLV_DNS_MSG rslv;
|
---|
1139 | T_RSLV_RR rr;
|
---|
1140 | ER_UINT length, offset;
|
---|
1141 | ER error = E_OK;
|
---|
1142 | int scount;
|
---|
1143 | uint8_t *msg;
|
---|
1144 |
|
---|
1145 | #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4)
|
---|
1146 |
|
---|
1147 | T_IN4_ADDR addr4;
|
---|
1148 |
|
---|
1149 | #endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
|
---|
1150 |
|
---|
1151 | if ((error = tget_mpf(MPF_RSLV_SRBUF, (void*)&msg, TMO_FEVR)) != E_OK)
|
---|
1152 | return error;
|
---|
1153 |
|
---|
1154 | if ((length = dns_lookup_host(flags, hostname, msg, DNS_UDP_MSG_LENGTH, &rslv)) < 0) {
|
---|
1155 | error = length;
|
---|
1156 | goto err_ret;
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | offset = rslv.an_offset;
|
---|
1160 | for (scount = 1; scount <= rslv.dns_hdr.ancount; scount ++) {
|
---|
1161 | if ((offset = dns_analyze_rr(&rr, offset, msg, length)) < 0) {
|
---|
1162 | error = offset;
|
---|
1163 | goto err_ret;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | #if defined(SUPPORT_INET6)
|
---|
1167 |
|
---|
1168 | if (rr.type == DNS_TYPE_AAAA && rr.class == DNS_CLASS_IN && rr.rdlength == sizeof(*addr)) {
|
---|
1169 | memcpy((void*)addr, (void*)(msg + rr.rdata_offset), sizeof(*addr));
|
---|
1170 | break;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | #if defined(SUPPORT_INET4)
|
---|
1174 |
|
---|
1175 | if (rr.type == DNS_TYPE_A && rr.class == DNS_CLASS_IN && rr.rdlength == sizeof(addr4)) {
|
---|
1176 | addr4 = msg2host32(msg + rr.rdata_offset);
|
---|
1177 | in6_make_ipv4mapped(addr, addr4);
|
---|
1178 | break;
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
1182 |
|
---|
1183 | #else /* of #if defined(SUPPORT_INET6) */
|
---|
1184 |
|
---|
1185 | #if defined(SUPPORT_INET4)
|
---|
1186 |
|
---|
1187 | if (rr.type == DNS_TYPE_A && rr.class == DNS_CLASS_IN && rr.rdlength == sizeof(*addr)) {
|
---|
1188 | *addr = msg2host32((msg + rr.rdata_offset));
|
---|
1189 | break;
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
1193 |
|
---|
1194 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
1195 |
|
---|
1196 | }
|
---|
1197 | if (scount > rslv.dns_hdr.ancount)
|
---|
1198 | error = EV_RSLV_UNEXPCT;
|
---|
1199 |
|
---|
1200 | err_ret:
|
---|
1201 | rel_mpf(MPF_RSLV_SRBUF, (void*)msg);
|
---|
1202 | return error;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | #endif /* of #if defined(USE_RESOLVER) */
|
---|