| 32 |
#include <sys/select.h> |
#include <sys/select.h> |
| 33 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 34 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 35 |
|
#include <sys/epoll.h> |
| 36 |
#include <netinet/in.h> |
#include <netinet/in.h> |
| 37 |
#include <netinet/ip.h> |
#include <netinet/ip.h> |
| 38 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
| 152 |
int bbsnet_connect(int n) |
int bbsnet_connect(int n) |
| 153 |
{ |
{ |
| 154 |
int sock, ret, loop, error; |
int sock, ret, loop, error; |
| 155 |
|
int sock_connected = 0; |
| 156 |
int flags_sock; |
int flags_sock; |
| 157 |
int flags_stdin; |
int flags_stdin; |
| 158 |
int flags_stdout; |
int flags_stdout; |
| 164 |
int output_buf_len = 0; |
int output_buf_len = 0; |
| 165 |
int input_buf_offset = 0; |
int input_buf_offset = 0; |
| 166 |
int output_buf_offset = 0; |
int output_buf_offset = 0; |
| 167 |
fd_set read_fds; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 168 |
fd_set write_fds; |
int nfds, epollfd; |
| 169 |
struct timeval timeout; |
int stdin_read_wait = 0; |
| 170 |
|
int stdout_write_wait = 0; |
| 171 |
|
int sock_read_wait = 0; |
| 172 |
|
int sock_write_wait = 0; |
| 173 |
struct hostent *p_host = NULL; |
struct hostent *p_host = NULL; |
| 174 |
int tos; |
int tos; |
|
int i; |
|
| 175 |
char remote_addr[IP_ADDR_LEN]; |
char remote_addr[IP_ADDR_LEN]; |
| 176 |
int remote_port; |
int remote_port; |
| 177 |
time_t t_used; |
time_t t_used; |
| 226 |
prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); |
prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); |
| 227 |
process_bar(0, MAX_PROCESS_BAR_LEN); |
process_bar(0, MAX_PROCESS_BAR_LEN); |
| 228 |
|
|
| 229 |
|
epollfd = epoll_create1(0); |
| 230 |
|
if (epollfd < 0) |
| 231 |
|
{ |
| 232 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 233 |
|
return -1; |
| 234 |
|
} |
| 235 |
|
|
| 236 |
|
ev.events = EPOLLOUT; |
| 237 |
|
ev.data.fd = sock; |
| 238 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) |
| 239 |
|
{ |
| 240 |
|
log_error("epoll_ctl(socket) error (%d)\n", errno); |
| 241 |
|
return -1; |
| 242 |
|
} |
| 243 |
|
|
| 244 |
|
ev.events = EPOLLIN; |
| 245 |
|
ev.data.fd = STDIN_FILENO; |
| 246 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 247 |
|
{ |
| 248 |
|
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 249 |
|
return -1; |
| 250 |
|
} |
| 251 |
|
|
| 252 |
// Set socket as non-blocking |
// Set socket as non-blocking |
| 253 |
flags_sock = fcntl(sock, F_GETFL, 0); |
flags_sock = fcntl(sock, F_GETFL, 0); |
| 254 |
fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); |
fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); |
| 255 |
|
|
| 256 |
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
while (!SYS_server_exit) |
| 257 |
{ |
{ |
| 258 |
if (errno != EINPROGRESS) |
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
| 259 |
{ |
{ |
| 260 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS) |
| 261 |
press_any_key(); |
{ |
| 262 |
return -1; |
log_std("Debug: %d\n", errno); |
| 263 |
|
// Use select / epoll to check writability of the socket, |
| 264 |
|
// then use getsockopt to check the status of the socket. |
| 265 |
|
// See man connect(2) |
| 266 |
|
break; |
| 267 |
|
} |
| 268 |
|
else if (errno == EINTR) |
| 269 |
|
{ |
| 270 |
|
continue; |
| 271 |
|
} |
| 272 |
|
else |
| 273 |
|
{ |
| 274 |
|
log_error("connect(socket) error (%d)\n", errno); |
| 275 |
|
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 276 |
|
press_any_key(); |
| 277 |
|
return -1; |
| 278 |
|
} |
| 279 |
} |
} |
| 280 |
} |
} |
| 281 |
|
|
| 282 |
for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) |
for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) |
| 283 |
{ |
{ |
| 284 |
ch = igetch(0); // 0.1 second |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second |
|
if (ch == Ctrl('C') || SYS_server_exit) |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
FD_ZERO(&read_fds); |
|
|
FD_SET(sock, &read_fds); |
|
|
|
|
|
FD_ZERO(&write_fds); |
|
|
FD_SET(sock, &write_fds); |
|
|
|
|
|
timeout.tv_sec = 0; |
|
|
timeout.tv_usec = 400 * 1000; // 0.4 second |
|
| 285 |
|
|
| 286 |
ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout); |
if (nfds < 0) |
|
|
|
|
if (ret == 0) // Timeout |
|
|
{ |
|
|
process_bar(i + 1, MAX_PROCESS_BAR_LEN); |
|
|
} |
|
|
else if (ret < 0) |
|
| 287 |
{ |
{ |
| 288 |
if (errno != EINTR) |
if (errno != EINTR) |
| 289 |
{ |
{ |
| 290 |
log_error("select() error (%d) !\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 291 |
return -1; |
return -1; |
| 292 |
} |
} |
| 293 |
} |
} |
| 294 |
// ret > 0 |
else if (nfds == 0) // timeout |
|
else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds)) |
|
| 295 |
{ |
{ |
| 296 |
len = sizeof(error); |
process_bar(j + 1, MAX_PROCESS_BAR_LEN); |
| 297 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
} |
| 298 |
|
else // ret > 0 |
| 299 |
|
{ |
| 300 |
|
for (int i = 0; i < nfds; i++) |
| 301 |
{ |
{ |
| 302 |
log_error("getsockopt() error (%d) !\n", error); |
if (events[i].data.fd == sock) |
| 303 |
return -1; |
{ |
| 304 |
|
len = sizeof(error); |
| 305 |
|
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
| 306 |
|
{ |
| 307 |
|
log_error("getsockopt() error (%d) !\n", error); |
| 308 |
|
return -1; |
| 309 |
|
} |
| 310 |
|
if (error == 0) |
| 311 |
|
{ |
| 312 |
|
sock_connected = 1; |
| 313 |
|
} |
| 314 |
|
} |
| 315 |
|
else if (events[i].data.fd == STDIN_FILENO) |
| 316 |
|
{ |
| 317 |
|
ch = igetch(0); |
| 318 |
|
if (ch == Ctrl('C')) |
| 319 |
|
{ |
| 320 |
|
return 0; |
| 321 |
|
} |
| 322 |
|
} |
| 323 |
} |
} |
|
|
|
|
break; // connected |
|
| 324 |
} |
} |
| 325 |
} |
} |
| 326 |
if (i == MAX_PROCESS_BAR_LEN) |
if (SYS_server_exit) |
| 327 |
|
{ |
| 328 |
|
return 0; |
| 329 |
|
} |
| 330 |
|
if (!sock_connected) |
| 331 |
{ |
{ |
| 332 |
prints("\033[1;31m连接超时!\033[m\r\n"); |
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 333 |
press_any_key(); |
press_any_key(); |
| 344 |
iflush(); |
iflush(); |
| 345 |
log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
| 346 |
|
|
| 347 |
|
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
| 348 |
|
ev.data.fd = sock; |
| 349 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
| 350 |
|
{ |
| 351 |
|
log_error("epoll_ctl(socket) error (%d)\n", errno); |
| 352 |
|
return -1; |
| 353 |
|
} |
| 354 |
|
|
| 355 |
|
ev.events = EPOLLOUT; |
| 356 |
|
ev.data.fd = STDOUT_FILENO; |
| 357 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 358 |
|
{ |
| 359 |
|
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 360 |
|
return -1; |
| 361 |
|
} |
| 362 |
|
|
| 363 |
// Set STDIN/STDOUT as non-blocking |
// Set STDIN/STDOUT as non-blocking |
| 364 |
flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0); |
flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 365 |
flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0); |
flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 371 |
|
|
| 372 |
while (loop && !SYS_server_exit) |
while (loop && !SYS_server_exit) |
| 373 |
{ |
{ |
| 374 |
FD_ZERO(&read_fds); |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
|
FD_SET(STDIN_FILENO, &read_fds); |
|
|
FD_SET(sock, &read_fds); |
|
|
|
|
|
FD_ZERO(&write_fds); |
|
|
FD_SET(STDOUT_FILENO, &write_fds); |
|
|
FD_SET(sock, &write_fds); |
|
|
|
|
|
timeout.tv_sec = 0; |
|
|
timeout.tv_usec = 100 * 1000; // 0.1 second |
|
| 375 |
|
|
| 376 |
ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout); |
if (nfds < 0) |
|
|
|
|
if (ret == 0) // timeout |
|
| 377 |
{ |
{ |
| 378 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (errno != EINTR) |
| 379 |
{ |
{ |
| 380 |
loop = 0; |
log_error("epoll_wait() error (%d)\n", errno); |
| 381 |
|
break; |
| 382 |
} |
} |
| 383 |
|
continue; |
| 384 |
} |
} |
| 385 |
else if (ret < 0) |
else if (nfds == 0) // timeout |
| 386 |
{ |
{ |
| 387 |
if (errno != EINTR) |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 388 |
{ |
{ |
| 389 |
log_error("select() error (%d) !\n", errno); |
break; |
|
loop = 0; |
|
| 390 |
} |
} |
| 391 |
|
continue; |
| 392 |
} |
} |
| 393 |
else // if (ret > 0) |
|
| 394 |
|
for (int i = 0; i < nfds; i++) |
| 395 |
{ |
{ |
| 396 |
if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds)) |
if (events[i].data.fd == STDIN_FILENO || stdin_read_wait) |
| 397 |
{ |
{ |
| 398 |
ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf)); |
stdin_read_wait = 1; |
| 399 |
if (ret < 0) |
while (input_buf_len < sizeof(input_buf) && !SYS_server_exit) |
| 400 |
{ |
{ |
| 401 |
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len); |
| 402 |
|
if (ret < 0) |
| 403 |
|
{ |
| 404 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 405 |
|
{ |
| 406 |
|
stdin_read_wait = 0; |
| 407 |
|
break; |
| 408 |
|
} |
| 409 |
|
else if (errno == EINTR) |
| 410 |
|
{ |
| 411 |
|
continue; |
| 412 |
|
} |
| 413 |
|
else |
| 414 |
|
{ |
| 415 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 416 |
|
loop = 0; |
| 417 |
|
break; |
| 418 |
|
} |
| 419 |
|
} |
| 420 |
|
else if (ret == 0) // broken pipe |
| 421 |
{ |
{ |
| 422 |
log_error("read(STDIN) error (%d)\n", errno); |
log_std("read(STDIN) EOF\n"); |
| 423 |
|
stdin_read_wait = 0; |
| 424 |
loop = 0; |
loop = 0; |
| 425 |
|
break; |
| 426 |
|
} |
| 427 |
|
else |
| 428 |
|
{ |
| 429 |
|
input_buf_len += ret; |
| 430 |
|
BBS_last_access_tm = time(0); |
| 431 |
|
continue; |
| 432 |
} |
} |
|
} |
|
|
else if (ret == 0) // broken pipe |
|
|
{ |
|
|
loop = 0; |
|
|
} |
|
|
else |
|
|
{ |
|
|
input_buf_len = ret; |
|
|
input_buf_offset = 0; |
|
|
|
|
|
BBS_last_access_tm = time(0); |
|
| 433 |
} |
} |
| 434 |
} |
} |
| 435 |
|
|
| 436 |
if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds)) |
if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT |
| 437 |
{ |
{ |
| 438 |
ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); |
sock_write_wait = 1; |
| 439 |
if (ret < 0) |
while (input_buf_offset < input_buf_len && !SYS_server_exit) |
| 440 |
{ |
{ |
| 441 |
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); |
| 442 |
|
if (ret < 0) |
| 443 |
{ |
{ |
| 444 |
log_error("write(socket) error (%d)\n", errno); |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 445 |
|
{ |
| 446 |
|
sock_write_wait = 0; |
| 447 |
|
break; |
| 448 |
|
} |
| 449 |
|
else if (errno == EINTR) |
| 450 |
|
{ |
| 451 |
|
continue; |
| 452 |
|
} |
| 453 |
|
else |
| 454 |
|
{ |
| 455 |
|
log_error("write(socket) error (%d)\n", errno); |
| 456 |
|
loop = 0; |
| 457 |
|
break; |
| 458 |
|
} |
| 459 |
|
} |
| 460 |
|
else if (ret == 0) // broken pipe |
| 461 |
|
{ |
| 462 |
|
log_std("write(socket) EOF\n"); |
| 463 |
|
sock_write_wait = 0; |
| 464 |
loop = 0; |
loop = 0; |
| 465 |
|
break; |
| 466 |
|
} |
| 467 |
|
else |
| 468 |
|
{ |
| 469 |
|
input_buf_offset += ret; |
| 470 |
|
if (input_buf_offset >= input_buf_len) // Output buffer complete |
| 471 |
|
{ |
| 472 |
|
input_buf_offset = 0; |
| 473 |
|
input_buf_len = 0; |
| 474 |
|
break; |
| 475 |
|
} |
| 476 |
|
continue; |
| 477 |
} |
} |
|
} |
|
|
else if (ret == 0) // broken pipe |
|
|
{ |
|
|
loop = 0; |
|
|
} |
|
|
else |
|
|
{ |
|
|
input_buf_offset += ret; |
|
| 478 |
} |
} |
| 479 |
} |
} |
| 480 |
|
|
| 481 |
if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds)) |
if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN |
| 482 |
{ |
{ |
| 483 |
ret = (int)read(sock, output_buf, sizeof(output_buf)); |
sock_read_wait = 1; |
| 484 |
if (ret < 0) |
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) |
| 485 |
{ |
{ |
| 486 |
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); |
| 487 |
|
if (ret < 0) |
| 488 |
|
{ |
| 489 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 490 |
|
{ |
| 491 |
|
sock_read_wait = 0; |
| 492 |
|
break; |
| 493 |
|
} |
| 494 |
|
else if (errno == EINTR) |
| 495 |
|
{ |
| 496 |
|
continue; |
| 497 |
|
} |
| 498 |
|
else |
| 499 |
|
{ |
| 500 |
|
log_error("read(socket) error (%d)\n", errno); |
| 501 |
|
loop = 0; |
| 502 |
|
break; |
| 503 |
|
} |
| 504 |
|
} |
| 505 |
|
else if (ret == 0) // broken pipe |
| 506 |
{ |
{ |
| 507 |
log_error("read(socket) error (%d)\n", errno); |
log_std("read(socket) EOF\n"); |
| 508 |
|
sock_read_wait = 0; |
| 509 |
loop = 0; |
loop = 0; |
| 510 |
|
break; |
| 511 |
|
} |
| 512 |
|
else |
| 513 |
|
{ |
| 514 |
|
output_buf_len += ret; |
| 515 |
|
continue; |
| 516 |
} |
} |
|
} |
|
|
else if (ret == 0) // broken pipe |
|
|
{ |
|
|
loop = 0; |
|
|
} |
|
|
else |
|
|
{ |
|
|
output_buf_len = ret; |
|
|
output_buf_offset = 0; |
|
| 517 |
} |
} |
| 518 |
} |
} |
| 519 |
|
|
| 520 |
if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds)) |
if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait) |
| 521 |
{ |
{ |
| 522 |
ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); |
stdout_write_wait = 1; |
| 523 |
if (ret < 0) |
while (output_buf_offset < output_buf_len && !SYS_server_exit) |
| 524 |
{ |
{ |
| 525 |
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); |
| 526 |
|
if (ret < 0) |
| 527 |
{ |
{ |
| 528 |
log_error("write(STDOUT) error (%d)\n", errno); |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 529 |
|
{ |
| 530 |
|
stdout_write_wait = 0; |
| 531 |
|
break; |
| 532 |
|
} |
| 533 |
|
else if (errno == EINTR) |
| 534 |
|
{ |
| 535 |
|
continue; |
| 536 |
|
} |
| 537 |
|
else |
| 538 |
|
{ |
| 539 |
|
log_error("write(STDOUT) error (%d)\n", errno); |
| 540 |
|
loop = 0; |
| 541 |
|
break; |
| 542 |
|
} |
| 543 |
|
} |
| 544 |
|
else if (ret == 0) // broken pipe |
| 545 |
|
{ |
| 546 |
|
log_std("write(STDOUT) EOF\n"); |
| 547 |
|
stdout_write_wait = 0; |
| 548 |
loop = 0; |
loop = 0; |
| 549 |
|
break; |
| 550 |
|
} |
| 551 |
|
else |
| 552 |
|
{ |
| 553 |
|
output_buf_offset += ret; |
| 554 |
|
if (output_buf_offset >= output_buf_len) // Output buffer complete |
| 555 |
|
{ |
| 556 |
|
output_buf_offset = 0; |
| 557 |
|
output_buf_len = 0; |
| 558 |
|
break; |
| 559 |
|
} |
| 560 |
|
continue; |
| 561 |
} |
} |
|
} |
|
|
else if (ret == 0) // broken pipe |
|
|
{ |
|
|
loop = 0; |
|
|
} |
|
|
else |
|
|
{ |
|
|
output_buf_offset += ret; |
|
| 562 |
} |
} |
| 563 |
} |
} |
| 564 |
} |
} |