| 31 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 32 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 33 |
|
|
| 34 |
char stdio_charset[32] = BBS_DEFAULT_CHARSET; |
char stdio_charset[20] = BBS_DEFAULT_CHARSET; |
| 35 |
|
|
| 36 |
// static input / output buffer |
// static input / output buffer |
| 37 |
static char stdin_buf[LINE_BUFFER_LEN]; |
static char stdin_buf[LINE_BUFFER_LEN]; |
| 38 |
static char stdin_conv[LINE_BUFFER_LEN]; |
static char stdout_buf[BUFSIZ]; |
| 39 |
static int stdin_buf_len = 0; |
static int stdin_buf_len = 0; |
| 40 |
static int stdin_conv_len = 0; |
static int stdout_buf_len = 0; |
| 41 |
static int stdin_buf_offset = 0; |
static int stdin_buf_offset = 0; |
| 42 |
static int stdin_conv_offset = 0; |
static int stdout_buf_offset = 0; |
| 43 |
|
|
| 44 |
static char stdout_buf[BUFSIZ]; |
static char stdin_conv[LINE_BUFFER_LEN * 2]; |
| 45 |
static char stdout_conv[BUFSIZ]; |
static char stdout_conv[BUFSIZ * 2]; |
| 46 |
static int stdout_buf_len = 0; |
static int stdin_conv_len = 0; |
| 47 |
static int stdout_conv_len = 0; |
static int stdout_conv_len = 0; |
| 48 |
static int stdout_buf_offset = 0; |
static int stdin_conv_offset = 0; |
| 49 |
static int stdout_conv_offset = 0; |
static int stdout_conv_offset = 0; |
| 50 |
|
|
| 51 |
static iconv_t stdin_cd = NULL; |
static iconv_t stdin_cd = NULL; |
| 170 |
if (ret < 0) |
if (ret < 0) |
| 171 |
{ |
{ |
| 172 |
log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len); |
log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len); |
| 173 |
|
stdout_buf_len = stdout_buf_offset; // Discard invalid sequence |
| 174 |
} |
} |
| 175 |
} |
} |
| 176 |
|
|
| 245 |
|
|
| 246 |
int igetch(int timeout) |
int igetch(int timeout) |
| 247 |
{ |
{ |
| 248 |
|
static int stdin_read_wait = 0; |
| 249 |
|
|
| 250 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 251 |
int nfds, epollfd; |
int nfds, epollfd; |
| 252 |
int ret; |
int ret; |
| 288 |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 289 |
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
| 290 |
|
|
| 291 |
for (loop = 1; loop && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;) |
for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;) |
| 292 |
{ |
{ |
| 293 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 294 |
{ |
{ |
| 297 |
break; |
break; |
| 298 |
} |
} |
| 299 |
|
|
| 300 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
if (!stdin_read_wait) |
|
|
|
|
if (nfds < 0) |
|
| 301 |
{ |
{ |
| 302 |
if (errno != EINTR) |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
| 303 |
|
|
| 304 |
|
if (nfds < 0) |
| 305 |
|
{ |
| 306 |
|
if (errno != EINTR) |
| 307 |
|
{ |
| 308 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 309 |
|
break; |
| 310 |
|
} |
| 311 |
|
continue; |
| 312 |
|
} |
| 313 |
|
else if (nfds == 0) // timeout |
| 314 |
{ |
{ |
| 315 |
log_error("epoll_wait() error (%d)\n", errno); |
out = KEY_TIMEOUT; |
| 316 |
break; |
break; |
| 317 |
} |
} |
| 318 |
continue; |
|
| 319 |
} |
for (int i = 0; i < nfds; i++) |
| 320 |
else if (nfds == 0) // timeout |
{ |
| 321 |
{ |
if (events[i].data.fd == STDIN_FILENO) |
| 322 |
out = KEY_TIMEOUT; |
{ |
| 323 |
break; |
stdin_read_wait = 1; |
| 324 |
|
} |
| 325 |
|
} |
| 326 |
} |
} |
| 327 |
|
|
| 328 |
for (int i = 0; i < nfds; i++) |
if (stdin_read_wait) |
| 329 |
{ |
{ |
| 330 |
if (events[i].data.fd == STDIN_FILENO) |
while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error |
| 331 |
{ |
{ |
| 332 |
while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error |
if (SSH_v2) |
| 333 |
{ |
{ |
| 334 |
if (SSH_v2) |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
| 335 |
|
if (ret == SSH_ERROR) |
| 336 |
{ |
{ |
| 337 |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 338 |
if (ret == SSH_ERROR) |
loop = 0; |
| 339 |
{ |
break; |
|
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else if (ret == SSH_EOF) |
|
|
{ |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else if (ret == 0) |
|
|
{ |
|
|
out = 0; |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 340 |
} |
} |
| 341 |
else |
else if (ret == SSH_EOF) |
| 342 |
{ |
{ |
| 343 |
ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len); |
stdin_read_wait = 0; |
| 344 |
|
loop = 0; |
| 345 |
|
break; |
| 346 |
} |
} |
| 347 |
if (ret < 0) |
else if (ret == 0) |
| 348 |
{ |
{ |
| 349 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
out = 0; |
| 350 |
{ |
stdin_read_wait = 0; |
| 351 |
out = 0; |
loop = 0; |
| 352 |
loop = 0; |
break; |
|
break; |
|
|
} |
|
|
else if (errno == EINTR) |
|
|
{ |
|
|
continue; |
|
|
} |
|
|
else |
|
|
{ |
|
|
#ifdef _DEBUG |
|
|
log_error("read(STDIN) error (%d)\n", errno); |
|
|
#endif |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 353 |
} |
} |
| 354 |
else if (ret == 0) // broken pipe |
} |
| 355 |
|
else |
| 356 |
|
{ |
| 357 |
|
ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len); |
| 358 |
|
} |
| 359 |
|
if (ret < 0) |
| 360 |
|
{ |
| 361 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 362 |
{ |
{ |
| 363 |
|
out = 0; |
| 364 |
|
stdin_read_wait = 0; |
| 365 |
loop = 0; |
loop = 0; |
| 366 |
break; |
break; |
| 367 |
} |
} |
| 368 |
else |
else if (errno == EINTR) |
| 369 |
{ |
{ |
|
stdin_buf_len += ret; |
|
| 370 |
continue; |
continue; |
| 371 |
} |
} |
| 372 |
|
else |
| 373 |
|
{ |
| 374 |
|
#ifdef _DEBUG |
| 375 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 376 |
|
#endif |
| 377 |
|
loop = 0; |
| 378 |
|
break; |
| 379 |
|
} |
| 380 |
|
} |
| 381 |
|
else if (ret == 0) // broken pipe |
| 382 |
|
{ |
| 383 |
|
stdin_read_wait = 0; |
| 384 |
|
loop = 0; |
| 385 |
|
break; |
| 386 |
|
} |
| 387 |
|
else |
| 388 |
|
{ |
| 389 |
|
stdin_buf_len += ret; |
| 390 |
|
continue; |
| 391 |
} |
} |
| 392 |
} |
} |
| 393 |
} |
} |
| 414 |
if (ret < 0) |
if (ret < 0) |
| 415 |
{ |
{ |
| 416 |
log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len); |
log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len); |
| 417 |
|
stdin_buf_len = stdin_buf_offset; // Discard invalid sequence |
| 418 |
} |
} |
| 419 |
|
|
| 420 |
// For debug |
// For debug |
| 952 |
{ |
{ |
| 953 |
if (out_bytes <= 0) |
if (out_bytes <= 0) |
| 954 |
{ |
{ |
| 955 |
log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); |
log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); |
| 956 |
return -2; |
return -2; |
| 957 |
} |
} |
| 958 |
|
|
| 1004 |
return -2; |
return -2; |
| 1005 |
} |
} |
| 1006 |
|
|
| 1007 |
|
// reset in_bytes when "//IGNORE" is applied |
| 1008 |
|
if (in_bytes == 0) |
| 1009 |
|
{ |
| 1010 |
|
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 1011 |
|
} |
| 1012 |
|
|
| 1013 |
*out_buf = *in_buf; |
*out_buf = *in_buf; |
| 1014 |
in_buf++; |
in_buf++; |
| 1015 |
out_buf++; |
out_buf++; |
| 1034 |
|
|
| 1035 |
int io_conv_init(const char *charset) |
int io_conv_init(const char *charset) |
| 1036 |
{ |
{ |
| 1037 |
|
char tocode[32]; |
| 1038 |
|
|
| 1039 |
if (charset == NULL) |
if (charset == NULL) |
| 1040 |
{ |
{ |
| 1041 |
log_error("NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1044 |
|
|
| 1045 |
io_conv_cleanup(); |
io_conv_cleanup(); |
| 1046 |
|
|
| 1047 |
snprintf(stdio_charset, sizeof(stdio_charset), "%s//TRANSLIT", charset); |
strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); |
| 1048 |
|
stdio_charset[sizeof(stdio_charset) - 1] = '\0'; |
| 1049 |
|
|
| 1050 |
stdin_cd = iconv_open(BBS_DEFAULT_CHARSET, stdio_charset); |
snprintf(tocode, sizeof(tocode), "%s%s", BBS_DEFAULT_CHARSET, |
| 1051 |
|
(strcasecmp(stdio_charset, BBS_DEFAULT_CHARSET) == 0 ? "" : "//IGNORE")); |
| 1052 |
|
stdin_cd = iconv_open(tocode, stdio_charset); |
| 1053 |
if (stdin_cd == (iconv_t)(-1)) |
if (stdin_cd == (iconv_t)(-1)) |
| 1054 |
{ |
{ |
| 1055 |
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET, errno); |
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); |
| 1056 |
return -2; |
return -2; |
| 1057 |
} |
} |
| 1058 |
stdout_cd = iconv_open(stdio_charset, BBS_DEFAULT_CHARSET); |
|
| 1059 |
|
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
| 1060 |
|
(strcasecmp(BBS_DEFAULT_CHARSET, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
| 1061 |
|
stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET); |
| 1062 |
if (stdout_cd == (iconv_t)(-1)) |
if (stdout_cd == (iconv_t)(-1)) |
| 1063 |
{ |
{ |
| 1064 |
log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, stdio_charset, errno); |
log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, tocode, errno); |
| 1065 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1066 |
return -2; |
return -2; |
| 1067 |
} |
} |