| 31 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 32 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 33 |
|
|
| 34 |
|
char stdio_charset[20] = BBS_DEFAULT_CHARSET; |
| 35 |
|
|
| 36 |
|
// static input / output buffer |
| 37 |
|
static char stdin_buf[LINE_BUFFER_LEN]; |
| 38 |
|
static char stdin_conv[LINE_BUFFER_LEN]; |
| 39 |
|
static int stdin_buf_len = 0; |
| 40 |
|
static int stdin_conv_len = 0; |
| 41 |
|
static int stdin_buf_offset = 0; |
| 42 |
|
static int stdin_conv_offset = 0; |
| 43 |
|
|
| 44 |
static char stdout_buf[BUFSIZ]; |
static char stdout_buf[BUFSIZ]; |
| 45 |
|
static char stdout_conv[BUFSIZ]; |
| 46 |
static int stdout_buf_len = 0; |
static int stdout_buf_len = 0; |
| 47 |
|
static int stdout_conv_len = 0; |
| 48 |
static int stdout_buf_offset = 0; |
static int stdout_buf_offset = 0; |
| 49 |
|
static int stdout_conv_offset = 0; |
| 50 |
|
|
| 51 |
|
static iconv_t stdin_cd = NULL; |
| 52 |
|
static iconv_t stdout_cd = NULL; |
| 53 |
|
|
| 54 |
int prints(const char *format, ...) |
int prints(const char *format, ...) |
| 55 |
{ |
{ |
| 164 |
{ |
{ |
| 165 |
if (events[i].data.fd == STDOUT_FILENO) |
if (events[i].data.fd == STDOUT_FILENO) |
| 166 |
{ |
{ |
| 167 |
while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error |
if (stdout_buf_offset < stdout_buf_len) |
| 168 |
|
{ |
| 169 |
|
ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len); |
| 170 |
|
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); |
| 173 |
|
} |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error |
| 177 |
{ |
{ |
| 178 |
if (SSH_v2) |
if (SSH_v2) |
| 179 |
{ |
{ |
| 180 |
ret = ssh_channel_write(SSH_channel, stdout_buf + stdout_buf_offset, (uint32_t)(stdout_buf_len - stdout_buf_offset)); |
ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset)); |
| 181 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 182 |
{ |
{ |
| 183 |
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)); |
| 187 |
} |
} |
| 188 |
else |
else |
| 189 |
{ |
{ |
| 190 |
ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset)); |
ret = (int)write(STDOUT_FILENO, stdout_conv + stdout_conv_offset, (size_t)(stdout_conv_len - stdout_conv_offset)); |
| 191 |
} |
} |
| 192 |
if (ret < 0) |
if (ret < 0) |
| 193 |
{ |
{ |
| 215 |
} |
} |
| 216 |
else |
else |
| 217 |
{ |
{ |
| 218 |
stdout_buf_offset += ret; |
stdout_conv_offset += ret; |
| 219 |
if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely |
if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely |
| 220 |
{ |
{ |
| 221 |
ret = 0; |
ret = 0; |
| 222 |
stdout_buf_offset = 0; |
stdout_conv_offset = 0; |
| 223 |
stdout_buf_len = 0; |
stdout_conv_len = 0; |
| 224 |
retry = 0; |
retry = 0; |
| 225 |
break; |
break; |
| 226 |
} |
} |
| 244 |
|
|
| 245 |
int igetch(int timeout) |
int igetch(int timeout) |
| 246 |
{ |
{ |
|
// static input buffer |
|
|
static unsigned char buf[LINE_BUFFER_LEN]; |
|
|
static int len = 0; |
|
|
static int pos = 0; |
|
|
|
|
| 247 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 248 |
int nfds, epollfd; |
int nfds, epollfd; |
| 249 |
int ret; |
int ret; |
| 257 |
int i = 0; |
int i = 0; |
| 258 |
int flags; |
int flags; |
| 259 |
|
|
| 260 |
if (pos >= len) |
if (stdin_conv_offset >= stdin_conv_len) |
| 261 |
{ |
{ |
| 262 |
len = 0; |
stdin_conv_len = 0; |
| 263 |
pos = 0; |
stdin_conv_offset = 0; |
| 264 |
|
|
| 265 |
epollfd = epoll_create1(0); |
epollfd = epoll_create1(0); |
| 266 |
if (epollfd < 0) |
if (epollfd < 0) |
| 285 |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 286 |
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
| 287 |
|
|
| 288 |
for (loop = 1; loop && pos >= len && !SYS_server_exit;) |
for (loop = 1; loop && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;) |
| 289 |
{ |
{ |
| 290 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 291 |
{ |
{ |
| 315 |
{ |
{ |
| 316 |
if (events[i].data.fd == STDIN_FILENO) |
if (events[i].data.fd == STDIN_FILENO) |
| 317 |
{ |
{ |
| 318 |
while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error |
while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error |
| 319 |
{ |
{ |
| 320 |
if (SSH_v2) |
if (SSH_v2) |
| 321 |
{ |
{ |
| 322 |
ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0); |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
| 323 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 324 |
{ |
{ |
| 325 |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 340 |
} |
} |
| 341 |
else |
else |
| 342 |
{ |
{ |
| 343 |
ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len); |
ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len); |
| 344 |
} |
} |
| 345 |
if (ret < 0) |
if (ret < 0) |
| 346 |
{ |
{ |
| 370 |
} |
} |
| 371 |
else |
else |
| 372 |
{ |
{ |
| 373 |
len += ret; |
stdin_buf_len += ret; |
| 374 |
continue; |
continue; |
| 375 |
} |
} |
| 376 |
} |
} |
| 379 |
|
|
| 380 |
// For debug |
// For debug |
| 381 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 382 |
for (int j = pos; j < len; j++) |
for (int j = stdin_buf_offset; j < stdin_buf_len; j++) |
| 383 |
{ |
{ |
| 384 |
log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256); |
log_common("Debug: <--[%u]\n", (stdin_buf[j] + 256) % 256); |
| 385 |
} |
} |
| 386 |
#endif |
#endif |
| 387 |
} |
} |
| 392 |
{ |
{ |
| 393 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)\n"); |
| 394 |
} |
} |
| 395 |
|
|
| 396 |
|
if (stdin_buf_offset < stdin_buf_len) |
| 397 |
|
{ |
| 398 |
|
ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len); |
| 399 |
|
if (ret < 0) |
| 400 |
|
{ |
| 401 |
|
log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len); |
| 402 |
|
} |
| 403 |
|
} |
| 404 |
} |
} |
| 405 |
|
|
| 406 |
while (pos < len) |
while (stdin_conv_offset < stdin_conv_len) |
| 407 |
{ |
{ |
| 408 |
unsigned char c = buf[pos++]; |
unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++]; |
| 409 |
|
|
| 410 |
// Convert \r\n to \r |
// Convert \r\n to \r |
| 411 |
if (c == CR && pos < len && buf[pos] == LF) |
if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF) |
| 412 |
{ |
{ |
| 413 |
pos++; |
stdin_conv_offset++; |
| 414 |
} |
} |
| 415 |
|
|
| 416 |
// Convert single \n to \r |
// Convert single \n to \r |
| 900 |
size_t out_bytes; |
size_t out_bytes; |
| 901 |
int ret; |
int ret; |
| 902 |
|
|
| 903 |
|
if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) |
| 904 |
|
{ |
| 905 |
|
log_error("NULL pointer error\n"); |
| 906 |
|
return -1; |
| 907 |
|
} |
| 908 |
|
|
| 909 |
in_buf = p_buf + *p_buf_offset; |
in_buf = p_buf + *p_buf_offset; |
| 910 |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 911 |
out_buf = p_conv + *p_conv_len; |
out_buf = p_conv + *p_conv_len; |
| 961 |
} |
} |
| 962 |
|
|
| 963 |
return 0; |
return 0; |
| 964 |
|
} |
| 965 |
|
|
| 966 |
|
int io_conv_init(const char *charset) |
| 967 |
|
{ |
| 968 |
|
if (charset == NULL) |
| 969 |
|
{ |
| 970 |
|
log_error("NULL pointer error\n"); |
| 971 |
|
return -1; |
| 972 |
|
} |
| 973 |
|
|
| 974 |
|
io_conv_cleanup(); |
| 975 |
|
|
| 976 |
|
strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); |
| 977 |
|
stdio_charset[sizeof(stdio_charset) - 1] = '\0'; |
| 978 |
|
|
| 979 |
|
stdin_cd = iconv_open(BBS_DEFAULT_CHARSET, stdio_charset); |
| 980 |
|
if (stdin_cd == (iconv_t)(-1)) |
| 981 |
|
{ |
| 982 |
|
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET, errno); |
| 983 |
|
return -2; |
| 984 |
|
} |
| 985 |
|
stdout_cd = iconv_open(stdio_charset, BBS_DEFAULT_CHARSET); |
| 986 |
|
if (stdout_cd == (iconv_t)(-1)) |
| 987 |
|
{ |
| 988 |
|
log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, stdio_charset, errno); |
| 989 |
|
iconv_close(stdin_cd); |
| 990 |
|
return -2; |
| 991 |
|
} |
| 992 |
|
|
| 993 |
|
return 0; |
| 994 |
|
} |
| 995 |
|
|
| 996 |
|
int io_conv_cleanup(void) |
| 997 |
|
{ |
| 998 |
|
if (stdin_cd != NULL) |
| 999 |
|
{ |
| 1000 |
|
iconv_close(stdin_cd); |
| 1001 |
|
stdin_cd = NULL; |
| 1002 |
|
} |
| 1003 |
|
if (stdout_cd != NULL) |
| 1004 |
|
{ |
| 1005 |
|
iconv_close(stdout_cd); |
| 1006 |
|
stdout_cd = NULL; |
| 1007 |
|
} |
| 1008 |
|
|
| 1009 |
|
return 0; |
| 1010 |
} |
} |