| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#define _POSIX_C_SOURCE 200809L |
|
|
|
|
| 17 |
#include "bbs.h" |
#include "bbs.h" |
| 18 |
#include "common.h" |
#include "common.h" |
|
#include "log.h" |
|
| 19 |
#include "io.h" |
#include "io.h" |
| 20 |
#include "screen.h" |
#include "log.h" |
| 21 |
|
#include "login.h" |
| 22 |
#include "menu.h" |
#include "menu.h" |
| 23 |
#include <stdio.h> |
#include "screen.h" |
|
#include <stdarg.h> |
|
| 24 |
#include <errno.h> |
#include <errno.h> |
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
| 25 |
#include <fcntl.h> |
#include <fcntl.h> |
| 26 |
|
#include <netdb.h> |
| 27 |
|
#include <stdarg.h> |
| 28 |
|
#include <stdio.h> |
| 29 |
|
#include <stdlib.h> |
| 30 |
|
#include <string.h> |
| 31 |
#include <time.h> |
#include <time.h> |
| 32 |
#include <unistd.h> |
#include <unistd.h> |
|
#include <netdb.h> |
|
|
#include <sys/select.h> |
|
|
#include <sys/ioctl.h> |
|
|
#include <sys/socket.h> |
|
|
#include <sys/epoll.h> |
|
|
#include <netinet/in.h> |
|
|
#include <netinet/ip.h> |
|
| 33 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
| 34 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 35 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 36 |
#include <libssh/callbacks.h> |
#include <libssh/callbacks.h> |
| 37 |
|
#include <netinet/in.h> |
| 38 |
|
#include <netinet/ip.h> |
| 39 |
|
#include <sys/select.h> |
| 40 |
|
#include <sys/ioctl.h> |
| 41 |
|
#include <sys/socket.h> |
| 42 |
|
#include <sys/epoll.h> |
| 43 |
|
|
| 44 |
#define MENU_CONF_DELIM " \t\r\n" |
#define MENU_CONF_DELIM " \t\r\n" |
| 45 |
|
|
| 53 |
char host2[40]; |
char host2[40]; |
| 54 |
char ip[40]; |
char ip[40]; |
| 55 |
in_port_t port; |
in_port_t port; |
| 56 |
|
char charset[20]; |
| 57 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 58 |
|
|
| 59 |
MENU_SET bbsnet_menu; |
MENU_SET bbsnet_menu; |
| 64 |
MENU *p_menu; |
MENU *p_menu; |
| 65 |
MENU_ITEM *p_menu_item; |
MENU_ITEM *p_menu_item; |
| 66 |
MENU_ITEM_ID menu_item_id; |
MENU_ITEM_ID menu_item_id; |
| 67 |
char t[256], *t1, *t2, *t3, *t4, *saveptr; |
char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr; |
| 68 |
|
|
| 69 |
fp = fopen(file_config, "r"); |
fp = fopen(file_config, "r"); |
| 70 |
if (fp == NULL) |
if (fp == NULL) |
| 102 |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 103 |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 104 |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 105 |
|
t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 106 |
|
|
| 107 |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*') |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*') |
| 108 |
{ |
{ |
| 109 |
continue; |
continue; |
| 110 |
} |
} |
| 116 |
strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1); |
strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1); |
| 117 |
bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0'; |
bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0'; |
| 118 |
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); |
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); |
| 119 |
|
strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1); |
| 120 |
|
bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0'; |
| 121 |
|
|
| 122 |
p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); |
p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); |
| 123 |
if (p_menu_item == NULL) |
if (p_menu_item == NULL) |
| 207 |
int output_buf_len = 0; |
int output_buf_len = 0; |
| 208 |
int input_buf_offset = 0; |
int input_buf_offset = 0; |
| 209 |
int output_buf_offset = 0; |
int output_buf_offset = 0; |
| 210 |
|
char input_conv[LINE_BUFFER_LEN * 2]; |
| 211 |
|
char output_conv[LINE_BUFFER_LEN * 2]; |
| 212 |
|
int input_conv_len = 0; |
| 213 |
|
int output_conv_len = 0; |
| 214 |
|
int input_conv_offset = 0; |
| 215 |
|
int output_conv_offset = 0; |
| 216 |
|
iconv_t input_cd = NULL; |
| 217 |
|
iconv_t output_cd = NULL; |
| 218 |
|
char tocode[32]; |
| 219 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 220 |
int nfds, epollfd; |
int nfds, epollfd; |
| 221 |
int stdin_read_wait = 0; |
int stdin_read_wait = 0; |
| 226 |
int tos; |
int tos; |
| 227 |
char remote_addr[IP_ADDR_LEN]; |
char remote_addr[IP_ADDR_LEN]; |
| 228 |
int remote_port; |
int remote_port; |
| 229 |
time_t t_used; |
char local_addr[IP_ADDR_LEN]; |
| 230 |
|
int local_port; |
| 231 |
|
socklen_t sock_len; |
| 232 |
|
time_t t_used = time(NULL); |
| 233 |
struct tm *tm_used; |
struct tm *tm_used; |
| 234 |
int ch; |
int ch; |
| 235 |
|
|
| 236 |
|
if (user_online_update("BBS_NET") < 0) |
| 237 |
|
{ |
| 238 |
|
log_error("user_online_update(BBS_NET) error\n"); |
| 239 |
|
} |
| 240 |
|
|
| 241 |
clearscr(); |
clearscr(); |
| 242 |
|
|
| 243 |
moveto(0, 0); |
moveto(0, 0); |
| 244 |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
prints("\033[1;32m姝e湪娴嬭瘯寰 %s (%s) 鐨勮繛鎺ワ紝璇风◢鍊... \033[m\r\n", |
| 245 |
bbsnet_conf[n].host1, bbsnet_conf[n].ip); |
bbsnet_conf[n].host1, bbsnet_conf[n].ip); |
| 246 |
iflush(); |
iflush(); |
| 247 |
|
|
| 249 |
|
|
| 250 |
if (p_host == NULL) |
if (p_host == NULL) |
| 251 |
{ |
{ |
| 252 |
prints("\033[1;31m查找主机名失败!\033[m\r\n"); |
prints("\033[1;31m鏌ユ壘涓绘満鍚嶅け璐ワ紒\033[m\r\n"); |
| 253 |
press_any_key(); |
press_any_key(); |
| 254 |
return -1; |
return -1; |
| 255 |
} |
} |
| 258 |
|
|
| 259 |
if (sock < 0) |
if (sock < 0) |
| 260 |
{ |
{ |
| 261 |
prints("\033[1;31m无法创建socket!\033[m\r\n"); |
prints("\033[1;31m鏃犳硶鍒涘缓socket锛乗033[m\r\n"); |
| 262 |
press_any_key(); |
press_any_key(); |
| 263 |
return -1; |
return -1; |
| 264 |
} |
} |
| 283 |
remote_addr[sizeof(remote_addr) - 1] = '\0'; |
remote_addr[sizeof(remote_addr) - 1] = '\0'; |
| 284 |
remote_port = ntohs(sin.sin_port); |
remote_port = ntohs(sin.sin_port); |
| 285 |
|
|
| 286 |
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"); |
| 287 |
process_bar(0, MAX_PROCESS_BAR_LEN); |
process_bar(0, MAX_PROCESS_BAR_LEN); |
| 288 |
|
|
| 289 |
// Set socket as non-blocking |
// Set socket as non-blocking |
| 303 |
return -1; |
return -1; |
| 304 |
} |
} |
| 305 |
|
|
| 306 |
ev.events = EPOLLOUT; |
ev.events = EPOLLOUT | EPOLLET; |
| 307 |
ev.data.fd = sock; |
ev.data.fd = sock; |
| 308 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) |
| 309 |
{ |
{ |
| 311 |
goto cleanup; |
goto cleanup; |
| 312 |
} |
} |
| 313 |
|
|
| 314 |
ev.events = EPOLLIN; |
ev.events = EPOLLIN | EPOLLET; |
| 315 |
ev.data.fd = STDIN_FILENO; |
ev.data.fd = STDIN_FILENO; |
| 316 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 317 |
{ |
{ |
| 338 |
{ |
{ |
| 339 |
log_error("connect(socket) error (%d)\n", errno); |
log_error("connect(socket) error (%d)\n", errno); |
| 340 |
|
|
| 341 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n"); |
| 342 |
press_any_key(); |
press_any_key(); |
| 343 |
|
|
| 344 |
goto cleanup; |
goto cleanup; |
| 396 |
} |
} |
| 397 |
if (!sock_connected) |
if (!sock_connected) |
| 398 |
{ |
{ |
| 399 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n"); |
| 400 |
press_any_key(); |
press_any_key(); |
| 401 |
|
|
| 402 |
goto cleanup; |
goto cleanup; |
| 408 |
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
| 409 |
} |
} |
| 410 |
|
|
| 411 |
prints("\033[1;31m连接成功!\033[m\r\n"); |
sock_len = sizeof(sin); |
| 412 |
|
if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0) |
| 413 |
|
{ |
| 414 |
|
log_error("getsockname() error: %d", errno); |
| 415 |
|
goto cleanup; |
| 416 |
|
} |
| 417 |
|
|
| 418 |
|
strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1); |
| 419 |
|
local_addr[sizeof(local_addr) - 1] = '\0'; |
| 420 |
|
local_port = ntohs(sin.sin_port); |
| 421 |
|
|
| 422 |
|
prints("\033[1;31m杩炴帴鎴愬姛锛乗033[m\r\n"); |
| 423 |
iflush(); |
iflush(); |
| 424 |
log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", |
| 425 |
|
remote_addr, remote_port, local_addr, local_port, BBS_username); |
| 426 |
|
|
| 427 |
|
snprintf(tocode, sizeof(tocode), "%s//IGNORE", bbsnet_conf[n].charset); |
| 428 |
|
input_cd = iconv_open(tocode, stdio_charset); |
| 429 |
|
if (input_cd == (iconv_t)(-1)) |
| 430 |
|
{ |
| 431 |
|
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); |
| 432 |
|
goto cleanup; |
| 433 |
|
} |
| 434 |
|
|
| 435 |
|
snprintf(tocode, sizeof(tocode), "%s//TRANSLIT", stdio_charset); |
| 436 |
|
output_cd = iconv_open(tocode, bbsnet_conf[n].charset); |
| 437 |
|
if (output_cd == (iconv_t)(-1)) |
| 438 |
|
{ |
| 439 |
|
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno); |
| 440 |
|
iconv_close(input_cd); |
| 441 |
|
goto cleanup; |
| 442 |
|
} |
| 443 |
|
|
| 444 |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
| 445 |
ev.data.fd = sock; |
ev.data.fd = sock; |
| 449 |
goto cleanup; |
goto cleanup; |
| 450 |
} |
} |
| 451 |
|
|
| 452 |
ev.events = EPOLLOUT; |
ev.events = EPOLLOUT | EPOLLET; |
| 453 |
ev.data.fd = STDOUT_FILENO; |
ev.data.fd = STDOUT_FILENO; |
| 454 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 455 |
{ |
{ |
| 457 |
goto cleanup; |
goto cleanup; |
| 458 |
} |
} |
| 459 |
|
|
| 460 |
BBS_last_access_tm = t_used = time(0); |
BBS_last_access_tm = t_used = time(NULL); |
| 461 |
loop = 1; |
loop = 1; |
| 462 |
|
|
| 463 |
while (loop && !SYS_server_exit) |
while (loop && !SYS_server_exit) |
| 482 |
} |
} |
| 483 |
else if (nfds == 0) // timeout |
else if (nfds == 0) // timeout |
| 484 |
{ |
{ |
| 485 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 486 |
{ |
{ |
| 487 |
break; |
break; |
| 488 |
} |
} |
|
continue; |
|
| 489 |
} |
} |
| 490 |
|
|
| 491 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 492 |
{ |
{ |
| 493 |
if (events[i].data.fd == STDIN_FILENO || stdin_read_wait) |
if (events[i].data.fd == STDIN_FILENO) |
| 494 |
{ |
{ |
| 495 |
stdin_read_wait = 1; |
stdin_read_wait = 1; |
| 496 |
while (input_buf_len < sizeof(input_buf) && !SYS_server_exit) |
} |
| 497 |
|
|
| 498 |
|
if (events[i].data.fd == sock) |
| 499 |
|
{ |
| 500 |
|
if (events[i].events & EPOLLIN) |
| 501 |
|
{ |
| 502 |
|
sock_read_wait = 1; |
| 503 |
|
} |
| 504 |
|
if (events[i].events & EPOLLOUT) |
| 505 |
|
{ |
| 506 |
|
sock_write_wait = 1; |
| 507 |
|
} |
| 508 |
|
} |
| 509 |
|
|
| 510 |
|
if (events[i].data.fd == STDOUT_FILENO) |
| 511 |
|
{ |
| 512 |
|
stdout_write_wait = 1; |
| 513 |
|
} |
| 514 |
|
} |
| 515 |
|
|
| 516 |
|
if (stdin_read_wait) |
| 517 |
|
{ |
| 518 |
|
while (input_buf_len < sizeof(input_buf) && !SYS_server_exit) |
| 519 |
|
{ |
| 520 |
|
if (SSH_v2) |
| 521 |
{ |
{ |
| 522 |
if (SSH_v2) |
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); |
| 523 |
|
if (ret == SSH_ERROR) |
| 524 |
{ |
{ |
| 525 |
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 526 |
if (ret == SSH_ERROR) |
loop = 0; |
| 527 |
{ |
break; |
|
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else if (ret == SSH_EOF) |
|
|
{ |
|
|
stdin_read_wait = 0; |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else if (ret == 0) |
|
|
{ |
|
|
stdin_read_wait = 0; |
|
|
break; // Check whether channel is still open |
|
|
} |
|
| 528 |
} |
} |
| 529 |
else |
else if (ret == SSH_EOF) |
| 530 |
{ |
{ |
| 531 |
ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len); |
stdin_read_wait = 0; |
| 532 |
|
loop = 0; |
| 533 |
|
break; |
| 534 |
} |
} |
| 535 |
if (ret < 0) |
else if (ret == 0) |
| 536 |
{ |
{ |
| 537 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
stdin_read_wait = 0; |
| 538 |
{ |
break; // Check whether channel is still open |
|
stdin_read_wait = 0; |
|
|
break; |
|
|
} |
|
|
else if (errno == EINTR) |
|
|
{ |
|
|
continue; |
|
|
} |
|
|
else |
|
|
{ |
|
|
log_error("read(STDIN) error (%d)\n", errno); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 539 |
} |
} |
| 540 |
else if (ret == 0) // broken pipe |
} |
| 541 |
|
else |
| 542 |
|
{ |
| 543 |
|
ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len); |
| 544 |
|
} |
| 545 |
|
if (ret < 0) |
| 546 |
|
{ |
| 547 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 548 |
{ |
{ |
|
log_common("read(STDIN) EOF\n"); |
|
| 549 |
stdin_read_wait = 0; |
stdin_read_wait = 0; |
|
loop = 0; |
|
| 550 |
break; |
break; |
| 551 |
} |
} |
| 552 |
else |
else if (errno == EINTR) |
| 553 |
{ |
{ |
|
input_buf_len += ret; |
|
|
BBS_last_access_tm = time(0); |
|
| 554 |
continue; |
continue; |
| 555 |
} |
} |
| 556 |
|
else |
| 557 |
|
{ |
| 558 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 559 |
|
loop = 0; |
| 560 |
|
break; |
| 561 |
|
} |
| 562 |
|
} |
| 563 |
|
else if (ret == 0) // broken pipe |
| 564 |
|
{ |
| 565 |
|
#ifdef _DEBUG |
| 566 |
|
log_error("read(STDIN) EOF\n"); |
| 567 |
|
#endif |
| 568 |
|
stdin_read_wait = 0; |
| 569 |
|
loop = 0; |
| 570 |
|
break; |
| 571 |
|
} |
| 572 |
|
else |
| 573 |
|
{ |
| 574 |
|
input_buf_len += ret; |
| 575 |
|
BBS_last_access_tm = time(NULL); |
| 576 |
|
|
| 577 |
|
// Refresh current action while user input |
| 578 |
|
if (user_online_update("BBS_NET") < 0) |
| 579 |
|
{ |
| 580 |
|
log_error("user_online_update(BBS_NET) error\n"); |
| 581 |
|
} |
| 582 |
|
|
| 583 |
|
continue; |
| 584 |
|
} |
| 585 |
|
} |
| 586 |
|
} |
| 587 |
|
|
| 588 |
|
if (sock_write_wait) |
| 589 |
|
{ |
| 590 |
|
if (input_buf_offset < input_buf_len) |
| 591 |
|
{ |
| 592 |
|
ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); |
| 593 |
|
if (ret < 0) |
| 594 |
|
{ |
| 595 |
|
log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); |
| 596 |
} |
} |
| 597 |
} |
} |
| 598 |
|
|
| 599 |
if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT |
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
| 600 |
{ |
{ |
| 601 |
sock_write_wait = 1; |
ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset)); |
| 602 |
while (input_buf_offset < input_buf_len && !SYS_server_exit) |
if (ret < 0) |
| 603 |
{ |
{ |
| 604 |
ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
|
if (ret < 0) |
|
| 605 |
{ |
{ |
| 606 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
sock_write_wait = 0; |
| 607 |
{ |
break; |
|
sock_write_wait = 0; |
|
|
break; |
|
|
} |
|
|
else if (errno == EINTR) |
|
|
{ |
|
|
continue; |
|
|
} |
|
|
else |
|
|
{ |
|
|
log_error("write(socket) error (%d)\n", errno); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 608 |
} |
} |
| 609 |
else if (ret == 0) // broken pipe |
else if (errno == EINTR) |
| 610 |
{ |
{ |
| 611 |
log_common("write(socket) EOF\n"); |
continue; |
| 612 |
sock_write_wait = 0; |
} |
| 613 |
|
else |
| 614 |
|
{ |
| 615 |
|
log_error("write(socket) error (%d)\n", errno); |
| 616 |
loop = 0; |
loop = 0; |
| 617 |
break; |
break; |
| 618 |
} |
} |
| 619 |
else |
} |
| 620 |
|
else if (ret == 0) // broken pipe |
| 621 |
|
{ |
| 622 |
|
#ifdef _DEBUG |
| 623 |
|
log_error("write(socket) EOF\n"); |
| 624 |
|
#endif |
| 625 |
|
sock_write_wait = 0; |
| 626 |
|
loop = 0; |
| 627 |
|
break; |
| 628 |
|
} |
| 629 |
|
else |
| 630 |
|
{ |
| 631 |
|
input_conv_offset += ret; |
| 632 |
|
if (input_conv_offset >= input_conv_len) // Output buffer complete |
| 633 |
{ |
{ |
| 634 |
input_buf_offset += ret; |
input_conv_offset = 0; |
| 635 |
if (input_buf_offset >= input_buf_len) // Output buffer complete |
input_conv_len = 0; |
| 636 |
{ |
break; |
|
input_buf_offset = 0; |
|
|
input_buf_len = 0; |
|
|
break; |
|
|
} |
|
|
continue; |
|
| 637 |
} |
} |
| 638 |
|
continue; |
| 639 |
} |
} |
| 640 |
} |
} |
| 641 |
|
} |
| 642 |
|
|
| 643 |
if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN |
if (sock_read_wait) |
| 644 |
|
{ |
| 645 |
|
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) |
| 646 |
{ |
{ |
| 647 |
sock_read_wait = 1; |
ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); |
| 648 |
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) |
if (ret < 0) |
| 649 |
{ |
{ |
| 650 |
ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
|
if (ret < 0) |
|
| 651 |
{ |
{ |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
|
|
{ |
|
|
sock_read_wait = 0; |
|
|
break; |
|
|
} |
|
|
else if (errno == EINTR) |
|
|
{ |
|
|
continue; |
|
|
} |
|
|
else |
|
|
{ |
|
|
log_error("read(socket) error (%d)\n", errno); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
} |
|
|
else if (ret == 0) // broken pipe |
|
|
{ |
|
|
log_common("read(socket) EOF\n"); |
|
| 652 |
sock_read_wait = 0; |
sock_read_wait = 0; |
|
loop = 0; |
|
| 653 |
break; |
break; |
| 654 |
} |
} |
| 655 |
else |
else if (errno == EINTR) |
| 656 |
{ |
{ |
|
output_buf_len += ret; |
|
| 657 |
continue; |
continue; |
| 658 |
} |
} |
| 659 |
|
else |
| 660 |
|
{ |
| 661 |
|
log_error("read(socket) error (%d)\n", errno); |
| 662 |
|
loop = 0; |
| 663 |
|
break; |
| 664 |
|
} |
| 665 |
|
} |
| 666 |
|
else if (ret == 0) // broken pipe |
| 667 |
|
{ |
| 668 |
|
#ifdef _DEBUG |
| 669 |
|
log_error("read(socket) EOF\n"); |
| 670 |
|
#endif |
| 671 |
|
sock_read_wait = 0; |
| 672 |
|
loop = 0; |
| 673 |
|
break; |
| 674 |
|
} |
| 675 |
|
else |
| 676 |
|
{ |
| 677 |
|
output_buf_len += ret; |
| 678 |
|
continue; |
| 679 |
} |
} |
| 680 |
} |
} |
| 681 |
|
} |
| 682 |
|
|
| 683 |
if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait) |
if (stdout_write_wait) |
| 684 |
|
{ |
| 685 |
|
if (output_buf_offset < output_buf_len) |
| 686 |
{ |
{ |
| 687 |
stdout_write_wait = 1; |
ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len); |
| 688 |
while (output_buf_offset < output_buf_len && !SYS_server_exit) |
if (ret < 0) |
| 689 |
|
{ |
| 690 |
|
log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); |
| 691 |
|
} |
| 692 |
|
} |
| 693 |
|
|
| 694 |
|
while (output_conv_offset < output_conv_len && !SYS_server_exit) |
| 695 |
|
{ |
| 696 |
|
if (SSH_v2) |
| 697 |
{ |
{ |
| 698 |
if (SSH_v2) |
ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); |
| 699 |
|
if (ret == SSH_ERROR) |
| 700 |
{ |
{ |
| 701 |
ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset)); |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
| 702 |
if (ret == SSH_ERROR) |
loop = 0; |
| 703 |
{ |
break; |
|
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 704 |
} |
} |
| 705 |
else |
} |
| 706 |
|
else |
| 707 |
|
{ |
| 708 |
|
ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset)); |
| 709 |
|
} |
| 710 |
|
if (ret < 0) |
| 711 |
|
{ |
| 712 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 713 |
{ |
{ |
| 714 |
ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); |
stdout_write_wait = 0; |
| 715 |
|
break; |
| 716 |
} |
} |
| 717 |
if (ret < 0) |
else if (errno == EINTR) |
| 718 |
{ |
{ |
| 719 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
continue; |
|
{ |
|
|
stdout_write_wait = 0; |
|
|
break; |
|
|
} |
|
|
else if (errno == EINTR) |
|
|
{ |
|
|
continue; |
|
|
} |
|
|
else |
|
|
{ |
|
|
log_error("write(STDOUT) error (%d)\n", errno); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 720 |
} |
} |
| 721 |
else if (ret == 0) // broken pipe |
else |
| 722 |
{ |
{ |
| 723 |
log_common("write(STDOUT) EOF\n"); |
log_error("write(STDOUT) error (%d)\n", errno); |
|
stdout_write_wait = 0; |
|
| 724 |
loop = 0; |
loop = 0; |
| 725 |
break; |
break; |
| 726 |
} |
} |
| 727 |
else |
} |
| 728 |
|
else if (ret == 0) // broken pipe |
| 729 |
|
{ |
| 730 |
|
#ifdef _DEBUG |
| 731 |
|
log_error("write(STDOUT) EOF\n"); |
| 732 |
|
#endif |
| 733 |
|
stdout_write_wait = 0; |
| 734 |
|
loop = 0; |
| 735 |
|
break; |
| 736 |
|
} |
| 737 |
|
else |
| 738 |
|
{ |
| 739 |
|
output_conv_offset += ret; |
| 740 |
|
if (output_conv_offset >= output_conv_len) // Output buffer complete |
| 741 |
{ |
{ |
| 742 |
output_buf_offset += ret; |
output_conv_offset = 0; |
| 743 |
if (output_buf_offset >= output_buf_len) // Output buffer complete |
output_conv_len = 0; |
| 744 |
{ |
break; |
|
output_buf_offset = 0; |
|
|
output_buf_len = 0; |
|
|
break; |
|
|
} |
|
|
continue; |
|
| 745 |
} |
} |
| 746 |
|
continue; |
| 747 |
} |
} |
| 748 |
} |
} |
| 749 |
} |
} |
| 750 |
} |
} |
| 751 |
|
|
| 752 |
|
iconv_close(input_cd); |
| 753 |
|
iconv_close(output_cd); |
| 754 |
|
|
| 755 |
cleanup: |
cleanup: |
| 756 |
if (close(epollfd) < 0) |
if (close(epollfd) < 0) |
| 757 |
{ |
{ |
| 770 |
log_error("Close socket failed\n"); |
log_error("Close socket failed\n"); |
| 771 |
} |
} |
| 772 |
|
|
| 773 |
t_used = time(0) - t_used; |
t_used = time(NULL) - t_used; |
| 774 |
tm_used = gmtime(&t_used); |
tm_used = gmtime(&t_used); |
| 775 |
|
|
| 776 |
log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n", |
log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n", |
| 798 |
moveto(22, 0); |
moveto(22, 0); |
| 799 |
prints(" ----------------------------------------------------------------------------- "); |
prints(" ----------------------------------------------------------------------------- "); |
| 800 |
moveto(23, 0); |
moveto(23, 0); |
| 801 |
prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); |
prints(" [\x1b[1;32mCtrl+C\x1b[m]閫鍑"); |
| 802 |
|
|
| 803 |
iflush(); |
iflush(); |
| 804 |
|
|
| 811 |
|
|
| 812 |
moveto(20, 0); |
moveto(20, 0); |
| 813 |
clrtoeol(); |
clrtoeol(); |
| 814 |
prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m 站名:\x1b[1;33m%s\x1b[m", |
prints("|\x1b[1m鍗曚綅:\x1b[1;33m%-18s\x1b[m 绔欏悕:\x1b[1;33m%s\x1b[m", |
| 815 |
bbsnet_conf[i].host2, bbsnet_conf[i].host1); |
bbsnet_conf[i].host2, bbsnet_conf[i].host1); |
| 816 |
moveto(20, 79); |
moveto(20, 79); |
| 817 |
prints("|"); |
prints("|"); |
| 818 |
moveto(21, 0); |
moveto(21, 0); |
| 819 |
clrtoeol(); |
clrtoeol(); |
| 820 |
prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip); |
prints("|\x1b[1m杩炲線:\x1b[1;33m%-20s", bbsnet_conf[i].ip); |
| 821 |
if (bbsnet_conf[i].port != 23) |
if (bbsnet_conf[i].port != 23) |
| 822 |
{ |
{ |
| 823 |
prints(" %d", bbsnet_conf[i].port); |
prints(" %d", bbsnet_conf[i].port); |
| 836 |
|
|
| 837 |
load_bbsnet_conf(CONF_BBSNET); |
load_bbsnet_conf(CONF_BBSNET); |
| 838 |
|
|
|
BBS_last_access_tm = time(0); |
|
|
|
|
| 839 |
clearscr(); |
clearscr(); |
| 840 |
bbsnet_refresh(); |
bbsnet_refresh(); |
| 841 |
display_menu(&bbsnet_menu); |
display_menu(&bbsnet_menu); |
| 844 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 845 |
{ |
{ |
| 846 |
ch = igetch(100); |
ch = igetch(100); |
| 847 |
|
|
| 848 |
|
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 849 |
|
{ |
| 850 |
|
BBS_last_access_tm = time(NULL); |
| 851 |
|
} |
| 852 |
|
|
| 853 |
switch (ch) |
switch (ch) |
| 854 |
{ |
{ |
| 855 |
case KEY_NULL: // broken pipe |
case KEY_NULL: // broken pipe |
| 856 |
case KEY_ESC: |
log_error("KEY_NULL\n"); |
|
case Ctrl('C'): // user cancel |
|
| 857 |
goto cleanup; |
goto cleanup; |
| 858 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 859 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 860 |
{ |
{ |
| 861 |
|
log_error("User input timeout\n"); |
| 862 |
goto cleanup; |
goto cleanup; |
| 863 |
} |
} |
| 864 |
continue; |
continue; |
| 865 |
|
case KEY_ESC: |
| 866 |
|
case Ctrl('C'): // user cancel |
| 867 |
|
goto cleanup; |
| 868 |
case CR: |
case CR: |
|
igetch_reset(); |
|
| 869 |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]); |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]); |
| 870 |
bbsnet_refresh(); |
bbsnet_refresh(); |
| 871 |
display_menu(&bbsnet_menu); |
display_menu(&bbsnet_menu); |
| 908 |
bbsnet_selchange(); |
bbsnet_selchange(); |
| 909 |
break; |
break; |
| 910 |
} |
} |
|
BBS_last_access_tm = time(0); |
|
| 911 |
} |
} |
| 912 |
|
|
| 913 |
cleanup: |
cleanup: |