- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/asp3_dcre/tinet/netapp/tcp_echo_srv1.c
r321 r331 2 2 * TINET (TCP/IP Protocol Stack) 3 3 * 4 * Copyright (C) 2001-20 09by Dep. of Computer Science and Engineering4 * Copyright (C) 2001-2017 by Dep. of Computer Science and Engineering 5 5 * Tomakomai National College of Technology, JAPAN 6 6 * … … 62 62 #endif /* of #ifdef TARGET_KERNEL_JSP */ 63 63 64 #include <tinet_defs.h>65 #include <tinet_config.h>66 67 #include <net/if.h>68 #include <net/if_ppp.h>69 #include <net/if_loop.h>70 #include <net/ethernet.h>71 #include <net/net.h>72 #include <net/net_timer.h>73 74 64 #include <netinet/in.h> 75 65 #include <netinet/in_itron.h> 76 #include <netinet/ip.h>77 #include <netinet/ip6.h>78 #include <netinet/tcp.h>79 66 80 67 #include <netapp/netapp.h> 81 68 #include <netapp/netapp_var.h> 82 #include <netapp/echo.h> 69 #include <netapp/tcp_echo_srv1.h> 70 #include <netapp/tcp_echo_srv1_var.h> 71 72 #if defined(_NET_CFG_BYTE_ORDER) 73 #error "net/net.h included." 74 #endif 83 75 84 76 #ifdef USE_TCP_ECHO_SRV1 … … 100 92 */ 101 93 94 #ifdef TOPPERS_S810_CLG3_85 95 96 #define CLS_TMO TMO_FEVR /* Close Wait は標準で 60秒 */ 97 //#define RCV_TMO TMO_FEVR 98 #define RCV_TMO (30*SYSTIM_HZ) 99 //#define SND_TMO TMO_FEVR 100 #define SND_TMO (30*SYSTIM_HZ) 101 102 #else /* of #ifdef TOPPERS_S810_CLG3_85 */ 103 102 104 //#define CLS_TMO TMO_FEVR /* Close Wait は標準で 60秒 */ 103 #define CLS_TMO (70*SYSTIM_HZ+(net _rand()%SYSTIM_HZ)*10)105 #define CLS_TMO (70*SYSTIM_HZ+(netapp_rand()%SYSTIM_HZ)*10) 104 106 //#define RCV_TMO TMO_FEVR 105 #define RCV_TMO (30*SYSTIM_HZ+(net _rand()%SYSTIM_HZ)*30)107 #define RCV_TMO (30*SYSTIM_HZ+(netapp_rand()%SYSTIM_HZ)*30) 106 108 //#define SND_TMO TMO_FEVR 107 #define SND_TMO (40*SYSTIM_HZ+(net_rand()%SYSTIM_HZ)*20) 109 #define SND_TMO (40*SYSTIM_HZ+(netapp_rand()%SYSTIM_HZ)*20) 110 111 #endif /* of #ifdef TOPPERS_S810_CLG3_85 */ 108 112 109 113 /* … … 121 125 #endif 122 126 127 #ifndef USE_TCP_NON_BLOCKING 128 123 129 /* 124 130 * 変数 125 131 */ 126 132 127 /* 128 * 注意: 129 * 130 * BUF_SIZE は TCP の 131 * 送信ウインドウバッファサイズ + 受信ウインドウバッファサイズの 132 * 3/2 倍以上の大きさがなければ、デッドロックする可能性がある。 133 */ 134 135 #define BUF_SIZE ((TCP_ECHO_SRV_SWBUF_SIZE + \ 136 TCP_ECHO_SRV_RWBUF_SIZE) * 3 / 2) 137 138 static T_IPEP dst; /* 接続相手 */ 139 140 #ifdef USE_TCP_NON_BLOCKING 141 142 static char buffer[BUF_SIZE]; 143 static ER nblk_error = E_OK; 144 static ER_UINT nblk_slen = 0; 145 static ER_UINT nblk_rlen = 0; 146 147 #else /* of #ifdef USE_TCP_NON_BLOCKING */ 148 149 #ifdef USE_COPYSAVE_API 150 151 #else /* of #ifdef USE_COPYSAVE_API */ 152 153 static char buffer[BUF_SIZE]; 154 155 #endif /* of #ifdef USE_COPYSAVE_API */ 156 157 #endif /* of #ifdef USE_TCP_NON_BLOCKING */ 158 159 #ifdef USE_TCP_NON_BLOCKING 160 161 /* 162 * ノンブロッキングコールのコールバック関数 163 */ 164 165 ER 166 callback_nblk_tcp_echo_srv (ID cepid, FN fncd, void *p_parblk) 133 #ifdef USE_TCP_EXTENTIONS 134 135 T_TCP_ECHO_SRV_INFO tcp_echo_srv_info[NUM_TCP_ECHO_SRV_TASKS]; 136 137 #else /* of #ifdef USE_TCP_EXTENTIONS */ 138 139 T_TCP_ECHO_SRV_INFO tcp_echo_srv_info[] = { 140 141 #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) && defined(USE_TCP4_ECHO_SRV) 142 143 { TCP4_ECHO_SRV_CEPID1, TCP4_ECHO_SRV_REPID }, 144 145 #else /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) && defined(USE_TCP4_ECHO_SRV) */ 146 147 { TCP_ECHO_SRV_CEPID1, TCP_ECHO_SRV_REPID }, 148 149 #endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) && defined(USE_TCP4_ECHO_SRV) */ 150 151 #if NUM_TCP_ECHO_SRV_TASKS >= 2 152 { TCP_ECHO_SRV_CEPID2, TCP_ECHO_SRV_REPID }, 153 #endif 154 #if NUM_TCP_ECHO_SRV_TASKS >= 3 155 { TCP_ECHO_SRV_CEPID3, TCP_ECHO_SRV_REPID }, 156 #endif 157 #if NUM_TCP_ECHO_SRV_TASKS >= 4 158 { TCP_ECHO_SRV_CEPID4, TCP_ECHO_SRV_REPID }, 159 #endif 160 #if NUM_TCP_ECHO_SRV_TASKS >= 5 161 { TCP_ECHO_SRV_CEPID5, TCP_ECHO_SRV_REPID }, 162 #endif 163 #if NUM_TCP_ECHO_SRV_TASKS >= 6 164 { TCP_ECHO_SRV_CEPID6, TCP_ECHO_SRV_REPID }, 165 #endif 166 #if NUM_TCP_ECHO_SRV_TASKS >= 7 167 { TCP_ECHO_SRV_CEPID7, TCP_ECHO_SRV_REPID }, 168 #endif 169 #if NUM_TCP_ECHO_SRV_TASKS >= 8 170 { TCP_ECHO_SRV_CEPID8, TCP_ECHO_SRV_REPID }, 171 #endif 172 173 }; 174 175 #endif /* of #ifdef USE_TCP_EXTENTIONS */ 176 177 /* 178 * tcp_passive_open -- 受動オープンを実行する。 179 * 180 * USE_TCP_NON_BLOCKING OFF 181 */ 182 183 #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) 184 185 static ER 186 tcp_passive_open (T_TCP_ECHO_SRV_INFO *info, char apip) 167 187 { 168 ER error = E_OK; 169 170 switch (fncd) { 171 172 case TFN_TCP_ACP_CEP: 173 nblk_error = *(ER*)p_parblk; 174 syscall(sig_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 175 break; 176 177 case TFN_TCP_RCV_DAT: 178 if ((nblk_rlen = *(ER*)p_parblk) < 0) 179 syslog(LOG_NOTICE, "[TES:%02d CBN] recv error: %s", cepid, itron_strerror(nblk_rlen)); 180 syscall(sig_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 181 break; 182 183 case TFN_TCP_SND_DAT: 184 if ((nblk_slen = *(ER*)p_parblk) < 0) 185 syslog(LOG_NOTICE, "[TES:%02d CBN] send error: %s", cepid, itron_strerror(nblk_slen)); 186 syscall(sig_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 187 break; 188 189 case TFN_TCP_CLS_CEP: 190 if ((nblk_error = *(ER*)p_parblk) < 0) 191 syslog(LOG_NOTICE, "[TES:%02d CBN] close error: %s", cepid, itron_strerror(nblk_error)); 192 syscall(sig_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 193 break; 194 195 case TFN_TCP_RCV_BUF: 196 if ((nblk_rlen = *(ER*)p_parblk) < 0) 197 syslog(LOG_NOTICE, "[TES:%02d CBN] rbuf error: %s", cepid, itron_strerror(nblk_rlen)); 198 syscall(sig_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 199 break; 200 201 case TFN_TCP_GET_BUF: 202 if ((nblk_slen = *(ER*)p_parblk) < 0) 203 syslog(LOG_NOTICE, "[TES:%02d CBN] sbuf error: %s", cepid, itron_strerror(nblk_slen)); 204 syscall(sig_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 205 break; 188 ER error = E_OK; 189 190 #if TNUM_TCP4_REPID > 0 191 T_IPV4EP dst4; 192 #endif 193 194 if (apip == API_PROTO_IPV6) { 195 196 #if TNUM_TCP6_REPID > 0 197 198 /* 受付口は IPv6 */ 199 if ((error = tcp6_acp_cep(info->cepid, info->repid, &info->dst, TMO_FEVR)) != E_OK) 200 return error; 206 201 207 202 #ifdef USE_TCP_EXTENTIONS 208 209 case TEV_TCP_RCV_OOB: 210 if ((nblk_rlen = *(ER*)p_parblk) < 0) 211 syslog(LOG_NOTICE, "[TES:%02d OOB] callback error: %s", cepid, itron_strerror(nblk_rlen)); 212 else if (nblk_rlen > 0) { 213 char ch; 214 215 if ((nblk_rlen = tcp_rcv_oob(cepid, &ch, sizeof(ch))) > 0) 216 syslog(LOG_NOTICE, "[TES:%02d OOB] recv oob: 0x%02x", cepid, ch); 217 else if (nblk_rlen < 0) 218 syslog(LOG_NOTICE, "[TES:%02d OOB] recv error: %s", cepid, itron_strerror(nblk_rlen)); 219 } 220 break; 221 203 if ((error = free_tcp6_rep(info->repid, true)) != E_OK) 204 return error; 222 205 #endif /* of #ifdef USE_TCP_EXTENTIONS */ 223 206 224 case TFN_TCP_CON_CEP: 225 case TFN_TCP_SND_OOB: 226 default: 227 error = E_PAR; 228 break; 229 } 207 #endif /* of #if TNUM_TCP6_REPID > 0 */ 208 209 } 210 else { 211 212 #if TNUM_TCP4_REPID > 0 213 214 /* 受付口は IPv4 */ 215 if ((error = tcp_acp_cep(info->cepid, info->repid, &dst4, TMO_FEVR)) != E_OK) 216 return error; 217 in6_make_ipv4mapped (&info->dst.ipaddr, dst4.ipaddr); 218 info->dst.portno = dst4.portno; 219 220 #ifdef USE_TCP_EXTENTIONS 221 if ((error = free_tcp4_rep(info->repid, true)) != E_OK) 222 return error; 223 #endif /* of #ifdef USE_TCP_EXTENTIONS */ 224 225 #endif /* of #if TNUM_TCP4_REPID > 0 */ 226 227 } 228 230 229 return error; 231 230 } 232 231 232 #else /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */ 233 234 /* 235 * tcp_passive_open -- 受動オープンを実行する。 236 * 237 * USE_TCP_NON_BLOCKING OFF 238 */ 239 240 static ER 241 tcp_passive_open (T_TCP_ECHO_SRV_INFO *info, char apip) 242 { 243 ER error = E_OK; 244 245 if ((error = TCP_ACP_CEP(info->cepid, info->repid, &info->dst, TMO_FEVR)) != E_OK) 246 return error; 247 248 #ifdef USE_TCP_EXTENTIONS 249 if ((error = FREE_TCP_REP(info->repid, true)) != E_OK) 250 return error; 251 #endif /* of #ifdef USE_TCP_EXTENTIONS */ 252 253 return error; 254 } 255 256 #endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */ 257 233 258 #ifdef USE_COPYSAVE_API 234 259 235 static ER 236 tcp_echo_srv (ID cepid, ID repid) 260 /* 261 * tcp_echo_srv -- TCP エコーサーバ 262 * 263 * USE_TCP_NON_BLOCKING OFF 264 * USE_COPYSAVE_API ON 265 */ 266 267 ER 268 tcp_echo_srv (uint_t six, char apip) 237 269 { 238 ER error; 270 ER error = E_OK; 271 ER_UINT rblen, sblen; 239 272 SYSTIM now; 240 273 uint32_t total; 241 uint16_t rblen, sblen, rlen, slen, soff, scount, rcount; 242 char *rbuf, *sbuf, head, tail; 243 244 if ((error = TCP_ACP_CEP(cepid, repid, &dst, TMO_NBLK)) != E_WBLK) { 245 syslog(LOG_NOTICE, "[TES:%02d ACP] error: %s", cepid, itron_strerror(error)); 274 uint16_t rlen, slen, soff, scount, rcount; 275 char *rbuf, *sbuf; 276 277 #ifdef SHOW_RCV_RANGE 278 char head, tail; 279 #endif 280 281 if ((error = tcp_passive_open(&tcp_echo_srv_info[six], apip)) != E_OK) { 282 syslog(LOG_NOTICE, "[TES%c:%02u OPN] error: %s", 283 apip, tcp_echo_srv_info[six].cepid, itron_strerror(error)); 246 284 return error; 247 285 } 248 286 249 /* 相手から接続されるまで待つ。*/ 250 syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 251 252 if (nblk_error == E_OK) { 253 syscall(get_tim(&now)); 254 syslog(LOG_NOTICE, "[TES:%02d ACP] connected: %6lu, from: %s.%u", 255 cepid, now / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno); 256 } 287 syscall(get_tim(&now)); 288 289 #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) 290 291 if (apip == API_PROTO_IPV6) 292 syslog(LOG_NOTICE, "[TES6:%02u ACP] conct: %7lu,from: %s.%u", 293 tcp_echo_srv_info[six].cepid, now / SYSTIM_HZ, 294 ipv62str(NULL, &tcp_echo_srv_info[six].dst.ipaddr), 295 tcp_echo_srv_info[six].dst.portno); 257 296 else { 258 syslog(LOG_NOTICE, "[TES:%02d ACP] error: %s", cepid, itron_strerror(nblk_error)); 259 return nblk_error; 260 } 261 262 #ifdef USE_TCP_EXTENTIONS 263 if ((error = free_tcp_rep(repid, true)) != E_OK) 264 syslog(LOG_NOTICE, "[TES:%02d DEL] REP delete error: %s", cepid, itron_strerror(error)); 265 #endif /* of #ifdef USE_TCP_EXTENTIONS */ 266 267 rlen = scount = rcount = total = 0; 297 T_IN4_ADDR addr; 298 299 addr = ntohl(tcp_echo_srv_info[six].dst.ipaddr.s6_addr32[3]); 300 syslog(LOG_NOTICE, "[TES4:%02u ACP] conct: %7lu,from: %s.%u", 301 tcp_echo_srv_info[six].cepid, now / SYSTIM_HZ, 302 ip2str(NULL, &addr), tcp_echo_srv_info[six].dst.portno); 303 } 304 305 #else /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */ 306 307 syslog(LOG_NOTICE, "[TES%c:%02u ACP] conct: %7lu,from: %s.%u", 308 apip, tcp_echo_srv_info[six].cepid, now / SYSTIM_HZ, 309 IP2STR(NULL, &tcp_echo_srv_info[six].dst.ipaddr), 310 tcp_echo_srv_info[six].dst.portno); 311 312 #endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */ 313 314 scount = rcount = total = 0; 268 315 while (true) { 269 if ((error = tcp_rcv_buf(cepid, (void**)&rbuf, TMO_NBLK)) != E_WBLK) { 270 syslog(LOG_NOTICE, "[TES:%02d RCV] error: %s", cepid, itron_strerror(error)); 316 if ((rblen = tcp_rcv_buf(tcp_echo_srv_info[six].cepid, (void**)&rbuf, RCV_TMO)) <= 0) { 317 if (rblen != E_OK) 318 syslog(LOG_NOTICE, "[TES%c:%02u RCV] error: %s", 319 apip, tcp_echo_srv_info[six].cepid, itron_strerror(rblen)); 271 320 break; 272 321 } 273 322 274 /* 受信するまで待つ。*/ 275 syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 276 277 if (nblk_rlen < 0) { /* エラー */ 278 syslog(LOG_NOTICE, "[TES:%02d RCV] error: %s", 279 cepid, itron_strerror(nblk_rlen)); 280 break; 281 } 282 else if (nblk_rlen == 0) /* 受信終了 */ 283 break; 284 285 rblen = nblk_rlen; 286 287 /* バッファの残りにより、受信長を調整する。*/ 288 if (rblen > BUF_SIZE - rlen) 289 rblen = BUF_SIZE - rlen; 290 total += rblen; 291 rlen = rblen; 292 323 rcount ++; 324 325 #ifdef SHOW_RCV_RANGE 293 326 head = *rbuf; 294 327 tail = *(rbuf + rblen - 1); 295 rcount ++; 296 297 #ifdef SHOW_RCV_RANGE 298 syslog(LOG_NOTICE, "[TES:%02d RCV] " 299 "rcount: %4d, len: %4d, data %02x -> %02x", 300 cepid, rcount, rblen, head, tail); 328 329 //syslog(LOG_NOTICE, "[TES%c:%02u RCV] len: %7lu", 330 // apip, tcp_echo_srv_info[six].cepid, (uint16_t)rblen); 331 syslog(LOG_NOTICE, "[TES%c:%02u RCV] rcount: %7lu, len: %6lu, data %02x -> %02x", 332 apip, tcp_echo_srv_info[six].cepid, rcount, (uint16_t)rblen, head, tail); 301 333 #endif /* of #ifdef SHOW_RCV_RANGE */ 302 334 303 memcpy(buffer, rbuf, rblen); 304 305 if ((error = tcp_rel_buf(cepid, rlen)) < 0) { 306 syslog(LOG_NOTICE, "[TES:%02d REL] error: %s", 307 cepid, itron_strerror(error)); 308 break; 309 } 310 335 rlen = (uint16_t)rblen; 336 total += rblen; 311 337 soff = 0; 312 338 while (rlen > 0) { 313 339 314 if (( error = tcp_get_buf(cepid, (void**)&sbuf, TMO_NBLK)) != E_WBLK) {315 syslog(LOG_NOTICE, "[TES :%02dGET] error: %s",316 cepid, itron_strerror(error));340 if ((sblen = tcp_get_buf(tcp_echo_srv_info[six].cepid, (void**)&sbuf, SND_TMO)) < 0) { 341 syslog(LOG_NOTICE, "[TES%c:%02u GET] error: %s", 342 apip, tcp_echo_srv_info[six].cepid, itron_strerror(sblen)); 317 343 goto err_fin; 318 344 } 319 345 320 /* 送信バッファの獲得が完了するまで待つ。*/ 321 syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 322 323 if (nblk_slen < 0) { 324 syslog(LOG_NOTICE, "[TES:%02d GET] error: %s", 325 cepid, itron_strerror(nblk_slen)); 326 goto err_fin; 327 } 328 329 sblen = nblk_slen; 346 //syslog(LOG_NOTICE, "[TES%c:%02u SND] len: %7lu", 347 // apip, tcp_echo_srv_info[six].cepid, (uint16_t)sblen); 330 348 scount ++; 331 slen = sblen < rlen ? sblen : rlen; 332 memcpy(sbuf, buffer + soff, slen); 333 334 if ((error = tcp_snd_buf(cepid, slen)) != E_OK) { 335 syslog(LOG_NOTICE, "[TES:%02d SND] error: %s", 336 cepid, itron_strerror(error)); 349 slen = rlen < (uint16_t)sblen ? rlen : (uint16_t)sblen; 350 memcpy(sbuf, rbuf + soff, slen); 351 if ((error = tcp_snd_buf(tcp_echo_srv_info[six].cepid, slen)) != E_OK) { 352 syslog(LOG_NOTICE, "[TES%c:%02u SND] error: %s", 353 apip, tcp_echo_srv_info[six].cepid, itron_strerror(error)); 337 354 goto err_fin; 338 355 } 339 356 #ifdef SHOW_RCV_RANGE 340 syslog(LOG_NOTICE, "[TES :%02d SND] scount: %4d, len: %4d",341 cepid, scount, slen);357 syslog(LOG_NOTICE, "[TES%c:%02u SND] scount: %4u, len: %4u", 358 apip, tcp_echo_srv_info[six].cepid, scount, slen); 342 359 #endif /* of #ifdef SHOW_RCV_RANGE */ 343 360 … … 345 362 soff += slen; 346 363 } 364 365 if ((error = tcp_rel_buf(tcp_echo_srv_info[six].cepid, rblen)) < 0) { 366 syslog(LOG_NOTICE, "[TES%c:%02u REL] error: %s", 367 apip, tcp_echo_srv_info[six].cepid, itron_strerror(error)); 368 break; 369 } 347 370 } 348 371 err_fin: 349 372 350 373 #ifdef USE_TCP_SHT_CEP 351 if ((error = tcp_sht_cep(cepid)) != E_OK) 352 syslog(LOG_NOTICE, "[TES:%02d SHT] error: %s", cepid, itron_strerror(error)); 374 if ((error = tcp_sht_cep(tcp_echo_srv_info[six].cepid)) != E_OK) 375 syslog(LOG_NOTICE, "[TES%c:%02u SHT] error: %s", 376 apip, tcp_echo_srv_info[six].cepid, itron_strerror(error)); 353 377 #endif /* of #ifdef USE_TCP_SHT_CEP */ 354 378 355 if ((error = tcp_cls_cep(cepid, TMO_NBLK)) != E_WBLK) 356 syslog(LOG_NOTICE, "[TES:%02d CLS] error: %s", cepid, itron_strerror(error)); 357 358 /* 開放が完了するまで待つ。*/ 359 syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 379 if ((error = tcp_cls_cep(tcp_echo_srv_info[six].cepid, CLS_TMO)) != E_OK) 380 syslog(LOG_NOTICE, "[TES%c:%02u CLS] error: %s", 381 apip, tcp_echo_srv_info[six].cepid, itron_strerror(error)); 360 382 361 383 syscall(get_tim(&now)); 362 syslog(LOG_NOTICE, "[TES:%02d FIN] finished: %6lu, snd: %4u, rcv: %4u, len: %lu", 363 cepid, now / SYSTIM_HZ, scount, rcount, total); 364 365 return error == E_WBLK ? E_OK : error; 384 #if 1 385 syslog(LOG_NOTICE, "[TES%c:%02u FIN] finsh: %7lu, ttl: %lu", 386 apip, tcp_echo_srv_info[six].cepid, now / SYSTIM_HZ, total); 387 #else 388 syslog(LOG_NOTICE, "[TES%c:%02u FIN] finsh: %7lu, ttl: %lu, error: %s", 389 apip, tcp_echo_srv_info[six].cepid, now / SYSTIM_HZ, total, itron_strerror(error)); 390 #endif 391 392 return error; 366 393 } 367 394 368 395 #else /* of #ifdef USE_COPYSAVE_API */ 369 396 370 static ER 371 tcp_echo_srv (ID cepid, ID repid) 397 /* 398 * tcp_echo_srv -- TCP エコーサーバ 399 * 400 * USE_TCP_NON_BLOCKING OFF 401 * USE_COPYSAVE_API OFF 402 */ 403 404 ER 405 tcp_echo_srv (uint_t six, char apip) 372 406 { 407 ID cepid; 408 ER error = E_OK; 409 ER_UINT rlen, slen; 373 410 SYSTIM now; 374 ER error;375 411 uint32_t total; 376 uint16_t rlen, slen,soff, scount, rcount;377 char head, tail; 378 379 if ((error = TCP_ACP_CEP(cepid, repid, &dst, TMO_NBLK)) != E_WBLK) {380 syslog(LOG_NOTICE, "[TES:%02d ACP] error: %s",cepid, itron_strerror(error));412 uint16_t soff, scount, rcount; 413 414 if ((error = tcp_passive_open(&tcp_echo_srv_info[six], apip)) != E_OK) { 415 syslog(LOG_NOTICE, "[TES%c:%02u OPN] error: %s", 416 apip, tcp_echo_srv_info[six].cepid, itron_strerror(error)); 381 417 return error; 382 418 } 383 384 /* 相手から接続されるまで待つ。*/ 385 syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 386 387 if (nblk_error == E_OK) { 388 syscall(get_tim(&now)); 389 syslog(LOG_NOTICE, "[TES:%02d ACP] connected: %6lu, from: %s.%u", 390 cepid, now / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno); 391 } 392 else { 393 syslog(LOG_NOTICE, "[TES:%02d ACP] error: %s", cepid, itron_strerror(nblk_error)); 394 return nblk_error; 395 } 396 397 #ifdef USE_TCP_EXTENTIONS 398 if ((error = free_tcp_rep(repid, true)) != E_OK) 399 syslog(LOG_NOTICE, "[TES:%02d DEL] REP delete error: %s", cepid, itron_strerror(error)); 400 #endif /* of #ifdef USE_TCP_EXTENTIONS */ 401 419 420 syscall(get_tim(&now)); 421 422 #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) 423 424 if (apip == API_PROTO_IPV6) 425 syslog(LOG_NOTICE, "[TES6:%02u ACP] conct: %7lu,from: %s.%u", 426 tcp_echo_srv_info[six].cepid, now / SYSTIM_HZ, 427 ipv62str(NULL, &tcp_echo_srv_info[six].dst.ipaddr), 428 tcp_echo_srv_info[six].dst.portno); 429 else { 430 T_IN4_ADDR addr; 431 432 addr = ntohl(tcp_echo_srv_info[six].dst.ipaddr.s6_addr32[3]); 433 syslog(LOG_NOTICE, "[TES4:%02u ACP] conct: %7lu,from: %s.%u", 434 tcp_echo_srv_info[six].cepid, now / SYSTIM_HZ, 435 ip2str(NULL, &addr), tcp_echo_srv_info[six].dst.portno); 436 } 437 438 #else /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */ 439 440 syslog(LOG_NOTICE, "[TES%c:%02u ACP] conct: %7lu,from: %s.%u", 441 apip, tcp_echo_srv_info[six].cepid, now / SYSTIM_HZ, 442 IP2STR(NULL, &tcp_echo_srv_info[six].dst.ipaddr), 443 tcp_echo_srv_info[six].dst.portno); 444 445 #endif /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */ 446 447 cepid = tcp_echo_srv_info[six].cepid; 402 448 scount = rcount = total = 0; 403 449 while (true) { 404 if ((error = tcp_rcv_dat(cepid, buffer, BUF_SIZE - 1, TMO_NBLK)) != E_WBLK) { 405 syslog(LOG_NOTICE, "[TES:%02d RCV] error: %s", 406 cepid, itron_strerror(error)); 450 if ((rlen = tcp_rcv_dat(cepid, tcp_echo_srv_info[six].buffer, BUF_SIZE - 1, RCV_TMO)) <= 0) { 451 if (rlen != E_OK) 452 syslog(LOG_NOTICE, "[TES%c:%02u RCV] error: %s", 453 apip, cepid, itron_strerror(rlen)); 407 454 break; 408 455 } 409 456 410 /* 受信完了まで待つ。*/411 syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY));412 413 if (nblk_rlen < 0) {414 syslog(LOG_NOTICE, "[TES:%02d RCV] error: %s",415 cepid, itron_strerror(nblk_rlen));416 break;417 }418 else if (nblk_rlen == 0)419 break;420 421 rlen = nblk_rlen;422 head = *buffer;423 tail = *(buffer + rlen - 1);424 457 rcount ++; 425 426 458 #ifdef SHOW_RCV_RANGE 427 syslog(LOG_NOTICE, "[TES:%02d RCV] rcount: %4d, len: %4d, data %02x -> %02x", 428 cepid, rcount, rlen, head, tail); 459 syslog(LOG_NOTICE, "[TES%c:%02u RCV] rcount: %4u, len: %4u, data %02x -> %02x", 460 apip, cepid, rcount, 461 (uint16_t)rlen, * tcp_echo_srv_info[six].tcp_echo_srv_info[six].buffer, 462 *(tcp_echo_srv_info[six].tcp_echo_srv_info[six].buffer + rlen - 1)); 429 463 #endif /* of #ifdef SHOW_RCV_RANGE */ 430 464 … … 433 467 while (rlen > 0) { 434 468 scount ++; 435 if (( error = tcp_snd_dat(cepid, &buffer[soff], rlen, TMO_NBLK)) != E_WBLK) {436 syslog(LOG_NOTICE, "[TES :%02dSND] error: %s",437 cepid, itron_strerror(error));469 if ((slen = tcp_snd_dat(cepid, &tcp_echo_srv_info[six].buffer[soff], rlen, SND_TMO)) < 0) { 470 syslog(LOG_NOTICE, "[TES%c:%02u SND] error: %s", 471 apip, cepid, itron_strerror(slen)); 438 472 goto err_fin; 439 473 } 440 441 /* 送信完了まで待つ。*/442 syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY));443 444 if (nblk_slen < 0) {445 syslog(LOG_NOTICE, "[TES:%02d SND] error: %s",446 cepid, itron_strerror(nblk_slen));447 goto err_fin;448 }449 450 slen = nblk_slen;451 452 474 #ifdef SHOW_RCV_RANGE 453 syslog(LOG_NOTICE, "[TES :%02d SND] scount: %4d, len: %4d",454 cepid, scount,slen);475 syslog(LOG_NOTICE, "[TES%c:%02u SND] scount: %4u, len: %4u", 476 apip, cepid, scount, (uint16_t)slen); 455 477 #endif /* of #ifdef SHOW_RCV_RANGE */ 456 478 … … 463 485 #ifdef USE_TCP_SHT_CEP 464 486 if ((error = tcp_sht_cep(cepid)) != E_OK) 465 syslog(LOG_NOTICE, "[TES:%02d SHT] error: %s", cepid, itron_strerror(error)); 487 syslog(LOG_NOTICE, "[TES%c:%02u SHT] error: %s", 488 apip, cepid, itron_strerror(error)); 466 489 #endif /* of #ifdef USE_TCP_SHT_CEP */ 467 490 468 if ((error = tcp_cls_cep(cepid, TMO_NBLK)) != E_WBLK) 469 syslog(LOG_NOTICE, "[TES:%02d CLS] error: %s", cepid, itron_strerror(error)); 470 471 /* 開放が完了するまで待つ。*/ 472 syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); 491 if ((error = tcp_cls_cep(cepid, CLS_TMO)) != E_OK) 492 syslog(LOG_NOTICE, "[TES%c:%02u CLS] error: %s", 493 apip, cepid, itron_strerror(error)); 473 494 474 495 syscall(get_tim(&now)); 475 syslog(LOG_NOTICE, "[TES:%02d FIN] finished: %6lu, snd: %4u, rcv: %4u, len: %lu", 476 cepid, now / SYSTIM_HZ, scount, rcount, total); 477 478 return error == E_WBLK ? E_OK : error; 479 } 480 481 #endif /* of #ifdef USE_COPYSAVE_API */ 482 483 #else /* of #ifdef USE_TCP_NON_BLOCKING */ 484 485 #ifdef USE_COPYSAVE_API 486 487 static ER 488 tcp_echo_srv (ID cepid, ID repid) 489 { 490 ER_UINT rblen, sblen; 491 SYSTIM now; 492 ER error; 493 uint32_t total; 494 uint16_t rlen, slen, soff, scount, rcount; 495 char *rbuf, *sbuf, head, tail; 496 497 if ((error = TCP_ACP_CEP(cepid, repid, &dst, TMO_FEVR)) != E_OK) { 498 syslog(LOG_NOTICE, "[TES:%02d ACP] error: %s", cepid, itron_strerror(error)); 499 return error; 500 } 501 502 #ifdef USE_TCP_EXTENTIONS 503 if ((error = free_tcp_rep(repid, true)) != E_OK) 504 syslog(LOG_NOTICE, "[TES:%02d DEL] REP delete error: %s", cepid, itron_strerror(error)); 505 #endif /* of #ifdef USE_TCP_EXTENTIONS */ 506 507 scount = rcount = total = 0; 508 syscall(get_tim(&now)); 509 syslog(LOG_NOTICE, "[TES:%02d ACP] connected: %6lu, from: %s.%u", 510 cepid, now / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno); 511 while (true) { 512 if ((rblen = tcp_rcv_buf(cepid, (void**)&rbuf, RCV_TMO)) <= 0) { 513 if (rblen != E_OK) 514 syslog(LOG_NOTICE, "[TES:%02d RCV] error: %s", 515 cepid, itron_strerror(rblen)); 516 break; 517 } 518 519 head = *rbuf; 520 tail = *(rbuf + rblen - 1); 521 rcount ++; 522 523 //syslog(LOG_NOTICE, "[TES:%02d RCV] len: %4d", cepid, (uint16_t)rblen); 524 #ifdef SHOW_RCV_RANGE 525 syslog(LOG_NOTICE, "[TES:%02d RCV] rcount: %4d, len: %4d, data %02x -> %02x", 526 cepid, rcount, (uint16_t)rblen, head, tail); 527 #endif /* of #ifdef SHOW_RCV_RANGE */ 528 529 rlen = (uint16_t)rblen; 530 total += rblen; 531 soff = 0; 532 while (rlen > 0) { 533 534 if ((sblen = tcp_get_buf(cepid, (void**)&sbuf, SND_TMO)) < 0) { 535 syslog(LOG_NOTICE, "[TES:%02d GET] error: %s", 536 cepid, itron_strerror(sblen)); 537 goto err_fin; 538 } 539 540 //syslog(LOG_NOTICE, "[TES:%02d SND] len: %4d", cepid, (uint16_t)sblen); 541 scount ++; 542 slen = rlen < (uint16_t)sblen ? rlen : (uint16_t)sblen; 543 memcpy(sbuf, rbuf + soff, slen); 544 if ((error = tcp_snd_buf(cepid, slen)) != E_OK) { 545 syslog(LOG_NOTICE, "[TES:%02d SND] error: %s", 546 cepid, itron_strerror(error)); 547 goto err_fin; 548 } 549 #ifdef SHOW_RCV_RANGE 550 syslog(LOG_NOTICE, "[TES:%02d SND] scount: %4d, len: %4d", cepid, scount, slen); 551 #endif /* of #ifdef SHOW_RCV_RANGE */ 552 553 rlen -= slen; 554 soff += slen; 555 } 556 557 if ((error = tcp_rel_buf(cepid, rblen)) < 0) { 558 syslog(LOG_NOTICE, "[TES:%02d REL] error: %s", cepid, itron_strerror(error)); 559 break; 560 } 561 } 562 err_fin: 563 564 #ifdef USE_TCP_SHT_CEP 565 if ((error = tcp_sht_cep(cepid)) != E_OK) 566 syslog(LOG_NOTICE, "[TES:%02d SHT] error: %s", cepid, itron_strerror(error)); 567 #endif /* of #ifdef USE_TCP_SHT_CEP */ 568 569 if ((error = tcp_cls_cep(cepid, CLS_TMO)) != E_OK) 570 syslog(LOG_NOTICE, "[TES:%02d CLS] error: %s", cepid, itron_strerror(error)); 571 572 syscall(get_tim(&now)); 573 syslog(LOG_NOTICE, "[TES:%02d FIN] finished: %6lu, snd: %4u, rcv: %4u, len: %lu", 574 cepid, now / SYSTIM_HZ, scount, rcount, total); 496 syslog(LOG_NOTICE, "[TES%c:%02u FIN] finsh: %7lu, ttl: %lu", 497 apip, cepid, now / SYSTIM_HZ, total); 575 498 576 499 return error; 577 500 } 578 501 579 #else /* of #ifdef USE_COPYSAVE_API */580 581 /*582 * tcp_echo_srv -- TCP エコーサーバ583 */584 585 static ER586 tcp_echo_srv (ID cepid, ID repid)587 {588 ER_UINT rlen, slen;589 ER error = E_OK;590 SYSTIM now;591 uint32_t total;592 uint16_t soff, scount, rcount;593 594 if ((error = TCP_ACP_CEP(cepid, repid, &dst, TMO_FEVR)) != E_OK) {595 syslog(LOG_NOTICE, "[TES:%02d ACP] error: %s", cepid, itron_strerror(error));596 return error;597 }598 599 #ifdef USE_TCP_EXTENTIONS600 if ((error = free_tcp_rep(repid, true)) != E_OK)601 syslog(LOG_NOTICE, "[TES:%02d DEL] REP delete error: %s", cepid, itron_strerror(error));602 #endif /* of #ifdef USE_TCP_EXTENTIONS */603 604 scount = rcount = total = 0;605 syscall(get_tim(&now));606 syslog(LOG_NOTICE, "[TES:%02d ACP] connected: %6lu, from: %s.%u",607 cepid, now / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno);608 while (true) {609 if ((rlen = tcp_rcv_dat(cepid, buffer, BUF_SIZE - 1, RCV_TMO)) <= 0) {610 if (rlen != E_OK)611 syslog(LOG_NOTICE, "[TES:%02d RCV] error: %s",612 cepid, itron_strerror(rlen));613 break;614 }615 616 rcount ++;617 #ifdef SHOW_RCV_RANGE618 syslog(LOG_NOTICE, "[TES:%02d RCV] rcount: %4d, len: %4d, data %02x -> %02x",619 cepid, rcount, (uint16_t)rlen, *buffer, *(buffer + rlen - 1));620 #endif /* of #ifdef SHOW_RCV_RANGE */621 622 total += rlen;623 soff = 0;624 while (rlen > 0) {625 scount ++;626 if ((slen = tcp_snd_dat(cepid, &buffer[soff], rlen, SND_TMO)) < 0) {627 syslog(LOG_NOTICE, "[TES:%02d SND] error: %s",628 cepid, itron_strerror(slen));629 goto err_fin;630 }631 #ifdef SHOW_RCV_RANGE632 syslog(LOG_NOTICE, "[TES:%02d SND] scount: %4d, len: %4d", cepid, scount, (uint16_t)slen);633 #endif /* of #ifdef SHOW_RCV_RANGE */634 635 rlen -= slen;636 soff += slen;637 }638 }639 err_fin:640 641 #ifdef USE_TCP_SHT_CEP642 if ((error = tcp_sht_cep(cepid)) != E_OK)643 syslog(LOG_NOTICE, "[TES:%02d SHT] error: %s", cepid, itron_strerror(error));644 #endif /* of #ifdef USE_TCP_SHT_CEP */645 646 if ((error = tcp_cls_cep(cepid, CLS_TMO)) != E_OK)647 syslog(LOG_NOTICE, "[TES:%02d CLS] error: %s", cepid, itron_strerror(error));648 649 syscall(get_tim(&now));650 syslog(LOG_NOTICE, "[TES:%02d FIN] finished: %6lu, snd: %4u, rcv: %4u, len: %lu",651 cepid, now / SYSTIM_HZ, scount, rcount, total);652 653 return error;654 }655 656 502 #endif /* of #ifdef USE_COPYSAVE_API */ 657 503 658 #endif /* of #ifdef USE_TCP_NON_BLOCKING */ 659 660 #ifdef USE_TCP_EXTENTIONS 661 662 /* 663 * get_tcp_rep -- TCP 受付口を獲得する。 664 */ 665 666 static ER 667 get_tcp_rep (ID *repid) 668 { 669 ID tskid; 670 T_TCP_CREP crep; 671 672 get_tid(&tskid); 673 674 crep.repatr = UINT_C(0); 675 crep.myaddr.portno = UINT_C(7); 676 677 #if defined(SUPPORT_INET4) 678 crep.myaddr.ipaddr = IPV4_ADDRANY; 679 #endif 680 681 #if defined(SUPPORT_INET6) 682 memcpy(&crep.myaddr.ipaddr, &ipv6_addrany, sizeof(T_IN6_ADDR)); 683 #endif 684 685 return alloc_tcp_rep(repid, tskid, &crep); 686 } 687 688 /* 689 * get_tcp_cep -- TCP 通信端点とを獲得する。 690 */ 691 692 static ER 693 get_tcp_cep (ID *cepid) 694 { 695 ID tskid; 696 T_TCP_CCEP ccep; 697 698 get_tid(&tskid); 699 700 ccep.cepatr = UINT_C(0); 701 ccep.sbufsz = TCP_ECHO_SRV_SWBUF_SIZE; 702 ccep.rbufsz = TCP_ECHO_SRV_RWBUF_SIZE; 703 704 #ifdef TCP_CFG_SWBUF_CSAVE 705 ccep.sbuf = NADR; 706 #else 707 ccep.sbuf = tcp_echo_srv_swbuf; 708 #endif 709 #ifdef TCP_CFG_RWBUF_CSAVE 710 ccep.rbuf = NADR; 711 #else 712 ccep.rbuf = tcp_echo_srv_rwbuf; 713 #endif 714 #ifdef USE_TCP_NON_BLOCKING 715 ccep.callback = (FP)callback_nblk_tcp_echo_srv; 716 #else 717 ccep.callback = NULL; 718 #endif 719 720 return alloc_tcp_cep(cepid, tskid, &ccep); 721 } 722 723 /* 724 * tcp_echo_srv_task -- TCP エコーサーバタスク 725 */ 726 727 void 728 tcp_echo_srv_task (intptr_t exinf) 729 { 730 ID tskid, cepid, repid; 731 ER error = E_OK; 732 733 syscall(get_tid(&tskid)); 734 syslog(LOG_NOTICE, "[TCP ECHO SRV:%d] started.", tskid); 735 while (true) { 736 737 syscall(slp_tsk()); 738 if ((error = get_tcp_cep (&cepid)) != E_OK) { 739 syslog(LOG_NOTICE, "[TES:00 EXT] CEP create error: %s", itron_strerror(error)); 740 continue; 741 } 742 743 while (true) { 744 745 if ((error = get_tcp_rep (&repid)) != E_OK) { 746 syslog(LOG_NOTICE, "[TES:00 EXT] REP create error: %s", itron_strerror(error)); 747 break; 748 } 749 else if ((error = tcp_echo_srv(cepid, repid)) != E_OK) { 750 error = free_tcp_rep(repid, error != E_DLT); 751 break; 752 } 753 } 754 755 if ((error = free_tcp_cep(cepid)) != E_OK) 756 syslog(LOG_NOTICE, "[TES:%02d EXT] CEP delete error: %s", cepid, itron_strerror(error)); 757 758 } 759 } 760 761 #else /* of #ifdef USE_TCP_EXTENTIONS */ 504 #endif /* of #ifndef USE_TCP_NON_BLOCKING */ 505 506 #ifndef USE_TCP_EXTENTIONS 762 507 763 508 /* … … 768 513 tcp_echo_srv_task(intptr_t exinf) 769 514 { 770 ID tskid ;515 ID tskid, cepid; 771 516 ER error; 517 uint_t six; 518 char apip; 519 520 six = INDEX_SRV_INFO((ID)exinf); 521 522 #if defined(SUPPORT_INET6) 523 524 #if defined(SUPPORT_INET4) && defined(USE_TCP4_ECHO_SRV) 525 526 if (six >= NUM_TCP4_ECHO_SRV_TASKS) 527 apip = API_PROTO_IPV6; 528 else 529 apip = API_PROTO_IPV4; 530 531 #else /* of #if defined(SUPPORT_INET4) && defined(USE_TCP4_ECHO_SRV) */ 532 533 apip = API_PROTO_IPV6; 534 535 #endif /* of #if defined(SUPPORT_INET4) && defined(USE_TCP4_ECHO_SRV) */ 536 537 #else /* of #if defined(SUPPORT_INET6) */ 538 539 apip = API_PROTO_IPV4; 540 541 #endif /* of #if defined(SUPPORT_INET6) */ 772 542 773 543 syscall(get_tid(&tskid)); 774 syslog(LOG_NOTICE, "[TCP ECHO SRV:%d,%d] started.", tskid, (ID)exinf); 544 cepid = tcp_echo_srv_info[six].cepid; 545 syslog(LOG_NOTICE, "[TCP%c ECHO SRV:%d,%d] started.", apip, tskid, cepid); 546 775 547 while (true) { 776 while ((error = tcp_echo_srv( (ID)exinf, TCP_ECHO_SRV_REPID)) == E_OK)548 while ((error = tcp_echo_srv(six, apip)) == E_OK) 777 549 ; 778 syslog(LOG_NOTICE, "[TES:%02d] goto sleep 60[s], error: %s", (ID)exinf, itron_strerror(error)); 779 tslp_tsk(60 * 1000); 550 syslog(LOG_NOTICE, "[TES%c:%02u TSK] sleep 60[s], error: %s", apip, cepid, itron_strerror(error)); 551 tslp_tsk(60 * 1000 * 1000); 552 syslog(LOG_NOTICE, "[TES%c:%02u TSK] resume.", apip, cepid); 780 553 } 781 554 } 782 555 783 #endif /* of #if def USE_TCP_EXTENTIONS */556 #endif /* of #ifndef USE_TCP_EXTENTIONS */ 784 557 785 558 #endif /* of #ifdef USE_TCP_ECHO_SRV1 */
Note:
See TracChangeset
for help on using the changeset viewer.