| 62 |
char org_name[40]; |
char org_name[40]; |
| 63 |
char site_name[40]; |
char site_name[40]; |
| 64 |
char host_name[IP_ADDR_LEN]; |
char host_name[IP_ADDR_LEN]; |
| 65 |
in_port_t port; |
char port[6]; |
| 66 |
int8_t use_ssh; |
int8_t use_ssh; |
| 67 |
char charset[CHARSET_MAX_LEN + 1]; |
char charset[CHARSET_MAX_LEN + 1]; |
| 68 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 143 |
unload_bbsnet_conf(); |
unload_bbsnet_conf(); |
| 144 |
return -3; |
return -3; |
| 145 |
} |
} |
| 146 |
bbsnet_conf[menu_item_id].port = (in_port_t)port; |
strncpy(bbsnet_conf[menu_item_id].port, t4, sizeof(bbsnet_conf[menu_item_id].port) - 1); |
| 147 |
|
bbsnet_conf[menu_item_id].port[sizeof(bbsnet_conf[menu_item_id].port) - 1] = '\0'; |
| 148 |
bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y'); |
bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y'); |
| 149 |
strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1); |
strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1); |
| 150 |
bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0'; |
bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0'; |
| 274 |
int stdout_write_wait = 0; |
int stdout_write_wait = 0; |
| 275 |
int sock_read_wait = 0; |
int sock_read_wait = 0; |
| 276 |
int sock_write_wait = 0; |
int sock_write_wait = 0; |
| 277 |
struct hostent *p_host = NULL; |
struct addrinfo hints, *res = NULL; |
| 278 |
int tos; |
int tos; |
| 279 |
char remote_addr[IP_ADDR_LEN]; |
char remote_addr[INET_ADDRSTRLEN]; |
| 280 |
int remote_port; |
int remote_port; |
| 281 |
char local_addr[IP_ADDR_LEN]; |
char local_addr[INET_ADDRSTRLEN]; |
| 282 |
int local_port; |
int local_port; |
| 283 |
socklen_t sock_len; |
socklen_t sock_len; |
| 284 |
time_t t_begin; |
time_t t_begin; |
| 343 |
bbsnet_conf[n].site_name, bbsnet_conf[n].host_name); |
bbsnet_conf[n].site_name, bbsnet_conf[n].host_name); |
| 344 |
iflush(); |
iflush(); |
| 345 |
|
|
| 346 |
p_host = gethostbyname(bbsnet_conf[n].host_name); |
memset(&hints, 0, sizeof(hints)); |
| 347 |
|
hints.ai_family = AF_INET; |
| 348 |
|
hints.ai_socktype = SOCK_STREAM; |
| 349 |
|
hints.ai_protocol = IPPROTO_TCP; |
| 350 |
|
|
| 351 |
if (p_host == NULL) |
if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0) |
| 352 |
{ |
{ |
| 353 |
prints("\033[1;31m查找主机名失败!\033[m\r\n"); |
log_error("getaddrinfo() error (%d)\n", ret); |
|
press_any_key(); |
|
| 354 |
goto cleanup; |
goto cleanup; |
| 355 |
} |
} |
| 356 |
|
|
| 357 |
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr), local_addr, sizeof(local_addr)) == NULL) |
| 358 |
|
{ |
| 359 |
|
log_error("inet_ntop() error (%d)\n", errno); |
| 360 |
|
goto cleanup; |
| 361 |
|
} |
| 362 |
|
local_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port); |
| 363 |
|
|
| 364 |
|
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 365 |
if (sock < 0) |
if (sock < 0) |
| 366 |
{ |
{ |
| 367 |
prints("\033[1;31m无法创建socket!\033[m\r\n"); |
log_error("socket() error (%d)\n", errno); |
|
press_any_key(); |
|
| 368 |
goto cleanup; |
goto cleanup; |
| 369 |
} |
} |
| 370 |
|
|
| 371 |
sin.sin_family = AF_INET; |
if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) |
|
sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY); |
|
|
sin.sin_port = 0; |
|
|
|
|
|
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
|
| 372 |
{ |
{ |
| 373 |
log_error("Bind address %s:%u failed (%d)\n", |
log_error("bind(%s:%u) error (%d)\n", local_addr, local_port, errno); |
|
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
|
| 374 |
goto cleanup; |
goto cleanup; |
| 375 |
} |
} |
| 376 |
|
|
| 377 |
memset(&sin, 0, sizeof(sin)); |
freeaddrinfo(res); |
| 378 |
sin.sin_family = AF_INET; |
res = NULL; |
| 379 |
sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0]; |
|
| 380 |
sin.sin_port = htons(bbsnet_conf[n].port); |
memset(&hints, 0, sizeof(hints)); |
| 381 |
|
hints.ai_family = AF_INET; |
| 382 |
|
hints.ai_flags = AI_NUMERICSERV; |
| 383 |
|
hints.ai_socktype = SOCK_STREAM; |
| 384 |
|
hints.ai_protocol = IPPROTO_TCP; |
| 385 |
|
|
| 386 |
strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1); |
if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0) |
| 387 |
remote_addr[sizeof(remote_addr) - 1] = '\0'; |
{ |
| 388 |
remote_port = ntohs(sin.sin_port); |
log_error("getaddrinfo() error (%d)\n", ret); |
| 389 |
|
prints("\033[1;31m查找主机名失败!\033[m\r\n"); |
| 390 |
|
press_any_key(); |
| 391 |
|
goto cleanup; |
| 392 |
|
} |
| 393 |
|
|
| 394 |
|
if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr), remote_addr, sizeof(remote_addr)) == NULL) |
| 395 |
|
{ |
| 396 |
|
log_error("inet_ntop() error (%d)\n", errno); |
| 397 |
|
goto cleanup; |
| 398 |
|
} |
| 399 |
|
remote_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port); |
| 400 |
|
|
| 401 |
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"); |
| 402 |
process_bar(0, MAX_PROCESS_BAR_LEN); |
process_bar(0, MAX_PROCESS_BAR_LEN); |
| 462 |
|
|
| 463 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 464 |
{ |
{ |
| 465 |
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) < 0) |
| 466 |
{ |
{ |
| 467 |
if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS) |
if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS) |
| 468 |
{ |
{ |
| 478 |
else |
else |
| 479 |
{ |
{ |
| 480 |
log_error("connect(socket) error (%d)\n", errno); |
log_error("connect(socket) error (%d)\n", errno); |
|
|
|
| 481 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 482 |
press_any_key(); |
press_any_key(); |
|
|
|
| 483 |
goto cleanup; |
goto cleanup; |
| 484 |
} |
} |
| 485 |
} |
} |
| 562 |
{ |
{ |
| 563 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 564 |
press_any_key(); |
press_any_key(); |
|
|
|
| 565 |
goto cleanup; |
goto cleanup; |
| 566 |
} |
} |
| 567 |
|
|
| 817 |
{ |
{ |
| 818 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 819 |
{ |
{ |
| 820 |
#ifdef _DEBUG |
log_debug("SSH channel is closed\n"); |
|
log_error("SSH channel is closed\n"); |
|
|
#endif |
|
| 821 |
loop = 0; |
loop = 0; |
| 822 |
break; |
break; |
| 823 |
} |
} |
| 824 |
|
|
| 825 |
if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel)) |
if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel)) |
| 826 |
{ |
{ |
| 827 |
#ifdef _DEBUG |
log_debug("Remote SSH channel is closed\n"); |
|
log_error("Remote SSH channel is closed\n"); |
|
|
#endif |
|
| 828 |
loop = 0; |
loop = 0; |
| 829 |
break; |
break; |
| 830 |
} |
} |
| 867 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 868 |
{ |
{ |
| 869 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 870 |
if (events[i].data.fd == STDIN_FILENO) |
if (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) |
| 871 |
|
#else |
| 872 |
|
if (pfds[i].revents & (POLLRDHUP | POLLHUP | POLLERR)) |
| 873 |
|
#endif |
| 874 |
|
{ |
| 875 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 876 |
|
log_debug("FD (%d) error events (%d)\n", events[i].data.fd, events[i].events); |
| 877 |
|
#else |
| 878 |
|
log_debug("FD (%d) error events (%d)\n", pfds[i].fd, pfds[i].revents); |
| 879 |
|
#endif |
| 880 |
|
loop = 0; |
| 881 |
|
break; |
| 882 |
|
} |
| 883 |
|
|
| 884 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 885 |
|
if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN)) |
| 886 |
#else |
#else |
| 887 |
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 888 |
#endif |
#endif |
| 916 |
} |
} |
| 917 |
|
|
| 918 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 919 |
if (events[i].data.fd == STDOUT_FILENO) |
if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT)) |
| 920 |
#else |
#else |
| 921 |
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
| 922 |
#endif |
#endif |
| 934 |
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); |
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); |
| 935 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 936 |
{ |
{ |
| 937 |
#ifdef _DEBUG |
log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
|
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
|
|
#endif |
|
| 938 |
loop = 0; |
loop = 0; |
| 939 |
break; |
break; |
| 940 |
} |
} |
| 979 |
} |
} |
| 980 |
else if (ret == 0) // broken pipe |
else if (ret == 0) // broken pipe |
| 981 |
{ |
{ |
| 982 |
#ifdef _DEBUG |
log_debug("read(STDIN) EOF\n"); |
|
log_error("read(STDIN) EOF\n"); |
|
|
#endif |
|
| 983 |
stdin_read_wait = 0; |
stdin_read_wait = 0; |
| 984 |
loop = 0; |
loop = 0; |
| 985 |
break; |
break; |
| 1008 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 1009 |
for (int j = input_buf_offset; j < input_buf_len; j++) |
for (int j = input_buf_offset; j < input_buf_len; j++) |
| 1010 |
{ |
{ |
| 1011 |
log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256); |
log_debug("input: <--[%u]\n", (input_buf[j] + 256) % 256); |
| 1012 |
} |
} |
| 1013 |
#endif |
#endif |
| 1014 |
|
|
| 1023 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 1024 |
for (int j = input_conv_offset; j < input_conv_len; j++) |
for (int j = input_conv_offset; j < input_conv_len; j++) |
| 1025 |
{ |
{ |
| 1026 |
log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256); |
log_debug("input_conv: <--[%u]\n", (input_conv[j] + 256) % 256); |
| 1027 |
} |
} |
| 1028 |
#endif |
#endif |
| 1029 |
} |
} |
| 1035 |
ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset)); |
ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset)); |
| 1036 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 1037 |
{ |
{ |
| 1038 |
#ifdef _DEBUG |
log_debug("ssh_channel_write() error: %s\n", ssh_get_error(session)); |
|
log_error("ssh_channel_write() error: %s\n", ssh_get_error(session)); |
|
|
#endif |
|
| 1039 |
loop = 0; |
loop = 0; |
| 1040 |
break; |
break; |
| 1041 |
} |
} |
| 1057 |
} |
} |
| 1058 |
else |
else |
| 1059 |
{ |
{ |
| 1060 |
#ifdef _DEBUG |
log_debug("write(socket) error (%d)\n", errno); |
|
log_error("write(socket) error (%d)\n", errno); |
|
|
#endif |
|
| 1061 |
loop = 0; |
loop = 0; |
| 1062 |
break; |
break; |
| 1063 |
} |
} |
| 1064 |
} |
} |
| 1065 |
else if (ret == 0) // broken pipe |
else if (ret == 0) // broken pipe |
| 1066 |
{ |
{ |
| 1067 |
#ifdef _DEBUG |
log_debug("write(socket) EOF\n"); |
|
log_error("write(socket) EOF\n"); |
|
|
#endif |
|
| 1068 |
sock_write_wait = 0; |
sock_write_wait = 0; |
| 1069 |
loop = 0; |
loop = 0; |
| 1070 |
break; |
break; |
| 1093 |
(uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0); |
(uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0); |
| 1094 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 1095 |
{ |
{ |
| 1096 |
#ifdef _DEBUG |
log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session)); |
|
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session)); |
|
|
#endif |
|
| 1097 |
loop = 0; |
loop = 0; |
| 1098 |
break; |
break; |
| 1099 |
} |
} |
| 1126 |
} |
} |
| 1127 |
else |
else |
| 1128 |
{ |
{ |
| 1129 |
#ifdef _DEBUG |
log_debug("read(socket) error (%d)\n", errno); |
|
log_error("read(socket) error (%d)\n", errno); |
|
|
#endif |
|
| 1130 |
loop = 0; |
loop = 0; |
| 1131 |
break; |
break; |
| 1132 |
} |
} |
| 1133 |
} |
} |
| 1134 |
else if (ret == 0) // broken pipe |
else if (ret == 0) // broken pipe |
| 1135 |
{ |
{ |
| 1136 |
#ifdef _DEBUG |
log_debug("read(socket) EOF\n"); |
|
log_error("read(socket) EOF\n"); |
|
|
#endif |
|
| 1137 |
sock_read_wait = 0; |
sock_read_wait = 0; |
| 1138 |
loop = 0; |
loop = 0; |
| 1139 |
break; |
break; |
| 1165 |
ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); |
ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); |
| 1166 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 1167 |
{ |
{ |
| 1168 |
#ifdef _DEBUG |
log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
|
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
|
|
#endif |
|
| 1169 |
loop = 0; |
loop = 0; |
| 1170 |
break; |
break; |
| 1171 |
} |
} |
| 1187 |
} |
} |
| 1188 |
else |
else |
| 1189 |
{ |
{ |
| 1190 |
#ifdef _DEBUG |
log_debug("write(STDOUT) error (%d)\n", errno); |
|
log_error("write(STDOUT) error (%d)\n", errno); |
|
|
#endif |
|
| 1191 |
loop = 0; |
loop = 0; |
| 1192 |
break; |
break; |
| 1193 |
} |
} |
| 1194 |
} |
} |
| 1195 |
else if (ret == 0) // broken pipe |
else if (ret == 0) // broken pipe |
| 1196 |
{ |
{ |
| 1197 |
#ifdef _DEBUG |
log_debug("write(STDOUT) EOF\n"); |
|
log_error("write(STDOUT) EOF\n"); |
|
|
#endif |
|
| 1198 |
stdout_write_wait = 0; |
stdout_write_wait = 0; |
| 1199 |
loop = 0; |
loop = 0; |
| 1200 |
break; |
break; |
| 1259 |
log_error("Close socket failed\n"); |
log_error("Close socket failed\n"); |
| 1260 |
} |
} |
| 1261 |
|
|
| 1262 |
|
if (res) |
| 1263 |
|
{ |
| 1264 |
|
freeaddrinfo(res); |
| 1265 |
|
} |
| 1266 |
|
|
| 1267 |
t_used = time(NULL) - t_used; |
t_used = time(NULL) - t_used; |
| 1268 |
tm_used = gmtime(&t_used); |
tm_used = gmtime(&t_used); |
| 1269 |
|
|
| 1314 |
prints("|"); |
prints("|"); |
| 1315 |
moveto(21, 1); |
moveto(21, 1); |
| 1316 |
clrtoeol(); |
clrtoeol(); |
| 1317 |
prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5d\033[m 编码: \033[1;33m%s\033[m", |
prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5s\033[m 编码: \033[1;33m%s\033[m", |
| 1318 |
bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset); |
bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset); |
| 1319 |
moveto(21, 80); |
moveto(21, 80); |
| 1320 |
prints("|"); |
prints("|"); |
| 1352 |
switch (ch) |
switch (ch) |
| 1353 |
{ |
{ |
| 1354 |
case KEY_NULL: // broken pipe |
case KEY_NULL: // broken pipe |
| 1355 |
#ifdef _DEBUG |
log_debug("KEY_NULL\n"); |
|
log_error("KEY_NULL\n"); |
|
|
#endif |
|
| 1356 |
goto cleanup; |
goto cleanup; |
| 1357 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 1358 |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |