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 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
8 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
9 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
10 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
11 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
12 | * スコード中に含まれていること.
|
---|
13 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
14 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
15 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
16 | * の無保証規定を掲載すること.
|
---|
17 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
18 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
19 | * と.
|
---|
20 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
21 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
22 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
23 | * 報告すること.
|
---|
24 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
25 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
26 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
27 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
28 | * 免責すること.
|
---|
29 | *
|
---|
30 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
31 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
32 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
33 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
34 | * の責任を負わない.
|
---|
35 | *
|
---|
36 | * @(#) $Id$
|
---|
37 | */
|
---|
38 | #include "shellif.h"
|
---|
39 | #include <kernel.h>
|
---|
40 | #include <t_stdlib.h>
|
---|
41 | #include <sil.h>
|
---|
42 | #include "syssvc/serial.h"
|
---|
43 | #include "syssvc/syslog.h"
|
---|
44 | #include <tinet_config.h>
|
---|
45 | #include <netinet/in.h>
|
---|
46 | #include <netinet/in_itron.h>
|
---|
47 | #include <tinet_nic_defs.h>
|
---|
48 | #include <tinet_cfg.h>
|
---|
49 | #include <netinet/in_var.h>
|
---|
50 | #include <net/ethernet.h>
|
---|
51 | #include <net/if6_var.h>
|
---|
52 | #include <net/net.h>
|
---|
53 | #include <net/if_var.h>
|
---|
54 | #include <netinet/udp_var.h>
|
---|
55 | #include <netapp/netapp_var.h>
|
---|
56 | #include <netapp/netapp.h>
|
---|
57 | #include <netapp/dhcp4_cli.h>
|
---|
58 | #include <netapp/resolver.h>
|
---|
59 | #include "core/ntlibc.h"
|
---|
60 | #include "ntp_cli.h"
|
---|
61 | #include "kernel_cfg.h"
|
---|
62 |
|
---|
63 | #if defined(SUPPORT_INET6)
|
---|
64 | #define DEFAULT_API_PROTO API_PROTO_IPV6
|
---|
65 | #else
|
---|
66 | #define DEFAULT_API_PROTO API_PROTO_IPV4
|
---|
67 | #endif
|
---|
68 | extern void ping6(T_IN6_ADDR *addr, uint_t tmo, uint_t len);
|
---|
69 | extern void ping4(T_IN4_ADDR *addr, uint_t tmo, uint_t len);
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * str_num -- cons_printf の数値変換
|
---|
73 | */
|
---|
74 |
|
---|
75 | static void
|
---|
76 | str_chr(char *text, int *pos, int size, char c)
|
---|
77 | {
|
---|
78 | if (*pos >= size)
|
---|
79 | return;
|
---|
80 | text[*pos] = c;
|
---|
81 | *pos = *pos + 1;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * str_num -- cons_printf の数値変換
|
---|
86 | */
|
---|
87 |
|
---|
88 | static int
|
---|
89 | str_num(char *text, int *pos, int size, ulong_t val, int radix,
|
---|
90 | const char *radchar, int width, bool_t minus, char padchar)
|
---|
91 | {
|
---|
92 | char digits[24];
|
---|
93 | int ix, pad, pchars;
|
---|
94 | bool_t left;
|
---|
95 |
|
---|
96 | if (width < 0) {
|
---|
97 | width = -width;
|
---|
98 | left = true;
|
---|
99 | }
|
---|
100 | else
|
---|
101 | left = false;
|
---|
102 |
|
---|
103 | ix = 0;
|
---|
104 | do {
|
---|
105 | digits[ix ++] = radchar[val % radix];
|
---|
106 | val /= radix;
|
---|
107 | } while (val != 0);
|
---|
108 |
|
---|
109 | if (minus)
|
---|
110 | digits[ix ++] = '-';
|
---|
111 |
|
---|
112 | if (width > ix)
|
---|
113 | pchars = width;
|
---|
114 | else
|
---|
115 | pchars = ix;
|
---|
116 |
|
---|
117 | pad = ix;
|
---|
118 | if (!left) /* 右詰め */
|
---|
119 | for ( ; pad < width; pad ++)
|
---|
120 | str_chr(text, pos, size, padchar);
|
---|
121 |
|
---|
122 | while (ix -- > 0)
|
---|
123 | str_chr(text, pos, size, digits[ix]);
|
---|
124 |
|
---|
125 | if (left) /* 左詰め */
|
---|
126 | for ( ; pad < width; pad ++)
|
---|
127 | str_chr(text, pos, size, padchar);
|
---|
128 |
|
---|
129 | return pchars;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * str_ipv4addr -- IPv4 アドレス出力
|
---|
134 | */
|
---|
135 |
|
---|
136 | int
|
---|
137 | str_ipv4addr (char *text, int size, T_IN4_ADDR *addr, int width)
|
---|
138 | {
|
---|
139 | int len = 3, pos = 0; /* 3 は '.' の文字数 */
|
---|
140 |
|
---|
141 | len += str_num(text, &pos, size, (*addr >> 24) & 0xff, 10, radhex, 0, false, ' ');
|
---|
142 | str_chr(text, &pos, size, '.');
|
---|
143 | len += str_num(text, &pos, size, (*addr >> 16) & 0xff, 10, radhex, 0, false, ' ');
|
---|
144 | str_chr(text, &pos, size, '.');
|
---|
145 | len += str_num(text, &pos, size, (*addr >> 8) & 0xff, 10, radhex, 0, false, ' ');
|
---|
146 | str_chr(text, &pos, size, '.');
|
---|
147 | len += str_num(text, &pos, size, *addr & 0xff, 10, radhex, 0, false, ' ');
|
---|
148 |
|
---|
149 | for ( ; len < width; len ++)
|
---|
150 | str_chr(text, &pos, size, ' ');
|
---|
151 |
|
---|
152 | return len;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * ipv6addr -- IPv6 アドレス出力
|
---|
157 | */
|
---|
158 |
|
---|
159 | int
|
---|
160 | str_ipv6addr (char *text, int size, const T_IN6_ADDR *addr, int width)
|
---|
161 | {
|
---|
162 | int len = 0, ix, len6, pos = 0;
|
---|
163 | bool_t omit = false, zero = false;
|
---|
164 |
|
---|
165 | if (addr == NULL || IN6_IS_ADDR_UNSPECIFIED(addr)) {
|
---|
166 | str_chr(text, &pos, size, '0');
|
---|
167 | str_chr(text, &pos, size, ':');
|
---|
168 | str_chr(text, &pos, size, ':');
|
---|
169 | str_chr(text, &pos, size, '0');
|
---|
170 | len = 4;
|
---|
171 | }
|
---|
172 | else {
|
---|
173 | if (in6_is_addr_ipv4mapped(addr))
|
---|
174 | len6 = sizeof(T_IN6_ADDR) / 2 - 2;
|
---|
175 | else
|
---|
176 | len6 = sizeof(T_IN6_ADDR) / 2;
|
---|
177 | for (ix = 0; ix < len6; ix ++) {
|
---|
178 | if (omit) {
|
---|
179 | len += str_num(text, &pos, size, ntohs(addr->s6_addr16[ix]), 16, radhex, 0, false, ' ');
|
---|
180 | if (ix < 7) {
|
---|
181 | str_chr(text, &pos, size, ':');
|
---|
182 | len ++;
|
---|
183 | }
|
---|
184 | }
|
---|
185 | else if (ix > 0 && ix < 7 && addr->s6_addr16[ix] == 0)
|
---|
186 | zero = true;
|
---|
187 | else {
|
---|
188 | if (zero) {
|
---|
189 | omit = true;
|
---|
190 | str_chr(text, &pos, size, ':');
|
---|
191 | len ++;
|
---|
192 | }
|
---|
193 | len += str_num(text, &pos, size, ntohs(addr->s6_addr16[ix]), 16, radhex, 0, false, ' ');
|
---|
194 | if (ix < 7) {
|
---|
195 | str_chr(text, &pos, size, ':');
|
---|
196 | len ++;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (len6 == sizeof(T_IN6_ADDR) / 2 - 2) {
|
---|
202 | T_IN4_ADDR ip4addr;
|
---|
203 |
|
---|
204 | ip4addr = ntohl(addr->s6_addr32[3]);
|
---|
205 | len += str_ipv4addr(&text[len], size - len, &ip4addr, 0);
|
---|
206 | }
|
---|
207 |
|
---|
208 | for ( ; len < width; len ++)
|
---|
209 | str_chr(text, &pos, size, ' ');
|
---|
210 | }
|
---|
211 | return len;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * str_macaddr -- MAC アドレス出力
|
---|
216 | */
|
---|
217 |
|
---|
218 | int
|
---|
219 | str_macaddr (char *text, int size, uint8_t *mac, int width)
|
---|
220 | {
|
---|
221 | int oct, len, pos = 0;
|
---|
222 |
|
---|
223 | for (oct = 5; oct -- > 0; ) {
|
---|
224 | str_num(text, &pos, size, *mac ++, 16, radhex, 2, false, '0');
|
---|
225 | str_chr(text, &pos, size, ':');
|
---|
226 | }
|
---|
227 | str_num(text, &pos, size, *mac, 16, radhex, 2, false, '0');
|
---|
228 |
|
---|
229 | for (len = 17; len < width; len ++)
|
---|
230 | str_chr(text, &pos, size, ' ');
|
---|
231 |
|
---|
232 | return len;
|
---|
233 | }
|
---|
234 |
|
---|
235 | int usrcmd_ping(int argc, char **argv)
|
---|
236 | {
|
---|
237 | int tmo = 3, size = 64;
|
---|
238 | char apip = DEFAULT_API_PROTO;
|
---|
239 | char *line = argv[1];
|
---|
240 | #if defined(SUPPORT_INET6)
|
---|
241 | T_IN6_ADDR addr;
|
---|
242 | #if defined(SUPPORT_INET4)
|
---|
243 | T_IN4_ADDR addr4;
|
---|
244 | #endif
|
---|
245 | #else
|
---|
246 | T_IN4_ADDR addr;
|
---|
247 | #endif
|
---|
248 | static const char i6rlp_pmtu_str1[] = " FF1E::1:2 1 1452";
|
---|
249 | static const char i6rlp_pmtu_str2[] = " FF1E::1:2 1 1352";
|
---|
250 | static const char i6rlp_pmtu_str3[] = " fe80::0200:00ff:fe00:0100 1 2";
|
---|
251 |
|
---|
252 | if (apip == '1')
|
---|
253 | strcpy(line, i6rlp_pmtu_str1);
|
---|
254 | else if (apip == '2')
|
---|
255 | strcpy(line, i6rlp_pmtu_str2);
|
---|
256 | else if (apip == '3')
|
---|
257 | strcpy(line, i6rlp_pmtu_str3);
|
---|
258 |
|
---|
259 | #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4)
|
---|
260 | if (argc <= 2)
|
---|
261 | return 0;
|
---|
262 |
|
---|
263 | if ('0' <= *line && *line <= '9') {
|
---|
264 | if (*line == '6')
|
---|
265 | apip = API_PROTO_IPV6;
|
---|
266 | if (*line == '4')
|
---|
267 | apip = API_PROTO_IPV4;
|
---|
268 | line++;
|
---|
269 | }
|
---|
270 |
|
---|
271 | line = argv[2];
|
---|
272 | #else
|
---|
273 | if (argc <= 1)
|
---|
274 | return 0;
|
---|
275 | #endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
|
---|
276 |
|
---|
277 | if ((line = lookup_ipaddr(&addr, line, apip)) == NULL) {
|
---|
278 | printf("[PING] unknown host.\n");
|
---|
279 | return 0;
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (argc > 2) {
|
---|
283 | line = argv[2];
|
---|
284 | if ('0' <= *line && *line <= '9')
|
---|
285 | line = get_int(&tmo, line);
|
---|
286 |
|
---|
287 | if (argc > 3) {
|
---|
288 | line = argv[3];
|
---|
289 | if ('0' <= *line && *line <= '9')
|
---|
290 | line = get_int(&size, line);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | #if defined(SUPPORT_INET6)
|
---|
295 | #if defined(SUPPORT_INET4)
|
---|
296 | if (apip == API_PROTO_IPV6) {
|
---|
297 | printf("[PING6] size: %d, tmo: %d, host: %s\n", size, tmo, ipv62str(NULL, &addr));
|
---|
298 | ping6(&addr, (uint_t)tmo, (uint_t)size);
|
---|
299 | }
|
---|
300 | else {
|
---|
301 | addr4 = ntohl(addr.s6_addr32[3]);
|
---|
302 | printf("[PING4] size: %d, tmo: %d, host: %s\n", size, tmo, ip2str(NULL, &addr4));
|
---|
303 | ping4(&addr4, (uint_t)tmo, (uint_t)size);
|
---|
304 | }
|
---|
305 | #else /* of #if defined(SUPPORT_INET4) */
|
---|
306 | printf("[PING6] size: %d, tmo: %d, host: %s\n", size, tmo, ipv62str(NULL, &addr));
|
---|
307 | ping6(&addr, (uint_t)tmo, (uint_t)size);
|
---|
308 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
309 | #else /* of #if defined(SUPPORT_INET6) */
|
---|
310 | printf("[PING4] size: %d, tmo: %d, host: %s\n", size, tmo, ip2str(NULL, &addr));
|
---|
311 | ping4(&addr, (uint_t)tmo, (uint_t)size);
|
---|
312 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
313 |
|
---|
314 | return 0;
|
---|
315 | }
|
---|
316 |
|
---|
317 | static void dhcp4c_info()
|
---|
318 | {
|
---|
319 | T_IN4_ADDR svaddr;
|
---|
320 | SYSTIM bind_start;
|
---|
321 | ER ret;
|
---|
322 | uint32_t expire, renew, rebind;
|
---|
323 | char temp[30];
|
---|
324 | int pos;
|
---|
325 |
|
---|
326 | if ((ret = dhcp4c_get_info(&svaddr, &expire, &renew, &rebind, &bind_start)) == E_OK) {
|
---|
327 | pos = str_ipv4addr(temp, sizeof(temp), &svaddr, 0);
|
---|
328 | temp[pos] = '\0';
|
---|
329 | printf("DHCPv4 server: %s,\n", temp);
|
---|
330 | printf(" Renew: %u:%02u:%02u,\n",
|
---|
331 | renew / 3600, (renew / 60) % 60, renew % 60);
|
---|
332 | printf(" Rebind: %u:%02u:%02u, Expire: %u:%02u:%02u.\n",
|
---|
333 | rebind / 3600, (rebind / 60) % 60, rebind % 60,
|
---|
334 | expire / 3600, (expire / 60) % 60, expire % 60);
|
---|
335 | }
|
---|
336 | else if (ret == E_OBJ)
|
---|
337 | printf("DHCPv4 server: not available.\n");
|
---|
338 | }
|
---|
339 |
|
---|
340 | int usrcmd_dhcp4c(int argc, char **argv)
|
---|
341 | {
|
---|
342 | ER ret;
|
---|
343 |
|
---|
344 | if (argc < 2)
|
---|
345 | return 0;
|
---|
346 |
|
---|
347 | if (strcmp(argv[1], "rel") == 0) {
|
---|
348 | ret = dhcp4c_rel_info();
|
---|
349 | printf("dhcp4c_rel_info %d\n", ret);
|
---|
350 | }
|
---|
351 | else if (strcmp(argv[1], "renew") == 0) {
|
---|
352 | ret = dhcp4c_renew_info();
|
---|
353 | printf("dhcp4c_renew_info %d\n", ret);
|
---|
354 | }
|
---|
355 | else {
|
---|
356 | dhcp4c_info();
|
---|
357 | }
|
---|
358 |
|
---|
359 | return 0;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * s_show_dns_domain_name -- DNS のドメイン名を表示する。
|
---|
364 | */
|
---|
365 |
|
---|
366 | static uint_t
|
---|
367 | s_show_dns_domain_name(uint8_t *hdr, uint_t offset)
|
---|
368 | {
|
---|
369 | uint8_t *ptr;
|
---|
370 | uint_t c;
|
---|
371 |
|
---|
372 | ptr = hdr + offset;
|
---|
373 | while (*ptr) {
|
---|
374 | if ((*ptr & DNS_MSG_COMP_MARK) == DNS_MSG_COMP_MARK) {
|
---|
375 | s_show_dns_domain_name(hdr, (*ptr & ~DNS_MSG_COMP_MARK) << 8 | *(ptr + 1));
|
---|
376 | ptr += 2;
|
---|
377 | break;
|
---|
378 | }
|
---|
379 | else {
|
---|
380 | for (c = 1; c <= *ptr; c++)
|
---|
381 | printf("%c", *(ptr + c));
|
---|
382 | ptr += *ptr + 1;
|
---|
383 | if (*ptr)
|
---|
384 | printf(".");
|
---|
385 | }
|
---|
386 | }
|
---|
387 | return ptr - hdr;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /*
|
---|
391 | * show_dns_soa -- DNS の SOA RDATA を表示する。
|
---|
392 | */
|
---|
393 |
|
---|
394 | static ER_UINT
|
---|
395 | show_dns_soa(uint8_t *msg, ER_UINT length, uint_t offset)
|
---|
396 | {
|
---|
397 | T_RSLV_SOA soa;
|
---|
398 | ER_UINT error;
|
---|
399 | uint_t rn_offset;
|
---|
400 |
|
---|
401 | if ((error = dns_analyze_soa(&soa, offset, msg, length)) < 0)
|
---|
402 | return error;
|
---|
403 |
|
---|
404 | printf(" mname: ");
|
---|
405 | rn_offset = s_show_dns_domain_name(msg, offset);
|
---|
406 | putchar('\n');
|
---|
407 | printf(" rname: ");
|
---|
408 | s_show_dns_domain_name(msg, rn_offset);
|
---|
409 | putchar('\n');
|
---|
410 |
|
---|
411 | printf(" serial: %d\n", soa.serial);
|
---|
412 | printf(" refresh: %d\n", soa.refresh);
|
---|
413 | printf(" retry: %d\n", soa.retry);
|
---|
414 | printf(" expirel: %d\n", soa.expire);
|
---|
415 | printf(" minimum: %d\n", soa.minimum);
|
---|
416 |
|
---|
417 | return E_OK;
|
---|
418 | }
|
---|
419 |
|
---|
420 | /*
|
---|
421 | * show_dns_qdsection -- DNS の Question セクションを表示する。
|
---|
422 | */
|
---|
423 |
|
---|
424 | static ER_UINT
|
---|
425 | show_dns_qdsection(uint8_t *msg, ER_UINT length, T_RSLV_DNS_MSG *rslv)
|
---|
426 | {
|
---|
427 | T_RSLV_QD qd;
|
---|
428 | ER_UINT offset, error;
|
---|
429 | int scount;
|
---|
430 |
|
---|
431 | printf("question section: %d\n", rslv->dns_hdr.qdcount);
|
---|
432 | offset = rslv->qd_offset;
|
---|
433 | for (scount = 1; scount <= rslv->dns_hdr.qdcount; scount++) {
|
---|
434 | if ((error = dns_analyze_qd(&qd, offset, msg, length)) < 0)
|
---|
435 | return error;
|
---|
436 |
|
---|
437 | printf("%2d: ", scount);
|
---|
438 | s_show_dns_domain_name(msg, offset);
|
---|
439 | printf("\n type: %-4s, class: %2s\n", dns_strtype(qd.type), dns_strclass(qd.class));
|
---|
440 | offset = error;
|
---|
441 | }
|
---|
442 |
|
---|
443 | return E_OK;
|
---|
444 | }
|
---|
445 |
|
---|
446 | /*
|
---|
447 | * show_dns_section -- DNS の各セクションを表示する。
|
---|
448 | */
|
---|
449 |
|
---|
450 | static ER_UINT
|
---|
451 | show_dns_section(uint8_t *msg, ER_UINT length, uint_t scount, uint_t offset, char *title)
|
---|
452 | {
|
---|
453 | T_RSLV_RR rr;
|
---|
454 | T_IN4_ADDR in4_addr;
|
---|
455 | ER_UINT error;
|
---|
456 | int count, dcount, col;
|
---|
457 | T_IN6_ADDR in6_addr;
|
---|
458 | char temp[30];
|
---|
459 | int pos;
|
---|
460 |
|
---|
461 | printf("%10s section: %d\n", title, scount);
|
---|
462 | for (count = 1; count <= scount; count++) {
|
---|
463 | if ((error = dns_analyze_rr(&rr, offset, msg, length)) < 0)
|
---|
464 | return error;
|
---|
465 |
|
---|
466 | printf("%2d: ", count);
|
---|
467 | s_show_dns_domain_name(msg, offset);
|
---|
468 | printf("\n type: %-4s, class: %2s, TTL: %2d, len: %3d, offset: 0x%02x\n",
|
---|
469 | dns_strtype(rr.type), dns_strclass(rr.class), rr.ttl, rr.rdlength, rr.rdata_offset);
|
---|
470 |
|
---|
471 | switch (rr.type) {
|
---|
472 | case DNS_TYPE_A:
|
---|
473 | memcpy((void*)&in4_addr, (void*)(msg + rr.rdata_offset), sizeof(in4_addr));
|
---|
474 | in4_addr = ntohl(in4_addr);
|
---|
475 | pos = str_ipv4addr(temp, sizeof(temp), &in4_addr, 0);
|
---|
476 | temp[pos] = '\0';
|
---|
477 | printf(" IPv4 addr: %s\n", temp);
|
---|
478 | break;
|
---|
479 | case DNS_TYPE_NS:
|
---|
480 | printf(" host: ");
|
---|
481 | s_show_dns_domain_name(msg, rr.rdata_offset);
|
---|
482 | putchar('\n');
|
---|
483 | break;
|
---|
484 | case DNS_TYPE_CNAME:
|
---|
485 | printf(" host: ");
|
---|
486 | s_show_dns_domain_name(msg, rr.rdata_offset);
|
---|
487 | putchar('\n');
|
---|
488 | break;
|
---|
489 | case DNS_TYPE_SOA:
|
---|
490 | show_dns_soa(msg, length, rr.rdata_offset);
|
---|
491 | break;
|
---|
492 | case DNS_TYPE_PTR:
|
---|
493 | printf(" PTR: ");
|
---|
494 | s_show_dns_domain_name(msg, rr.rdata_offset);
|
---|
495 | putchar('\n');
|
---|
496 | break;
|
---|
497 | case DNS_TYPE_AAAA:
|
---|
498 | memcpy((void*)&in6_addr, (void*)(msg + rr.rdata_offset), sizeof(in6_addr));
|
---|
499 | pos = str_ipv6addr(temp, sizeof(temp), &in6_addr, 0);
|
---|
500 | temp[pos] = '\0';
|
---|
501 | printf(" IPv6 addr: %s\n", temp);
|
---|
502 | break;
|
---|
503 | default:
|
---|
504 | printf(" data: ");
|
---|
505 | col = 32;
|
---|
506 | for (dcount = 0; dcount < rr.rdlength; dcount++) {
|
---|
507 | printf("%02x", *(msg + rr.rdata_offset + dcount));
|
---|
508 | if (--col == 0) {
|
---|
509 | printf("\n ");
|
---|
510 | col = 32;
|
---|
511 | }
|
---|
512 | }
|
---|
513 | putchar('\n');
|
---|
514 | break;
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | return E_OK;
|
---|
519 | }
|
---|
520 | /*
|
---|
521 | * dns_info -- DNS 情報の表示
|
---|
522 | */
|
---|
523 |
|
---|
524 | static void dns_info()
|
---|
525 | {
|
---|
526 | #if defined(SUPPORT_INET6)
|
---|
527 | T_IN6_ADDR in6_addr;
|
---|
528 | #endif
|
---|
529 | #if defined(SUPPORT_INET4)
|
---|
530 | T_IN4_ADDR in4_addr;
|
---|
531 | #endif
|
---|
532 | char temp[30];
|
---|
533 | int pos;
|
---|
534 |
|
---|
535 | #if defined(SUPPORT_INET6)
|
---|
536 |
|
---|
537 | printf("domain name: %s\n", dns_in6_get_dname());
|
---|
538 |
|
---|
539 | #else /* of #if defined(SUPPORT_INET6) */
|
---|
540 |
|
---|
541 | printf("domain name: %s\n", dns_in4_get_dname());
|
---|
542 |
|
---|
543 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
544 |
|
---|
545 | #if defined(SUPPORT_INET6)
|
---|
546 | dns_in6_get_addr(&in6_addr);
|
---|
547 | printf("IPv6 DNS server: ");
|
---|
548 | if (IN6_IS_ADDR_UNSPECIFIED(&in6_addr))
|
---|
549 | printf("not available.\n");
|
---|
550 | else {
|
---|
551 | pos = str_ipv6addr(temp, sizeof(temp), &in6_addr, 0);
|
---|
552 | temp[pos] = '\0';
|
---|
553 | printf("%s.\n", temp);
|
---|
554 | }
|
---|
555 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
556 |
|
---|
557 | #if defined(SUPPORT_INET4)
|
---|
558 | dns_in4_get_addr(&in4_addr);
|
---|
559 | printf("IPv4 DNS server: ");
|
---|
560 | if (in4_addr == IPV4_ADDRANY)
|
---|
561 | printf("not available.\n");
|
---|
562 | else {
|
---|
563 | pos = str_ipv4addr(temp, sizeof(temp), &in4_addr, 0);
|
---|
564 | temp[pos] = '\0';
|
---|
565 | printf("%s.\n", temp);
|
---|
566 | }
|
---|
567 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
568 | }
|
---|
569 |
|
---|
570 | /*
|
---|
571 | * name_lookup -- ホスト名-IP アドレス変換
|
---|
572 | */
|
---|
573 |
|
---|
574 | const char *rcode_str[] = {
|
---|
575 | "no error",
|
---|
576 | "format error",
|
---|
577 | "server error",
|
---|
578 | "name error",
|
---|
579 | "not implement",
|
---|
580 | "refused",
|
---|
581 | };
|
---|
582 |
|
---|
583 | int usrcmd_dnsc(int argc, char **argv)
|
---|
584 | {
|
---|
585 | char *line = argv[1];
|
---|
586 | static char hostname[DBG_LINE_SIZE + 1];
|
---|
587 |
|
---|
588 | T_RSLV_DNS_MSG rslv;
|
---|
589 | ER_UINT length, offset;
|
---|
590 | ER error;
|
---|
591 | uint_t flags = 0;
|
---|
592 | uint8_t *msg;
|
---|
593 |
|
---|
594 | if (strcmp(line, "info") == 0) {
|
---|
595 | dns_info();
|
---|
596 | return 0;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /* コマンドのオプションを設定する。*/
|
---|
600 | line = skip_blanks(resolv_options(&flags, line, DEFAULT_API_PROTO));
|
---|
601 | if ((flags & (DNS_LUP_FLAGS_PROTO_IPV6 | DNS_LUP_FLAGS_PROTO_IPV4)) == 0) {
|
---|
602 | printf("DNS server not available.\n");
|
---|
603 | return 0;
|
---|
604 | }
|
---|
605 |
|
---|
606 | /* 照会するホスト名・IP アドレスを解析する。*/
|
---|
607 | resolv_hoststr(&flags, hostname, sizeof(hostname), line);
|
---|
608 |
|
---|
609 | /* 正引きでも逆引きでもプロトコル上は正引きを指定する。*/
|
---|
610 | flags |= DNS_LUP_OPCODE_FORWARD;
|
---|
611 |
|
---|
612 | /* IPv6 アドレス、または IPv4 アドレスが指定された時は、照会タイプは PTR に設定する。*/
|
---|
613 | if (((flags & DNS_LUP_FLAGS_NAME_MASK) == DNS_LUP_FLAGS_NAME_IPV6) ||
|
---|
614 | ((flags & DNS_LUP_FLAGS_NAME_MASK) == DNS_LUP_FLAGS_NAME_IPV4))
|
---|
615 | flags = (flags & ~DNS_LUP_FLAGS_QTYPE_MASK) | DNS_LUP_FLAGS_QTYPE_PTR;
|
---|
616 |
|
---|
617 | if ((error = tget_mpf(MPF_RSLV_SRBUF, (void*)&msg, TMO_FEVR)) != E_OK) {
|
---|
618 | printf("get buffer error: %s.\n", itron_strerror(error));
|
---|
619 | return 0;
|
---|
620 | }
|
---|
621 |
|
---|
622 | if ((length = dns_lookup_host(flags | DNS_LUP_FLAGS_MSG, line, msg, DNS_UDP_MSG_LENGTH, &rslv)) < 0) {
|
---|
623 | //printf("error: %s.\n", itron_strerror(length));
|
---|
624 | goto err_ret;
|
---|
625 | }
|
---|
626 |
|
---|
627 | dly_tsk(1 * 1000);
|
---|
628 | printf("DNS header: flags: ");
|
---|
629 | if (rslv.dns_hdr.code & (DNS_QR_RESPONSE | DNS_AUTHORITATIVE |
|
---|
630 | DNS_TRUN_CATION | DNS_RECURSION_DESIRED | DNS_RECURSION_AVAILABLE)) {
|
---|
631 | printf((rslv.dns_hdr.code & DNS_QR_RESPONSE) ? "QR," : "");
|
---|
632 | printf((rslv.dns_hdr.code & DNS_AUTHORITATIVE) ? "AA," : "");
|
---|
633 | printf((rslv.dns_hdr.code & DNS_TRUN_CATION) ? "TC," : "");
|
---|
634 | printf((rslv.dns_hdr.code & DNS_RECURSION_DESIRED) ? "RD," : "");
|
---|
635 | printf((rslv.dns_hdr.code & DNS_RECURSION_AVAILABLE) ? "RA," : "");
|
---|
636 | printf(" ");
|
---|
637 | }
|
---|
638 | printf("opcode: ");
|
---|
639 | printf((rslv.dns_hdr.code & DNS_OPCODE_REVERSE) ? "RV" : "FW");
|
---|
640 | printf((rslv.dns_hdr.code & DNS_OPCODE_STATUS) ? ",ST" : "");
|
---|
641 | printf(", rcode: %s.\n",
|
---|
642 | (rslv.dns_hdr.code & DNS_RCODE_MASK) > DNS_RCODE_REFUSED
|
---|
643 | ? "6" : rcode_str[rslv.dns_hdr.code & DNS_RCODE_MASK]);
|
---|
644 |
|
---|
645 | if ((offset = show_dns_qdsection(msg, length, &rslv)) < 0) {
|
---|
646 | printf("msg error: %s.\n", itron_strerror(offset));
|
---|
647 | }
|
---|
648 | if ((offset = show_dns_section(msg, length, rslv.dns_hdr.ancount, rslv.an_offset, "answer")) < 0) {
|
---|
649 | printf("msg error: %s.\n", itron_strerror(offset));
|
---|
650 | }
|
---|
651 | if ((offset = show_dns_section(msg, length, rslv.dns_hdr.nscount, rslv.ns_offset, "authority")) < 0) {
|
---|
652 | printf("msg error: %s.\n", itron_strerror(offset));
|
---|
653 | }
|
---|
654 | if ((offset = show_dns_section(msg, length, rslv.dns_hdr.arcount, rslv.ar_offset, "additional")) < 0) {
|
---|
655 | printf("msg error: %s.\n", itron_strerror(offset));
|
---|
656 | }
|
---|
657 |
|
---|
658 | err_ret:
|
---|
659 | if ((error = rel_mpf(MPF_RSLV_SRBUF, msg)) != E_OK)
|
---|
660 | printf("release buffer error: %s.\n", itron_strerror(error));
|
---|
661 | return 0;
|
---|
662 | }
|
---|
663 |
|
---|
664 | int usrcmd_ntpc(int argc, char **argv)
|
---|
665 | {
|
---|
666 | ntp_cli_execute();
|
---|
667 | return 0;
|
---|
668 | }
|
---|