source: asp3_tinet_ecnl_rx/trunk/asp3_dcre/tinet/netapp/udp4_echo_cli.c@ 337

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

ASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 13.1 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 * 上記著作権者は,以下の (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 * UDP4 ECHO クライアント
36 */
37
38#include <string.h>
39
40#if defined(TARGET_KERNEL_ASP)
41
42#include <kernel.h>
43#include <sil.h>
44#include <t_syslog.h>
45#include "kernel_cfg.h"
46
47#endif /* of #if defined(TARGET_KERNEL_ASP) */
48
49#if defined(TARGET_KERNEL_JSP)
50
51#include <t_services.h>
52#include "kernel_id.h"
53#include "tinet_id.h"
54
55#endif /* of #if defined(TARGET_KERNEL_JSP) */
56
57#include <netinet/in.h>
58#include <netinet/in_itron.h>
59
60#include <netapp/netapp.h>
61#include <netapp/netapp_var.h>
62#include <netapp/udp4_echo_cli.h>
63
64#if defined(USE_UDP4_ECHO_CLI_TSK)
65
66/* echo サーバのポート番号 */
67
68#define ECHO_SRV_PORTNO UINT_C(7)
69
70/* 送信間隔 */
71
72#if defined(TOPPERS_S810_CLG3_85)
73
74#define SLP_ITV (30*SYSTIM_HZ)
75
76#else /* of #if defined(TOPPERS_S810_CLG3_85) */
77
78#define SLP_ITV (60*SYSTIM_HZ)
79
80#endif /* of #if defined(TOPPERS_S810_CLG3_85) */
81
82/* 自動実行 */
83
84#if defined(UDP4_ECHO_CLI_AUTO_RUN_STR)
85#else
86#if 0
87#define UDP4_ECHO_CLI_AUTO_RUN_STR "172.25.193.140 - 1000"
88#endif
89#endif
90
91/*
92 * 全域変数
93 */
94
95bool_t udp4_echo_cli_valid;
96
97/*
98 * 変数
99 */
100
101#define BUF_SIZE 2048
102
103static char udp_buf[BUF_SIZE];
104static T_IPV4EP dst;
105
106#if defined(USE_UDP_NON_BLOCKING)
107
108static ER_UINT nblk_len;
109static ID nblk_tskid;
110
111/*
112 * ノンブロッキングコールのコールバック関数
113 */
114
115ER
116callback_nblk_udp4_echo_cli (ID cepid, FN fncd, void *p_parblk)
117{
118 nblk_len = *(ER_UINT*)p_parblk;
119 syscall(wup_tsk(nblk_tskid));
120 return E_OK;
121 }
122
123/*
124 * udp4_echo_cli -- ECHO/UDP サーバにメッセージを送信する (ノンブロッキングコール使用)。
125 */
126
127ER
128udp4_echo_cli (ID cepid, T_IN4_ADDR *ipaddr, uint16_t portno, char *line)
129{
130 ER_UINT error;
131 SYSTIM time;
132 uint_t len;
133
134 dst.ipaddr = *ipaddr;
135 dst.portno = portno;
136 len = strlen(line);
137 get_tim(&time);
138 get_tid(&nblk_tskid);
139 if ((error = udp_snd_dat(cepid, &dst, line, len, TMO_NBLK)) != E_WBLK) {
140 syslog(LOG_NOTICE, "[UEC4:%02u SND(NBLK)] error: %7lu,%s",
141 cepid, time / SYSTIM_HZ, itron_strerror(nblk_len));
142 return error;
143 }
144 else
145 syslog(LOG_NOTICE, "[UEC4:%02u SND(NBLK)] send: %7lu, to: %s.%d\n"
146 " msg: %s",
147 cepid, time / SYSTIM_HZ, ip2str(NULL, &dst.ipaddr), dst.portno, line);
148
149 syscall(slp_tsk());
150 get_tim(&time);
151 if (nblk_len < 0) { /* 0 以下の場合は、エラーコード */
152 syslog(LOG_NOTICE, "[UEC4:%02u CBR(NBLK)] error: %7lu,%s",
153 cepid, time / SYSTIM_HZ, itron_strerror(nblk_len));
154 return nblk_len;
155 }
156
157 if ((error = udp_rcv_dat(cepid, &dst, udp_buf, sizeof(udp_buf), TMO_NBLK)) != E_WBLK) {
158 syslog(LOG_NOTICE, "[UEC4:%02u RCV(NBLK)] error: %7lu,%s",
159 cepid, time / SYSTIM_HZ, itron_strerror(nblk_len));
160 return error;
161 }
162
163 syscall(slp_tsk());
164 get_tim(&time);
165 if (nblk_len < 0) { /* 0 以下の場合は、エラーコード */
166 syslog(LOG_NOTICE, "[UEC4:%02u RCV(NBLK)] error: %7lu,%s",
167 cepid, time / SYSTIM_HZ, itron_strerror(nblk_len));
168 return nblk_len;
169 }
170 else {
171 udp_buf[nblk_len] = '\0';
172 get_tim(&time);
173 syslog(LOG_NOTICE, "[UEC4:%02u RCV(NBLK)] recv: %7lu,from: %s.%d\n"
174 " msg: %s",
175 cepid, time / SYSTIM_HZ, ip2str(NULL, &dst.ipaddr), dst.portno, udp_buf);
176 nblk_len = E_OK;
177 }
178
179 return nblk_len;
180 }
181
182#else /* of #if defined(USE_UDP_NON_BLOCKING) */
183
184#if defined(USE_UDP_CALL_BACK)
185
186/*
187 * コールバック関数
188 */
189
190ER
191callback_udp4_echo_cli (ID cepid, FN fncd, void *p_parblk)
192{
193 SYSTIM time;
194 ER_UINT len;
195
196 get_tim(&time);
197 if ((len = udp_rcv_dat(cepid, &dst, udp_buf, sizeof(udp_buf), TMO_FEVR)) >= 0) {
198 *(udp_buf + len) = '\0';
199 syslog(LOG_NOTICE, "[UEC4:%02u RCV(CB)] recv: %7lu,from: %s.%d\n"
200 " msg: %s",
201 cepid, time / SYSTIM_HZ, ip2str(NULL, &dst.ipaddr), dst.portno, udp_buf);
202 }
203 else
204 syslog(LOG_NOTICE, "[UEC4:%02u RCV(CB)] error: %7lu,%s",
205 cepid, time / SYSTIM_HZ, itron_strerror(len));
206
207 return E_OK;
208 }
209
210/*
211 * udp4_echo_cli -- ECHO/UDP サーバにメッセージを送信する (コールバック関数使用)。
212 */
213
214ER
215udp4_echo_cli (ID cepid, T_IN4_ADDR *ipaddr, uint16_t portno, char *line)
216{
217 SYSTIM time;
218 ER_UINT len;
219
220 dst.ipaddr = *ipaddr;
221 dst.portno = portno;
222 len = strlen(line);
223 get_tim(&time);
224 syslog(LOG_NOTICE, "[UEC4:%02u SND(CB)] send: %7lu, to: %s.%d\n"
225 " msg: %s",
226 cepid, time / SYSTIM_HZ, ip2str(NULL, &dst.ipaddr), dst.portno, line);
227 if ((len = udp_snd_dat(cepid, &dst, line, len, TMO_FEVR)) < 0)
228 syslog(LOG_NOTICE, "[UEC4:%02u SND(CB)] error: %7lu,%s",
229 cepid, time / SYSTIM_HZ, itron_strerror(len));
230
231 return E_OK;
232 }
233
234#else /* of #if defined(USE_UDP_CALL_BACK) */
235
236/*
237 * udp4_echo_cli -- ECHO/UDP サーバにメッセージを送信する (コールバック関数未使用)。
238 */
239
240ER
241udp4_echo_cli (ID cepid, T_IN4_ADDR *ipaddr, uint16_t portno, char *line)
242{
243 SYSTIM time;
244 ER_UINT len;
245
246 dst.ipaddr = *ipaddr;
247 dst.portno = portno;
248 len = strlen(line);
249 get_tim(&time);
250 syslog(LOG_NOTICE, "[UEC4:%02u SND] send: %7lu, to: %s.%d\n"
251 " msg: %s",
252 cepid, time / SYSTIM_HZ, ip2str(NULL, &dst.ipaddr), dst.portno, line);
253 if ((len = udp_snd_dat(cepid, &dst, line, len, TMO_FEVR)) < 0) {
254 syslog(LOG_NOTICE, "[UEC4:%02u SND] error: %7lu,%s",
255 cepid, time / SYSTIM_HZ, itron_strerror(len));
256 return len;
257 }
258
259 if ((len = udp_rcv_dat(cepid, &dst, udp_buf, sizeof(udp_buf), 10*1000)) >= 0) {
260 *(udp_buf + len) = '\0';
261 get_tim(&time);
262 syslog(LOG_NOTICE, "[UEC4:%02u RCV] recv: %7lu,from: %s.%d\n"
263 " msg: %s",
264 cepid, time / SYSTIM_HZ, ip2str(NULL, &dst.ipaddr), dst.portno, udp_buf);
265 return E_OK;
266 }
267 else {
268 get_tim(&time);
269 syslog(LOG_NOTICE, "[UEC4:%02u RCV] error: %7lu,%s",
270 cepid, time / SYSTIM_HZ, itron_strerror(len));
271 return len;
272 }
273 }
274
275#endif /* of #if defined(USE_UDP_CALL_BACK) */
276
277#endif /* of #if defined(USE_UDP_NON_BLOCKING) */
278
279/*
280 * getcomd -- コマンドを得る。
281 */
282
283#if defined(UDP4_ECHO_CLI_AUTO_RUN_STR)
284
285static char *
286getcomd (ID cepid, bool_t retry)
287{
288 ER error;
289 char *line = NULL;
290 static char auto_run_str[] = UDP4_ECHO_CLI_AUTO_RUN_STR;
291 static int_t count = 0;
292
293 if (retry || count == 0) {
294 line = auto_run_str;
295 dly_tsk(6 * 1000);
296 }
297 else {
298 while ((error = rcv_dtq(DTQ_UDP4_ECHO_CLI, (intptr_t*)&line)) != E_OK) {
299 syslog(LOG_NOTICE, "[UEC4:%02u TSK] error:%s", cepid, itron_strerror(error));
300 dly_tsk(SLP_ITV);
301 }
302 }
303
304 count ++;
305 return line;
306 }
307
308#else /* of #if defined(UDP4_ECHO_CLI_AUTO_RUN_STR) */
309
310static char *
311getcomd (ID cepid, bool_t retry)
312{
313 ER error;
314 char *line = NULL;
315
316 while ((error = rcv_dtq(DTQ_UDP4_ECHO_CLI, (intptr_t*)&line)) != E_OK) {
317 syslog(LOG_NOTICE, "[UEC4:%02u TSK] error:%s", cepid, itron_strerror(error));
318 dly_tsk(SLP_ITV);
319 }
320 return line;
321 }
322
323#endif /* of #if defined(UDP4_ECHO_CLI_AUTO_RUN_STR) */
324
325#define MESSAGE_FORMAT "MSG 1000000"
326
327/*
328 * UDP4 ECHO クライアント送信タスク
329 */
330
331void
332udp4_echo_cli_task (intptr_t exinf)
333{
334 static char msg[sizeof(MESSAGE_FORMAT)] = MESSAGE_FORMAT;
335
336 ID tskid, cepid;
337 ER error = E_OK;
338 SYSTIM time;
339 T_IN4_ADDR addr;
340
341#if defined(SUPPORT_INET6) && defined(SUPPORT_INET4)
342 T_IN6_ADDR addr6;
343#endif
344
345 char *line, *p;
346 int_t rep;
347 uint32_t count, msgno;
348 bool_t retry = false;
349 uint16_t portno = ECHO_SRV_PORTNO;
350
351#if defined(USE_UDP_EXTENTIONS)
352 T_UDP_CCEP ccep;
353#endif
354
355 dly_tsk(10*1000);
356 get_tid(&tskid);
357 syslog(LOG_NOTICE, "[UDP4 ECHO CLI:%d,%d] started.", tskid, (ID)exinf);
358 while (true) {
359 line = skip_blanks(getcomd((ID)exinf, retry));
360
361#if defined(SUPPORT_INET6) && defined(SUPPORT_INET4)
362
363 line = lookup_ipaddr(&addr6, line, API_PROTO_IPV4);
364 if (line == NULL || !in6_is_addr_ipv4mapped(&addr6)) {
365 syslog(LOG_NOTICE, "[UEC4:%02u TSK] sleep %d.%03u[s], unknown host.",
366 (ID)exinf, SLP_ITV / SYSTIM_HZ, SLP_ITV % SYSTIM_HZ);
367 dly_tsk(SLP_ITV);
368 syslog(LOG_NOTICE, "[UEC4:%02u TSK] resume.", (ID)exinf);
369 retry = true;
370 continue;
371 }
372 addr = ntohl(addr6.s6_addr32[3]);
373
374#else /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
375
376 if ((line = lookup_ipaddr(&addr, line, DEFAULT_API_PROTO)) == NULL) {
377 syslog(LOG_NOTICE, "[UEC4:%02u TSK] sleep %d.%03u[s], unknown host.",
378 (ID)exinf, SLP_ITV / SYSTIM_HZ, SLP_ITV % SYSTIM_HZ);
379 dly_tsk(SLP_ITV);
380 syslog(LOG_NOTICE, "[UEC4:%02u TSK] resume.", (ID)exinf);
381 retry = true;
382 continue;
383 }
384
385#endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
386
387 line = skip_blanks(line);
388 if ('0' <= *line && *line <= '9') { /* Port No */
389 line = get_int(&rep, line);
390 portno = (uint16_t)rep;
391 }
392 else if (*line == '-')
393 line ++;
394
395#if defined(USE_UDP_EXTENTIONS)
396
397 ccep.cepatr = UINT_C(0);
398 ccep.myaddr.portno = UDP_PORTANY;
399
400#if defined(SUPPORT_INET6)
401
402 memcpy(&ccep.myaddr.ipaddr, &ipv6_addrany, sizeof(T_IN6_ADDR));
403
404#else /* #if defined(SUPPORT_INET6) */
405
406#if defined(SUPPORT_INET4)
407 ccep.myaddr.ipaddr = IPV4_ADDRANY;
408#endif
409
410#endif /* #if defined(SUPPORT_INET6) */
411
412#if defined(USE_UDP_NON_BLOCKING)
413 ccep.callback = (FP)callback_nblk_udp4_echo_cli;
414#else
415 ccep.callback = NULL;
416#endif
417
418 if ((error = alloc_udp4_cep(&cepid, tskid, &ccep)) != E_OK) {
419 syslog(LOG_NOTICE, "[UEC4:%02u TSK] CEP create error:%s", cepid, itron_strerror(error));
420 continue;
421 }
422
423#else/* of #if defined(USE_UDP_EXTENTIONS) */
424
425 cepid = (ID)exinf;
426
427#endif/* of #if defined(USE_UDP_EXTENTIONS) */
428
429 line = skip_blanks(line);
430 if ('0' <= *line && *line <= '9') { /* Repeat */
431 line = get_int(&rep, line);
432#if 0
433 if (rep > 1000000)
434 rep = 1000000;
435#endif
436 udp4_echo_cli_valid = true;
437 count = 0;
438 while (rep == 0 || count < rep) {
439 if (!udp4_echo_cli_valid) {
440 syslog(LOG_NOTICE, "[UEC4:%02u TSK] canceled.", cepid);
441 break;
442 }
443
444 count ++;
445 p = &msg[sizeof(MESSAGE_FORMAT)] - 1;
446 for (msgno = count; msgno > 0; msgno /= 10)
447 *(-- p) = msgno % 10 + '0';
448 while (*(p - 2) != 'G')
449 *(-- p) = ' ';
450
451 if ((error = udp4_echo_cli(cepid, &addr, portno, msg)) != E_OK)
452 dly_tsk(30 * SYSTIM_HZ);
453
454 dly_tsk(25 * SYSTIM_HZ + netapp_rand() % (10 * SYSTIM_HZ));
455 }
456 }
457
458 else /* Single Message */
459 error = udp4_echo_cli(cepid, &addr, portno, line);
460
461 if (error != E_OK) {
462 get_tim(&time);
463 syslog(LOG_NOTICE, "[UEC4:%02u SND] error: %7lu,%s",
464 cepid, time / SYSTIM_HZ, itron_strerror(error));
465 }
466
467#if defined(USE_UDP_EXTENTIONS)
468
469 if ((error = free_udp4_cep(cepid, !(error == E_NOEXS || error == E_DLT))) != E_OK)
470 syslog(LOG_NOTICE, "[UEC4:%02u TSK] CEP delete error:%s", cepid, itron_strerror(error));
471
472#endif /* of #if defined(USE_UDP_EXTENTIONS) */
473
474 }
475 }
476
477#endif /* of #if defined(USE_UDP4_ECHO_CLI_TSK) */
Note: See TracBrowser for help on using the repository browser.