| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
bbs_net.c - description |
bbs_net.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
#include "screen.h" |
#include "screen.h" |
| 22 |
#include "menu.h" |
#include "menu.h" |
|
#include "tcplib.h" |
|
| 23 |
#include <stdio.h> |
#include <stdio.h> |
| 24 |
#include <stdarg.h> |
#include <stdarg.h> |
| 25 |
|
#include <errno.h> |
| 26 |
|
#include <string.h> |
| 27 |
|
#include <stdlib.h> |
| 28 |
|
#include <fcntl.h> |
| 29 |
|
#include <time.h> |
| 30 |
|
#include <unistd.h> |
| 31 |
|
#include <netdb.h> |
| 32 |
|
#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> |
| 38 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
|
#include <time.h> |
|
|
#include <unistd.h> |
|
|
#include <netdb.h> |
|
| 39 |
|
|
| 40 |
#define TIME_OUT 15 |
#define MENU_CONF_DELIM " \t\r\n" |
| 41 |
|
|
| 42 |
#define MAX_PROCESS_BAR_LEN 30 |
#define MAX_PROCESS_BAR_LEN 30 |
| 43 |
#define MAXSTATION 26 * 2 |
#define MAXSTATION 26 * 2 |
| 44 |
#define STATION_PER_LINE 4 |
#define STATION_PER_LINE 4 |
| 48 |
char host1[20]; |
char host1[20]; |
| 49 |
char host2[40]; |
char host2[40]; |
| 50 |
char ip[40]; |
char ip[40]; |
| 51 |
int port; |
in_port_t port; |
| 52 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 53 |
|
|
| 54 |
MENU_SET bbsnet_menu; |
MENU_SET bbsnet_menu; |
| 63 |
|
|
| 64 |
fp = fopen(file_config, "r"); |
fp = fopen(file_config, "r"); |
| 65 |
if (fp == NULL) |
if (fp == NULL) |
| 66 |
|
{ |
| 67 |
return -1; |
return -1; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU)); |
p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU)); |
| 71 |
strcpy(p_menu->name, "BBSNET"); |
strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1); |
| 72 |
|
p_menu->name[sizeof(p_menu->name) - 1] = '\0'; |
| 73 |
p_menu->title.show = 0; |
p_menu->title.show = 0; |
| 74 |
p_menu->screen.show = 0; |
p_menu->screen.show = 0; |
| 75 |
|
|
| 76 |
while (fgets(t, 255, fp) && item_count < MAXSTATION) |
while (fgets(t, 255, fp) && item_count < MAXSTATION) |
| 77 |
{ |
{ |
| 78 |
if (t[0] == '#' || t[0] == '*') |
t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr); |
| 79 |
{ |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 80 |
continue; |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 81 |
} |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
|
|
|
|
t1 = strtok_r(t, " \t", &saveptr); |
|
|
t2 = strtok_r(NULL, " \t\n", &saveptr); |
|
|
t3 = strtok_r(NULL, " \t\n", &saveptr); |
|
|
t4 = strtok_r(NULL, " \t\n", &saveptr); |
|
| 82 |
|
|
| 83 |
if (t1 == NULL || t2 == NULL || t3 == NULL) |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*') |
| 84 |
{ |
{ |
| 85 |
continue; |
continue; |
| 86 |
} |
} |
| 87 |
|
|
| 88 |
strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1)); |
strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1) - 1); |
| 89 |
strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2)); |
bbsnet_conf[item_count].host1[sizeof(bbsnet_conf[item_count].host1) - 1] = '\0'; |
| 90 |
strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip)); |
strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2) - 1); |
| 91 |
bbsnet_conf[item_count].port = t4 ? atoi(t4) : 23; |
bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0'; |
| 92 |
|
strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip) - 1); |
| 93 |
|
bbsnet_conf[item_count].ip[sizeof(bbsnet_conf[item_count].ip) - 1] = '\0'; |
| 94 |
|
bbsnet_conf[item_count].port = (in_port_t)(t4 ? atoi(t4) : 23); |
| 95 |
|
|
| 96 |
p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM)); |
p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM)); |
| 97 |
p_menuitem->row = 2 + item_count / STATION_PER_LINE; |
p_menuitem->row = 2 + item_count / STATION_PER_LINE; |
| 98 |
p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20; |
p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20; |
| 99 |
sprintf(p_menuitem->action, "%d", item_count); |
snprintf(p_menuitem->action, sizeof(p_menuitem->action), "%d", item_count); |
| 100 |
p_menuitem->submenu = 0; |
p_menuitem->submenu = 0; |
| 101 |
p_menuitem->priv = 0; |
p_menuitem->priv = 0; |
| 102 |
p_menuitem->level = 0; |
p_menuitem->level = 0; |
| 103 |
p_menuitem->display = 0; |
p_menuitem->display = 0; |
| 104 |
p_menuitem->name[0] = |
p_menuitem->name[0] = |
| 105 |
(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count); |
(char)(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count); |
| 106 |
p_menuitem->name[1] = '\0'; |
p_menuitem->name[1] = '\0'; |
| 107 |
snprintf(p_menuitem->text, sizeof(p_menuitem->text), "[1;36m%c.[m %s", |
snprintf(p_menuitem->text, sizeof(p_menuitem->text), "[1;36m%c.[m %s", |
| 108 |
p_menuitem->name[0], t2); |
p_menuitem->name[0], bbsnet_conf[item_count].host1); |
| 109 |
|
|
| 110 |
item_count++; |
item_count++; |
| 111 |
} |
} |
| 121 |
return 0; |
return 0; |
| 122 |
} |
} |
| 123 |
|
|
| 124 |
static void |
static void process_bar(int n, int len) |
|
process_bar(int n, int len) |
|
| 125 |
{ |
{ |
| 126 |
char buf[256]; |
char buf[LINE_BUFFER_LEN]; |
| 127 |
char buf2[256]; |
char buf2[LINE_BUFFER_LEN]; |
| 128 |
char *ptr; |
|
| 129 |
char *ptr2; |
if (len > LINE_BUFFER_LEN) |
| 130 |
char *ptr3; |
{ |
| 131 |
|
len = LINE_BUFFER_LEN - 1; |
| 132 |
|
} |
| 133 |
|
if (n < 0) |
| 134 |
|
{ |
| 135 |
|
n = 0; |
| 136 |
|
} |
| 137 |
|
else if (n > len) |
| 138 |
|
{ |
| 139 |
|
n = len; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
moveto(4, 0); |
moveto(4, 0); |
| 143 |
prints("┌──────────────────────────────┐\r\n"); |
prints(" ------------------------------ \r\n"); |
| 144 |
sprintf(buf2, " %3d%% ", n * 100 / len); |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
| 145 |
ptr = buf; |
strncpy(buf2, buf, (size_t)n); |
| 146 |
ptr2 = buf2; |
buf2[n] = '\0'; |
| 147 |
ptr3 = buf + n; |
prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n); |
| 148 |
while (ptr != ptr3) |
prints(" ------------------------------ \r\n"); |
|
*ptr++ = *ptr2++; |
|
|
*ptr++ = '\x1b'; |
|
|
*ptr++ = '['; |
|
|
*ptr++ = '4'; |
|
|
*ptr++ = '4'; |
|
|
*ptr++ = 'm'; |
|
|
while (*ptr2 != '\0') |
|
|
*ptr++ = *ptr2++; |
|
|
*ptr++ = '\0'; |
|
|
prints("│\033[46m%s\033[m│\r\n", buf); |
|
|
prints("└──────────────────────────────┘\r\n"); |
|
| 149 |
iflush(); |
iflush(); |
| 150 |
} |
} |
| 151 |
|
|
| 152 |
int bbsnet_connect(int n) |
int bbsnet_connect(int n) |
| 153 |
{ |
{ |
| 154 |
int sock, result, len, loop; |
int sock, ret, loop, error; |
| 155 |
|
int sock_connected = 0; |
| 156 |
|
int flags_sock; |
| 157 |
|
int flags_stdin; |
| 158 |
|
int flags_stdout; |
| 159 |
|
int len; |
| 160 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 161 |
char buf[256]; |
char input_buf[LINE_BUFFER_LEN]; |
| 162 |
fd_set inputs, testfds; |
char output_buf[LINE_BUFFER_LEN]; |
| 163 |
struct timeval timeout; |
int input_buf_len = 0; |
| 164 |
struct hostent *pHost = NULL; |
int output_buf_len = 0; |
| 165 |
int rv, tos = 020, i; |
int input_buf_offset = 0; |
| 166 |
char remote_addr[256]; |
int output_buf_offset = 0; |
| 167 |
|
struct epoll_event ev, events[MAX_EVENTS]; |
| 168 |
|
int nfds, epollfd; |
| 169 |
|
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; |
| 174 |
|
int tos; |
| 175 |
|
char remote_addr[IP_ADDR_LEN]; |
| 176 |
int remote_port; |
int remote_port; |
| 177 |
time_t t_used; |
time_t t_used; |
| 178 |
struct tm *tm_used; |
struct tm *tm_used; |
| 179 |
|
int ch; |
| 180 |
|
|
| 181 |
clearscr(); |
clearscr(); |
| 182 |
|
|
| 183 |
moveto(0, 0); |
moveto(0, 0); |
| 184 |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
| 185 |
bbsnet_conf[n].host1, bbsnet_conf[n].ip); |
bbsnet_conf[n].host1, bbsnet_conf[n].ip); |
|
prints("\033[1;32m如果在 %d 秒内无法连上,穿梭程序将放弃连接。\033[m\r\n", |
|
|
TIME_OUT); |
|
| 186 |
iflush(); |
iflush(); |
| 187 |
|
|
| 188 |
pHost = gethostbyname(bbsnet_conf[n].ip); |
p_host = gethostbyname(bbsnet_conf[n].ip); |
| 189 |
|
|
| 190 |
if (pHost == NULL) |
if (p_host == NULL) |
| 191 |
{ |
{ |
| 192 |
prints("\033[1;31m查找主机名失败!\033[m\r\n"); |
prints("\033[1;31m查找主机名失败!\033[m\r\n"); |
| 193 |
press_any_key(); |
press_any_key(); |
| 204 |
} |
} |
| 205 |
|
|
| 206 |
sin.sin_family = AF_INET; |
sin.sin_family = AF_INET; |
| 207 |
sin.sin_addr.s_addr = |
sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY); |
|
(strlen(hostaddr_server) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY); |
|
| 208 |
sin.sin_port = 0; |
sin.sin_port = 0; |
| 209 |
|
|
| 210 |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 211 |
{ |
{ |
| 212 |
log_error("Bind address %s:%u failed\n", |
log_error("Bind address %s:%u failed (%d)\n", |
| 213 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
| 214 |
return -2; |
return -2; |
| 215 |
} |
} |
| 216 |
|
|
| 217 |
bzero(&sin, sizeof(sin)); |
bzero(&sin, sizeof(sin)); |
| 218 |
sin.sin_family = AF_INET; |
sin.sin_family = AF_INET; |
| 219 |
sin.sin_addr = *(struct in_addr *)pHost->h_addr_list[0]; |
sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0]; |
| 220 |
sin.sin_port = htons(bbsnet_conf[n].port); |
sin.sin_port = htons(bbsnet_conf[n].port); |
| 221 |
|
|
| 222 |
strcpy(remote_addr, inet_ntoa(sin.sin_addr)); |
strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1); |
| 223 |
|
remote_addr[sizeof(remote_addr) - 1] = '\0'; |
| 224 |
remote_port = ntohs(sin.sin_port); |
remote_port = ntohs(sin.sin_port); |
| 225 |
|
|
| 226 |
prints("\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 |
for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) |
|
| 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 |
if (i == 0) |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 249 |
rv = |
return -1; |
| 250 |
NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin), |
} |
| 251 |
500, 1); |
|
| 252 |
else |
// Set socket as non-blocking |
| 253 |
rv = |
flags_sock = fcntl(sock, F_GETFL, 0); |
| 254 |
NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin), |
fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); |
| 255 |
500, 0); |
|
| 256 |
if (rv == ERR_TCPLIB_TIMEOUT) |
while (!SYS_server_exit) |
| 257 |
|
{ |
| 258 |
|
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
| 259 |
{ |
{ |
| 260 |
process_bar(i + 1, MAX_PROCESS_BAR_LEN); |
if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS) |
| 261 |
continue; |
{ |
| 262 |
|
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 |
else if (rv == 0) |
} |
| 281 |
break; |
|
| 282 |
else |
for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) |
| 283 |
|
{ |
| 284 |
|
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second |
| 285 |
|
|
| 286 |
|
if (nfds < 0) |
| 287 |
|
{ |
| 288 |
|
if (errno != EINTR) |
| 289 |
|
{ |
| 290 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 291 |
|
return -1; |
| 292 |
|
} |
| 293 |
|
} |
| 294 |
|
else if (nfds == 0) // timeout |
| 295 |
{ |
{ |
| 296 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
process_bar(j + 1, MAX_PROCESS_BAR_LEN); |
|
press_any_key(); |
|
|
return -1; |
|
| 297 |
} |
} |
| 298 |
|
else // ret > 0 |
| 299 |
|
{ |
| 300 |
|
for (int i = 0; i < nfds; i++) |
| 301 |
|
{ |
| 302 |
|
if (events[i].data.fd == sock) |
| 303 |
|
{ |
| 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 |
|
} |
| 324 |
|
} |
| 325 |
|
} |
| 326 |
|
if (SYS_server_exit) |
| 327 |
|
{ |
| 328 |
|
return 0; |
| 329 |
} |
} |
| 330 |
if (i == MAX_PROCESS_BAR_LEN) |
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(); |
| 334 |
return -1; |
return -1; |
| 335 |
} |
} |
| 336 |
setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int)); |
|
| 337 |
|
tos = IPTOS_LOWDELAY; |
| 338 |
|
if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) |
| 339 |
|
{ |
| 340 |
|
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
| 341 |
|
} |
| 342 |
|
|
| 343 |
prints("\033[1;31m连接成功!\033[m\r\n"); |
prints("\033[1;31m连接成功!\033[m\r\n"); |
| 344 |
|
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 |
FD_ZERO(&inputs); |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
| 348 |
FD_SET(0, &inputs); |
ev.data.fd = sock; |
| 349 |
FD_SET(sock, &inputs); |
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 |
BBS_last_access_tm = t_used = time(0); |
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 |
| 364 |
|
flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 365 |
|
flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 366 |
|
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); |
| 367 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); |
| 368 |
|
|
| 369 |
|
BBS_last_access_tm = t_used = time(0); |
| 370 |
loop = 1; |
loop = 1; |
| 371 |
|
|
| 372 |
while (loop) |
while (loop && !SYS_server_exit) |
| 373 |
{ |
{ |
| 374 |
testfds = inputs; |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
|
timeout.tv_sec = TIME_OUT; |
|
|
timeout.tv_usec = 0; |
|
| 375 |
|
|
| 376 |
result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL, |
if (nfds < 0) |
|
(fd_set *)NULL, &timeout); |
|
|
|
|
|
if (result == 0) |
|
| 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 |
if (result < 0) |
else if (nfds == 0) // timeout |
| 386 |
{ |
{ |
| 387 |
log_error("select() error (%d) !\n", result); |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 388 |
loop = 0; |
{ |
| 389 |
|
break; |
| 390 |
|
} |
| 391 |
|
continue; |
| 392 |
} |
} |
| 393 |
if (result > 0) |
|
| 394 |
|
for (int i = 0; i < nfds; i++) |
| 395 |
{ |
{ |
| 396 |
if (FD_ISSET(0, &testfds)) |
if (events[i].data.fd == STDIN_FILENO || stdin_read_wait) |
| 397 |
|
{ |
| 398 |
|
stdin_read_wait = 1; |
| 399 |
|
while (input_buf_len < sizeof(input_buf) && !SYS_server_exit) |
| 400 |
|
{ |
| 401 |
|
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_std("read(STDIN) EOF\n"); |
| 423 |
|
stdin_read_wait = 0; |
| 424 |
|
loop = 0; |
| 425 |
|
break; |
| 426 |
|
} |
| 427 |
|
else |
| 428 |
|
{ |
| 429 |
|
input_buf_len += ret; |
| 430 |
|
BBS_last_access_tm = time(0); |
| 431 |
|
continue; |
| 432 |
|
} |
| 433 |
|
} |
| 434 |
|
} |
| 435 |
|
|
| 436 |
|
if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT |
| 437 |
|
{ |
| 438 |
|
sock_write_wait = 1; |
| 439 |
|
while (input_buf_offset < input_buf_len && !SYS_server_exit) |
| 440 |
|
{ |
| 441 |
|
ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); |
| 442 |
|
if (ret < 0) |
| 443 |
|
{ |
| 444 |
|
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; |
| 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 |
|
} |
| 478 |
|
} |
| 479 |
|
} |
| 480 |
|
|
| 481 |
|
if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN |
| 482 |
{ |
{ |
| 483 |
len = read(0, buf, 255); |
sock_read_wait = 1; |
| 484 |
if (len == 0) |
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) |
| 485 |
{ |
{ |
| 486 |
loop = 0; |
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_std("read(socket) EOF\n"); |
| 508 |
|
sock_read_wait = 0; |
| 509 |
|
loop = 0; |
| 510 |
|
break; |
| 511 |
|
} |
| 512 |
|
else |
| 513 |
|
{ |
| 514 |
|
output_buf_len += ret; |
| 515 |
|
continue; |
| 516 |
|
} |
| 517 |
} |
} |
|
write(sock, buf, len); |
|
| 518 |
} |
} |
| 519 |
if (FD_ISSET(sock, &testfds)) |
|
| 520 |
|
if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait) |
| 521 |
{ |
{ |
| 522 |
len = read(sock, buf, 255); |
stdout_write_wait = 1; |
| 523 |
if (len == 0) |
while (output_buf_offset < output_buf_len && !SYS_server_exit) |
| 524 |
{ |
{ |
| 525 |
loop = 0; |
ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); |
| 526 |
|
if (ret < 0) |
| 527 |
|
{ |
| 528 |
|
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; |
| 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 |
|
} |
| 562 |
} |
} |
|
write(1, buf, len); |
|
| 563 |
} |
} |
|
BBS_last_access_tm = time(0); |
|
| 564 |
} |
} |
| 565 |
} |
} |
| 566 |
|
|
| 567 |
|
// Restore STDIN/STDOUT flags |
| 568 |
|
fcntl(STDIN_FILENO, F_SETFL, flags_stdin); |
| 569 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); |
| 570 |
|
|
| 571 |
|
// Restore socket flags |
| 572 |
|
fcntl(sock, F_SETFL, flags_sock); |
| 573 |
|
|
| 574 |
if (close(sock) == -1) |
if (close(sock) == -1) |
| 575 |
{ |
{ |
| 576 |
log_error("Close socket failed\n"); |
log_error("Close socket failed\n"); |
| 589 |
static int |
static int |
| 590 |
bbsnet_refresh() |
bbsnet_refresh() |
| 591 |
{ |
{ |
|
int i; |
|
|
|
|
| 592 |
clearscr(); |
clearscr(); |
| 593 |
moveto(1, 0); |
moveto(1, 0); |
| 594 |
prints("╭═════════════════════════════════════════════════════════════════════════════╮"); |
prints(" ----------------------------------------------------------------------------- "); |
| 595 |
for (i = 2; i < 19; i++) |
for (int i = 2; i < 19; i++) |
| 596 |
{ |
{ |
| 597 |
moveto(i, 0); |
moveto(i, 0); |
| 598 |
prints("║"); |
prints("|"); |
| 599 |
moveto(i, 79); |
moveto(i, 79); |
| 600 |
prints("║"); |
prints("|"); |
| 601 |
} |
} |
| 602 |
moveto(19, 0); |
moveto(19, 0); |
| 603 |
prints("║—————————————————————————————————————————————————————————————————————————————║"); |
prints("|-----------------------------------------------------------------------------|"); |
| 604 |
moveto(22, 0); |
moveto(22, 0); |
| 605 |
prints("╰═════════════════════════════════════════════════════════════════════════════╯"); |
prints(" ----------------------------------------------------------------------------- "); |
| 606 |
moveto(23, 0); |
moveto(23, 0); |
| 607 |
prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); |
prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); |
| 608 |
|
|
| 615 |
{ |
{ |
| 616 |
moveto(20, 0); |
moveto(20, 0); |
| 617 |
clrtoeol(); |
clrtoeol(); |
| 618 |
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", |
| 619 |
bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1); |
bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1); |
| 620 |
moveto(20, 79); |
moveto(20, 79); |
| 621 |
prints("║"); |
prints("|"); |
| 622 |
moveto(21, 0); |
moveto(21, 0); |
| 623 |
clrtoeol(); |
clrtoeol(); |
| 624 |
prints("║\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip); |
prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip); |
| 625 |
if (bbsnet_conf[new_pos].port != 23) |
if (bbsnet_conf[new_pos].port != 23) |
| 626 |
|
{ |
| 627 |
prints(" %d", bbsnet_conf[new_pos].port); |
prints(" %d", bbsnet_conf[new_pos].port); |
| 628 |
|
} |
| 629 |
prints("\x1b[m"); |
prints("\x1b[m"); |
| 630 |
moveto(21, 79); |
moveto(21, 79); |
| 631 |
prints("║"); |
prints("|"); |
| 632 |
iflush(); |
iflush(); |
| 633 |
|
|
| 634 |
return 0; |
return 0; |
| 648 |
display_menu(get_menu(&bbsnet_menu, "BBSNET")); |
display_menu(get_menu(&bbsnet_menu, "BBSNET")); |
| 649 |
bbsnet_selchange(pos); |
bbsnet_selchange(pos); |
| 650 |
|
|
| 651 |
while (1) |
while (!SYS_server_exit) |
| 652 |
{ |
{ |
| 653 |
ch = igetch(0); |
ch = igetch(0); |
| 654 |
switch (ch) |
switch (ch) |
| 655 |
{ |
{ |
| 656 |
case KEY_NULL: |
case KEY_NULL: // broken pipe |
| 657 |
case Ctrl('C'): |
case Ctrl('C'): // user cancel |
| 658 |
return 0; |
return 0; |
| 659 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 660 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 661 |
{ |
{ |
| 662 |
return -1; |
return 0; |
| 663 |
} |
} |
| 664 |
continue; |
continue; |
| 665 |
case CR: |
case CR: |
| 671 |
break; |
break; |
| 672 |
case KEY_UP: |
case KEY_UP: |
| 673 |
for (i = 0; i < STATION_PER_LINE; i++) |
for (i = 0; i < STATION_PER_LINE; i++) |
| 674 |
|
{ |
| 675 |
menu_control(&bbsnet_menu, KEY_UP); |
menu_control(&bbsnet_menu, KEY_UP); |
| 676 |
|
} |
| 677 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
| 678 |
bbsnet_selchange(pos); |
bbsnet_selchange(pos); |
| 679 |
break; |
break; |
| 680 |
case KEY_DOWN: |
case KEY_DOWN: |
| 681 |
for (i = 0; i < STATION_PER_LINE; i++) |
for (i = 0; i < STATION_PER_LINE; i++) |
| 682 |
|
{ |
| 683 |
menu_control(&bbsnet_menu, KEY_DOWN); |
menu_control(&bbsnet_menu, KEY_DOWN); |
| 684 |
|
} |
| 685 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
| 686 |
bbsnet_selchange(pos); |
bbsnet_selchange(pos); |
| 687 |
break; |
break; |