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