[270] | 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 DISCARD クライアント
|
---|
| 36 | *
|
---|
| 37 | * ・ノンブロッキングコール
|
---|
| 38 | * ・IPv4
|
---|
| 39 | * ・IPv6
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 |
|
---|
| 44 | #ifdef TARGET_KERNEL_ASP
|
---|
| 45 |
|
---|
| 46 | #include <kernel.h>
|
---|
| 47 | #include <sil.h>
|
---|
| 48 | #include <t_syslog.h>
|
---|
| 49 | #include "kernel_cfg.h"
|
---|
| 50 |
|
---|
| 51 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
| 52 |
|
---|
| 53 | #ifdef TARGET_KERNEL_JSP
|
---|
| 54 |
|
---|
| 55 | #include <t_services.h>
|
---|
| 56 | #include "kernel_id.h"
|
---|
| 57 |
|
---|
| 58 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
| 59 |
|
---|
| 60 | #include <tinet_defs.h>
|
---|
| 61 | #include <tinet_config.h>
|
---|
| 62 |
|
---|
| 63 | #include <net/if.h>
|
---|
| 64 | #include <net/if_ppp.h>
|
---|
| 65 | #include <net/if_loop.h>
|
---|
| 66 | #include <net/ethernet.h>
|
---|
| 67 | #include <net/net.h>
|
---|
| 68 | #include <net/net_var.h>
|
---|
| 69 | #include <net/net_timer.h>
|
---|
| 70 |
|
---|
| 71 | #include <netinet/in.h>
|
---|
| 72 | #include <netinet/in_itron.h>
|
---|
| 73 | #include <netinet/ip.h>
|
---|
| 74 | #include <netinet/ip6.h>
|
---|
| 75 | #include <netinet/tcp.h>
|
---|
| 76 |
|
---|
| 77 | #include <netapp/netapp.h>
|
---|
| 78 | #include <netapp/netapp_var.h>
|
---|
| 79 | #include <netapp/discard.h>
|
---|
| 80 |
|
---|
| 81 | #ifdef USE_TCP_DISCARD_CLI
|
---|
| 82 |
|
---|
| 83 | /* discard サーバのポート番号 */
|
---|
| 84 |
|
---|
| 85 | #define DISCARD_SRV_PORTNO UINT_C(9)
|
---|
| 86 |
|
---|
| 87 | /* 表示 */
|
---|
| 88 |
|
---|
| 89 | /*#define SHOW_RCV_RANGE*/
|
---|
| 90 |
|
---|
| 91 | /*
|
---|
| 92 | * バッファサイズの定義
|
---|
| 93 | */
|
---|
| 94 |
|
---|
| 95 | #define NUM_DISCARD 5
|
---|
| 96 | #define NUM_REP_PAT 40
|
---|
| 97 | #define PAT_BEGIN ' '
|
---|
| 98 | #define PAT_END '~'
|
---|
| 99 | #define SND_BUF_SIZE ((PAT_END - PAT_BEGIN + 1) * NUM_REP_PAT)
|
---|
| 100 |
|
---|
| 101 | /*
|
---|
| 102 | * 全域変数
|
---|
| 103 | */
|
---|
| 104 |
|
---|
| 105 | bool_t tcp_discard_cli_valid;
|
---|
| 106 |
|
---|
| 107 | /* TCP 送受信ウィンドバッファ */
|
---|
| 108 |
|
---|
| 109 | #ifndef TCP_CFG_SWBUF_CSAVE_ONLY
|
---|
| 110 | uint8_t tcp_discard_cli_swbuf[TCP_DISCARD_CLI_SWBUF_SIZE];
|
---|
| 111 | #endif
|
---|
| 112 |
|
---|
| 113 | #ifdef USE_TCP_NON_BLOCKING
|
---|
| 114 |
|
---|
| 115 | /*
|
---|
| 116 | * 変数
|
---|
| 117 | */
|
---|
| 118 |
|
---|
| 119 | static T_IPEP nblk_src = {
|
---|
| 120 | IP_ADDRANY,
|
---|
| 121 | TCP_PORTANY,
|
---|
| 122 | };
|
---|
| 123 | static T_IPEP nblk_dst;
|
---|
| 124 | static ER_UINT nblk_error;
|
---|
| 125 |
|
---|
| 126 | /*
|
---|
| 127 | * ノンブロッキングコールのコールバック関数
|
---|
| 128 | */
|
---|
| 129 |
|
---|
| 130 | ER
|
---|
| 131 | callback_nblk_tcp_discard_cli (ID cepid, FN fncd, void *p_parblk)
|
---|
| 132 | {
|
---|
| 133 | ER error = E_OK;
|
---|
| 134 |
|
---|
| 135 | nblk_error = *(ER*)p_parblk;
|
---|
| 136 |
|
---|
| 137 | switch (fncd) {
|
---|
| 138 |
|
---|
| 139 | case TFN_TCP_CON_CEP:
|
---|
| 140 | syscall(sig_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
|
---|
| 141 | break;
|
---|
| 142 |
|
---|
| 143 | case TFN_TCP_CLS_CEP:
|
---|
| 144 | if (nblk_error < 0)
|
---|
| 145 | syslog(LOG_NOTICE, "[TDC:%02d CBN] close error: %s", cepid, itron_strerror(nblk_error));
|
---|
| 146 | syscall(sig_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
|
---|
| 147 | break;
|
---|
| 148 |
|
---|
| 149 | case TFN_TCP_ACP_CEP:
|
---|
| 150 | case TFN_TCP_SND_DAT:
|
---|
| 151 | case TFN_TCP_GET_BUF:
|
---|
| 152 | case TFN_TCP_SND_OOB:
|
---|
| 153 | default:
|
---|
| 154 | error = E_PAR;
|
---|
| 155 | break;
|
---|
| 156 | }
|
---|
| 157 | return error;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /*
|
---|
| 161 | * send_tcp_discard -- DISCARD/TCP サーバにメッセージを送信する。
|
---|
| 162 | */
|
---|
| 163 |
|
---|
| 164 | static void
|
---|
| 165 | send_tcp_discard (ID cepid, T_IN_ADDR *ipaddr, uint16_t portno)
|
---|
| 166 | {
|
---|
| 167 | static char smsg[SND_BUF_SIZE];
|
---|
| 168 |
|
---|
| 169 | ER_UINT slen;
|
---|
| 170 | ER error;
|
---|
| 171 | SYSTIM time;
|
---|
| 172 | uint32_t total;
|
---|
| 173 | uint16_t soff, discard, rep, scount;
|
---|
| 174 | uint8_t pat, *p;
|
---|
| 175 |
|
---|
| 176 | nblk_dst.ipaddr = *ipaddr;
|
---|
| 177 | nblk_dst.portno = portno;
|
---|
| 178 |
|
---|
| 179 | p = smsg;
|
---|
| 180 | for (rep = NUM_REP_PAT; rep -- > 0; )
|
---|
| 181 | for (pat = PAT_BEGIN; pat <= PAT_END; pat ++)
|
---|
| 182 | *p ++ = pat;
|
---|
| 183 |
|
---|
| 184 | if ((error = TCP_CON_CEP(cepid, &nblk_src, &nblk_dst, TMO_NBLK)) != E_WBLK) {
|
---|
| 185 | syslog(LOG_NOTICE, "[TDC:%02d SND] connect error: %s", cepid, itron_strerror(error));
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /* 接続が完了するまで待つ。*/
|
---|
| 190 | syscall(wai_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
|
---|
| 191 |
|
---|
| 192 | if (nblk_error != E_OK)
|
---|
| 193 | syslog(LOG_NOTICE, "[TDC:%02d SND] connect error: %s", cepid, itron_strerror(nblk_error));
|
---|
| 194 | else {
|
---|
| 195 | get_tim(&time);
|
---|
| 196 | syslog(LOG_NOTICE, "[TDC:%02d SND] connecting: %6ld, to: %s.%d",
|
---|
| 197 | cepid, time / SYSTIM_HZ, IP2STR(NULL, ipaddr), nblk_dst.portno);
|
---|
| 198 |
|
---|
| 199 | scount = total = 0;
|
---|
| 200 | for (discard = NUM_DISCARD; discard -- > 0; ) {
|
---|
| 201 | soff = 0;
|
---|
| 202 | while (soff < SND_BUF_SIZE) {
|
---|
| 203 | if ((slen = tcp_snd_dat(cepid, smsg + soff, sizeof(smsg) - soff, TMO_FEVR)) < 0) {
|
---|
| 204 | syslog(LOG_NOTICE, "[TDC:%02d SND] send error: %s",
|
---|
| 205 | cepid, itron_strerror(slen));
|
---|
| 206 | goto cls_ret;
|
---|
| 207 | }
|
---|
| 208 | soff += (uint16_t)slen;
|
---|
| 209 | total += slen;
|
---|
| 210 | scount ++;
|
---|
| 211 |
|
---|
| 212 | #ifdef SHOW_RCV_RANGE
|
---|
| 213 | syslog(LOG_NOTICE, "[TDC:%02d SND] snd: %2d, len: %4d, off: %4d",
|
---|
| 214 | cepid, scount, (uint16_t)slen, soff);
|
---|
| 215 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
| 216 |
|
---|
| 217 | syscall(dly_tsk(500 + net_rand() % SYSTIM_HZ));
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | cls_ret:
|
---|
| 222 | get_tim(&time);
|
---|
| 223 | syslog(LOG_NOTICE, "[TDC:%02d SND] finished: %6ld, snd: %4d, len: %ld",
|
---|
| 224 | cepid, time / SYSTIM_HZ, scount, total);
|
---|
| 225 |
|
---|
| 226 | if ((error = tcp_sht_cep(cepid)) < 0)
|
---|
| 227 | syslog(LOG_NOTICE, "[TDC:%02d SND] shutdown error: %s", cepid, itron_strerror(error));
|
---|
| 228 |
|
---|
| 229 | if ((error = tcp_cls_cep(cepid, TMO_NBLK)) != E_WBLK)
|
---|
| 230 | syslog(LOG_NOTICE, "[TDC:%02d SND] close error: %s", cepid, itron_strerror(error));
|
---|
| 231 |
|
---|
| 232 | /* 開放が完了するまで待つ。*/
|
---|
| 233 | syscall(wai_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | #else /* of #ifdef USE_TCP_NON_BLOCKING */
|
---|
| 238 |
|
---|
| 239 | /*
|
---|
| 240 | * send_tcp_discard -- DISCARD/TCP サーバにメッセージを送信する。
|
---|
| 241 | */
|
---|
| 242 |
|
---|
| 243 | static void
|
---|
| 244 | send_tcp_discard (ID cepid, T_IN_ADDR *ipaddr, uint16_t portno)
|
---|
| 245 | {
|
---|
| 246 | static char smsg[SND_BUF_SIZE];
|
---|
| 247 | static T_IPEP src = {
|
---|
| 248 | IP_ADDRANY,
|
---|
| 249 | TCP_PORTANY,
|
---|
| 250 | };
|
---|
| 251 |
|
---|
| 252 | T_IPEP dst;
|
---|
| 253 | ER_UINT slen;
|
---|
| 254 | ER error;
|
---|
| 255 | SYSTIM time;
|
---|
| 256 | uint16_t pat, soff, discard, rep, scount, total;
|
---|
| 257 | uint8_t *p;
|
---|
| 258 |
|
---|
| 259 | dst.ipaddr = *ipaddr;
|
---|
| 260 | dst.portno = portno;
|
---|
| 261 |
|
---|
| 262 | p = smsg;
|
---|
| 263 | for (rep = NUM_REP_PAT; rep -- > 0; )
|
---|
| 264 | for (pat = PAT_BEGIN; pat <= PAT_END; pat ++)
|
---|
| 265 | *p ++ = pat;
|
---|
| 266 |
|
---|
| 267 | if ((error = TCP_CON_CEP(cepid, &src, &dst, 60 * 1000)) != E_OK) {
|
---|
| 268 | syslog(LOG_NOTICE, "[TDC:%02d SND] connect error: %s", cepid, itron_strerror(error));
|
---|
| 269 | return;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | get_tim(&time);
|
---|
| 273 | syslog(LOG_NOTICE, "[TDC:%02d SND] connecting: %6ld, to: %s.%d",
|
---|
| 274 | cepid, time / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno);
|
---|
| 275 |
|
---|
| 276 | scount = total = 0;
|
---|
| 277 | for (discard = NUM_DISCARD; discard -- > 0; ) {
|
---|
| 278 | soff = 0;
|
---|
| 279 | while (soff < SND_BUF_SIZE) {
|
---|
| 280 | if ((slen = tcp_snd_dat(cepid, smsg + soff, sizeof(smsg) - soff, TMO_FEVR)) < 0) {
|
---|
| 281 | syslog(LOG_NOTICE, "[TDC:%02d SND] send error: %s", cepid, itron_strerror(slen));
|
---|
| 282 | goto cls_ret;
|
---|
| 283 | }
|
---|
| 284 | soff += (uint16_t)slen;
|
---|
| 285 | total += slen;
|
---|
| 286 | scount ++;
|
---|
| 287 |
|
---|
| 288 | #ifdef SHOW_RCV_RANGE
|
---|
| 289 | syslog(LOG_NOTICE, "[TDC:%02d SND] snd: %3d, len: %4d, off: %4d",
|
---|
| 290 | cepid, scount, (uint16_t)slen, soff);
|
---|
| 291 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
| 292 |
|
---|
| 293 | syscall(dly_tsk(500 + net_rand() % SYSTIM_HZ));
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | cls_ret:
|
---|
| 298 | get_tim(&time);
|
---|
| 299 | syslog(LOG_NOTICE, "[TDC:%02d SND] finished: %6ld, snd: %4d, len: %ld",
|
---|
| 300 | cepid, time / SYSTIM_HZ, scount, total);
|
---|
| 301 |
|
---|
| 302 | if ((error = tcp_sht_cep(cepid)) < 0)
|
---|
| 303 | syslog(LOG_NOTICE, "[TDC:%02d SND] shutdown error: %s", cepid, itron_strerror(error));
|
---|
| 304 |
|
---|
| 305 | if ((error = tcp_cls_cep(cepid, TMO_FEVR)) < 0)
|
---|
| 306 | syslog(LOG_NOTICE, "[TDC:%02d SND] close error: %s", cepid, itron_strerror(error));
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | #endif /* of #ifdef USE_TCP_NON_BLOCKING */
|
---|
| 310 |
|
---|
| 311 | void
|
---|
| 312 | tcp_discard_cli_task (intptr_t exinf)
|
---|
| 313 | {
|
---|
| 314 | T_IN_ADDR addr;
|
---|
| 315 | ID tskid, cepid;
|
---|
| 316 | int_t rep;
|
---|
| 317 | uint32_t count;
|
---|
| 318 | uint16_t portno;
|
---|
| 319 | uint8_t *line;
|
---|
| 320 |
|
---|
| 321 | #ifdef USE_TCP_EXTENTIONS
|
---|
| 322 |
|
---|
| 323 | ER error;
|
---|
| 324 | T_TCP_CCEP ccep;
|
---|
| 325 |
|
---|
| 326 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
| 327 |
|
---|
| 328 | get_tid(&tskid);
|
---|
| 329 | syslog(LOG_NOTICE, "[TCP DISCARD CLI:%d,%d] started.", tskid, (ID)exinf);
|
---|
| 330 | while (true) {
|
---|
| 331 | if (rcv_dtq(DTQ_TCP_DISCARD_CLI, (intptr_t*)&line) == E_OK) {
|
---|
| 332 | line = skip_blanks(GET_IPADDR(&addr, skip_blanks(line))); /* IP Address */
|
---|
| 333 |
|
---|
| 334 | if ('0' <= *line && *line <= '9') { /* Port No */
|
---|
| 335 | line = get_int(&rep, line);
|
---|
| 336 | portno = (uint16_t)rep;
|
---|
| 337 | }
|
---|
| 338 | else {
|
---|
| 339 | line ++;
|
---|
| 340 | portno = DISCARD_SRV_PORTNO;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | line = skip_blanks(line);
|
---|
| 344 | if ('0' <= *line && *line <= '9') /* Repeat */
|
---|
| 345 | line = get_int(&rep, line);
|
---|
| 346 | else
|
---|
| 347 | rep = 1;
|
---|
| 348 |
|
---|
| 349 | #ifdef USE_TCP_EXTENTIONS
|
---|
| 350 |
|
---|
| 351 | ccep.cepatr = 0;
|
---|
| 352 | ccep.sbufsz = TCP_DISCARD_CLI_SWBUF_SIZE;
|
---|
| 353 | ccep.rbufsz = 0;
|
---|
| 354 | ccep.rbuf = NADR;
|
---|
| 355 |
|
---|
| 356 | #ifdef TCP_CFG_SWBUF_CSAVE
|
---|
| 357 | ccep.sbuf = NADR;
|
---|
| 358 | #else
|
---|
| 359 | ccep.sbuf = tcp_discard_cli_swbuf;
|
---|
| 360 | #endif
|
---|
| 361 | #ifdef USE_TCP_NON_BLOCKING
|
---|
| 362 | ccep.callback = (FP)callback_nblk_tcp_discard_cli;
|
---|
| 363 | #else
|
---|
| 364 | ccep.callback = NULL;
|
---|
| 365 | #endif
|
---|
| 366 |
|
---|
| 367 | if ((error = alloc_tcp_cep(&cepid, tskid, &ccep)) != E_OK) {
|
---|
| 368 | syslog(LOG_NOTICE, "[TDC:%02d TSK] CEP create error: %s", cepid, itron_strerror(error));
|
---|
| 369 | continue;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | #else /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
| 373 |
|
---|
| 374 | cepid = (ID)exinf;
|
---|
| 375 |
|
---|
| 376 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
| 377 |
|
---|
| 378 | tcp_discard_cli_valid = true;
|
---|
| 379 | for (count = rep; count -- > 0; ) {
|
---|
| 380 | if (!tcp_discard_cli_valid) {
|
---|
| 381 | syslog(LOG_NOTICE, "[TDC:%02d TSK] canceled.", (ID)exinf);
|
---|
| 382 | break;
|
---|
| 383 | }
|
---|
| 384 | syslog(LOG_NOTICE, "[TDC:%02d TSK] start: repeat: %d/%d",
|
---|
| 385 | (ID)exinf, rep - count, rep);
|
---|
| 386 | send_tcp_discard(cepid, &addr, portno);
|
---|
| 387 | syslog(LOG_NOTICE, "[TDC:%02d TSK] finished: repeat: %d/%d",
|
---|
| 388 | (ID)exinf, rep - count, rep);
|
---|
| 389 | syscall(dly_tsk(500 + net_rand() % SYSTIM_HZ));
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | #ifdef USE_TCP_EXTENTIONS
|
---|
| 393 |
|
---|
| 394 | if ((error = free_tcp_cep(cepid)) != E_OK)
|
---|
| 395 | syslog(LOG_NOTICE, "[TDC:%02d TSK] CEP delete error: %s", cepid, itron_strerror(error));
|
---|
| 396 |
|
---|
| 397 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
| 398 |
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | #endif /* of #ifdef USE_TCP_DISCARD_CLI */
|
---|