| 1 |
/***************************************************************************
|
| 2 |
bbs_net.c - description
|
| 3 |
-------------------
|
| 4 |
Copyright : (C) 2004-2025 by Leaflet
|
| 5 |
Email : leaflet@leafok.com
|
| 6 |
***************************************************************************/
|
| 7 |
|
| 8 |
/***************************************************************************
|
| 9 |
* *
|
| 10 |
* 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 *
|
| 12 |
* the Free Software Foundation; either version 3 of the License, or *
|
| 13 |
* (at your option) any later version. *
|
| 14 |
* *
|
| 15 |
***************************************************************************/
|
| 16 |
|
| 17 |
#include "bbs.h"
|
| 18 |
#include "common.h"
|
| 19 |
#include "io.h"
|
| 20 |
#include "log.h"
|
| 21 |
#include "login.h"
|
| 22 |
#include "menu.h"
|
| 23 |
#include "screen.h"
|
| 24 |
#include <errno.h>
|
| 25 |
#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>
|
| 32 |
#include <unistd.h>
|
| 33 |
#include <arpa/inet.h>
|
| 34 |
#include <iconv.h>
|
| 35 |
#include <libssh/libssh.h>
|
| 36 |
#include <libssh/server.h>
|
| 37 |
#include <libssh/callbacks.h>
|
| 38 |
#include <netinet/in.h>
|
| 39 |
#include <netinet/ip.h>
|
| 40 |
#include <sys/select.h>
|
| 41 |
#include <sys/ioctl.h>
|
| 42 |
#include <sys/socket.h>
|
| 43 |
#include <sys/epoll.h>
|
| 44 |
|
| 45 |
#define MENU_CONF_DELIM " \t\r\n"
|
| 46 |
|
| 47 |
#define MAX_PROCESS_BAR_LEN 30
|
| 48 |
#define MAXSTATION 26 * 2
|
| 49 |
#define STATION_PER_LINE 4
|
| 50 |
|
| 51 |
#define BBS_NET_DEFAULT_CHARSET "UTF-8"
|
| 52 |
|
| 53 |
struct _bbsnet_conf
|
| 54 |
{
|
| 55 |
char host1[20];
|
| 56 |
char host2[40];
|
| 57 |
char ip[40];
|
| 58 |
in_port_t port;
|
| 59 |
char charset[20];
|
| 60 |
} bbsnet_conf[MAXSTATION];
|
| 61 |
|
| 62 |
MENU_SET bbsnet_menu;
|
| 63 |
|
| 64 |
int load_bbsnet_conf(const char *file_config)
|
| 65 |
{
|
| 66 |
FILE *fp;
|
| 67 |
MENU *p_menu;
|
| 68 |
MENU_ITEM *p_menu_item;
|
| 69 |
MENU_ITEM_ID menu_item_id;
|
| 70 |
char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr;
|
| 71 |
|
| 72 |
fp = fopen(file_config, "r");
|
| 73 |
if (fp == NULL)
|
| 74 |
{
|
| 75 |
return -1;
|
| 76 |
}
|
| 77 |
|
| 78 |
bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));
|
| 79 |
if (bbsnet_menu.p_menu_pool == NULL)
|
| 80 |
{
|
| 81 |
log_error("calloc(p_menu_pool) error\n");
|
| 82 |
return -3;
|
| 83 |
}
|
| 84 |
bbsnet_menu.menu_count = 1;
|
| 85 |
|
| 86 |
bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));
|
| 87 |
if (bbsnet_menu.p_menu_item_pool == NULL)
|
| 88 |
{
|
| 89 |
log_error("calloc(p_menu_item_pool) error\n");
|
| 90 |
return -3;
|
| 91 |
}
|
| 92 |
bbsnet_menu.menu_item_count = MAXSTATION;
|
| 93 |
|
| 94 |
p_menu = (MENU *)get_menu_by_id(&bbsnet_menu, 0);
|
| 95 |
|
| 96 |
strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
|
| 97 |
p_menu->name[sizeof(p_menu->name) - 1] = '\0';
|
| 98 |
p_menu->title.show = 0;
|
| 99 |
p_menu->screen_show = 0;
|
| 100 |
|
| 101 |
menu_item_id = 0;
|
| 102 |
while (fgets(t, 255, fp) && menu_item_id < MAXSTATION)
|
| 103 |
{
|
| 104 |
t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);
|
| 105 |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
|
| 106 |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
|
| 107 |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
|
| 108 |
t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
|
| 109 |
|
| 110 |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*')
|
| 111 |
{
|
| 112 |
continue;
|
| 113 |
}
|
| 114 |
|
| 115 |
strncpy(bbsnet_conf[menu_item_id].host1, t2, sizeof(bbsnet_conf[menu_item_id].host1) - 1);
|
| 116 |
bbsnet_conf[menu_item_id].host1[sizeof(bbsnet_conf[menu_item_id].host1) - 1] = '\0';
|
| 117 |
strncpy(bbsnet_conf[menu_item_id].host2, t1, sizeof(bbsnet_conf[menu_item_id].host2) - 1);
|
| 118 |
bbsnet_conf[menu_item_id].host2[sizeof(bbsnet_conf[menu_item_id].host2) - 1] = '\0';
|
| 119 |
strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);
|
| 120 |
bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';
|
| 121 |
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);
|
| 122 |
strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
|
| 123 |
bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
|
| 124 |
|
| 125 |
p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
|
| 126 |
if (p_menu_item == NULL)
|
| 127 |
{
|
| 128 |
log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);
|
| 129 |
return -1;
|
| 130 |
}
|
| 131 |
|
| 132 |
p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE);
|
| 133 |
p_menu_item->col = (int16_t)(5 + menu_item_id % STATION_PER_LINE * 20);
|
| 134 |
snprintf(p_menu_item->action, sizeof(p_menu_item->action), "%d", (int16_t)menu_item_id);
|
| 135 |
p_menu_item->submenu = 0;
|
| 136 |
p_menu_item->priv = 0;
|
| 137 |
p_menu_item->level = 0;
|
| 138 |
p_menu_item->name[0] =
|
| 139 |
(char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id);
|
| 140 |
p_menu_item->name[1] = '\0';
|
| 141 |
snprintf(p_menu_item->text, sizeof(p_menu_item->text), "[1;36m%c.[m %s",
|
| 142 |
p_menu_item->name[0], bbsnet_conf[menu_item_id].host1);
|
| 143 |
|
| 144 |
p_menu->items[p_menu->item_count] = menu_item_id;
|
| 145 |
p_menu->item_count++;
|
| 146 |
menu_item_id++;
|
| 147 |
}
|
| 148 |
|
| 149 |
bbsnet_menu.menu_item_count = (int16_t)menu_item_id;
|
| 150 |
bbsnet_menu.menu_id_path[0] = 0;
|
| 151 |
bbsnet_menu.menu_item_pos[0] = 0;
|
| 152 |
bbsnet_menu.choose_step = 0;
|
| 153 |
|
| 154 |
fclose(fp);
|
| 155 |
|
| 156 |
return 0;
|
| 157 |
}
|
| 158 |
|
| 159 |
void unload_bbsnet_conf(void)
|
| 160 |
{
|
| 161 |
bbsnet_menu.menu_count = 0;
|
| 162 |
bbsnet_menu.menu_item_count = 0;
|
| 163 |
|
| 164 |
free(bbsnet_menu.p_menu_pool);
|
| 165 |
bbsnet_menu.p_menu_pool = NULL;
|
| 166 |
free(bbsnet_menu.p_menu_item_pool);
|
| 167 |
bbsnet_menu.p_menu_item_pool = NULL;
|
| 168 |
}
|
| 169 |
|
| 170 |
void process_bar(int n, int len)
|
| 171 |
{
|
| 172 |
char buf[LINE_BUFFER_LEN];
|
| 173 |
char buf2[LINE_BUFFER_LEN];
|
| 174 |
|
| 175 |
if (len > LINE_BUFFER_LEN)
|
| 176 |
{
|
| 177 |
len = LINE_BUFFER_LEN - 1;
|
| 178 |
}
|
| 179 |
if (n < 0)
|
| 180 |
{
|
| 181 |
n = 0;
|
| 182 |
}
|
| 183 |
else if (n > len)
|
| 184 |
{
|
| 185 |
n = len;
|
| 186 |
}
|
| 187 |
|
| 188 |
moveto(4, 0);
|
| 189 |
prints(" ------------------------------ \r\n");
|
| 190 |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len);
|
| 191 |
strncpy(buf2, buf, (size_t)n);
|
| 192 |
buf2[n] = '\0';
|
| 193 |
prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
|
| 194 |
prints(" ------------------------------ \r\n");
|
| 195 |
iflush();
|
| 196 |
}
|
| 197 |
|
| 198 |
int bbsnet_io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len)
|
| 199 |
{
|
| 200 |
char *in_buf;
|
| 201 |
char *out_buf;
|
| 202 |
size_t in_bytes;
|
| 203 |
size_t out_bytes;
|
| 204 |
int ret;
|
| 205 |
|
| 206 |
in_buf = p_buf + *p_buf_offset;
|
| 207 |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
|
| 208 |
out_buf = p_conv + *p_conv_len;
|
| 209 |
out_bytes = conv_size - (size_t)(*p_conv_len);
|
| 210 |
|
| 211 |
while (in_bytes > 0)
|
| 212 |
{
|
| 213 |
ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
|
| 214 |
if (ret == -1)
|
| 215 |
{
|
| 216 |
if (errno == EINVAL) // Incomplete
|
| 217 |
{
|
| 218 |
#ifdef _DEBUG
|
| 219 |
log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes);
|
| 220 |
#endif
|
| 221 |
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
|
| 222 |
*p_buf_offset = 0;
|
| 223 |
*p_conv_len = (int)(conv_size - out_bytes);
|
| 224 |
memmove(p_buf, in_buf, (size_t)(*p_buf_len));
|
| 225 |
|
| 226 |
break;
|
| 227 |
}
|
| 228 |
else if (errno == E2BIG)
|
| 229 |
{
|
| 230 |
log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
|
| 231 |
return -1;
|
| 232 |
}
|
| 233 |
else if (errno == EILSEQ)
|
| 234 |
{
|
| 235 |
if (in_bytes > out_bytes || out_bytes <= 0)
|
| 236 |
{
|
| 237 |
log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
|
| 238 |
return -2;
|
| 239 |
}
|
| 240 |
|
| 241 |
*out_buf = *in_buf;
|
| 242 |
in_buf++;
|
| 243 |
out_buf++;
|
| 244 |
in_bytes--;
|
| 245 |
out_bytes--;
|
| 246 |
|
| 247 |
continue;
|
| 248 |
}
|
| 249 |
}
|
| 250 |
else
|
| 251 |
{
|
| 252 |
*p_buf_len = 0;
|
| 253 |
*p_buf_offset = 0;
|
| 254 |
*p_conv_len = (int)(conv_size - out_bytes);
|
| 255 |
|
| 256 |
break;
|
| 257 |
}
|
| 258 |
}
|
| 259 |
|
| 260 |
return 0;
|
| 261 |
}
|
| 262 |
|
| 263 |
int bbsnet_connect(int n)
|
| 264 |
{
|
| 265 |
int sock, ret, loop, error;
|
| 266 |
int sock_connected = 0;
|
| 267 |
int flags_sock;
|
| 268 |
int flags_stdin;
|
| 269 |
int flags_stdout;
|
| 270 |
int len;
|
| 271 |
struct sockaddr_in sin;
|
| 272 |
char input_buf[LINE_BUFFER_LEN];
|
| 273 |
char output_buf[LINE_BUFFER_LEN];
|
| 274 |
int input_buf_len = 0;
|
| 275 |
int output_buf_len = 0;
|
| 276 |
int input_buf_offset = 0;
|
| 277 |
int output_buf_offset = 0;
|
| 278 |
iconv_t input_cd = NULL;
|
| 279 |
char input_conv[LINE_BUFFER_LEN * 2];
|
| 280 |
char output_conv[LINE_BUFFER_LEN * 2];
|
| 281 |
int input_conv_len = 0;
|
| 282 |
int output_conv_len = 0;
|
| 283 |
int input_conv_offset = 0;
|
| 284 |
int output_conv_offset = 0;
|
| 285 |
iconv_t output_cd = NULL;
|
| 286 |
struct epoll_event ev, events[MAX_EVENTS];
|
| 287 |
int nfds, epollfd;
|
| 288 |
int stdin_read_wait = 0;
|
| 289 |
int stdout_write_wait = 0;
|
| 290 |
int sock_read_wait = 0;
|
| 291 |
int sock_write_wait = 0;
|
| 292 |
struct hostent *p_host = NULL;
|
| 293 |
int tos;
|
| 294 |
char remote_addr[IP_ADDR_LEN];
|
| 295 |
int remote_port;
|
| 296 |
char local_addr[IP_ADDR_LEN];
|
| 297 |
int local_port;
|
| 298 |
socklen_t sock_len;
|
| 299 |
time_t t_used = time(NULL);
|
| 300 |
struct tm *tm_used;
|
| 301 |
int ch;
|
| 302 |
|
| 303 |
if (user_online_update("BBS_NET") < 0)
|
| 304 |
{
|
| 305 |
log_error("user_online_update(BBS_NET) error\n");
|
| 306 |
}
|
| 307 |
|
| 308 |
clearscr();
|
| 309 |
|
| 310 |
moveto(0, 0);
|
| 311 |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
|
| 312 |
bbsnet_conf[n].host1, bbsnet_conf[n].ip);
|
| 313 |
iflush();
|
| 314 |
|
| 315 |
p_host = gethostbyname(bbsnet_conf[n].ip);
|
| 316 |
|
| 317 |
if (p_host == NULL)
|
| 318 |
{
|
| 319 |
prints("\033[1;31m查找主机名失败!\033[m\r\n");
|
| 320 |
press_any_key();
|
| 321 |
return -1;
|
| 322 |
}
|
| 323 |
|
| 324 |
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
| 325 |
|
| 326 |
if (sock < 0)
|
| 327 |
{
|
| 328 |
prints("\033[1;31m无法创建socket!\033[m\r\n");
|
| 329 |
press_any_key();
|
| 330 |
return -1;
|
| 331 |
}
|
| 332 |
|
| 333 |
sin.sin_family = AF_INET;
|
| 334 |
sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY);
|
| 335 |
sin.sin_port = 0;
|
| 336 |
|
| 337 |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
|
| 338 |
{
|
| 339 |
log_error("Bind address %s:%u failed (%d)\n",
|
| 340 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
|
| 341 |
return -2;
|
| 342 |
}
|
| 343 |
|
| 344 |
memset(&sin, 0, sizeof(sin));
|
| 345 |
sin.sin_family = AF_INET;
|
| 346 |
sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
|
| 347 |
sin.sin_port = htons(bbsnet_conf[n].port);
|
| 348 |
|
| 349 |
strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);
|
| 350 |
remote_addr[sizeof(remote_addr) - 1] = '\0';
|
| 351 |
remote_port = ntohs(sin.sin_port);
|
| 352 |
|
| 353 |
prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
|
| 354 |
process_bar(0, MAX_PROCESS_BAR_LEN);
|
| 355 |
|
| 356 |
// Set socket as non-blocking
|
| 357 |
flags_sock = fcntl(sock, F_GETFL, 0);
|
| 358 |
fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);
|
| 359 |
|
| 360 |
// Set STDIN/STDOUT as non-blocking
|
| 361 |
flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);
|
| 362 |
flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);
|
| 363 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
|
| 364 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
|
| 365 |
|
| 366 |
epollfd = epoll_create1(0);
|
| 367 |
if (epollfd < 0)
|
| 368 |
{
|
| 369 |
log_error("epoll_create1() error (%d)\n", errno);
|
| 370 |
return -1;
|
| 371 |
}
|
| 372 |
|
| 373 |
ev.events = EPOLLOUT | EPOLLET;
|
| 374 |
ev.data.fd = sock;
|
| 375 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
|
| 376 |
{
|
| 377 |
log_error("epoll_ctl(socket) error (%d)\n", errno);
|
| 378 |
goto cleanup;
|
| 379 |
}
|
| 380 |
|
| 381 |
ev.events = EPOLLIN | EPOLLET;
|
| 382 |
ev.data.fd = STDIN_FILENO;
|
| 383 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
|
| 384 |
{
|
| 385 |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
|
| 386 |
goto cleanup;
|
| 387 |
}
|
| 388 |
|
| 389 |
while (!SYS_server_exit)
|
| 390 |
{
|
| 391 |
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
|
| 392 |
{
|
| 393 |
if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
|
| 394 |
{
|
| 395 |
// Use select / epoll to check writability of the socket,
|
| 396 |
// then use getsockopt to check the status of the socket.
|
| 397 |
// See man connect(2)
|
| 398 |
break;
|
| 399 |
}
|
| 400 |
else if (errno == EINTR)
|
| 401 |
{
|
| 402 |
continue;
|
| 403 |
}
|
| 404 |
else
|
| 405 |
{
|
| 406 |
log_error("connect(socket) error (%d)\n", errno);
|
| 407 |
|
| 408 |
prints("\033[1;31m连接失败!\033[m\r\n");
|
| 409 |
press_any_key();
|
| 410 |
|
| 411 |
goto cleanup;
|
| 412 |
}
|
| 413 |
}
|
| 414 |
}
|
| 415 |
|
| 416 |
for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)
|
| 417 |
{
|
| 418 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
|
| 419 |
|
| 420 |
if (nfds < 0)
|
| 421 |
{
|
| 422 |
if (errno != EINTR)
|
| 423 |
{
|
| 424 |
log_error("epoll_wait() error (%d)\n", errno);
|
| 425 |
break;
|
| 426 |
}
|
| 427 |
}
|
| 428 |
else if (nfds == 0) // timeout
|
| 429 |
{
|
| 430 |
process_bar(j + 1, MAX_PROCESS_BAR_LEN);
|
| 431 |
}
|
| 432 |
else // ret > 0
|
| 433 |
{
|
| 434 |
for (int i = 0; i < nfds; i++)
|
| 435 |
{
|
| 436 |
if (events[i].data.fd == sock)
|
| 437 |
{
|
| 438 |
len = sizeof(error);
|
| 439 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
|
| 440 |
{
|
| 441 |
log_error("getsockopt() error (%d) !\n", error);
|
| 442 |
goto cleanup;
|
| 443 |
}
|
| 444 |
if (error == 0)
|
| 445 |
{
|
| 446 |
sock_connected = 1;
|
| 447 |
}
|
| 448 |
}
|
| 449 |
else if (events[i].data.fd == STDIN_FILENO)
|
| 450 |
{
|
| 451 |
ch = igetch(0);
|
| 452 |
if (ch == Ctrl('C') || ch == KEY_ESC)
|
| 453 |
{
|
| 454 |
goto cleanup;
|
| 455 |
}
|
| 456 |
}
|
| 457 |
}
|
| 458 |
}
|
| 459 |
}
|
| 460 |
if (SYS_server_exit)
|
| 461 |
{
|
| 462 |
goto cleanup;
|
| 463 |
}
|
| 464 |
if (!sock_connected)
|
| 465 |
{
|
| 466 |
prints("\033[1;31m连接失败!\033[m\r\n");
|
| 467 |
press_any_key();
|
| 468 |
|
| 469 |
goto cleanup;
|
| 470 |
}
|
| 471 |
|
| 472 |
tos = IPTOS_LOWDELAY;
|
| 473 |
if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
|
| 474 |
{
|
| 475 |
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
|
| 476 |
}
|
| 477 |
|
| 478 |
sock_len = sizeof(sin);
|
| 479 |
if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
|
| 480 |
{
|
| 481 |
log_error("getsockname() error: %d", errno);
|
| 482 |
goto cleanup;
|
| 483 |
}
|
| 484 |
|
| 485 |
strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
|
| 486 |
local_addr[sizeof(local_addr) - 1] = '\0';
|
| 487 |
local_port = ntohs(sin.sin_port);
|
| 488 |
|
| 489 |
prints("\033[1;31m连接成功!\033[m\r\n");
|
| 490 |
iflush();
|
| 491 |
log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",
|
| 492 |
remote_addr, remote_port, local_addr, local_port, BBS_username);
|
| 493 |
|
| 494 |
input_cd = iconv_open(bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET);
|
| 495 |
if (input_cd == (iconv_t)(-1))
|
| 496 |
{
|
| 497 |
log_error("iconv_open(%s->%s) error: %d\n", BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset, errno);
|
| 498 |
goto cleanup;
|
| 499 |
}
|
| 500 |
output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset);
|
| 501 |
if (input_cd == (iconv_t)(-1))
|
| 502 |
{
|
| 503 |
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET, errno);
|
| 504 |
iconv_close(input_cd);
|
| 505 |
goto cleanup;
|
| 506 |
}
|
| 507 |
|
| 508 |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
|
| 509 |
ev.data.fd = sock;
|
| 510 |
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
|
| 511 |
{
|
| 512 |
log_error("epoll_ctl(socket) error (%d)\n", errno);
|
| 513 |
goto cleanup;
|
| 514 |
}
|
| 515 |
|
| 516 |
ev.events = EPOLLOUT | EPOLLET;
|
| 517 |
ev.data.fd = STDOUT_FILENO;
|
| 518 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
|
| 519 |
{
|
| 520 |
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
|
| 521 |
goto cleanup;
|
| 522 |
}
|
| 523 |
|
| 524 |
BBS_last_access_tm = t_used = time(NULL);
|
| 525 |
loop = 1;
|
| 526 |
|
| 527 |
while (loop && !SYS_server_exit)
|
| 528 |
{
|
| 529 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
|
| 530 |
{
|
| 531 |
log_error("SSH channel is closed\n");
|
| 532 |
loop = 0;
|
| 533 |
break;
|
| 534 |
}
|
| 535 |
|
| 536 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
|
| 537 |
|
| 538 |
if (nfds < 0)
|
| 539 |
{
|
| 540 |
if (errno != EINTR)
|
| 541 |
{
|
| 542 |
log_error("epoll_wait() error (%d)\n", errno);
|
| 543 |
break;
|
| 544 |
}
|
| 545 |
continue;
|
| 546 |
}
|
| 547 |
else if (nfds == 0) // timeout
|
| 548 |
{
|
| 549 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
|
| 550 |
{
|
| 551 |
break;
|
| 552 |
}
|
| 553 |
}
|
| 554 |
|
| 555 |
for (int i = 0; i < nfds; i++)
|
| 556 |
{
|
| 557 |
if (events[i].data.fd == STDIN_FILENO)
|
| 558 |
{
|
| 559 |
stdin_read_wait = 1;
|
| 560 |
}
|
| 561 |
|
| 562 |
if (events[i].data.fd == sock)
|
| 563 |
{
|
| 564 |
if (events[i].events & EPOLLIN)
|
| 565 |
{
|
| 566 |
sock_read_wait = 1;
|
| 567 |
}
|
| 568 |
if (events[i].events & EPOLLOUT)
|
| 569 |
{
|
| 570 |
sock_write_wait = 1;
|
| 571 |
}
|
| 572 |
}
|
| 573 |
|
| 574 |
if (events[i].data.fd == STDOUT_FILENO)
|
| 575 |
{
|
| 576 |
stdout_write_wait = 1;
|
| 577 |
}
|
| 578 |
}
|
| 579 |
|
| 580 |
if (stdin_read_wait)
|
| 581 |
{
|
| 582 |
while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
|
| 583 |
{
|
| 584 |
if (SSH_v2)
|
| 585 |
{
|
| 586 |
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
|
| 587 |
if (ret == SSH_ERROR)
|
| 588 |
{
|
| 589 |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
|
| 590 |
loop = 0;
|
| 591 |
break;
|
| 592 |
}
|
| 593 |
else if (ret == SSH_EOF)
|
| 594 |
{
|
| 595 |
stdin_read_wait = 0;
|
| 596 |
loop = 0;
|
| 597 |
break;
|
| 598 |
}
|
| 599 |
else if (ret == 0)
|
| 600 |
{
|
| 601 |
stdin_read_wait = 0;
|
| 602 |
break; // Check whether channel is still open
|
| 603 |
}
|
| 604 |
}
|
| 605 |
else
|
| 606 |
{
|
| 607 |
ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
|
| 608 |
}
|
| 609 |
if (ret < 0)
|
| 610 |
{
|
| 611 |
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
| 612 |
{
|
| 613 |
stdin_read_wait = 0;
|
| 614 |
break;
|
| 615 |
}
|
| 616 |
else if (errno == EINTR)
|
| 617 |
{
|
| 618 |
continue;
|
| 619 |
}
|
| 620 |
else
|
| 621 |
{
|
| 622 |
log_error("read(STDIN) error (%d)\n", errno);
|
| 623 |
loop = 0;
|
| 624 |
break;
|
| 625 |
}
|
| 626 |
}
|
| 627 |
else if (ret == 0) // broken pipe
|
| 628 |
{
|
| 629 |
#ifdef _DEBUG
|
| 630 |
log_error("read(STDIN) EOF\n");
|
| 631 |
#endif
|
| 632 |
stdin_read_wait = 0;
|
| 633 |
loop = 0;
|
| 634 |
break;
|
| 635 |
}
|
| 636 |
else
|
| 637 |
{
|
| 638 |
input_buf_len += ret;
|
| 639 |
BBS_last_access_tm = time(NULL);
|
| 640 |
|
| 641 |
// Refresh current action while user input
|
| 642 |
if (user_online_update("BBS_NET") < 0)
|
| 643 |
{
|
| 644 |
log_error("user_online_update(BBS_NET) error\n");
|
| 645 |
}
|
| 646 |
|
| 647 |
continue;
|
| 648 |
}
|
| 649 |
}
|
| 650 |
}
|
| 651 |
|
| 652 |
if (sock_write_wait)
|
| 653 |
{
|
| 654 |
if (input_buf_offset < input_buf_len)
|
| 655 |
{
|
| 656 |
ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
|
| 657 |
if (ret < 0)
|
| 658 |
{
|
| 659 |
log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
|
| 660 |
}
|
| 661 |
}
|
| 662 |
|
| 663 |
while (input_conv_offset < input_conv_len && !SYS_server_exit)
|
| 664 |
{
|
| 665 |
ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
|
| 666 |
if (ret < 0)
|
| 667 |
{
|
| 668 |
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
| 669 |
{
|
| 670 |
sock_write_wait = 0;
|
| 671 |
break;
|
| 672 |
}
|
| 673 |
else if (errno == EINTR)
|
| 674 |
{
|
| 675 |
continue;
|
| 676 |
}
|
| 677 |
else
|
| 678 |
{
|
| 679 |
log_error("write(socket) error (%d)\n", errno);
|
| 680 |
loop = 0;
|
| 681 |
break;
|
| 682 |
}
|
| 683 |
}
|
| 684 |
else if (ret == 0) // broken pipe
|
| 685 |
{
|
| 686 |
#ifdef _DEBUG
|
| 687 |
log_error("write(socket) EOF\n");
|
| 688 |
#endif
|
| 689 |
sock_write_wait = 0;
|
| 690 |
loop = 0;
|
| 691 |
break;
|
| 692 |
}
|
| 693 |
else
|
| 694 |
{
|
| 695 |
input_conv_offset += ret;
|
| 696 |
if (input_conv_offset >= input_conv_len) // Output buffer complete
|
| 697 |
{
|
| 698 |
input_conv_offset = 0;
|
| 699 |
input_conv_len = 0;
|
| 700 |
break;
|
| 701 |
}
|
| 702 |
continue;
|
| 703 |
}
|
| 704 |
}
|
| 705 |
}
|
| 706 |
|
| 707 |
if (sock_read_wait)
|
| 708 |
{
|
| 709 |
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
|
| 710 |
{
|
| 711 |
ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
|
| 712 |
if (ret < 0)
|
| 713 |
{
|
| 714 |
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
| 715 |
{
|
| 716 |
sock_read_wait = 0;
|
| 717 |
break;
|
| 718 |
}
|
| 719 |
else if (errno == EINTR)
|
| 720 |
{
|
| 721 |
continue;
|
| 722 |
}
|
| 723 |
else
|
| 724 |
{
|
| 725 |
log_error("read(socket) error (%d)\n", errno);
|
| 726 |
loop = 0;
|
| 727 |
break;
|
| 728 |
}
|
| 729 |
}
|
| 730 |
else if (ret == 0) // broken pipe
|
| 731 |
{
|
| 732 |
#ifdef _DEBUG
|
| 733 |
log_error("read(socket) EOF\n");
|
| 734 |
#endif
|
| 735 |
sock_read_wait = 0;
|
| 736 |
loop = 0;
|
| 737 |
break;
|
| 738 |
}
|
| 739 |
else
|
| 740 |
{
|
| 741 |
output_buf_len += ret;
|
| 742 |
continue;
|
| 743 |
}
|
| 744 |
}
|
| 745 |
}
|
| 746 |
|
| 747 |
if (stdout_write_wait)
|
| 748 |
{
|
| 749 |
if (output_buf_offset < output_buf_len)
|
| 750 |
{
|
| 751 |
ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
|
| 752 |
if (ret < 0)
|
| 753 |
{
|
| 754 |
log_error("bbsnet_io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
|
| 755 |
}
|
| 756 |
}
|
| 757 |
|
| 758 |
while (output_conv_offset < output_conv_len && !SYS_server_exit)
|
| 759 |
{
|
| 760 |
if (SSH_v2)
|
| 761 |
{
|
| 762 |
ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
|
| 763 |
if (ret == SSH_ERROR)
|
| 764 |
{
|
| 765 |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
|
| 766 |
loop = 0;
|
| 767 |
break;
|
| 768 |
}
|
| 769 |
}
|
| 770 |
else
|
| 771 |
{
|
| 772 |
ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
|
| 773 |
}
|
| 774 |
if (ret < 0)
|
| 775 |
{
|
| 776 |
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
| 777 |
{
|
| 778 |
stdout_write_wait = 0;
|
| 779 |
break;
|
| 780 |
}
|
| 781 |
else if (errno == EINTR)
|
| 782 |
{
|
| 783 |
continue;
|
| 784 |
}
|
| 785 |
else
|
| 786 |
{
|
| 787 |
log_error("write(STDOUT) error (%d)\n", errno);
|
| 788 |
loop = 0;
|
| 789 |
break;
|
| 790 |
}
|
| 791 |
}
|
| 792 |
else if (ret == 0) // broken pipe
|
| 793 |
{
|
| 794 |
#ifdef _DEBUG
|
| 795 |
log_error("write(STDOUT) EOF\n");
|
| 796 |
#endif
|
| 797 |
stdout_write_wait = 0;
|
| 798 |
loop = 0;
|
| 799 |
break;
|
| 800 |
}
|
| 801 |
else
|
| 802 |
{
|
| 803 |
output_conv_offset += ret;
|
| 804 |
if (output_conv_offset >= output_conv_len) // Output buffer complete
|
| 805 |
{
|
| 806 |
output_conv_offset = 0;
|
| 807 |
output_conv_len = 0;
|
| 808 |
break;
|
| 809 |
}
|
| 810 |
continue;
|
| 811 |
}
|
| 812 |
}
|
| 813 |
}
|
| 814 |
}
|
| 815 |
|
| 816 |
iconv_close(input_cd);
|
| 817 |
iconv_close(output_cd);
|
| 818 |
|
| 819 |
cleanup:
|
| 820 |
if (close(epollfd) < 0)
|
| 821 |
{
|
| 822 |
log_error("close(epoll) error (%d)\n");
|
| 823 |
}
|
| 824 |
|
| 825 |
// Restore STDIN/STDOUT flags
|
| 826 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
|
| 827 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
|
| 828 |
|
| 829 |
// Restore socket flags
|
| 830 |
fcntl(sock, F_SETFL, flags_sock);
|
| 831 |
|
| 832 |
if (close(sock) == -1)
|
| 833 |
{
|
| 834 |
log_error("Close socket failed\n");
|
| 835 |
}
|
| 836 |
|
| 837 |
t_used = time(NULL) - t_used;
|
| 838 |
tm_used = gmtime(&t_used);
|
| 839 |
|
| 840 |
log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
|
| 841 |
tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,
|
| 842 |
tm_used->tm_sec);
|
| 843 |
|
| 844 |
return 0;
|
| 845 |
}
|
| 846 |
|
| 847 |
static int
|
| 848 |
bbsnet_refresh()
|
| 849 |
{
|
| 850 |
clearscr();
|
| 851 |
moveto(1, 0);
|
| 852 |
prints(" ----------------------------------------------------------------------------- ");
|
| 853 |
for (int i = 2; i < 19; i++)
|
| 854 |
{
|
| 855 |
moveto(i, 0);
|
| 856 |
prints("|");
|
| 857 |
moveto(i, 79);
|
| 858 |
prints("|");
|
| 859 |
}
|
| 860 |
moveto(19, 0);
|
| 861 |
prints("|-----------------------------------------------------------------------------|");
|
| 862 |
moveto(22, 0);
|
| 863 |
prints(" ----------------------------------------------------------------------------- ");
|
| 864 |
moveto(23, 0);
|
| 865 |
prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");
|
| 866 |
|
| 867 |
iflush();
|
| 868 |
|
| 869 |
return 0;
|
| 870 |
}
|
| 871 |
|
| 872 |
int bbsnet_selchange()
|
| 873 |
{
|
| 874 |
int i = bbsnet_menu.menu_item_pos[0];
|
| 875 |
|
| 876 |
moveto(20, 0);
|
| 877 |
clrtoeol();
|
| 878 |
prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m 站名:\x1b[1;33m%s\x1b[m",
|
| 879 |
bbsnet_conf[i].host2, bbsnet_conf[i].host1);
|
| 880 |
moveto(20, 79);
|
| 881 |
prints("|");
|
| 882 |
moveto(21, 0);
|
| 883 |
clrtoeol();
|
| 884 |
prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
|
| 885 |
if (bbsnet_conf[i].port != 23)
|
| 886 |
{
|
| 887 |
prints(" %d", bbsnet_conf[i].port);
|
| 888 |
}
|
| 889 |
prints("\x1b[m");
|
| 890 |
moveto(21, 79);
|
| 891 |
prints("|");
|
| 892 |
iflush();
|
| 893 |
|
| 894 |
return 0;
|
| 895 |
}
|
| 896 |
|
| 897 |
int bbs_net()
|
| 898 |
{
|
| 899 |
int ch, i;
|
| 900 |
|
| 901 |
load_bbsnet_conf(CONF_BBSNET);
|
| 902 |
|
| 903 |
BBS_last_access_tm = time(NULL);
|
| 904 |
|
| 905 |
clearscr();
|
| 906 |
bbsnet_refresh();
|
| 907 |
display_menu(&bbsnet_menu);
|
| 908 |
bbsnet_selchange();
|
| 909 |
|
| 910 |
while (!SYS_server_exit)
|
| 911 |
{
|
| 912 |
ch = igetch(100);
|
| 913 |
|
| 914 |
switch (ch)
|
| 915 |
{
|
| 916 |
case KEY_NULL: // broken pipe
|
| 917 |
case KEY_ESC:
|
| 918 |
case Ctrl('C'): // user cancel
|
| 919 |
goto cleanup;
|
| 920 |
case KEY_TIMEOUT:
|
| 921 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
|
| 922 |
{
|
| 923 |
goto cleanup;
|
| 924 |
}
|
| 925 |
continue;
|
| 926 |
case CR:
|
| 927 |
igetch_reset();
|
| 928 |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
|
| 929 |
bbsnet_refresh();
|
| 930 |
display_menu(&bbsnet_menu);
|
| 931 |
bbsnet_selchange();
|
| 932 |
break;
|
| 933 |
case KEY_UP:
|
| 934 |
for (i = 0; i < STATION_PER_LINE; i++)
|
| 935 |
{
|
| 936 |
menu_control(&bbsnet_menu, KEY_UP);
|
| 937 |
}
|
| 938 |
bbsnet_selchange();
|
| 939 |
break;
|
| 940 |
case KEY_DOWN:
|
| 941 |
for (i = 0; i < STATION_PER_LINE; i++)
|
| 942 |
{
|
| 943 |
menu_control(&bbsnet_menu, KEY_DOWN);
|
| 944 |
}
|
| 945 |
bbsnet_selchange();
|
| 946 |
break;
|
| 947 |
case KEY_LEFT:
|
| 948 |
menu_control(&bbsnet_menu, KEY_UP);
|
| 949 |
bbsnet_selchange();
|
| 950 |
break;
|
| 951 |
case KEY_RIGHT:
|
| 952 |
menu_control(&bbsnet_menu, KEY_DOWN);
|
| 953 |
bbsnet_selchange();
|
| 954 |
break;
|
| 955 |
case KEY_HOME:
|
| 956 |
case KEY_PGUP:
|
| 957 |
menu_control(&bbsnet_menu, KEY_PGUP);
|
| 958 |
bbsnet_selchange();
|
| 959 |
break;
|
| 960 |
case KEY_END:
|
| 961 |
case KEY_PGDN:
|
| 962 |
menu_control(&bbsnet_menu, KEY_PGDN);
|
| 963 |
bbsnet_selchange();
|
| 964 |
break;
|
| 965 |
default:
|
| 966 |
menu_control(&bbsnet_menu, ch);
|
| 967 |
bbsnet_selchange();
|
| 968 |
break;
|
| 969 |
}
|
| 970 |
BBS_last_access_tm = time(NULL);
|
| 971 |
}
|
| 972 |
|
| 973 |
cleanup:
|
| 974 |
unload_bbsnet_conf();
|
| 975 |
|
| 976 |
return 0;
|
| 977 |
}
|