/* * TINET (TCP/IP Protocol Stack) * * Copyright (C) 2001-2017 by Dep. of Computer Science and Engineering * Tomakomai National College of Technology, JAPAN * * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する. * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー * スコード中に含まれていること. * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記 * の無保証規定を掲載すること. * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ * と. * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著 * 作権表示,この利用条件および下記の無保証規定を掲載すること. * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに * 報告すること. * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること. * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを * 免責すること. * * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ * の責任を負わない. * * @(#) $Id$ */ #include "shellif.h" #include #include #include #include #include "syssvc/serial.h" #include "syssvc/syslog.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "core/ntlibc.h" #include "util/ntstdio.h" #include "ntp_cli.h" #include "kernel_cfg.h" extern ntstdio_t ntstdio; #if defined(SUPPORT_INET6) #define DEFAULT_API_PROTO API_PROTO_IPV6 #else #define DEFAULT_API_PROTO API_PROTO_IPV4 #endif extern void ping6(T_IN6_ADDR *addr, uint_t tmo, uint_t len); extern void ping4(T_IN4_ADDR *addr, uint_t tmo, uint_t len); int usrcmd_ping(int argc, char **argv) { int_t tmo, size; char apip = DEFAULT_API_PROTO; char *line = argv[1]; #if defined(SUPPORT_INET6) T_IN6_ADDR addr; #if defined(SUPPORT_INET4) T_IN4_ADDR addr4; #endif #else T_IN4_ADDR addr; #endif static const char i6rlp_pmtu_str1[] = " FF1E::1:2 1 1452"; static const char i6rlp_pmtu_str2[] = " FF1E::1:2 1 1352"; static const char i6rlp_pmtu_str3[] = " fe80::0200:00ff:fe00:0100 1 2"; if (apip == '1') ntlibc_strcpy(line, i6rlp_pmtu_str1); else if (apip == '2') ntlibc_strcpy(line, i6rlp_pmtu_str2); else if (apip == '3') ntlibc_strcpy(line, i6rlp_pmtu_str3); #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) if ('0' <= *line && *line <= '9') { if (*line == '6') apip = API_PROTO_IPV6; if (*line == '4') apip = API_PROTO_IPV4; line++; } #endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */ if ((line = lookup_ipaddr(&addr, line, apip)) == NULL) { ntstdio_printf(&ntstdio, "[PING] unknown host.\n"); return 0; } line = argv[2]; if ('0' <= *line && *line <= '9') line = get_int(&tmo, line); else tmo = 3; line = argv[3]; if ('0' <= *line && *line <= '9') line = get_int(&size, line); else size = 64; #if defined(SUPPORT_INET6) #if defined(SUPPORT_INET4) if (apip == API_PROTO_IPV6) { ntstdio_printf(&ntstdio, "[PING6] size: %d, tmo: %d, host: %s\n", size, tmo, ipv62str(NULL, &addr)); ping6(&addr, (uint_t)tmo, (uint_t)size); } else { addr4 = ntohl(addr.s6_addr32[3]); ntstdio_printf(&ntstdio, "[PING4] size: %d, tmo: %d, host: %s\n", size, tmo, ip2str(NULL, &addr4)); ping4(&addr4, (uint_t)tmo, (uint_t)size); } #else /* of #if defined(SUPPORT_INET4) */ ntstdio_printf(&ntstdio, "[PING6] size: %d, tmo: %d, host: %s\n", size, tmo, ipv62str(NULL, &addr)); ping6(&addr, (uint_t)tmo, (uint_t)size); #endif /* of #if defined(SUPPORT_INET4) */ #else /* of #if defined(SUPPORT_INET6) */ ntstdio_printf(&ntstdio, "[PING4] size: %d, tmo: %d, host: %s\n", size, tmo, ip2str(NULL, &addr)); ping4(&addr, (uint_t)tmo, (uint_t)size); #endif /* of #if defined(SUPPORT_INET6) */ return 0; } static void dhcp4c_info() { T_IN4_ADDR svaddr; SYSTIM bind_start; ER ret; uint32_t expire, renew, rebind; if ((ret = dhcp4c_get_info(&svaddr, &expire, &renew, &rebind, &bind_start)) == E_OK) { ntstdio_printf(&ntstdio, "DHCPv4 server: %hI,\n", &svaddr); ntstdio_printf(&ntstdio, " Renew: %u:%02u:%02u,\n", renew / 3600, (renew / 60) % 60, renew % 60); ntstdio_printf(&ntstdio, " Rebind: %u:%02u:%02u, Expire: %u:%02u:%02u.\n", rebind / 3600, (rebind / 60) % 60, rebind % 60, expire / 3600, (expire / 60) % 60, expire % 60); } else if (ret == E_OBJ) ntstdio_printf(&ntstdio, "DHCPv4 server: not available.\n"); } int usrcmd_dhcp4c(int argc, char **argv) { ER ret; if (ntlibc_strcmp(argv[1], "rel") == 0) { ret = dhcp4c_rel_info(); ntstdio_printf(&ntstdio, "dhcp4c_rel_info %d\n", ret); } else if (ntlibc_strcmp(argv[1], "renew") == 0) { ret = dhcp4c_renew_info(); ntstdio_printf(&ntstdio, "dhcp4c_renew_info %d\n", ret); } else { dhcp4c_info(); } return 0; } /* * s_show_dns_domain_name -- DNS のドメイン名を表示する。 */ static uint_t s_show_dns_domain_name(uint8_t *hdr, uint_t offset) { uint8_t *ptr; uint_t c; ptr = hdr + offset; while (*ptr) { if ((*ptr & DNS_MSG_COMP_MARK) == DNS_MSG_COMP_MARK) { s_show_dns_domain_name(hdr, (*ptr & ~DNS_MSG_COMP_MARK) << 8 | *(ptr + 1)); ptr += 2; break; } else { for (c = 1; c <= *ptr; c++) ntstdio_printf(&ntstdio, "%c", *(ptr + c)); ptr += *ptr + 1; if (*ptr) ntstdio_printf(&ntstdio, "."); } } return ptr - hdr; } /* * show_dns_soa -- DNS の SOA RDATA を表示する。 */ static ER_UINT show_dns_soa(uint8_t *msg, ER_UINT length, uint_t offset) { T_RSLV_SOA soa; ER_UINT error; uint_t rn_offset; if ((error = dns_analyze_soa(&soa, offset, msg, length)) < 0) return error; ntstdio_printf(&ntstdio, " mname: "); rn_offset = s_show_dns_domain_name(msg, offset); ntstdio_putc(&ntstdio, '\n'); ntstdio_printf(&ntstdio, " rname: "); s_show_dns_domain_name(msg, rn_offset); ntstdio_putc(&ntstdio, '\n'); ntstdio_printf(&ntstdio, " serial: %d\n", soa.serial); ntstdio_printf(&ntstdio, " refresh: %d\n", soa.refresh); ntstdio_printf(&ntstdio, " retry: %d\n", soa.retry); ntstdio_printf(&ntstdio, " expirel: %d\n", soa.expire); ntstdio_printf(&ntstdio, " minimum: %d\n", soa.minimum); return E_OK; } /* * show_dns_qdsection -- DNS の Question セクションを表示する。 */ static ER_UINT show_dns_qdsection(uint8_t *msg, ER_UINT length, T_RSLV_DNS_MSG *rslv) { T_RSLV_QD qd; ER_UINT offset, error; int scount; ntstdio_printf(&ntstdio, "question section: %d\n", rslv->dns_hdr.qdcount); offset = rslv->qd_offset; for (scount = 1; scount <= rslv->dns_hdr.qdcount; scount++) { if ((error = dns_analyze_qd(&qd, offset, msg, length)) < 0) return error; ntstdio_printf(&ntstdio, "%2d: ", scount); s_show_dns_domain_name(msg, offset); ntstdio_printf(&ntstdio, "\n type: %-4s, class: %2s\n", dns_strtype(qd.type), dns_strclass(qd.class)); offset = error; } return E_OK; } /* * show_dns_section -- DNS の各セクションを表示する。 */ static ER_UINT show_dns_section(uint8_t *msg, ER_UINT length, uint_t scount, uint_t offset, char *title) { T_RSLV_RR rr; T_IN4_ADDR in4_addr; ER_UINT error; int count, dcount, col; T_IN6_ADDR in6_addr; ntstdio_printf(&ntstdio, "%10s section: %d\n", title, scount); for (count = 1; count <= scount; count++) { if ((error = dns_analyze_rr(&rr, offset, msg, length)) < 0) return error; ntstdio_printf(&ntstdio, "%2d: ", count); s_show_dns_domain_name(msg, offset); ntstdio_printf(&ntstdio, "\n type: %-4s, class: %2s, TTL: %2d, len: %3d, offset: 0x%02x\n", dns_strtype(rr.type), dns_strclass(rr.class), rr.ttl, rr.rdlength, rr.rdata_offset); switch (rr.type) { case DNS_TYPE_A: memcpy((void*)&in4_addr, (void*)(msg + rr.rdata_offset), sizeof(in4_addr)); in4_addr = ntohl(in4_addr); ntstdio_printf(&ntstdio, " IPv4 addr: %hI\n", &in4_addr); break; case DNS_TYPE_NS: ntstdio_printf(&ntstdio, " host: "); s_show_dns_domain_name(msg, rr.rdata_offset); ntstdio_putc(&ntstdio, '\n'); break; case DNS_TYPE_CNAME: ntstdio_printf(&ntstdio, " host: "); s_show_dns_domain_name(msg, rr.rdata_offset); ntstdio_putc(&ntstdio, '\n'); break; case DNS_TYPE_SOA: show_dns_soa(msg, length, rr.rdata_offset); break; case DNS_TYPE_PTR: ntstdio_printf(&ntstdio, " PTR: "); s_show_dns_domain_name(msg, rr.rdata_offset); ntstdio_putc(&ntstdio, '\n'); break; case DNS_TYPE_AAAA: memcpy((void*)&in6_addr, (void*)(msg + rr.rdata_offset), sizeof(in6_addr)); ntstdio_printf(&ntstdio, " IPv6 addr: %lI\n", &in6_addr); break; default: ntstdio_printf(&ntstdio, " data: "); col = 32; for (dcount = 0; dcount < rr.rdlength; dcount++) { ntstdio_printf(&ntstdio, "%02x", *(msg + rr.rdata_offset + dcount)); if (--col == 0) { ntstdio_printf(&ntstdio, "\n "); col = 32; } } ntstdio_putc(&ntstdio, '\n'); break; } } return E_OK; } /* * dns_info -- DNS 情報の表示 */ static void dns_info() { #if defined(SUPPORT_INET6) T_IN6_ADDR in6_addr; #endif #if defined(SUPPORT_INET4) T_IN4_ADDR in4_addr; #endif #if defined(SUPPORT_INET6) ntstdio_printf(&ntstdio, "domain name: %s\n", dns_in6_get_dname()); #else /* of #if defined(SUPPORT_INET6) */ ntstdio_printf(&ntstdio, "domain name: %s\n", dns_in4_get_dname()); #endif /* of #if defined(SUPPORT_INET6) */ #if defined(SUPPORT_INET6) dns_in6_get_addr(&in6_addr); ntstdio_printf(&ntstdio, "IPv6 DNS server: "); if (IN6_IS_ADDR_UNSPECIFIED(&in6_addr)) ntstdio_printf(&ntstdio, "not available.\n"); else ntstdio_printf(&ntstdio, "%lI.\n", &in6_addr); #endif /* of #if defined(SUPPORT_INET6) */ #if defined(SUPPORT_INET4) dns_in4_get_addr(&in4_addr); ntstdio_printf(&ntstdio, "IPv4 DNS server: "); if (in4_addr == IPV4_ADDRANY) ntstdio_printf(&ntstdio, "not available.\n"); else ntstdio_printf(&ntstdio, "%hI.\n", &in4_addr); #endif /* of #if defined(SUPPORT_INET4) */ } /* * name_lookup -- ホスト名-IP アドレス変換 */ const char *rcode_str[] = { "no error", "format error", "server error", "name error", "not implement", "refused", }; int usrcmd_dnsc(int argc, char **argv) { char *line = argv[1]; static char hostname[DBG_LINE_SIZE + 1]; T_RSLV_DNS_MSG rslv; ER_UINT length, offset; ER error; uint_t flags = 0; uint8_t *msg; if (ntlibc_strcmp(line, "info") == 0) { dns_info(); return 0; } /* コマンドのオプションを設定する。*/ line = skip_blanks(resolv_options(&flags, line, DEFAULT_API_PROTO)); if ((flags & (DNS_LUP_FLAGS_PROTO_IPV6 | DNS_LUP_FLAGS_PROTO_IPV4)) == 0) { ntstdio_printf(&ntstdio, "DNS server not available.\n"); return 0; } /* 照会するホスト名・IP アドレスを解析する。*/ resolv_hoststr(&flags, hostname, sizeof(hostname), line); /* 正引きでも逆引きでもプロトコル上は正引きを指定する。*/ flags |= DNS_LUP_OPCODE_FORWARD; /* IPv6 アドレス、または IPv4 アドレスが指定された時は、照会タイプは PTR に設定する。*/ if (((flags & DNS_LUP_FLAGS_NAME_MASK) == DNS_LUP_FLAGS_NAME_IPV6) || ((flags & DNS_LUP_FLAGS_NAME_MASK) == DNS_LUP_FLAGS_NAME_IPV4)) flags = (flags & ~DNS_LUP_FLAGS_QTYPE_MASK) | DNS_LUP_FLAGS_QTYPE_PTR; if ((error = tget_mpf(MPF_RSLV_SRBUF, (void*)&msg, TMO_FEVR)) != E_OK) { ntstdio_printf(&ntstdio, "get buffer error: %s.\n", itron_strerror(error)); return 0; } if ((length = dns_lookup_host(flags | DNS_LUP_FLAGS_MSG, line, msg, DNS_UDP_MSG_LENGTH, &rslv)) < 0) { //ntstdio_printf(&ntstdio, "error: %s.\n", itron_strerror(length)); goto err_ret; } dly_tsk(1 * 1000); ntstdio_printf(&ntstdio, "DNS header: flags: "); if (rslv.dns_hdr.code & (DNS_QR_RESPONSE | DNS_AUTHORITATIVE | DNS_TRUN_CATION | DNS_RECURSION_DESIRED | DNS_RECURSION_AVAILABLE)) { ntstdio_printf(&ntstdio, (rslv.dns_hdr.code & DNS_QR_RESPONSE) ? "QR," : ""); ntstdio_printf(&ntstdio, (rslv.dns_hdr.code & DNS_AUTHORITATIVE) ? "AA," : ""); ntstdio_printf(&ntstdio, (rslv.dns_hdr.code & DNS_TRUN_CATION) ? "TC," : ""); ntstdio_printf(&ntstdio, (rslv.dns_hdr.code & DNS_RECURSION_DESIRED) ? "RD," : ""); ntstdio_printf(&ntstdio, (rslv.dns_hdr.code & DNS_RECURSION_AVAILABLE) ? "RA," : ""); ntstdio_printf(&ntstdio, " "); } ntstdio_printf(&ntstdio, "opcode: "); ntstdio_printf(&ntstdio, (rslv.dns_hdr.code & DNS_OPCODE_REVERSE) ? "RV" : "FW"); ntstdio_printf(&ntstdio, (rslv.dns_hdr.code & DNS_OPCODE_STATUS) ? ",ST" : ""); ntstdio_printf(&ntstdio, ", rcode: %s.\n", (rslv.dns_hdr.code & DNS_RCODE_MASK) > DNS_RCODE_REFUSED ? "6" : rcode_str[rslv.dns_hdr.code & DNS_RCODE_MASK]); if ((offset = show_dns_qdsection(msg, length, &rslv)) < 0) { ntstdio_printf(&ntstdio, "msg error: %s.\n", itron_strerror(offset)); } if ((offset = show_dns_section(msg, length, rslv.dns_hdr.ancount, rslv.an_offset, "answer")) < 0) { ntstdio_printf(&ntstdio, "msg error: %s.\n", itron_strerror(offset)); } if ((offset = show_dns_section(msg, length, rslv.dns_hdr.nscount, rslv.ns_offset, "authority")) < 0) { ntstdio_printf(&ntstdio, "msg error: %s.\n", itron_strerror(offset)); } if ((offset = show_dns_section(msg, length, rslv.dns_hdr.arcount, rslv.ar_offset, "additional")) < 0) { ntstdio_printf(&ntstdio, "msg error: %s.\n", itron_strerror(offset)); } err_ret: if ((error = rel_mpf(MPF_RSLV_SRBUF, msg)) != E_OK) ntstdio_printf(&ntstdio, "release buffer error: %s.\n", itron_strerror(error)); return 0; } int usrcmd_ntpc(int argc, char **argv) { ntp_cli_execute(); return 0; }