| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
bbs_net.c - description |
/* |
| 3 |
------------------- |
* bbs_net |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - user interactive feature of site shuttle |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "bbs.h" |
#include "bbs.h" |
| 10 |
#include "common.h" |
#include "common.h" |
| 23 |
#include <time.h> |
#include <time.h> |
| 24 |
#include <unistd.h> |
#include <unistd.h> |
| 25 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
|
#include <iconv.h> |
|
| 26 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 27 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 28 |
#include <libssh/callbacks.h> |
#include <libssh/callbacks.h> |
| 33 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 34 |
#include <sys/epoll.h> |
#include <sys/epoll.h> |
| 35 |
|
|
| 36 |
#define MENU_CONF_DELIM " \t\r\n" |
static const char MENU_CONF_DELIM[] = " \t\r\n"; |
| 37 |
|
|
| 38 |
#define MAX_PROCESS_BAR_LEN 30 |
enum _bbs_net_constant_t |
| 39 |
#define MAXSTATION 26 * 2 |
{ |
| 40 |
#define STATION_PER_LINE 4 |
MAX_PROCESS_BAR_LEN = 30, |
| 41 |
|
MAXSTATION = 26 * 2, |
| 42 |
|
STATION_PER_LINE = 4, |
| 43 |
|
}; |
| 44 |
|
|
| 45 |
struct _bbsnet_conf |
struct _bbsnet_conf |
| 46 |
{ |
{ |
| 48 |
char host2[40]; |
char host2[40]; |
| 49 |
char ip[40]; |
char ip[40]; |
| 50 |
in_port_t port; |
in_port_t port; |
| 51 |
char charset[20]; |
char charset[CHARSET_MAX_LEN + 1]; |
| 52 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 53 |
|
|
| 54 |
MENU_SET bbsnet_menu; |
static MENU_SET bbsnet_menu; |
| 55 |
|
|
| 56 |
int load_bbsnet_conf(const char *file_config) |
static int load_bbsnet_conf(const char *file_config) |
| 57 |
{ |
{ |
| 58 |
FILE *fp; |
FILE *fp; |
| 59 |
MENU *p_menu; |
MENU *p_menu; |
| 148 |
return 0; |
return 0; |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
void unload_bbsnet_conf(void) |
static void unload_bbsnet_conf(void) |
| 152 |
{ |
{ |
| 153 |
bbsnet_menu.menu_count = 0; |
bbsnet_menu.menu_count = 0; |
| 154 |
bbsnet_menu.menu_item_count = 0; |
bbsnet_menu.menu_item_count = 0; |
| 159 |
bbsnet_menu.p_menu_item_pool = NULL; |
bbsnet_menu.p_menu_item_pool = NULL; |
| 160 |
} |
} |
| 161 |
|
|
| 162 |
void process_bar(int n, int len) |
static void process_bar(int n, int len) |
| 163 |
{ |
{ |
| 164 |
char buf[LINE_BUFFER_LEN]; |
char buf[LINE_BUFFER_LEN]; |
| 165 |
char buf2[LINE_BUFFER_LEN]; |
char buf2[LINE_BUFFER_LEN]; |
| 180 |
moveto(4, 0); |
moveto(4, 0); |
| 181 |
prints(" ------------------------------ \r\n"); |
prints(" ------------------------------ \r\n"); |
| 182 |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
| 183 |
strncpy(buf2, buf, (size_t)n); |
memcpy(buf2, buf, (size_t)n); |
| 184 |
buf2[n] = '\0'; |
buf2[n] = '\0'; |
| 185 |
prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n); |
prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n); |
| 186 |
prints(" ------------------------------ \r\n"); |
prints(" ------------------------------ \r\n"); |
| 187 |
iflush(); |
iflush(); |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
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) |
static int bbsnet_connect(int n) |
|
{ |
|
|
char *in_buf; |
|
|
char *out_buf; |
|
|
size_t in_bytes; |
|
|
size_t out_bytes; |
|
|
int ret; |
|
|
|
|
|
in_buf = p_buf + *p_buf_offset; |
|
|
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
|
|
out_buf = p_conv + *p_conv_len; |
|
|
out_bytes = conv_size - (size_t)(*p_conv_len); |
|
|
|
|
|
while (in_bytes > 0) |
|
|
{ |
|
|
ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes); |
|
|
if (ret == -1) |
|
|
{ |
|
|
if (errno == EINVAL) // Incomplete |
|
|
{ |
|
|
#ifdef _DEBUG |
|
|
log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes); |
|
|
#endif |
|
|
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf); |
|
|
*p_buf_offset = 0; |
|
|
*p_conv_len = (int)(conv_size - out_bytes); |
|
|
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
|
|
|
|
|
break; |
|
|
} |
|
|
else if (errno == E2BIG) |
|
|
{ |
|
|
log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); |
|
|
return -1; |
|
|
} |
|
|
else if (errno == EILSEQ) |
|
|
{ |
|
|
if (in_bytes > out_bytes || out_bytes <= 0) |
|
|
{ |
|
|
log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); |
|
|
return -2; |
|
|
} |
|
|
|
|
|
*out_buf = *in_buf; |
|
|
in_buf++; |
|
|
out_buf++; |
|
|
in_bytes--; |
|
|
out_bytes--; |
|
|
|
|
|
continue; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
*p_buf_len = 0; |
|
|
*p_buf_offset = 0; |
|
|
*p_conv_len = (int)(conv_size - out_bytes); |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
int bbsnet_connect(int n) |
|
| 191 |
{ |
{ |
| 192 |
int sock, ret, loop, error; |
int sock, ret, loop, error; |
| 193 |
int sock_connected = 0; |
int sock_connected = 0; |
| 202 |
int output_buf_len = 0; |
int output_buf_len = 0; |
| 203 |
int input_buf_offset = 0; |
int input_buf_offset = 0; |
| 204 |
int output_buf_offset = 0; |
int output_buf_offset = 0; |
|
iconv_t input_cd = NULL; |
|
| 205 |
char input_conv[LINE_BUFFER_LEN * 2]; |
char input_conv[LINE_BUFFER_LEN * 2]; |
| 206 |
char output_conv[LINE_BUFFER_LEN * 2]; |
char output_conv[LINE_BUFFER_LEN * 2]; |
| 207 |
int input_conv_len = 0; |
int input_conv_len = 0; |
| 208 |
int output_conv_len = 0; |
int output_conv_len = 0; |
| 209 |
int input_conv_offset = 0; |
int input_conv_offset = 0; |
| 210 |
int output_conv_offset = 0; |
int output_conv_offset = 0; |
| 211 |
|
iconv_t input_cd = NULL; |
| 212 |
iconv_t output_cd = NULL; |
iconv_t output_cd = NULL; |
| 213 |
|
char tocode[32]; |
| 214 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 215 |
int nfds, epollfd; |
int nfds, epollfd; |
| 216 |
int stdin_read_wait = 0; |
int stdin_read_wait = 0; |
| 221 |
int tos; |
int tos; |
| 222 |
char remote_addr[IP_ADDR_LEN]; |
char remote_addr[IP_ADDR_LEN]; |
| 223 |
int remote_port; |
int remote_port; |
| 224 |
|
char local_addr[IP_ADDR_LEN]; |
| 225 |
|
int local_port; |
| 226 |
|
socklen_t sock_len; |
| 227 |
time_t t_used = time(NULL); |
time_t t_used = time(NULL); |
| 228 |
struct tm *tm_used; |
struct tm *tm_used; |
| 229 |
int ch; |
int ch; |
| 403 |
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
| 404 |
} |
} |
| 405 |
|
|
| 406 |
|
sock_len = sizeof(sin); |
| 407 |
|
if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0) |
| 408 |
|
{ |
| 409 |
|
log_error("getsockname() error: %d", errno); |
| 410 |
|
goto cleanup; |
| 411 |
|
} |
| 412 |
|
|
| 413 |
|
strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1); |
| 414 |
|
local_addr[sizeof(local_addr) - 1] = '\0'; |
| 415 |
|
local_port = ntohs(sin.sin_port); |
| 416 |
|
|
| 417 |
prints("\033[1;31m连接成功!\033[m\r\n"); |
prints("\033[1;31m连接成功!\033[m\r\n"); |
| 418 |
iflush(); |
iflush(); |
| 419 |
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", |
| 420 |
|
remote_addr, remote_port, local_addr, local_port, BBS_username); |
| 421 |
|
|
| 422 |
input_cd = iconv_open(bbsnet_conf[n].charset, "UTF-8"); |
snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset, |
| 423 |
|
(strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE")); |
| 424 |
|
input_cd = iconv_open(tocode, stdio_charset); |
| 425 |
if (input_cd == (iconv_t)(-1)) |
if (input_cd == (iconv_t)(-1)) |
| 426 |
{ |
{ |
| 427 |
log_error("iconv_open(UTF8->GBK) error: %d\n", errno); |
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); |
| 428 |
goto cleanup; |
goto cleanup; |
| 429 |
} |
} |
| 430 |
output_cd = iconv_open("UTF-8", bbsnet_conf[n].charset); |
|
| 431 |
if (input_cd == (iconv_t)(-1)) |
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
| 432 |
|
(strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
| 433 |
|
output_cd = iconv_open(tocode, bbsnet_conf[n].charset); |
| 434 |
|
if (output_cd == (iconv_t)(-1)) |
| 435 |
{ |
{ |
| 436 |
log_error("iconv_open(GBK->UTF-8) error: %d\n", errno); |
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno); |
| 437 |
iconv_close(input_cd); |
iconv_close(input_cd); |
| 438 |
goto cleanup; |
goto cleanup; |
| 439 |
} |
} |
| 479 |
} |
} |
| 480 |
else if (nfds == 0) // timeout |
else if (nfds == 0) // timeout |
| 481 |
{ |
{ |
| 482 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 483 |
{ |
{ |
| 484 |
break; |
break; |
| 485 |
} |
} |
| 531 |
} |
} |
| 532 |
else if (ret == 0) |
else if (ret == 0) |
| 533 |
{ |
{ |
| 534 |
|
// Send NO-OP to remote server |
| 535 |
|
input_buf[input_buf_len] = '\0'; |
| 536 |
|
input_buf_len++; |
| 537 |
|
BBS_last_access_tm = time(NULL); |
| 538 |
|
|
| 539 |
stdin_read_wait = 0; |
stdin_read_wait = 0; |
| 540 |
break; // Check whether channel is still open |
break; // Check whether channel is still open |
| 541 |
} |
} |
| 591 |
{ |
{ |
| 592 |
if (input_buf_offset < input_buf_len) |
if (input_buf_offset < input_buf_len) |
| 593 |
{ |
{ |
| 594 |
ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); |
// For debug |
| 595 |
|
#ifdef _DEBUG |
| 596 |
|
for (int j = input_buf_offset; j < input_buf_len; j++) |
| 597 |
|
{ |
| 598 |
|
log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256); |
| 599 |
|
} |
| 600 |
|
#endif |
| 601 |
|
|
| 602 |
|
ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); |
| 603 |
if (ret < 0) |
if (ret < 0) |
| 604 |
{ |
{ |
| 605 |
log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); |
log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); |
| 606 |
|
input_buf_len = input_buf_offset; // Discard invalid sequence |
| 607 |
} |
} |
| 608 |
|
|
| 609 |
|
// For debug |
| 610 |
|
#ifdef _DEBUG |
| 611 |
|
for (int j = input_conv_offset; j < input_conv_len; j++) |
| 612 |
|
{ |
| 613 |
|
log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256); |
| 614 |
|
} |
| 615 |
|
#endif |
| 616 |
} |
} |
| 617 |
|
|
| 618 |
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
| 703 |
{ |
{ |
| 704 |
if (output_buf_offset < output_buf_len) |
if (output_buf_offset < output_buf_len) |
| 705 |
{ |
{ |
| 706 |
ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len); |
ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len); |
| 707 |
if (ret < 0) |
if (ret < 0) |
| 708 |
{ |
{ |
| 709 |
log_error("bbsnet_io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); |
log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); |
| 710 |
|
output_buf_len = output_buf_offset; // Discard invalid sequence |
| 711 |
} |
} |
| 712 |
} |
} |
| 713 |
|
|
| 797 |
tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, |
tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, |
| 798 |
tm_used->tm_sec); |
tm_used->tm_sec); |
| 799 |
|
|
| 800 |
|
BBS_last_access_tm = time(NULL); |
| 801 |
|
|
| 802 |
return 0; |
return 0; |
| 803 |
} |
} |
| 804 |
|
|
| 805 |
static int |
static int bbsnet_refresh() |
|
bbsnet_refresh() |
|
| 806 |
{ |
{ |
| 807 |
clearscr(); |
clearscr(); |
| 808 |
moveto(1, 0); |
moveto(1, 0); |
| 826 |
return 0; |
return 0; |
| 827 |
} |
} |
| 828 |
|
|
| 829 |
int bbsnet_selchange() |
static int bbsnet_selchange() |
| 830 |
{ |
{ |
| 831 |
int i = bbsnet_menu.menu_item_pos[0]; |
int i = bbsnet_menu.menu_item_pos[0]; |
| 832 |
|
|
| 851 |
return 0; |
return 0; |
| 852 |
} |
} |
| 853 |
|
|
| 854 |
int bbs_net() |
extern int bbs_net() |
| 855 |
{ |
{ |
| 856 |
int ch, i; |
int ch, i; |
| 857 |
|
|
| 858 |
load_bbsnet_conf(CONF_BBSNET); |
load_bbsnet_conf(CONF_BBSNET); |
| 859 |
|
|
|
BBS_last_access_tm = time(NULL); |
|
|
|
|
| 860 |
clearscr(); |
clearscr(); |
| 861 |
bbsnet_refresh(); |
bbsnet_refresh(); |
| 862 |
display_menu(&bbsnet_menu); |
display_menu(&bbsnet_menu); |
| 866 |
{ |
{ |
| 867 |
ch = igetch(100); |
ch = igetch(100); |
| 868 |
|
|
| 869 |
|
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 870 |
|
{ |
| 871 |
|
BBS_last_access_tm = time(NULL); |
| 872 |
|
} |
| 873 |
|
|
| 874 |
switch (ch) |
switch (ch) |
| 875 |
{ |
{ |
| 876 |
case KEY_NULL: // broken pipe |
case KEY_NULL: // broken pipe |
| 877 |
case KEY_ESC: |
log_error("KEY_NULL\n"); |
|
case Ctrl('C'): // user cancel |
|
| 878 |
goto cleanup; |
goto cleanup; |
| 879 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 880 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 881 |
{ |
{ |
| 882 |
|
log_error("User input timeout\n"); |
| 883 |
goto cleanup; |
goto cleanup; |
| 884 |
} |
} |
| 885 |
continue; |
continue; |
| 886 |
|
case KEY_ESC: |
| 887 |
|
case Ctrl('C'): // user cancel |
| 888 |
|
goto cleanup; |
| 889 |
case CR: |
case CR: |
|
igetch_reset(); |
|
| 890 |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]); |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]); |
| 891 |
bbsnet_refresh(); |
bbsnet_refresh(); |
| 892 |
display_menu(&bbsnet_menu); |
display_menu(&bbsnet_menu); |
| 929 |
bbsnet_selchange(); |
bbsnet_selchange(); |
| 930 |
break; |
break; |
| 931 |
} |
} |
|
BBS_last_access_tm = time(NULL); |
|
| 932 |
} |
} |
| 933 |
|
|
| 934 |
cleanup: |
cleanup: |