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