| 31 |
#include <time.h> |
#include <time.h> |
| 32 |
#include <unistd.h> |
#include <unistd.h> |
| 33 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
|
#include <iconv.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> |
| 47 |
#define MAXSTATION 26 * 2 |
#define MAXSTATION 26 * 2 |
| 48 |
#define STATION_PER_LINE 4 |
#define STATION_PER_LINE 4 |
| 49 |
|
|
|
#define BBS_NET_DEFAULT_CHARSET "UTF-8" |
|
|
|
|
| 50 |
struct _bbsnet_conf |
struct _bbsnet_conf |
| 51 |
{ |
{ |
| 52 |
char host1[20]; |
char host1[20]; |
| 56 |
char charset[20]; |
char charset[20]; |
| 57 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 58 |
|
|
| 59 |
MENU_SET bbsnet_menu; |
static MENU_SET bbsnet_menu; |
| 60 |
|
|
| 61 |
int load_bbsnet_conf(const char *file_config) |
static int load_bbsnet_conf(const char *file_config) |
| 62 |
{ |
{ |
| 63 |
FILE *fp; |
FILE *fp; |
| 64 |
MENU *p_menu; |
MENU *p_menu; |
| 153 |
return 0; |
return 0; |
| 154 |
} |
} |
| 155 |
|
|
| 156 |
void unload_bbsnet_conf(void) |
static void unload_bbsnet_conf(void) |
| 157 |
{ |
{ |
| 158 |
bbsnet_menu.menu_count = 0; |
bbsnet_menu.menu_count = 0; |
| 159 |
bbsnet_menu.menu_item_count = 0; |
bbsnet_menu.menu_item_count = 0; |
| 164 |
bbsnet_menu.p_menu_item_pool = NULL; |
bbsnet_menu.p_menu_item_pool = NULL; |
| 165 |
} |
} |
| 166 |
|
|
| 167 |
void process_bar(int n, int len) |
static void process_bar(int n, int len) |
| 168 |
{ |
{ |
| 169 |
char buf[LINE_BUFFER_LEN]; |
char buf[LINE_BUFFER_LEN]; |
| 170 |
char buf2[LINE_BUFFER_LEN]; |
char buf2[LINE_BUFFER_LEN]; |
| 185 |
moveto(4, 0); |
moveto(4, 0); |
| 186 |
prints(" ------------------------------ \r\n"); |
prints(" ------------------------------ \r\n"); |
| 187 |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
| 188 |
strncpy(buf2, buf, (size_t)n); |
memcpy(buf2, buf, (size_t)n); |
| 189 |
buf2[n] = '\0'; |
buf2[n] = '\0'; |
| 190 |
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); |
| 191 |
prints(" ------------------------------ \r\n"); |
prints(" ------------------------------ \r\n"); |
| 192 |
iflush(); |
iflush(); |
| 193 |
} |
} |
| 194 |
|
|
| 195 |
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) |
|
| 196 |
{ |
{ |
| 197 |
int sock, ret, loop, error; |
int sock, ret, loop, error; |
| 198 |
int sock_connected = 0; |
int sock_connected = 0; |
| 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; |
|
iconv_t input_cd = NULL; |
|
| 210 |
char input_conv[LINE_BUFFER_LEN * 2]; |
char input_conv[LINE_BUFFER_LEN * 2]; |
| 211 |
char output_conv[LINE_BUFFER_LEN * 2]; |
char output_conv[LINE_BUFFER_LEN * 2]; |
| 212 |
int input_conv_len = 0; |
int input_conv_len = 0; |
| 213 |
int output_conv_len = 0; |
int output_conv_len = 0; |
| 214 |
int input_conv_offset = 0; |
int input_conv_offset = 0; |
| 215 |
int output_conv_offset = 0; |
int output_conv_offset = 0; |
| 216 |
|
iconv_t input_cd = NULL; |
| 217 |
iconv_t output_cd = NULL; |
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; |
| 424 |
log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", |
log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", |
| 425 |
remote_addr, remote_port, local_addr, local_port, BBS_username); |
remote_addr, remote_port, local_addr, local_port, BBS_username); |
| 426 |
|
|
| 427 |
input_cd = iconv_open(bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET); |
snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset, |
| 428 |
|
(strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE")); |
| 429 |
|
input_cd = iconv_open(tocode, stdio_charset); |
| 430 |
if (input_cd == (iconv_t)(-1)) |
if (input_cd == (iconv_t)(-1)) |
| 431 |
{ |
{ |
| 432 |
log_error("iconv_open(%s->%s) error: %d\n", BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset, errno); |
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); |
| 433 |
goto cleanup; |
goto cleanup; |
| 434 |
} |
} |
| 435 |
output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset); |
|
| 436 |
if (input_cd == (iconv_t)(-1)) |
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
| 437 |
|
(strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
| 438 |
|
output_cd = iconv_open(tocode, bbsnet_conf[n].charset); |
| 439 |
|
if (output_cd == (iconv_t)(-1)) |
| 440 |
{ |
{ |
| 441 |
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET, errno); |
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno); |
| 442 |
iconv_close(input_cd); |
iconv_close(input_cd); |
| 443 |
goto cleanup; |
goto cleanup; |
| 444 |
} |
} |
| 536 |
} |
} |
| 537 |
else if (ret == 0) |
else if (ret == 0) |
| 538 |
{ |
{ |
| 539 |
|
// Send NO-OP to remote server |
| 540 |
|
input_buf[input_buf_len] = '\0'; |
| 541 |
|
input_buf_len++; |
| 542 |
|
BBS_last_access_tm = time(NULL); |
| 543 |
|
|
| 544 |
stdin_read_wait = 0; |
stdin_read_wait = 0; |
| 545 |
break; // Check whether channel is still open |
break; // Check whether channel is still open |
| 546 |
} |
} |
| 596 |
{ |
{ |
| 597 |
if (input_buf_offset < input_buf_len) |
if (input_buf_offset < input_buf_len) |
| 598 |
{ |
{ |
| 599 |
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 |
| 600 |
|
#ifdef _DEBUG |
| 601 |
|
for (int j = input_buf_offset; j < input_buf_len; j++) |
| 602 |
|
{ |
| 603 |
|
log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256); |
| 604 |
|
} |
| 605 |
|
#endif |
| 606 |
|
|
| 607 |
|
ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); |
| 608 |
if (ret < 0) |
if (ret < 0) |
| 609 |
{ |
{ |
| 610 |
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); |
| 611 |
|
input_buf_len = input_buf_offset; // Discard invalid sequence |
| 612 |
|
} |
| 613 |
|
|
| 614 |
|
// For debug |
| 615 |
|
#ifdef _DEBUG |
| 616 |
|
for (int j = input_conv_offset; j < input_conv_len; j++) |
| 617 |
|
{ |
| 618 |
|
log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256); |
| 619 |
} |
} |
| 620 |
|
#endif |
| 621 |
} |
} |
| 622 |
|
|
| 623 |
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
| 708 |
{ |
{ |
| 709 |
if (output_buf_offset < output_buf_len) |
if (output_buf_offset < output_buf_len) |
| 710 |
{ |
{ |
| 711 |
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); |
| 712 |
if (ret < 0) |
if (ret < 0) |
| 713 |
{ |
{ |
| 714 |
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); |
| 715 |
|
output_buf_len = output_buf_offset; // Discard invalid sequence |
| 716 |
} |
} |
| 717 |
} |
} |
| 718 |
|
|
| 802 |
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, |
| 803 |
tm_used->tm_sec); |
tm_used->tm_sec); |
| 804 |
|
|
| 805 |
|
BBS_last_access_tm = time(NULL); |
| 806 |
|
|
| 807 |
return 0; |
return 0; |
| 808 |
} |
} |
| 809 |
|
|
| 810 |
static int |
static int bbsnet_refresh() |
|
bbsnet_refresh() |
|
| 811 |
{ |
{ |
| 812 |
clearscr(); |
clearscr(); |
| 813 |
moveto(1, 0); |
moveto(1, 0); |
| 831 |
return 0; |
return 0; |
| 832 |
} |
} |
| 833 |
|
|
| 834 |
int bbsnet_selchange() |
static int bbsnet_selchange() |
| 835 |
{ |
{ |
| 836 |
int i = bbsnet_menu.menu_item_pos[0]; |
int i = bbsnet_menu.menu_item_pos[0]; |
| 837 |
|
|
| 856 |
return 0; |
return 0; |
| 857 |
} |
} |
| 858 |
|
|
| 859 |
int bbs_net() |
extern int bbs_net() |
| 860 |
{ |
{ |
| 861 |
int ch, i; |
int ch, i; |
| 862 |
|
|
| 871 |
{ |
{ |
| 872 |
ch = igetch(100); |
ch = igetch(100); |
| 873 |
|
|
| 874 |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 875 |
{ |
{ |
| 876 |
BBS_last_access_tm = time(NULL); |
BBS_last_access_tm = time(NULL); |
| 877 |
} |
} |
| 878 |
|
|
| 879 |
switch (ch) |
switch (ch) |
| 880 |
{ |
{ |