| 31 |
#include <time.h> |
#include <time.h> |
| 32 |
#include <unistd.h> |
#include <unistd.h> |
| 33 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
| 34 |
|
#include <iconv.h> |
| 35 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 36 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 37 |
#include <libssh/callbacks.h> |
#include <libssh/callbacks.h> |
| 54 |
char host2[40]; |
char host2[40]; |
| 55 |
char ip[40]; |
char ip[40]; |
| 56 |
in_port_t port; |
in_port_t port; |
| 57 |
|
char charset[20]; |
| 58 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 59 |
|
|
| 60 |
MENU_SET bbsnet_menu; |
MENU_SET bbsnet_menu; |
| 65 |
MENU *p_menu; |
MENU *p_menu; |
| 66 |
MENU_ITEM *p_menu_item; |
MENU_ITEM *p_menu_item; |
| 67 |
MENU_ITEM_ID menu_item_id; |
MENU_ITEM_ID menu_item_id; |
| 68 |
char t[256], *t1, *t2, *t3, *t4, *saveptr; |
char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr; |
| 69 |
|
|
| 70 |
fp = fopen(file_config, "r"); |
fp = fopen(file_config, "r"); |
| 71 |
if (fp == NULL) |
if (fp == NULL) |
| 103 |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 104 |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 105 |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 106 |
|
t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 107 |
|
|
| 108 |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*') |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*') |
| 109 |
{ |
{ |
| 110 |
continue; |
continue; |
| 111 |
} |
} |
| 117 |
strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1); |
strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1); |
| 118 |
bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0'; |
bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0'; |
| 119 |
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); |
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); |
| 120 |
|
strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1); |
| 121 |
|
bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0'; |
| 122 |
|
|
| 123 |
p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); |
p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); |
| 124 |
if (p_menu_item == NULL) |
if (p_menu_item == NULL) |
| 193 |
iflush(); |
iflush(); |
| 194 |
} |
} |
| 195 |
|
|
| 196 |
|
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) |
| 197 |
|
{ |
| 198 |
|
char *in_buf; |
| 199 |
|
char *out_buf; |
| 200 |
|
size_t in_bytes; |
| 201 |
|
size_t out_bytes; |
| 202 |
|
int ret; |
| 203 |
|
|
| 204 |
|
in_buf = p_buf + *p_buf_offset; |
| 205 |
|
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 206 |
|
out_buf = p_conv + *p_conv_len; |
| 207 |
|
out_bytes = conv_size - (size_t)(*p_conv_len); |
| 208 |
|
|
| 209 |
|
while (in_bytes > 0) |
| 210 |
|
{ |
| 211 |
|
ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes); |
| 212 |
|
if (ret == -1) |
| 213 |
|
{ |
| 214 |
|
if (errno == EINVAL) // Incomplete |
| 215 |
|
{ |
| 216 |
|
#ifdef _DEBUG |
| 217 |
|
log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes); |
| 218 |
|
#endif |
| 219 |
|
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf); |
| 220 |
|
*p_buf_offset = 0; |
| 221 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 222 |
|
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
| 223 |
|
|
| 224 |
|
break; |
| 225 |
|
} |
| 226 |
|
else if (errno == E2BIG) |
| 227 |
|
{ |
| 228 |
|
log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); |
| 229 |
|
return -1; |
| 230 |
|
} |
| 231 |
|
else if (errno == EILSEQ) |
| 232 |
|
{ |
| 233 |
|
if (in_bytes > out_bytes || out_bytes <= 0) |
| 234 |
|
{ |
| 235 |
|
log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); |
| 236 |
|
return -2; |
| 237 |
|
} |
| 238 |
|
|
| 239 |
|
*out_buf = *in_buf; |
| 240 |
|
in_buf++; |
| 241 |
|
out_buf++; |
| 242 |
|
in_bytes--; |
| 243 |
|
out_bytes--; |
| 244 |
|
|
| 245 |
|
continue; |
| 246 |
|
} |
| 247 |
|
} |
| 248 |
|
else |
| 249 |
|
{ |
| 250 |
|
*p_buf_len = 0; |
| 251 |
|
*p_buf_offset = 0; |
| 252 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 253 |
|
|
| 254 |
|
break; |
| 255 |
|
} |
| 256 |
|
} |
| 257 |
|
|
| 258 |
|
return 0; |
| 259 |
|
} |
| 260 |
|
|
| 261 |
int bbsnet_connect(int n) |
int bbsnet_connect(int n) |
| 262 |
{ |
{ |
| 263 |
int sock, ret, loop, error; |
int sock, ret, loop, error; |
| 273 |
int output_buf_len = 0; |
int output_buf_len = 0; |
| 274 |
int input_buf_offset = 0; |
int input_buf_offset = 0; |
| 275 |
int output_buf_offset = 0; |
int output_buf_offset = 0; |
| 276 |
|
iconv_t input_cd = NULL; |
| 277 |
|
char input_conv[LINE_BUFFER_LEN * 2]; |
| 278 |
|
char output_conv[LINE_BUFFER_LEN * 2]; |
| 279 |
|
int input_conv_len = 0; |
| 280 |
|
int output_conv_len = 0; |
| 281 |
|
int input_conv_offset = 0; |
| 282 |
|
int output_conv_offset = 0; |
| 283 |
|
iconv_t output_cd = NULL; |
| 284 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 285 |
int nfds, epollfd; |
int nfds, epollfd; |
| 286 |
int stdin_read_wait = 0; |
int stdin_read_wait = 0; |
| 474 |
iflush(); |
iflush(); |
| 475 |
log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
| 476 |
|
|
| 477 |
|
input_cd = iconv_open(bbsnet_conf[n].charset, "UTF-8"); |
| 478 |
|
if (input_cd == (iconv_t)(-1)) |
| 479 |
|
{ |
| 480 |
|
log_error("iconv_open(UTF8->GBK) error: %d\n", errno); |
| 481 |
|
goto cleanup; |
| 482 |
|
} |
| 483 |
|
output_cd = iconv_open("UTF-8", bbsnet_conf[n].charset); |
| 484 |
|
if (input_cd == (iconv_t)(-1)) |
| 485 |
|
{ |
| 486 |
|
log_error("iconv_open(GBK->UTF-8) error: %d\n", errno); |
| 487 |
|
iconv_close(input_cd); |
| 488 |
|
goto cleanup; |
| 489 |
|
} |
| 490 |
|
|
| 491 |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
| 492 |
ev.data.fd = sock; |
ev.data.fd = sock; |
| 493 |
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
| 634 |
|
|
| 635 |
if (sock_write_wait) |
if (sock_write_wait) |
| 636 |
{ |
{ |
| 637 |
while (input_buf_offset < input_buf_len && !SYS_server_exit) |
if (input_buf_offset < input_buf_len) |
| 638 |
|
{ |
| 639 |
|
ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); |
| 640 |
|
if (ret < 0) |
| 641 |
|
{ |
| 642 |
|
log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); |
| 643 |
|
} |
| 644 |
|
} |
| 645 |
|
|
| 646 |
|
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
| 647 |
{ |
{ |
| 648 |
ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); |
ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset)); |
| 649 |
if (ret < 0) |
if (ret < 0) |
| 650 |
{ |
{ |
| 651 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 675 |
} |
} |
| 676 |
else |
else |
| 677 |
{ |
{ |
| 678 |
input_buf_offset += ret; |
input_conv_offset += ret; |
| 679 |
if (input_buf_offset >= input_buf_len) // Output buffer complete |
if (input_conv_offset >= input_conv_len) // Output buffer complete |
| 680 |
{ |
{ |
| 681 |
input_buf_offset = 0; |
input_conv_offset = 0; |
| 682 |
input_buf_len = 0; |
input_conv_len = 0; |
| 683 |
break; |
break; |
| 684 |
} |
} |
| 685 |
continue; |
continue; |
| 729 |
|
|
| 730 |
if (stdout_write_wait) |
if (stdout_write_wait) |
| 731 |
{ |
{ |
| 732 |
while (output_buf_offset < output_buf_len && !SYS_server_exit) |
if (output_buf_offset < output_buf_len) |
| 733 |
|
{ |
| 734 |
|
ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len); |
| 735 |
|
if (ret < 0) |
| 736 |
|
{ |
| 737 |
|
log_error("bbsnet_io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); |
| 738 |
|
} |
| 739 |
|
} |
| 740 |
|
|
| 741 |
|
while (output_conv_offset < output_conv_len && !SYS_server_exit) |
| 742 |
{ |
{ |
| 743 |
if (SSH_v2) |
if (SSH_v2) |
| 744 |
{ |
{ |
| 745 |
ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset)); |
ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); |
| 746 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 747 |
{ |
{ |
| 748 |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
| 752 |
} |
} |
| 753 |
else |
else |
| 754 |
{ |
{ |
| 755 |
ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); |
ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset)); |
| 756 |
} |
} |
| 757 |
if (ret < 0) |
if (ret < 0) |
| 758 |
{ |
{ |
| 783 |
} |
} |
| 784 |
else |
else |
| 785 |
{ |
{ |
| 786 |
output_buf_offset += ret; |
output_conv_offset += ret; |
| 787 |
if (output_buf_offset >= output_buf_len) // Output buffer complete |
if (output_conv_offset >= output_conv_len) // Output buffer complete |
| 788 |
{ |
{ |
| 789 |
output_buf_offset = 0; |
output_conv_offset = 0; |
| 790 |
output_buf_len = 0; |
output_conv_len = 0; |
| 791 |
break; |
break; |
| 792 |
} |
} |
| 793 |
continue; |
continue; |
| 796 |
} |
} |
| 797 |
} |
} |
| 798 |
|
|
| 799 |
|
iconv_close(input_cd); |
| 800 |
|
iconv_close(output_cd); |
| 801 |
|
|
| 802 |
cleanup: |
cleanup: |
| 803 |
if (close(epollfd) < 0) |
if (close(epollfd) < 0) |
| 804 |
{ |
{ |