source: azure_iot_hub/trunk/asp3_dcre/tinet/netapp/tcp_discard_cli.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 13.3 KB
Line 
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) の条件か,Free Software Foundation
9 * によってå…
10¬è¡¨ã•ã‚Œã¦ã„ã‚‹ GNU General Public License の Version 2 に記
11 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
12 * を改変したものを含む.以下同じ)を使用・複製・改変・再é…
13å¸ƒï¼ˆä»¥ä¸‹ï¼Œ
14 * 利用と呼ぶ)することを無償で許諾する.
15 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
16 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
17 * スコード中に含まれていること.
18 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
19 * 用できる形で再é…
20å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
21å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
22 * 者
23マニュアルなど)に,上記の著作権表示,この利用条件および下記
24 * の無保証規定を掲載すること.
25 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
26 * 用できない形で再é…
27å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®æ¡ä»¶ã‚’満たすこと.
28 * (a) 再é…
29å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
30マニュアルなど)に,上記の著
31 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
32 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
33 * 害からも,上記著作権者
34およびTOPPERSプロジェクトをå…
35è²¬ã™ã‚‹ã“と.
36 *
37 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者
38お
39 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
40 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
41 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
42 *
43 * @(#) $Id: tcp_discard_cli.c 388 2019-05-22 11:25:18Z coas-nagasima $
44 */
45
46/*
47 * TCP DISCARD クライアント
48 *
49 * ・ノンブロッキングコール
50 * ・IPv4
51 * ・IPv6
52 */
53
54#include <stdlib.h>
55
56#ifdef TARGET_KERNEL_ASP
57
58#include <kernel.h>
59#include <sil.h>
60#include <t_syslog.h>
61#include "kernel_cfg.h"
62
63#endif /* of #ifdef TARGET_KERNEL_ASP */
64
65#ifdef TARGET_KERNEL_JSP
66
67#include <t_services.h>
68#include "kernel_id.h"
69
70#endif /* of #ifdef TARGET_KERNEL_JSP */
71
72#include <netinet/in.h>
73#include <netinet/in_itron.h>
74
75#include <netapp/netapp.h>
76#include <netapp/netapp_var.h>
77#include <netapp/tcp_discard_cli.h>
78
79#ifdef USE_TCP_DISCARD_CLI
80
81/* discard サーバのポート番号 */
82
83#define DISCARD_SRV_PORTNO UINT_C(9)
84
85/* 表示 */
86
87/*#define SHOW_RCV_RANGE*/
88
89/* 送信間隔 */
90
91#ifdef TOPPERS_S810_CLG3_85
92
93#define START_DLY (10*SYSTIM_HZ)
94#define SND_ITV (5*SYSTIM_HZ)
95#define SLP_ITV (30*SYSTIM_HZ)
96
97#else /* of #ifdef TOPPERS_S810_CLG3_85 */
98
99#define START_DLY (10*SYSTIM_HZ)
100#define SND_ITV (5*SYSTIM_HZ)
101#define SLP_ITV (60*SYSTIM_HZ)
102
103#endif /* of #ifdef TOPPERS_S810_CLG3_85 */
104
105/* 自動実行 */
106
107#if defined(TCP_DISCARD_CLI_AUTO_RUN_STR)
108#else
109#if 0
110#if defined(SUPPORT_INET6)
111#define TCP_DISCARD_CLI_AUTO_RUN_STR "fd90:cce5:25f6:ff81:201:2ff:fe81:e7c9 - 0"
112#else
113#define TCP_DISCARD_CLI_AUTO_RUN_STR "172.25.129.142 - 0"
114#endif
115#endif
116#endif
117
118/*
119 * バッファサイズの定義
120 */
121
122#define NUM_DISCARD 5
123#define NUM_REP_PAT 40
124#define PAT_BEGIN ' '
125#define PAT_END '~'
126#define SND_BUF_SIZE ((PAT_END - PAT_BEGIN + 1) * NUM_REP_PAT)
127
128/*
129 * å…
130¨åŸŸå¤‰æ•°
131 */
132
133bool_t tcp_discard_cli_valid;
134
135/* TCP 送受信ウィンドバッファ */
136
137#ifndef TCP_CFG_SWBUF_CSAVE_ONLY
138uint8_t tcp_discard_cli_swbuf[TCP_DISCARD_CLI_SWBUF_SIZE];
139#endif
140
141#ifdef USE_TCP_NON_BLOCKING
142
143/*
144 * 変数
145 */
146
147static T_IPEP nblk_src = {
148 IP_ADDRANY,
149 TCP_PORTANY,
150 };
151static T_IPEP nblk_dst;
152static ER_UINT nblk_error;
153
154/*
155 * ノンブロッキングコールのコールバック関数
156 */
157
158ER
159callback_nblk_tcp_discard_cli (ID cepid, FN fncd, void *p_parblk)
160{
161 ER error = E_OK;
162
163 nblk_error = *(ER*)p_parblk;
164
165 switch (fncd) {
166
167 case TFN_TCP_CON_CEP:
168 syscall(sig_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
169 break;
170
171 case TFN_TCP_CLS_CEP:
172 if (nblk_error < 0)
173 syslog(LOG_NOTICE, "[TDCn:%02u CBN] close error: %s", cepid, itron_strerror(nblk_error));
174 syscall(sig_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
175 break;
176
177 case TFN_TCP_ACP_CEP:
178 case TFN_TCP_SND_DAT:
179 case TFN_TCP_GET_BUF:
180 case TFN_TCP_SND_OOB:
181 default:
182 error = E_PAR;
183 break;
184 }
185 return error;
186 }
187
188/*
189 * send_tcp_discard -- DISCARD/TCP サーバにメッセージを送信する。
190 */
191
192static ER
193send_tcp_discard (ID cepid, T_IN_ADDR *ipaddr, uint16_t portno)
194{
195 static char smsg[SND_BUF_SIZE];
196
197 ER_UINT slen;
198 ER error;
199 SYSTIM time;
200 uint32_t total;
201 uint16_t soff, discard, rep, scount;
202 uint8_t pat, *p;
203
204 nblk_dst.ipaddr = *ipaddr;
205 nblk_dst.portno = portno;
206
207 p = smsg;
208 for (rep = NUM_REP_PAT; rep -- > 0; )
209 for (pat = PAT_BEGIN; pat <= PAT_END; pat ++)
210 *p ++ = pat;
211
212 if ((error = TCP_CON_CEP(cepid, &nblk_src, &nblk_dst, TMO_NBLK)) != E_WBLK) {
213 syslog(LOG_NOTICE, "[TDCn:%02u SND] connect error: %s", cepid, itron_strerror(error));
214 return error;
215 }
216
217 /* 接続が完了するまで待
218つ。*/
219 syscall(wai_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
220
221 if (nblk_error != E_OK)
222 syslog(LOG_NOTICE, "[TDCn:%02u SND] connect error: %s", cepid, itron_strerror(nblk_error));
223 else {
224 get_tim(&time);
225 syslog(LOG_NOTICE, "[TDCn:%02u SND] conct: %7lu, to: %s.%d",
226 cepid, time / SYSTIM_HZ, IP2STR(NULL, ipaddr), nblk_dst.portno);
227
228 scount = total = 0;
229 for (discard = NUM_DISCARD; discard -- > 0; ) {
230 soff = 0;
231 while (soff < SND_BUF_SIZE) {
232 if ((slen = tcp_snd_dat(cepid, smsg + soff, sizeof(smsg) - soff, TMO_FEVR)) < 0) {
233 syslog(LOG_NOTICE, "[TDCn:%02u SND] send error: %s",
234 cepid, itron_strerror(slen));
235 goto cls_ret;
236 }
237 soff += (uint16_t)slen;
238 total += slen;
239 scount ++;
240
241#ifdef SHOW_RCV_RANGE
242 syslog(LOG_NOTICE, "[TDCn:%02u SND] send: %7lu, len: %4u, off: %4u",
243 cepid, scount, (uint16_t)slen, soff);
244#endif /* of #ifdef SHOW_RCV_RANGE */
245
246 syscall(dly_tsk(500 + netapp_rand() % SYSTIM_HZ));
247 }
248 }
249
250 cls_ret:
251 get_tim(&time);
252 syslog(LOG_NOTICE, "[TDCn:%02u SND] finsh: %7lu, ttl: %lu",
253 cepid, time / SYSTIM_HZ, scount, total);
254
255 if ((error = tcp_sht_cep(cepid)) < 0)
256 syslog(LOG_NOTICE, "[TDCn:%02u SND] shutdown error: %s", cepid, itron_strerror(error));
257
258 if ((error = tcp_cls_cep(cepid, TMO_NBLK)) != E_WBLK)
259 syslog(LOG_NOTICE, "[TDCn:%02u SND] close error: %s", cepid, itron_strerror(error));
260
261 /* 開放が完了するまで待
262つ。*/
263 syscall(wai_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
264 }
265
266 return E_OK;
267 }
268
269#else /* of #ifdef USE_TCP_NON_BLOCKING */
270
271/*
272 * send_tcp_discard -- DISCARD/TCP サーバにメッセージを送信する。
273 */
274
275static ER
276send_tcp_discard (ID cepid, T_IN_ADDR *ipaddr, uint16_t portno)
277{
278 static char smsg[SND_BUF_SIZE];
279 static T_IPEP src = {
280 IP_ADDRANY,
281 TCP_PORTANY,
282 };
283
284 T_IPEP dst;
285 ER_UINT slen;
286 ER error;
287 SYSTIM time;
288 uint16_t pat, soff, discard, rep, scount, total;
289 uint8_t *p;
290
291 dst.ipaddr = *ipaddr;
292 dst.portno = portno;
293
294 p = smsg;
295 for (rep = NUM_REP_PAT; rep -- > 0; )
296 for (pat = PAT_BEGIN; pat <= PAT_END; pat ++)
297 *p ++ = pat;
298
299 if ((error = TCP_CON_CEP(cepid, &src, &dst, 60 * 1000)) != E_OK) {
300 syslog(LOG_NOTICE, "[TDCn:%02u SND] connect error: %s", cepid, itron_strerror(error));
301 return error;
302 }
303
304 get_tim(&time);
305 syslog(LOG_NOTICE, "[TDCn:%02u SND] conct: %7lu, to: %s.%d",
306 cepid, time / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno);
307
308 scount = total = 0;
309 for (discard = NUM_DISCARD; discard -- > 0; ) {
310 soff = 0;
311 while (soff < SND_BUF_SIZE) {
312 if ((slen = tcp_snd_dat(cepid, smsg + soff, sizeof(smsg) - soff, TMO_FEVR)) < 0) {
313 syslog(LOG_NOTICE, "[TDCn:%02u SND] send error: %s", cepid, itron_strerror(slen));
314 goto cls_ret;
315 }
316 soff += (uint16_t)slen;
317 total += slen;
318 scount ++;
319
320#ifdef SHOW_RCV_RANGE
321 syslog(LOG_NOTICE, "[TDCn:%02u SND] send: %7lu, len: %4u, off: %4u",
322 cepid, scount, (uint16_t)slen, soff);
323#endif /* of #ifdef SHOW_RCV_RANGE */
324
325 syscall(dly_tsk(500 + netapp_rand() % SYSTIM_HZ));
326 }
327 }
328
329cls_ret:
330 get_tim(&time);
331 syslog(LOG_NOTICE, "[TDCn:%02u SND] finsh: %7lu, ttl: %lu",
332 cepid, time / SYSTIM_HZ, scount, total);
333
334 if ((error = tcp_sht_cep(cepid)) < 0)
335 syslog(LOG_NOTICE, "[TDCn:%02u SND] shutdown error: %s", cepid, itron_strerror(error));
336
337 if ((error = tcp_cls_cep(cepid, TMO_FEVR)) < 0)
338 syslog(LOG_NOTICE, "[TDCn:%02u SND] close error: %s", cepid, itron_strerror(error));
339
340 return E_OK;
341 }
342
343#endif /* of #ifdef USE_TCP_NON_BLOCKING */
344
345/*
346 * getcomd -- コマンドを得る。
347 */
348
349#ifdef TCP_DISCARD_CLI_AUTO_RUN_STR
350
351static char *
352getcomd (ID cepid, bool_t retry)
353{
354 ER error;
355 char *line = NULL;
356 static char auto_run_str[] = TCP_DISCARD_CLI_AUTO_RUN_STR;
357 static int_t count = 0;
358
359 if (retry || (count == 0)) {
360 line = auto_run_str;
361 dly_tsk(3 * 1000);
362 }
363 else {
364 while ((error = rcv_dtq(DTQ_TCP_DISCARD_CLI, (intptr_t*)&line)) != E_OK) {
365 syslog(LOG_NOTICE, "[TDCn:%02u TSK] error: %s", cepid, itron_strerror(error));
366 dly_tsk(SLP_ITV);
367 }
368 }
369
370 count ++;
371 return line;
372 }
373
374#else /* of #ifdef TCP_DISCARD_CLI_AUTO_RUN_STR */
375
376static char *
377getcomd (ID cepid, bool_t retry)
378{
379 ER error;
380 char *line = NULL;
381
382 while ((error = rcv_dtq(DTQ_TCP_DISCARD_CLI, (intptr_t*)&line)) != E_OK) {
383 syslog(LOG_NOTICE, "[TDCn:%02u TSK] error: %s", cepid, itron_strerror(error));
384 dly_tsk(SLP_ITV);
385 }
386 return line;
387 }
388
389#endif /* of #ifdef TCP_DISCARD_CLI_AUTO_RUN_STR */
390
391void
392tcp_discard_cli_task (intptr_t exinf)
393{
394 ID tskid, cepid;
395 T_IN_ADDR addr;
396 ER error;
397 uint8_t *line;
398 int_t rep, count;
399 bool_t retry = false;
400 uint16_t portno;
401 char apip;
402
403#ifdef USE_TCP_EXTENTIONS
404
405 T_TCP_CCEP ccep;
406
407#endif /* of #ifdef USE_TCP_EXTENTIONS */
408
409#ifdef TCP_ECHO_CLI_AUTO_RUN_STR
410 dly_tsk(START_DLY);
411#endif
412 get_tid(&tskid);
413
414#if defined(SUPPORT_INET6)
415 apip = API_PROTO_IPV6;
416#if defined(SUPPORT_INET4)
417 syslog(LOG_NOTICE, "[TCPn DISCARD CLI:%d,%d] started.", tskid, (ID)exinf);
418#else
419 syslog(LOG_NOTICE, "[TCP6 DISCARD CLI:%d,%d] started.", tskid, (ID)exinf);
420#endif
421#else
422 apip = API_PROTO_IPV4;
423 syslog(LOG_NOTICE, "[TCP4 DISCARD CLI:%d,%d] started.", tskid, (ID)exinf);
424#endif
425
426 while (true) {
427 line = skip_blanks(getcomd((ID)exinf, retry));
428
429 if ((line = lookup_ipaddr(&addr, line, apip)) == NULL) {
430 syslog(LOG_NOTICE, "[TDCn:%02u TSK] sleep %d.%03u[s], unknown host.",
431 (ID)exinf, SLP_ITV / SYSTIM_HZ, SLP_ITV % SYSTIM_HZ);
432 dly_tsk(SLP_ITV);
433 syslog(LOG_NOTICE, "[TDCn:%02u TSK] resume.", (ID)exinf);
434 retry = true;
435 continue;
436 }
437 else
438 retry = false;
439
440 line = skip_blanks(line);
441 if ('0' <= *line && *line <= '9') { /* Port No */
442 line = get_int(&rep, line);
443 portno = (uint16_t)rep;
444 }
445 else {
446 line ++;
447 portno = DISCARD_SRV_PORTNO;
448 }
449
450 line = skip_blanks(line);
451 if ('0' <= *line && *line <= '9') /* Repeat */
452 line = get_int(&rep, line);
453 else
454 rep = 1;
455
456#ifdef USE_TCP_EXTENTIONS
457
458 ccep.cepatr = 0;
459 ccep.sbufsz = TCP_DISCARD_CLI_SWBUF_SIZE;
460 ccep.rbufsz = 0;
461 ccep.rbuf = NADR;
462
463#ifdef TCP_CFG_SWBUF_CSAVE
464 ccep.sbuf = NADR;
465#else
466 ccep.sbuf = tcp_discard_cli_swbuf;
467#endif
468#ifdef USE_TCP_NON_BLOCKING
469 ccep.callback = (FP)callback_nblk_tcp_discard_cli;
470#else
471 ccep.callback = NULL;
472#endif
473
474 if ((error = ALLOC_TCP_CEP(&cepid, tskid, &ccep)) != E_OK) {
475 syslog(LOG_NOTICE, "[TDCn:%02u TSK] CEP create error: %s", cepid, itron_strerror(error));
476 continue;
477 }
478
479#else /* of #ifdef USE_TCP_EXTENTIONS */
480
481 cepid = (ID)exinf;
482
483#endif /* of #ifdef USE_TCP_EXTENTIONS */
484
485 tcp_discard_cli_valid = true;
486 count = 0;
487 while (rep == 0 || count < rep) {
488 if (!tcp_discard_cli_valid) {
489 syslog(LOG_NOTICE, "[TDCn:%02u TSK] canceled.", cepid);
490 break;
491 }
492
493 count ++;
494 if (rep == 0) {
495 syslog(LOG_NOTICE, "[TDCn:%02u TSK] start: repeat: %d", cepid, count);
496 error = send_tcp_discard(cepid, &addr, portno);
497 syslog(LOG_NOTICE, "[TDCn:%02u TSK] finsh: repeat: %d", cepid, count);
498 }
499 else {
500 syslog(LOG_NOTICE, "[TDCn:%02u TSK] start: repeat: %d/%d", cepid, count, rep);
501 error = send_tcp_discard(cepid, &addr, portno);
502 syslog(LOG_NOTICE, "[TDCn:%02u TSK] finsh: repeat: %d/%d", cepid, count, rep);
503 }
504
505 if (error != E_OK) {
506 syslog(LOG_NOTICE, "[TDCn:%02u TSK] sleep %d.%03u[s], error: %s",
507 cepid, SLP_ITV / SYSTIM_HZ, SLP_ITV % SYSTIM_HZ, itron_strerror(error));
508 tslp_tsk(SLP_ITV);
509 syslog(LOG_NOTICE, "[TDCn:%02u TSK] resume.", cepid);
510 }
511
512#if defined(SND_ITV)
513#if SND_ITV > 0
514 if (count < rep) {
515 uint_t itv;
516
517 itv = SND_ITV;
518 syslog(LOG_NOTICE, "[TDCn:%02u TSK] interval: %d[s].", cepid, itv / SYSTIM_HZ);
519 syscall(dly_tsk(SND_ITV));
520 }
521#endif
522#endif
523 }
524
525#ifdef USE_TCP_EXTENTIONS
526
527 if ((error = FREE_TCP_CEP(cepid)) != E_OK)
528 syslog(LOG_NOTICE, "[TDCn:%02u TSK] CEP delete error: %s", cepid, itron_strerror(error));
529
530#endif /* of #ifdef USE_TCP_EXTENTIONS */
531
532 }
533 }
534
535#endif /* of #ifdef USE_TCP_DISCARD_CLI */
Note: See TracBrowser for help on using the repository browser.