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