[352] | 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 | * DISCARD サーバ
|
---|
| 36 | *
|
---|
| 37 | * ・省コピー API
|
---|
| 38 | * ・IPv4
|
---|
| 39 | * ・IPv6
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | #include <string.h>
|
---|
| 43 |
|
---|
| 44 | #ifdef TARGET_KERNEL_ASP
|
---|
| 45 |
|
---|
| 46 | #include <kernel.h>
|
---|
| 47 | #include <sil.h>
|
---|
| 48 | #include <t_syslog.h>
|
---|
| 49 | #include "tinet_cfg.h"
|
---|
| 50 |
|
---|
| 51 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
| 52 |
|
---|
| 53 | #ifdef TARGET_KERNEL_JSP
|
---|
| 54 |
|
---|
| 55 | #include <t_services.h>
|
---|
| 56 | #include "tinet_id.h"
|
---|
| 57 |
|
---|
| 58 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
| 59 |
|
---|
| 60 | #include <netinet/in.h>
|
---|
| 61 | #include <netinet/in_itron.h>
|
---|
| 62 |
|
---|
| 63 | #include <netapp/netapp.h>
|
---|
| 64 | #include <netapp/netapp_var.h>
|
---|
| 65 | #include <netapp/tcp_discard_srv.h>
|
---|
| 66 |
|
---|
| 67 | #ifdef USE_TCP_DISCARD_SRV
|
---|
| 68 |
|
---|
| 69 | /*
|
---|
| 70 | * 表示
|
---|
| 71 | */
|
---|
| 72 |
|
---|
| 73 | //#define SHOW_RCV_RANGE
|
---|
| 74 | //#define SHOW_RCV_DATA
|
---|
| 75 |
|
---|
| 76 | /*
|
---|
| 77 | * 全域変数
|
---|
| 78 | */
|
---|
| 79 |
|
---|
| 80 | /* TCP 送受信ウィンドバッファ */
|
---|
| 81 |
|
---|
| 82 | #ifndef TCP_CFG_RWBUF_CSAVE_ONLY
|
---|
| 83 | uint8_t tcp_discard_srv_rwbuf[TCP_DISCARD_SRV_RWBUF_SIZE];
|
---|
| 84 | #endif
|
---|
| 85 |
|
---|
| 86 | /*
|
---|
| 87 | * TCP DISCARD サーバタスク
|
---|
| 88 | */
|
---|
| 89 |
|
---|
| 90 | #define BUF_SIZE TCP_DISCARD_SRV_RWBUF_SIZE
|
---|
| 91 |
|
---|
| 92 | #ifdef USE_COPYSAVE_API
|
---|
| 93 |
|
---|
| 94 | static ER
|
---|
| 95 | tcp_discard_srv (ID cepid, ID repid)
|
---|
| 96 | {
|
---|
| 97 | T_IPEP dst;
|
---|
| 98 | ER_UINT rlen;
|
---|
| 99 | ER error;
|
---|
| 100 | SYSTIM time;
|
---|
| 101 | uint32_t total;
|
---|
| 102 | uint16_t count;
|
---|
| 103 | uint8_t *buf;
|
---|
| 104 |
|
---|
| 105 | #ifdef SHOW_RCV_DATA
|
---|
| 106 | uint_t ix;
|
---|
| 107 | #endif /* of #ifdef SHOW_RCV_DATA */
|
---|
| 108 |
|
---|
| 109 | if ((error = TCP_ACP_CEP(cepid, repid, &dst, TMO_FEVR)) != E_OK) {
|
---|
| 110 | syslog(LOG_NOTICE, "[TDSn:%02u ACP] accept error: %s", cepid, itron_strerror(error));
|
---|
| 111 | return error;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | #ifdef USE_TCP_EXTENTIONS
|
---|
| 115 | if ((error = FREE_TCP_REP(repid, true)) != E_OK) {
|
---|
| 116 | syslog(LOG_NOTICE, "[TDSn:%02u DEL] REP delete error: %s", cepid, itron_strerror(error));
|
---|
| 117 | return error;
|
---|
| 118 | }
|
---|
| 119 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
| 120 |
|
---|
| 121 | count = total = 0;
|
---|
| 122 | get_tim(&time);
|
---|
| 123 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] conct: %7lu, from: %s.%d",
|
---|
| 124 | cepid, time / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno);
|
---|
| 125 | while ((rlen = tcp_rcv_buf(cepid, (void*)&buf, TMO_FEVR)) > 0) {
|
---|
| 126 | count ++;
|
---|
| 127 |
|
---|
| 128 | #ifdef SHOW_RCV_RANGE
|
---|
| 129 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] count: %6lu, len: %4u, data: %02x -> %02x",
|
---|
| 130 | cepid, count, (uint16_t)rlen, *buf, *(buf + rlen - 1));
|
---|
| 131 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
| 132 |
|
---|
| 133 | #ifdef SHOW_RCV_DATA
|
---|
| 134 | for (ix = 0; ix < rlen; ix ++)
|
---|
| 135 | cons_putchar(CONSOLE_PORTID, *(buf + ix));
|
---|
| 136 | #endif /* of #ifdef SHOW_RCV_DATA */
|
---|
| 137 |
|
---|
| 138 | if ((error = tcp_rel_buf(cepid, rlen)) != E_OK) {
|
---|
| 139 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] rel buf error: %s",
|
---|
| 140 | cepid, itron_strerror(error));
|
---|
| 141 | rlen = 0;
|
---|
| 142 | break;
|
---|
| 143 | }
|
---|
| 144 | total += rlen;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | if (rlen != 0)
|
---|
| 148 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] recv buf error: %s", cepid, itron_strerror(rlen));
|
---|
| 149 |
|
---|
| 150 | if ((error = tcp_sht_cep(cepid)) != E_OK)
|
---|
| 151 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] shutdown error: %s", cepid, itron_strerror(error));
|
---|
| 152 |
|
---|
| 153 | if ((error = tcp_cls_cep(cepid, TMO_FEVR)) != E_OK)
|
---|
| 154 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] close error: %s", cepid, itron_strerror(error));
|
---|
| 155 |
|
---|
| 156 | get_tim(&time);
|
---|
| 157 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] finsh: %7lu, ttl: %lu",
|
---|
| 158 | cepid, time / SYSTIM_HZ, count, total);
|
---|
| 159 |
|
---|
| 160 | return error;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | #else /* of #ifdef USE_COPYSAVE_API */
|
---|
| 164 |
|
---|
| 165 | static ER
|
---|
| 166 | tcp_discard_srv (ID cepid, ID repid)
|
---|
| 167 | {
|
---|
| 168 | static char buffer[BUF_SIZE];
|
---|
| 169 |
|
---|
| 170 | T_IPEP dst;
|
---|
| 171 | ER_UINT rlen;
|
---|
| 172 | ER error;
|
---|
| 173 | SYSTIM time;
|
---|
| 174 | uint32_t total;
|
---|
| 175 | uint16_t count;
|
---|
| 176 | uint8_t *buf = buffer;
|
---|
| 177 |
|
---|
| 178 | if ((error = TCP_ACP_CEP(cepid, repid, &dst, TMO_FEVR)) != E_OK) {
|
---|
| 179 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] accept error: %s", cepid, itron_strerror(error));
|
---|
| 180 | return error;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | #ifdef USE_TCP_EXTENTIONS
|
---|
| 184 | if ((error = FREE_TCP_REP(repid, true)) != E_OK) {
|
---|
| 185 | syslog(LOG_NOTICE, "[TDSn:%02u DEL] REP delete error: %s", cepid, itron_strerror(error));
|
---|
| 186 | return error;
|
---|
| 187 | }
|
---|
| 188 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
| 189 |
|
---|
| 190 | count = total = 0;
|
---|
| 191 | get_tim(&time);
|
---|
| 192 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] conct: %7lu, from: %s.%d",
|
---|
| 193 | cepid, time / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno);
|
---|
| 194 | while ((rlen = tcp_rcv_dat(cepid, buf, BUF_SIZE - 1, TMO_FEVR)) > 0) {
|
---|
| 195 | count ++;
|
---|
| 196 |
|
---|
| 197 | #ifdef SHOW_RCV_RANGE
|
---|
| 198 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] count: %6lu, len: %4u, data: %02x -> %02x",
|
---|
| 199 | cepid, count, (uint16_t)rlen, *buf, *(buf + rlen - 1));
|
---|
| 200 | #endif /* of #ifdef SHOW_RCV_RANGE */
|
---|
| 201 |
|
---|
| 202 | total += rlen;
|
---|
| 203 | }
|
---|
| 204 | if (rlen != 0)
|
---|
| 205 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] recv error: %s", cepid, itron_strerror(rlen));
|
---|
| 206 |
|
---|
| 207 | if ((error = tcp_sht_cep(cepid)) != E_OK)
|
---|
| 208 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] shutdown error: %s", cepid, itron_strerror(error));
|
---|
| 209 |
|
---|
| 210 | if ((error = tcp_cls_cep(cepid, TMO_FEVR)) != E_OK)
|
---|
| 211 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] close error: %s", cepid, itron_strerror(error));
|
---|
| 212 |
|
---|
| 213 | get_tim(&time);
|
---|
| 214 | syslog(LOG_NOTICE, "[TDSn:%02u RCV] finsh: %7lu, ttl: %lu",
|
---|
| 215 | cepid, time / SYSTIM_HZ, count, total);
|
---|
| 216 |
|
---|
| 217 | return error;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | #endif /* of #ifdef USE_COPYSAVE_API */
|
---|
| 221 |
|
---|
| 222 | #ifdef USE_TCP_EXTENTIONS
|
---|
| 223 |
|
---|
| 224 | /*
|
---|
| 225 | * get_tcp_rep -- TCP 受付口を獲得する。
|
---|
| 226 | */
|
---|
| 227 |
|
---|
| 228 | static ER
|
---|
| 229 | get_tcp_rep (ID *repid)
|
---|
| 230 | {
|
---|
| 231 | ID tskid;
|
---|
| 232 | T_TCPN_CREP crep;
|
---|
| 233 |
|
---|
| 234 | get_tid(&tskid);
|
---|
| 235 |
|
---|
| 236 | crep.repatr = UINT_C(0);
|
---|
| 237 | crep.myaddr.portno = UINT_C(9);
|
---|
| 238 |
|
---|
| 239 | #if defined(SUPPORT_INET6)
|
---|
| 240 |
|
---|
| 241 | memcpy(&crep.myaddr.ipaddr, &ipv6_addrany, sizeof(T_IN6_ADDR));
|
---|
| 242 |
|
---|
| 243 | #else /* #if defined(SUPPORT_INET6) */
|
---|
| 244 |
|
---|
| 245 | #if defined(SUPPORT_INET4)
|
---|
| 246 | crep.myaddr.ipaddr = IPV4_ADDRANY;
|
---|
| 247 | #endif
|
---|
| 248 |
|
---|
| 249 | #endif /* #if defined(SUPPORT_INET6) */
|
---|
| 250 |
|
---|
| 251 | return ALLOC_TCP_REP(repid, tskid, &crep);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | /*
|
---|
| 255 | * get_tcp_cep -- TCP 通信端点とを獲得する。
|
---|
| 256 | */
|
---|
| 257 |
|
---|
| 258 | static ER
|
---|
| 259 | get_tcp_cep (ID *cepid)
|
---|
| 260 | {
|
---|
| 261 | ID tskid;
|
---|
| 262 | T_TCP_CCEP ccep;
|
---|
| 263 |
|
---|
| 264 | get_tid(&tskid);
|
---|
| 265 |
|
---|
| 266 | ccep.cepatr = UINT_C(0);
|
---|
| 267 | ccep.sbuf = NADR;
|
---|
| 268 | ccep.sbufsz = 0;
|
---|
| 269 | ccep.rbufsz = TCP_DISCARD_SRV_RWBUF_SIZE;
|
---|
| 270 | ccep.callback = NULL;
|
---|
| 271 |
|
---|
| 272 | #ifdef TCP_CFG_RWBUF_CSAVE
|
---|
| 273 | ccep.rbuf = NADR;
|
---|
| 274 | #else
|
---|
| 275 | ccep.rbuf = tcp_discard_srv_rwbuf;
|
---|
| 276 | #endif
|
---|
| 277 |
|
---|
| 278 | return ALLOC_TCP_CEP(cepid, tskid, &ccep);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | /*
|
---|
| 282 | * tcp_discard_srv_task -- TCP エコーサーバタスク
|
---|
| 283 | */
|
---|
| 284 |
|
---|
| 285 | void
|
---|
| 286 | tcp_discard_srv_task (intptr_t exinf)
|
---|
| 287 | {
|
---|
| 288 | ID tskid, cepid, repid;
|
---|
| 289 | ER error = E_OK;
|
---|
| 290 |
|
---|
| 291 | syscall(get_tid(&tskid));
|
---|
| 292 |
|
---|
| 293 | #if defined(SUPPORT_INET6)
|
---|
| 294 | #if defined(SUPPORT_INET4)
|
---|
| 295 | syslog(LOG_NOTICE, "[TCPn DISCARD SRV:%d] started.", tskid);
|
---|
| 296 | #else
|
---|
| 297 | syslog(LOG_NOTICE, "[TCP6 DISCARD SRV:%d] started.", tskid);
|
---|
| 298 | #endif
|
---|
| 299 | #else
|
---|
| 300 | syslog(LOG_NOTICE, "[TCP4 DISCARD SRV:%d] started.", tskid);
|
---|
| 301 | #endif
|
---|
| 302 |
|
---|
| 303 | while (true) {
|
---|
| 304 |
|
---|
| 305 | syscall(slp_tsk());
|
---|
| 306 | if ((error = get_tcp_cep (&cepid)) != E_OK) {
|
---|
| 307 | syslog(LOG_NOTICE, "[TDSn:00 EXT] CEP create error: %s", itron_strerror(error));
|
---|
| 308 | continue;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | while (true) {
|
---|
| 312 |
|
---|
| 313 | if ((error = get_tcp_rep (&repid)) != E_OK) {
|
---|
| 314 | syslog(LOG_NOTICE, "[TDSn:00 EXT] REP create error: %s", itron_strerror(error));
|
---|
| 315 | break;
|
---|
| 316 | }
|
---|
| 317 | else if ((error = tcp_discard_srv(cepid, repid)) != E_OK) {
|
---|
| 318 | error = FREE_TCP_REP(repid, error != E_DLT);
|
---|
| 319 | break;
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | if ((error = FREE_TCP_CEP(cepid)) != E_OK)
|
---|
| 324 | syslog(LOG_NOTICE, "[TDSn:%02u EXT] CEP delete error: %s", cepid, itron_strerror(error));
|
---|
| 325 |
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | #else /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
| 330 |
|
---|
| 331 | void
|
---|
| 332 | tcp_discard_srv_task(intptr_t exinf)
|
---|
| 333 | {
|
---|
| 334 | ID tskid;
|
---|
| 335 |
|
---|
| 336 | get_tid(&tskid);
|
---|
| 337 |
|
---|
| 338 | #if defined(SUPPORT_INET6)
|
---|
| 339 | #if defined(SUPPORT_INET4)
|
---|
| 340 | syslog(LOG_NOTICE, "[TCPn DISCARD SRV:%d,%d] started.", tskid, (ID)exinf);
|
---|
| 341 | #else
|
---|
| 342 | syslog(LOG_NOTICE, "[TCP6 DISCARD SRV:%d,%d] started.", tskid, (ID)exinf);
|
---|
| 343 | #endif
|
---|
| 344 | #else
|
---|
| 345 | syslog(LOG_NOTICE, "[TCP4 DISCARD SRV:%d,%d] started.", tskid, (ID)exinf);
|
---|
| 346 | #endif
|
---|
| 347 |
|
---|
| 348 | while (true) {
|
---|
| 349 | while (tcp_discard_srv((ID)exinf, TCP_DISCARD_SRV_REPID) == E_OK)
|
---|
| 350 | ;
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | #endif /* of #ifdef USE_TCP_EXTENTIONS */
|
---|
| 355 |
|
---|
| 356 | #endif /* of #ifdef USE_TCP_DISCARD_SRV */
|
---|