1 | /*
|
---|
2 | * TINET (TCP/IP Protocol Stack)
|
---|
3 | *
|
---|
4 | * Copyright (C) 2001-2009 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$
|
---|
32 | */
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * TCP ECHO クライアント
|
---|
36 | *
|
---|
37 | * ・送受信タスク分離型
|
---|
38 | * ・ノンブロッキングコール
|
---|
39 | * ・省コピー API
|
---|
40 | * ・IPv4
|
---|
41 | * ・IPv6
|
---|
42 | */
|
---|
43 |
|
---|
44 | #include <stdlib.h>
|
---|
45 |
|
---|
46 | #ifdef TARGET_KERNEL_ASP
|
---|
47 |
|
---|
48 | #include <kernel.h>
|
---|
49 | #include <sil.h>
|
---|
50 | #include <t_syslog.h>
|
---|
51 | #include "kernel_cfg.h"
|
---|
52 |
|
---|
53 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
54 |
|
---|
55 | #ifdef TARGET_KERNEL_JSP
|
---|
56 |
|
---|
57 | #include <t_services.h>
|
---|
58 | #include "kernel_id.h"
|
---|
59 |
|
---|
60 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
61 |
|
---|
62 | #include <tinet_defs.h>
|
---|
63 | #include <tinet_config.h>
|
---|
64 |
|
---|
65 | #include <net/if.h>
|
---|
66 | #include <net/if_ppp.h>
|
---|
67 | #include <net/if_loop.h>
|
---|
68 | #include <net/net.h>
|
---|
69 | #include <net/ethernet.h>
|
---|
70 | #include <net/net_var.h>
|
---|
71 | #include <net/net_timer.h>
|
---|
72 |
|
---|
73 | #include <netinet/in.h>
|
---|
74 | #include <netinet/in_itron.h>
|
---|
75 | #include <netinet/ip.h>
|
---|
76 | #include <netinet/ip6.h>
|
---|
77 | #include <netinet/tcp.h>
|
---|
78 |
|
---|
79 | #include <netapp/netapp.h>
|
---|
80 | #include <netapp/netapp_var.h>
|
---|
81 | #include <netapp/echo.h>
|
---|
82 |
|
---|
83 | #ifdef USE_TCP_ECHO_CLI
|
---|
84 |
|
---|
85 | /* echo サーバのポート番号 */
|
---|
86 |
|
---|
87 | #define ECHO_SRV_PORTNO UINT_C(7)
|
---|
88 |
|
---|
89 | /* 緊急データの送信 */
|
---|
90 |
|
---|
91 | #ifdef USE_TCP_EXTENTIONS
|
---|
92 |
|
---|
93 | //#define SND_URG_DATA_SIZE 4
|
---|
94 | #define SND_URG_COUNT 10
|
---|
95 |
|
---|
96 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
97 |
|
---|
98 | /* 表示 */
|
---|
99 |
|
---|
100 | //#define SHOW_RCV_RANGE
|
---|
101 |
|
---|
102 | /* タイムアウト */
|
---|
103 |
|
---|
104 | #define CON_TMO TMO_FEVR
|
---|
105 | //#define CON_TMO (60*SYSTIM_HZ)
|
---|
106 | //#define CLS_TMO TMO_FEVR /* Close Wait は標準で 60秒 */
|
---|
107 | #define CLS_TMO (70*SYSTIM_HZ+(net_rand()%SYSTIM_HZ)*10)
|
---|
108 | //#define RCV_TMO TMO_FEVR
|
---|
109 | #define RCV_TMO (40*SYSTIM_HZ+(net_rand()%SYSTIM_HZ)*20)
|
---|
110 | //#define SND_TMO TMO_FEVR
|
---|
111 | #define SND_TMO (30*SYSTIM_HZ+(net_rand()%SYSTIM_HZ)*30)
|
---|
112 |
|
---|
113 | /* 送信間隔 */
|
---|
114 |
|
---|
115 | //#define SND_DLY ULONG_C(500)
|
---|
116 | #define SND_ITV (5*SYSTM_HZ)
|
---|
117 | #define SLP_ITV (60*SYSTIM_HZ)
|
---|
118 |
|
---|
119 | /* 自動実行 */
|
---|
120 |
|
---|
121 | #if 0
|
---|
122 | #if defined(SUPPORT_INET6)
|
---|
123 | #define AUTO_RUN_STR "fe80::211:2fff:fe8a:e8c0 - 0"
|
---|
124 | #else
|
---|
125 | #define AUTO_RUN_STR "172.25.193.140 - 0"
|
---|
126 | #endif
|
---|
127 | #endif
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * バッファサイズの定義
|
---|
131 | */
|
---|
132 |
|
---|
133 | #define NUM_ECHO 20
|
---|
134 | #define NUM_REP_PAT 20
|
---|
135 | #define PAT_BEGIN ' '
|
---|
136 | #define PAT_END '~'
|
---|
137 | #define SND_BUF_SIZE ((PAT_END - PAT_BEGIN + 1) * NUM_REP_PAT)
|
---|
138 | #define RCV_BUF_SIZE (SND_BUF_SIZE*15/10)
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * 全域変数
|
---|
142 | */
|
---|
143 |
|
---|
144 | bool_t tcp_echo_cli_valid;
|
---|
145 |
|
---|
146 | /* TCP 送受信ウィンドバッファ */
|
---|
147 |
|
---|
148 | #ifndef TCP_CFG_SWBUF_CSAVE
|
---|
149 | uint8_t tcp_echo_cli_swbuf[TCP_ECHO_CLI_SWBUF_SIZE];
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | #ifndef TCP_CFG_RWBUF_CSAVE
|
---|
153 | uint8_t tcp_echo_cli_rwbuf[TCP_ECHO_CLI_RWBUF_SIZE];
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * 変数
|
---|
158 | */
|
---|
159 |
|
---|
160 | static uint_t scount;
|
---|
161 | static uint_t rcount;
|
---|
162 |
|
---|
163 | #ifdef USE_TCP_NON_BLOCKING
|
---|
164 |
|
---|
165 | static T_IPEP nblk_src = {
|
---|
166 | IP_ADDRANY,
|
---|
167 | TCP_PORTANY,
|
---|
168 | };
|
---|
169 | static T_IPEP nblk_dst;
|
---|
170 | static ER_UINT nblk_error;
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * ノンブロッキングコールのコールバック関数
|
---|
174 | */
|
---|
175 |
|
---|
176 | ER
|
---|
177 | callback_nblk_tcp_echo_cli (ID cepid, FN fncd, void *p_parblk)
|
---|
178 | {
|
---|
179 | ER error = E_OK;
|
---|
180 |
|
---|
181 | nblk_error = *(ER*)p_parblk;
|
---|
182 |
|
---|
183 | switch (fncd) {
|
---|
184 |
|
---|
185 | case TFN_TCP_CON_CEP:
|
---|
186 | syscall(sig_sem(SEM_TCP_ECHO_CLI_NBLK_READY));
|
---|
187 | break;
|
---|
188 |
|
---|
189 | case TFN_TCP_RCV_BUF:
|
---|
190 | if (nblk_error < 0)
|
---|
191 | syslog(LOG_NOTICE, "[TEC:%02d CBN] rcv buf error: %s", cepid, itron_strerror(nblk_error));
|
---|
192 | syscall(sig_sem(SEM_TCP_ECHO_CLI_NBLK_READY));
|
---|
193 | break;
|
---|
194 |
|
---|
195 | case TFN_TCP_CLS_CEP:
|
---|
196 | if (nblk_error < 0)
|
---|
197 | syslog(LOG_NOTICE, "[TEC:%02d CBN] close error: %s", cepid, itron_strerror(nblk_error));
|
---|
198 | syscall(sig_sem(SEM_TCP_ECHO_CLI_NBLK_READY));
|
---|
199 | break;
|
---|
200 |
|
---|
201 | case TFN_TCP_RCV_DAT:
|
---|
202 | if (nblk_error < 0)
|
---|
203 | syslog(LOG_NOTICE, "[TEC:%02d CBN] rcv dat error: %s", cepid, itron_strerror(nblk_error));
|
---|
204 | syscall(sig_sem(SEM_TCP_ECHO_CLI_NBLK_READY));
|
---|
205 | break;
|
---|
206 |
|
---|
207 | case TFN_TCP_SND_OOB:
|
---|
208 | if (nblk_error > 0)
|
---|
209 | syslog(LOG_NOTICE, "[TEC:%02d CBN] snd urg: %4d", cepid, nblk_error);
|
---|
210 | else
|
---|
211 | syslog(LOG_NOTICE, "[TEC:%02d CBN] snd urg error: %s", cepid, itron_strerror(nblk_error));
|
---|
212 | break;
|
---|
213 |
|
---|
214 | case TFN_TCP_ACP_CEP:
|
---|
215 | case TFN_TCP_SND_DAT:
|
---|
216 | case TFN_TCP_GET_BUF:
|
---|
217 | default:
|
---|
218 | error = E_PAR;
|
---|
219 | break;
|
---|
220 | }
|
---|
221 | return error;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * send_tcp_echo -- ECHO/TCP サーバにメッセージを送信する。
|
---|
226 | */
|
---|
227 |
|
---|
228 | static ER
|
---|
229 | send_tcp_echo (ID cepid, T_IN_ADDR *ipaddr, uint16_t portno)
|
---|
230 | {
|
---|
231 | static char smsg[SND_BUF_SIZE];
|
---|
232 |
|
---|
233 | ER_UINT slen;
|
---|
234 | ER error;
|
---|
235 | SYSTIM time;
|
---|
236 | uint32_t total;
|
---|
237 | uint16_t soff, echo, rep;
|
---|
238 | char *p, pat;
|
---|
239 |
|
---|
240 | #ifdef SND_URG_DATA_SIZE
|
---|
241 | int_t urg = SND_URG_COUNT;
|
---|
242 | #endif
|
---|
243 |
|
---|
244 | nblk_dst.ipaddr = *ipaddr;
|
---|
245 | nblk_dst.portno = portno;
|
---|
246 |
|
---|
247 | p = smsg;
|
---|
248 | for (rep = NUM_REP_PAT; rep -- > 0; )
|
---|
249 | for (pat = PAT_BEGIN; pat <= PAT_END; pat ++)
|
---|
250 | *p ++ = pat;
|
---|
251 |
|
---|
252 | if ((error = TCP_CON_CEP(cepid, &nblk_src, &nblk_dst, TMO_NBLK)) != E_WBLK) {
|
---|
253 | syslog(LOG_NOTICE, "[TEC:%02d SND] connect error: %s", cepid, itron_strerror(error));
|
---|
254 | return error;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* 接続が完了するまで待つ。*/
|
---|
258 | syscall(wai_sem(SEM_TCP_ECHO_CLI_NBLK_READY));
|
---|
259 |
|
---|
260 | if (nblk_error != E_OK)
|
---|
261 | syslog(LOG_NOTICE, "[TEC:%02d SND] connect error: %s", cepid, itron_strerror(nblk_error));
|
---|
262 | else {
|
---|
263 | get_tim(&time);
|
---|
264 | syslog(LOG_NOTICE, "[TEC:%02d SND] connecting: %6lu, to: %s.%u",
|
---|
265 | cepid, time / SYSTIM_HZ, IP2STR(NULL, ipaddr), nblk_dst.portno);
|
---|
266 |
|
---|
267 | if ((error = psnd_dtq(DTQ_TCP_ECHO_CLI_RCV, (intptr_t)cepid)) != E_OK) {
|
---|
268 | syslog(LOG_NOTICE, "[TEC:%02d SND] sync error: %s", cepid, itron_strerror(error));
|
---|
269 | goto cls_ret;
|
---|
270 | }
|
---|
271 |
|
---|
272 | scount = total = 0;
|
---|
273 | for (echo = NUM_ECHO; echo -- > 0; ) {
|
---|
274 | soff = 0;
|
---|
275 | while (soff < SND_BUF_SIZE) {
|
---|
276 | if ((slen = tcp_snd_dat(cepid, smsg + soff, sizeof(smsg) - soff, SND_TMO)) < 0) {
|
---|
277 | syslog(LOG_NOTICE, "[TEC:%02d SND] snd dat error: %s",
|
---|
278 | cepid, itron_strerror(slen));
|
---|
279 | goto cls_ret;
|
---|
280 | }
|
---|
281 | soff += (uint16_t)slen;
|
---|
282 | total += slen;
|
---|
283 | scount ++;
|
---|
284 | #ifdef SHOW_RCV_RANGE
|
---|
285 | syslog(LOG_NOTICE, "[TEC:%02d SND] send cnt: %2d, len: %4d, off: %4d",
|
---|
286 | cepid, scount, (uint16_t)slen, soff);
|
---|
287 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
288 |
|
---|
289 | #ifdef SND_URG_DATA_SIZE
|
---|
290 | if (urg) {
|
---|
291 | if (urg == 1) {
|
---|
292 | if ((error = tcp_snd_oob(cepid, smsg, SND_URG_DATA_SIZE, TMO_NBLK)) != E_WBLK)
|
---|
293 | syslog(LOG_NOTICE, "[TEC:%02d SND] snd urg error: %s",
|
---|
294 | cepid, itron_strerror(error));
|
---|
295 | }
|
---|
296 | urg --;
|
---|
297 | }
|
---|
298 | #endif /* of #ifdef SND_URG_DATA_SIZE */
|
---|
299 |
|
---|
300 | #if SND_DLY > 0
|
---|
301 | syscall(dly_tsk(SND_DLY + net_rand() % SYSTIM_HZ));
|
---|
302 | #endif
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | cls_ret:
|
---|
307 | if ((error = tcp_sht_cep(cepid)) < 0)
|
---|
308 | syslog(LOG_NOTICE, "[TEC:%02d SND] shutdown error: %s", cepid, itron_strerror(error));
|
---|
309 |
|
---|
310 | /* 受信が完了するまで待つ。*/
|
---|
311 | syscall(slp_tsk());
|
---|
312 | }
|
---|
313 |
|
---|
314 | return error;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * TCP ECHO クライアント受信タスク
|
---|
319 | */
|
---|
320 |
|
---|
321 | #ifdef USE_COPYSAVE_API
|
---|
322 |
|
---|
323 | void
|
---|
324 | tcp_echo_cli_rcv_task (intptr_t exinf)
|
---|
325 | {
|
---|
326 | ID tskid, cepid;
|
---|
327 | ER error;
|
---|
328 | SYSTIM time;
|
---|
329 | uint32_t total;
|
---|
330 | uint16_t roff, rlen;
|
---|
331 | char *rmsg, head, tail;
|
---|
332 |
|
---|
333 | get_tid(&tskid);
|
---|
334 | syslog(LOG_NOTICE, "[TCP ECHO CLI (NBLK,CS) RCV:%d] started.", tskid);
|
---|
335 | while (true) {
|
---|
336 | if ((error = rcv_dtq(DTQ_TCP_ECHO_CLI_RCV, (intptr_t*)&cepid)) != E_OK) {
|
---|
337 | syslog(LOG_NOTICE, "[TEC:%02d RCV] sync error: %s",
|
---|
338 | cepid, itron_strerror(error));
|
---|
339 | }
|
---|
340 | else {
|
---|
341 | roff = rcount = total = 0;
|
---|
342 | while (true) {
|
---|
343 | if ((error = tcp_rcv_buf(cepid, (void**)&rmsg, TMO_NBLK)) != E_WBLK) {
|
---|
344 | syslog(LOG_NOTICE, "[TEC:%02d RCV] rcv buf error: %s", cepid, itron_strerror(error));
|
---|
345 | break;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /* 受信バッファの獲得が完了するまで待つ。*/
|
---|
349 | syscall(wai_sem(SEM_TCP_ECHO_CLI_NBLK_READY));
|
---|
350 | if (nblk_error < 0)
|
---|
351 | break;
|
---|
352 |
|
---|
353 | rlen = nblk_error;
|
---|
354 | head = *rmsg;
|
---|
355 | tail = *(rmsg + rlen - 1);
|
---|
356 | if (rlen > 0) {
|
---|
357 | roff += rlen;
|
---|
358 | total += rlen;
|
---|
359 | rcount ++;
|
---|
360 | #ifdef SHOW_RCV_RANGE
|
---|
361 | syslog(LOG_NOTICE, "[TEC:%02d RCV] "
|
---|
362 | "recv cnt: %2d, len: %4d, off: %4d, data: %02x -> %02x",
|
---|
363 | cepid, rcount, rlen, roff, head, tail);
|
---|
364 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
365 | }
|
---|
366 | else
|
---|
367 | break;
|
---|
368 |
|
---|
369 | if ((error = tcp_rel_buf(cepid, rlen)) != E_OK)
|
---|
370 | syslog(LOG_NOTICE, "[TEC:%02d RCV] rel buf error: %s",
|
---|
371 | cepid, itron_strerror(error));
|
---|
372 | }
|
---|
373 |
|
---|
374 | if ((error = tcp_cls_cep(cepid, TMO_NBLK)) != E_WBLK && error != E_OK)
|
---|
375 | syslog(LOG_NOTICE, "[TEC:%02d SND] close error: %s", cepid, itron_strerror(error));
|
---|
376 |
|
---|
377 | /* 切断が完了するまで待つ。*/
|
---|
378 | syscall(wai_sem(SEM_TCP_ECHO_CLI_NBLK_READY));
|
---|
379 |
|
---|
380 | get_tim(&time);
|
---|
381 | syslog(LOG_NOTICE, "[TEC:%02u RCV] finished: %6lu, snd: %4u, rcv: %4u, len: %u",
|
---|
382 | cepid, time / SYSTIM_HZ, scount, rcount, total);
|
---|
383 | }
|
---|
384 |
|
---|
385 | syscall(wup_tsk(TCP_ECHO_CLI_SND_TASK));
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | #else /* of #ifdef USE_COPYSAVE_API */
|
---|
390 |
|
---|
391 | void
|
---|
392 | tcp_echo_cli_rcv_task (intptr_t exinf)
|
---|
393 | {
|
---|
394 | static char rmsg[RCV_BUF_SIZE];
|
---|
395 |
|
---|
396 | ID tskid, cepid;
|
---|
397 | ER error;
|
---|
398 | SYSTIM time;
|
---|
399 | uint32_t total;
|
---|
400 | uint16_t roff, rlen;
|
---|
401 | char head, tail;
|
---|
402 |
|
---|
403 | get_tid(&tskid);
|
---|
404 | syslog(LOG_NOTICE, "[TCP ECHO CLI (NBLK) RCV:%d] started.", tskid);
|
---|
405 | while (true) {
|
---|
406 | if ((error = rcv_dtq(DTQ_TCP_ECHO_CLI_RCV, (intptr_t*)&cepid)) != E_OK) {
|
---|
407 | syslog(LOG_NOTICE, "[TEC:%02d RCV] sync error: %s",
|
---|
408 | cepid, itron_strerror(error));
|
---|
409 | }
|
---|
410 | else {
|
---|
411 | roff = rcount = total = 0;
|
---|
412 | while (true) {
|
---|
413 | if ((error = tcp_rcv_dat(cepid, rmsg, sizeof(rmsg), TMO_NBLK)) != E_WBLK) {
|
---|
414 | syslog(LOG_NOTICE, "[TEC:%02d RCV] rcv error: %s", cepid, itron_strerror(error));
|
---|
415 | break;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /* 受信が完了するまで待つ。*/
|
---|
419 | syscall(wai_sem(SEM_TCP_ECHO_CLI_NBLK_READY));
|
---|
420 | if (nblk_error < 0)
|
---|
421 | break;
|
---|
422 |
|
---|
423 | rlen = nblk_error;
|
---|
424 | head = *rmsg;
|
---|
425 | tail = *(rmsg + rlen - 1);
|
---|
426 | rcount ++;
|
---|
427 | if (rlen > 0) {
|
---|
428 | roff += rlen;
|
---|
429 | total += rlen;
|
---|
430 | #ifdef SHOW_RCV_RANGE
|
---|
431 | syslog(LOG_NOTICE, "[TEC:%02d RCV] "
|
---|
432 | "recv cnt: %2d, len: %4d, off: %4d, data: %02x -> %02x",
|
---|
433 | cepid, rcount, rlen, roff, head, tail);
|
---|
434 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
435 | }
|
---|
436 | else
|
---|
437 | break;
|
---|
438 | }
|
---|
439 |
|
---|
440 | if ((error = tcp_cls_cep(cepid, TMO_NBLK)) != E_WBLK && error != E_OK)
|
---|
441 | syslog(LOG_NOTICE, "[TEC:%02d SND] close error: %s", cepid, itron_strerror(error));
|
---|
442 |
|
---|
443 | /* 切断が完了するまで待つ。*/
|
---|
444 | syscall(wai_sem(SEM_TCP_ECHO_CLI_NBLK_READY));
|
---|
445 |
|
---|
446 | get_tim(&time);
|
---|
447 | syslog(LOG_NOTICE, "[TEC:%02u RCV] finished: %6lu, snd: %4u, rcv: %4u, len: %u",
|
---|
448 | cepid, time / SYSTIM_HZ, scount, rcount, total);
|
---|
449 | }
|
---|
450 |
|
---|
451 | syscall(wup_tsk(TCP_ECHO_CLI_SND_TASK));
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | #endif /* of #ifdef USE_COPYSAVE_API */
|
---|
456 |
|
---|
457 | #else /* of #ifdef USE_TCP_NON_BLOCKING */
|
---|
458 |
|
---|
459 | /*
|
---|
460 | * send_tcp_echo -- ECHO/TCP サーバにメッセージを送信する。
|
---|
461 | */
|
---|
462 |
|
---|
463 | static ER
|
---|
464 | send_tcp_echo (ID cepid, T_IN_ADDR *ipaddr, uint16_t portno)
|
---|
465 | {
|
---|
466 | static char smsg[SND_BUF_SIZE];
|
---|
467 | static T_IPEP src = {
|
---|
468 | IP_ADDRANY,
|
---|
469 | TCP_PORTANY,
|
---|
470 | };
|
---|
471 |
|
---|
472 | T_IPEP dst;
|
---|
473 | ER_UINT slen;
|
---|
474 | ER error;
|
---|
475 | SYSTIM time;
|
---|
476 | uint32_t total;
|
---|
477 | uint16_t soff, echo, rep;
|
---|
478 | char pat, *p;
|
---|
479 |
|
---|
480 | #ifdef SND_URG_DATA_SIZE
|
---|
481 | int_t urg = SND_URG_COUNT;
|
---|
482 | #endif
|
---|
483 |
|
---|
484 | dst.ipaddr = *ipaddr;
|
---|
485 | dst.portno = portno;
|
---|
486 |
|
---|
487 | p = smsg;
|
---|
488 | for (rep = NUM_REP_PAT; rep -- > 0; )
|
---|
489 | for (pat = PAT_BEGIN; pat <= PAT_END; pat ++)
|
---|
490 | *p ++ = pat;
|
---|
491 |
|
---|
492 | if ((error = TCP_CON_CEP(cepid, &src, &dst, CON_TMO)) != E_OK) {
|
---|
493 | syslog(LOG_NOTICE, "[TEC:%02d SND] connect error: %s", cepid, itron_strerror(error));
|
---|
494 | return error;
|
---|
495 | }
|
---|
496 |
|
---|
497 | get_tim(&time);
|
---|
498 | syslog(LOG_NOTICE, "[TEC:%02u SND] connecting: %6lu, to: %s.%u",
|
---|
499 | cepid, time / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno);
|
---|
500 |
|
---|
501 | if ((error = psnd_dtq(DTQ_TCP_ECHO_CLI_RCV, (intptr_t)cepid)) != E_OK) {
|
---|
502 | syslog(LOG_NOTICE, "[TEC:%02d SND] sync error: %s", cepid, itron_strerror(error));
|
---|
503 | goto cls_ret;
|
---|
504 | }
|
---|
505 |
|
---|
506 | scount = total = 0;
|
---|
507 | for (echo = NUM_ECHO; echo -- > 0; ) {
|
---|
508 | soff = 0;
|
---|
509 | while (soff < SND_BUF_SIZE) {
|
---|
510 | if ((slen = tcp_snd_dat(cepid, smsg + soff, sizeof(smsg) - soff, SND_TMO)) < 0) {
|
---|
511 | syslog(LOG_NOTICE, "[TEC:%02d SND] snd dat error: %s", cepid, itron_strerror(slen));
|
---|
512 | goto cls_ret;
|
---|
513 | }
|
---|
514 | soff += (uint16_t)slen;
|
---|
515 | total += slen;
|
---|
516 | scount ++;
|
---|
517 | #ifdef SHOW_RCV_RANGE
|
---|
518 | syslog(LOG_NOTICE, "[TEC:%02d SND] send count: %3d, len: %4d, offset: %4d",
|
---|
519 | cepid, scount, (uint16_t)slen, soff);
|
---|
520 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
521 |
|
---|
522 | #ifdef SND_URG_DATA_SIZE
|
---|
523 | if (urg > 0) {
|
---|
524 | if (urg == 1) {
|
---|
525 | if ((slen = tcp_snd_oob(cepid, smsg, SND_URG_DATA_SIZE, SND_TMO)) >= 0)
|
---|
526 | syslog(LOG_NOTICE, "[TEC:%02d SND] snd urg: %4d",
|
---|
527 | cepid, (uint16_6)slen);
|
---|
528 | else
|
---|
529 | syslog(LOG_NOTICE, "[TEC:%02d SND] snd urg error: %s",
|
---|
530 | cepid, itron_strerror(slen));
|
---|
531 | }
|
---|
532 | urg --;
|
---|
533 | }
|
---|
534 | #endif /* of #ifdef SND_URG_DATA_SIZE */
|
---|
535 |
|
---|
536 | #if SND_DLY > 0
|
---|
537 | syscall(dly_tsk(SND_DLY + net_rand() % SYSTIM_HZ));
|
---|
538 | #endif
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | cls_ret:
|
---|
543 | if ((error = tcp_sht_cep(cepid)) < 0)
|
---|
544 | syslog(LOG_NOTICE, "[TEC:%02d SND] shutdown error: %s", cepid, itron_strerror(error));
|
---|
545 |
|
---|
546 | /* 受信が完了するまで待つ。*/
|
---|
547 | syscall(slp_tsk());
|
---|
548 |
|
---|
549 | return error;
|
---|
550 | }
|
---|
551 |
|
---|
552 | /*
|
---|
553 | * TCP ECHO クライアント受信タスク
|
---|
554 | */
|
---|
555 |
|
---|
556 | #ifdef USE_COPYSAVE_API
|
---|
557 |
|
---|
558 | void
|
---|
559 | tcp_echo_cli_rcv_task (intptr_t exinf)
|
---|
560 | {
|
---|
561 | ID tskid, cepid;
|
---|
562 | ER_UINT rlen;
|
---|
563 | ER error;
|
---|
564 | SYSTIM time;
|
---|
565 | uint32_t total;
|
---|
566 | uint16_t roff;
|
---|
567 | char *rmsg;
|
---|
568 |
|
---|
569 | get_tid(&tskid);
|
---|
570 | syslog(LOG_NOTICE, "[TCP ECHO CLI (CS) RCV:%d] started.", tskid);
|
---|
571 | while (true) {
|
---|
572 | if ((error = rcv_dtq(DTQ_TCP_ECHO_CLI_RCV, (intptr_t*)&cepid)) != E_OK) {
|
---|
573 | syslog(LOG_NOTICE, "[TEC:%02d RCV] sync error: %s",
|
---|
574 | cepid, itron_strerror(error));
|
---|
575 | }
|
---|
576 | else {
|
---|
577 | roff = rcount = total = 0;
|
---|
578 | while (true) {
|
---|
579 | if ((rlen = tcp_rcv_buf(cepid, (void**)&rmsg, RCV_TMO)) < 0) {
|
---|
580 | syslog(LOG_NOTICE, "[TEC:%02d RCV] rcv buf error: %s",
|
---|
581 | cepid, itron_strerror(rlen));
|
---|
582 | break;
|
---|
583 | }
|
---|
584 | else if (rlen > 0) {
|
---|
585 | roff += (uint16_t)rlen;
|
---|
586 | total += rlen;
|
---|
587 | rcount ++;
|
---|
588 | #ifdef SHOW_RCV_RANGE
|
---|
589 | syslog(LOG_NOTICE, "[TEC:%02d RCV] recv count: %3d, len: %4d, data: %02x -> %02x",
|
---|
590 | cepid, rcount, (uint16_t)rlen, *rmsg, *(rmsg + rlen - 1));
|
---|
591 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
592 | }
|
---|
593 | else
|
---|
594 | break;
|
---|
595 | if ((error = tcp_rel_buf(cepid, rlen)) != E_OK)
|
---|
596 | syslog(LOG_NOTICE, "[TEC:%02d RCV] rel buf error: %s", cepid, itron_strerror(error));
|
---|
597 | }
|
---|
598 |
|
---|
599 | if ((error = tcp_cls_cep(cepid, CLS_TMO)) < 0)
|
---|
600 | syslog(LOG_NOTICE, "[TEC:%02d RCV] close error: %s", cepid, itron_strerror(error));
|
---|
601 |
|
---|
602 | get_tim(&time);
|
---|
603 | syslog(LOG_NOTICE, "[TEC:%02u RCV] finished: %6lu, snd: %4u, rcv: %4u, len: %u",
|
---|
604 | cepid, time / SYSTIM_HZ, scount, rcount, total);
|
---|
605 | }
|
---|
606 |
|
---|
607 | syscall(wup_tsk(TCP_ECHO_CLI_SND_TASK));
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | #else /* of #ifdef USE_COPYSAVE_API */
|
---|
612 |
|
---|
613 | void
|
---|
614 | tcp_echo_cli_rcv_task (intptr_t exinf)
|
---|
615 | {
|
---|
616 | static char rmsg[RCV_BUF_SIZE];
|
---|
617 |
|
---|
618 | ID tskid, cepid;
|
---|
619 | ER_UINT rlen;
|
---|
620 | ER error;
|
---|
621 | SYSTIM time;
|
---|
622 | uint32_t total;
|
---|
623 | uint16_t roff;
|
---|
624 |
|
---|
625 | get_tid(&tskid);
|
---|
626 | syslog(LOG_NOTICE, "[TCP ECHO CLI RCV:%d] started.", tskid);
|
---|
627 | while (true) {
|
---|
628 | if ((error = rcv_dtq(DTQ_TCP_ECHO_CLI_RCV, (intptr_t*)&cepid)) != E_OK) {
|
---|
629 | syslog(LOG_NOTICE, "[TEC:%02d RCV] sync error: %s",
|
---|
630 | cepid, itron_strerror(error));
|
---|
631 | }
|
---|
632 | else {
|
---|
633 | roff = rcount = total = 0;
|
---|
634 | while (true) {
|
---|
635 | if ((rlen = tcp_rcv_dat(cepid, rmsg, sizeof(rmsg), RCV_TMO)) < 0) {
|
---|
636 | syslog(LOG_NOTICE, "[TEC:%02d RCV] rcv dat error: %s", cepid, itron_strerror(rlen));
|
---|
637 | tcp_can_cep(cepid, TFN_TCP_ALL);
|
---|
638 | break;
|
---|
639 | }
|
---|
640 | else if (rlen > 0) {
|
---|
641 | roff += (uint16_t)rlen;
|
---|
642 | total += rlen;
|
---|
643 | rcount ++;
|
---|
644 | #ifdef SHOW_RCV_RANGE
|
---|
645 | syslog(LOG_NOTICE, "[TEC:%02d RCV] recv count: %3d, len: %4d, data: %02x -> %02x",
|
---|
646 | cepid, rcount, (uint16_t)rlen, *rmsg, *(rmsg + rlen - 1));
|
---|
647 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
648 | }
|
---|
649 | else
|
---|
650 | break;
|
---|
651 | }
|
---|
652 |
|
---|
653 | if ((error = tcp_cls_cep(cepid, CLS_TMO)) < 0)
|
---|
654 | syslog(LOG_NOTICE, "[TEC:%02d RCV] close error: %s", cepid, itron_strerror(error));
|
---|
655 |
|
---|
656 | get_tim(&time);
|
---|
657 | syslog(LOG_NOTICE, "[TEC:%02u RCV] received: %6lu, snd: %4u, rcv: %4u, len: %u",
|
---|
658 | cepid, time / SYSTIM_HZ, scount, rcount, total);
|
---|
659 | }
|
---|
660 |
|
---|
661 | syscall(wup_tsk(TCP_ECHO_CLI_SND_TASK));
|
---|
662 | }
|
---|
663 | }
|
---|
664 |
|
---|
665 | #endif /* of #ifdef USE_COPYSAVE_API */
|
---|
666 |
|
---|
667 | #endif /* of #ifdef USE_TCP_NON_BLOCKING */
|
---|
668 |
|
---|
669 | /*
|
---|
670 | * getcomd -- コマンドを得る。
|
---|
671 | */
|
---|
672 |
|
---|
673 | #ifdef AUTO_RUN_STR
|
---|
674 |
|
---|
675 | static char *
|
---|
676 | getcomd (ID cepid)
|
---|
677 | {
|
---|
678 | ER error;
|
---|
679 | char *line = NULL;
|
---|
680 | static char auto_run_str[] = AUTO_RUN_STR;
|
---|
681 | static int_t count = 0;
|
---|
682 |
|
---|
683 | if (count == 0) {
|
---|
684 | line = auto_run_str;
|
---|
685 | dly_tsk(3 * 1000);
|
---|
686 | }
|
---|
687 | else {
|
---|
688 | while ((error = rcv_dtq(DTQ_TCP_ECHO_CLI_SND, (intptr_t*)&line)) != E_OK) {
|
---|
689 | syslog(LOG_NOTICE, "[TEC:%02d TSK] error: %s", cepid, itron_strerror(error));
|
---|
690 | dly_tsk(SLP_ITV);
|
---|
691 | }
|
---|
692 | }
|
---|
693 |
|
---|
694 | count ++;
|
---|
695 | return line;
|
---|
696 | }
|
---|
697 |
|
---|
698 | #else /* of #ifdef AUTO_RUN_STR */
|
---|
699 |
|
---|
700 | static char *
|
---|
701 | getcomd (ID cepid)
|
---|
702 | {
|
---|
703 | ER error;
|
---|
704 | char *line = NULL;
|
---|
705 |
|
---|
706 | while ((error = rcv_dtq(DTQ_TCP_ECHO_CLI_SND, (intptr_t*)&line)) != E_OK) {
|
---|
707 | syslog(LOG_NOTICE, "[TEC:%02d TSK] error: %s", cepid, itron_strerror(error));
|
---|
708 | dly_tsk(SLP_ITV);
|
---|
709 | }
|
---|
710 | return line;
|
---|
711 | }
|
---|
712 |
|
---|
713 | #endif /* of #ifdef AUTO_RUN_STR */
|
---|
714 |
|
---|
715 | /*
|
---|
716 | * TCP ECHO クライアント送信タスク
|
---|
717 | */
|
---|
718 |
|
---|
719 | void
|
---|
720 | tcp_echo_cli_snd_task (intptr_t exinf)
|
---|
721 | {
|
---|
722 | ID tskid, cepid;
|
---|
723 | ER error;
|
---|
724 | T_IN_ADDR addr;
|
---|
725 | uint16_t portno;
|
---|
726 | int_t rep, count;
|
---|
727 | char *line;
|
---|
728 |
|
---|
729 | #ifdef USE_TCP_EXTENTIONS
|
---|
730 |
|
---|
731 | T_TCP_CCEP ccep;
|
---|
732 |
|
---|
733 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
734 |
|
---|
735 | get_tid(&tskid);
|
---|
736 | syslog(LOG_NOTICE, "[TCP ECHO CLI SND:%d,%d] started.", tskid, (ID)exinf);
|
---|
737 | while (true) {
|
---|
738 | line = getcomd((ID)exinf);
|
---|
739 | line = skip_blanks(GET_IPADDR(&addr, skip_blanks(line))); /* IP Address */
|
---|
740 |
|
---|
741 | if ('0' <= *line && *line <= '9') { /* Port No */
|
---|
742 | line = get_int(&rep, line);
|
---|
743 | portno = (uint16_t)rep;
|
---|
744 | }
|
---|
745 | else {
|
---|
746 | line ++;
|
---|
747 | portno = ECHO_SRV_PORTNO;
|
---|
748 | }
|
---|
749 |
|
---|
750 | line = skip_blanks(line);
|
---|
751 | if ('0' <= *line && *line <= '9') /* Repeat */
|
---|
752 | line = get_int(&rep, line);
|
---|
753 | else
|
---|
754 | rep = 1;
|
---|
755 |
|
---|
756 | #ifdef USE_TCP_EXTENTIONS
|
---|
757 |
|
---|
758 | ccep.cepatr = 0;
|
---|
759 | ccep.sbufsz = TCP_ECHO_CLI_SWBUF_SIZE;
|
---|
760 | ccep.rbufsz = TCP_ECHO_CLI_RWBUF_SIZE;
|
---|
761 |
|
---|
762 | #ifdef TCP_CFG_SWBUF_CSAVE
|
---|
763 | ccep.sbuf = NADR;
|
---|
764 | #else
|
---|
765 | ccep.sbuf = tcp_echo_cli_swbuf;
|
---|
766 | #endif
|
---|
767 | #ifdef TCP_CFG_RWBUF_CSAVE
|
---|
768 | ccep.rbuf = NADR;
|
---|
769 | #else
|
---|
770 | ccep.rbuf = tcp_echo_cli_rwbuf;
|
---|
771 | #endif
|
---|
772 | #ifdef USE_TCP_NON_BLOCKING
|
---|
773 | ccep.callback = (FP)callback_nblk_tcp_echo_cli;
|
---|
774 | #else
|
---|
775 | ccep.callback = NULL;
|
---|
776 | #endif
|
---|
777 |
|
---|
778 | if ((error = alloc_tcp_cep(&cepid, tskid, &ccep)) != E_OK) {
|
---|
779 | syslog(LOG_NOTICE, "[TEC:%02d TSK] CEP create error: %s", cepid, itron_strerror(error));
|
---|
780 | continue;
|
---|
781 | }
|
---|
782 |
|
---|
783 | #else /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
784 |
|
---|
785 | cepid = (ID)exinf;
|
---|
786 |
|
---|
787 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
788 |
|
---|
789 | tcp_echo_cli_valid = true;
|
---|
790 | count = 0;
|
---|
791 | while (rep == 0 || count < rep) {
|
---|
792 | if (!tcp_echo_cli_valid) {
|
---|
793 | syslog(LOG_NOTICE, "[TEC:%02d TSK] canceled.", cepid);
|
---|
794 | break;
|
---|
795 | }
|
---|
796 |
|
---|
797 | count ++;
|
---|
798 | if (rep == 0) {
|
---|
799 | syslog(LOG_NOTICE, "[TEC:%02d TSK] start: repeat: %d", cepid, count);
|
---|
800 | error = send_tcp_echo(cepid, &addr, portno);
|
---|
801 | syslog(LOG_NOTICE, "[TEC:%02d TSK] finished: repeat: %d", cepid, count);
|
---|
802 | }
|
---|
803 | else {
|
---|
804 | syslog(LOG_NOTICE, "[TEC:%02d TSK] start: repeat: %d/%d", cepid, count, rep);
|
---|
805 | error = send_tcp_echo(cepid, &addr, portno);
|
---|
806 | syslog(LOG_NOTICE, "[TEC:%02d TSK] finished: repeat: %d/%d", cepid, count, rep);
|
---|
807 | }
|
---|
808 |
|
---|
809 | if (error != E_OK) {
|
---|
810 | syslog(LOG_NOTICE, "[TEC:%02d TSK] sleep %d[ms], error: %s",
|
---|
811 | cepid, SLP_ITV, itron_strerror(error));
|
---|
812 | tslp_tsk(SLP_ITV);
|
---|
813 | syslog(LOG_NOTICE, "[TEC:%02d TSK] resume.", cepid);
|
---|
814 | }
|
---|
815 |
|
---|
816 | #if defined(SND_ITV)
|
---|
817 | #if SND_ITV > 0
|
---|
818 | if (count > 0) {
|
---|
819 | uint_t itv;
|
---|
820 |
|
---|
821 | itv = SND_ITV;
|
---|
822 | syslog(LOG_NOTICE, "[TEC:%02d TSK] interval: %d[ms].", cepid, itv);
|
---|
823 | syscall(dly_tsk(SND_ITV));
|
---|
824 | }
|
---|
825 | #endif
|
---|
826 | #endif
|
---|
827 | }
|
---|
828 |
|
---|
829 | #ifdef USE_TCP_EXTENTIONS
|
---|
830 |
|
---|
831 | if ((error = free_tcp_cep(cepid)) != E_OK)
|
---|
832 | syslog(LOG_NOTICE, "[TEC:%02d TSK] CEP delete error: %s", cepid, itron_strerror(error));
|
---|
833 |
|
---|
834 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
835 |
|
---|
836 | }
|
---|
837 | }
|
---|
838 |
|
---|
839 | #endif /* of #ifdef USE_TCP_ECHO_CLI */
|
---|