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) の条件か,Free Software Foundation
|
---|
8 | * によって公表されている GNU General Public License の Version 2 に記
|
---|
9 | * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
|
---|
10 | * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
|
---|
11 | * 利用と呼ぶ)することを無償で許諾する.
|
---|
12 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
13 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
14 | * スコード中に含まれていること.
|
---|
15 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
16 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
17 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
18 | * の無保証規定を掲載すること.
|
---|
19 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
20 | * 用できない形で再配布する場合には,次の条件を満たすこと.
|
---|
21 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
22 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
23 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
24 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
25 | *
|
---|
26 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
27 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
|
---|
28 | * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
|
---|
29 | * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
|
---|
30 | *
|
---|
31 | * @(#) $Id: ping.c 315 2017-07-23 05:29:40Z coas-nagasima $
|
---|
32 | */
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * ping -- ICMP ECHO メッセージを送信する。
|
---|
36 | */
|
---|
37 |
|
---|
38 | #ifdef TARGET_KERNEL_ASP
|
---|
39 |
|
---|
40 | #include <kernel.h>
|
---|
41 | #include <sil.h>
|
---|
42 | #include <t_syslog.h>
|
---|
43 |
|
---|
44 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
45 |
|
---|
46 | #ifdef TARGET_KERNEL_JSP
|
---|
47 |
|
---|
48 | #include <s_services.h>
|
---|
49 | #include <t_services.h>
|
---|
50 |
|
---|
51 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
52 |
|
---|
53 | #include <tinet_defs.h>
|
---|
54 | #include <tinet_config.h>
|
---|
55 |
|
---|
56 | #include <net/if.h>
|
---|
57 | #include <net/if_ppp.h>
|
---|
58 | #include <net/if_loop.h>
|
---|
59 | #include <net/ethernet.h>
|
---|
60 | #include <net/ppp_var.h>
|
---|
61 | #include <net/ppp_ipcp.h>
|
---|
62 | #include <net/net.h>
|
---|
63 | #include <net/net_endian.h>
|
---|
64 | #include <net/net_buf.h>
|
---|
65 | #include <net/net_timer.h>
|
---|
66 | #include <net/net_count.h>
|
---|
67 |
|
---|
68 | #include <netinet/in.h>
|
---|
69 | #include <netinet/in_var.h>
|
---|
70 | #include <netinet/ip.h>
|
---|
71 | #include <netinet/ip_var.h>
|
---|
72 | #include <netinet/ip_icmp.h>
|
---|
73 | #include <netinet/icmp_var.h>
|
---|
74 |
|
---|
75 | #ifdef USE_PING
|
---|
76 |
|
---|
77 | #if defined(SUPPORT_INET4)
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * 変数
|
---|
81 | */
|
---|
82 |
|
---|
83 | #if 0
|
---|
84 | static uint16_t curr_icmp_id;
|
---|
85 | static uint16_t curr_icmp_len;
|
---|
86 | #endif
|
---|
87 |
|
---|
88 | static uint16_t icmp_id = 0;
|
---|
89 | static SYSTIM icmp_start_time;
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * icmp_reply_timeout -- ICMP REPLY タイムアウト処理
|
---|
93 | */
|
---|
94 |
|
---|
95 | static void
|
---|
96 | icmp_reply_timeout (void *arg)
|
---|
97 | {
|
---|
98 | syslog(LOG_NOTICE, "[PING4] request timeout.");
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * icmp_echo_reply -- ICMP ECHO REPLY メッセージを処理する。
|
---|
103 | */
|
---|
104 |
|
---|
105 | void
|
---|
106 | icmp_echo_reply (T_NET_BUF *input, uint_t ihoff)
|
---|
107 | {
|
---|
108 | SYSTIM time;
|
---|
109 | T_IN4_ADDR addr;
|
---|
110 |
|
---|
111 | syscall(get_tim(&time));
|
---|
112 | addr = ntohl(GET_IP4_HDR(input)->src);
|
---|
113 | untimeout(icmp_reply_timeout, NULL);
|
---|
114 | syslog(LOG_NOTICE, "[PING4] reply: %5ld [ms] from: %s",
|
---|
115 | (time - icmp_start_time) * 1000 / SYSTIM_HZ,
|
---|
116 | ip2str(NULL, &addr));
|
---|
117 | }
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * ping4 -- ICMP ECHO メッセージを送信する。
|
---|
121 | */
|
---|
122 |
|
---|
123 | void
|
---|
124 | ping4 (T_IN4_ADDR *addr, uint_t tmo, uint_t len)
|
---|
125 | {
|
---|
126 | T_ICMP4_HDR *icmp4h;
|
---|
127 | T_NET_BUF *output;
|
---|
128 | uint8_t *sdu;
|
---|
129 | int_t ix;
|
---|
130 |
|
---|
131 | /* 送信用の IP データグラムを獲得する。*/
|
---|
132 | if (in4_get_datagram(&output, ICMP4_HDR_SIZE + len, 0,
|
---|
133 | addr, NULL, IPPROTO_ICMP,
|
---|
134 | IP4_DEFTTL, NBA_SEARCH_ASCENT, TMO_FEVR) != E_OK)
|
---|
135 | return;
|
---|
136 |
|
---|
137 | /* ICMP ヘッダを設定する。*/
|
---|
138 | icmp4h = GET_ICMP4_HDR(output, IF_IP4_ICMP4_HDR_OFFSET);
|
---|
139 | icmp4h->type = ICMP4_ECHO_REQUEST;
|
---|
140 | icmp4h->code = 0;
|
---|
141 | icmp4h->data.id_seq.id = htons(icmp_id);
|
---|
142 | icmp4h->data.id_seq.seq = 0;
|
---|
143 |
|
---|
144 | /* データを設定する。*/
|
---|
145 | sdu = GET_ICMP4_SDU(output, IF_IP4_ICMP4_HDR_OFFSET) + len;
|
---|
146 | for (ix = len; ix -- > 0; )
|
---|
147 | *-- sdu = ('0' + icmp_id + ix) & 0xff;
|
---|
148 |
|
---|
149 | /* チェックサムを計算する。*/
|
---|
150 | icmp4h->sum = 0;
|
---|
151 | icmp4h->sum = in_cksum(icmp4h, ICMP4_HDR_SIZE + len);
|
---|
152 |
|
---|
153 | /* 応答チェック用の変数を設定する。*/
|
---|
154 | #if 0
|
---|
155 | curr_icmp_id = icmp_id ++;
|
---|
156 | curr_icmp_len = len;
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | NET_COUNT_MIB(icmp_stats.icmpOutMsgs, 1);
|
---|
160 | NET_COUNT_MIB(icmp_stats.icmpOutEchos, 1);
|
---|
161 |
|
---|
162 | /* 送信後、現在の時間を記憶し、タイムアウトを設定する。*/
|
---|
163 | ip_output(output, TMO_FEVR);
|
---|
164 |
|
---|
165 | syscall(get_tim(&icmp_start_time));
|
---|
166 | timeout(icmp_reply_timeout, NULL, tmo * NET_TIMER_HZ);
|
---|
167 |
|
---|
168 | }
|
---|
169 |
|
---|
170 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
171 |
|
---|
172 | #endif /* of #ifdef USE_PING */
|
---|