| 150 |
|
|
| 151 |
int bbsnet_connect(int n) |
int bbsnet_connect(int n) |
| 152 |
{ |
{ |
| 153 |
int sock, flags, ret, loop, error; |
int sock, ret, loop, error; |
| 154 |
ssize_t len; |
int flags_sock; |
| 155 |
|
int flags_stdin; |
| 156 |
|
int flags_stdout; |
| 157 |
|
int len; |
| 158 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 159 |
char buf[LINE_BUFFER_LEN]; |
char input_buf[LINE_BUFFER_LEN]; |
| 160 |
|
char output_buf[LINE_BUFFER_LEN]; |
| 161 |
|
int input_buf_len = 0; |
| 162 |
|
int output_buf_len = 0; |
| 163 |
|
int input_buf_offset = 0; |
| 164 |
|
int output_buf_offset = 0; |
| 165 |
fd_set read_fds; |
fd_set read_fds; |
| 166 |
fd_set write_fds; |
fd_set write_fds; |
| 167 |
struct timeval timeout; |
struct timeval timeout; |
| 173 |
time_t t_used; |
time_t t_used; |
| 174 |
struct tm *tm_used; |
struct tm *tm_used; |
| 175 |
int ch; |
int ch; |
|
int offset; |
|
| 176 |
|
|
| 177 |
clearscr(); |
clearscr(); |
| 178 |
|
|
| 223 |
process_bar(0, MAX_PROCESS_BAR_LEN); |
process_bar(0, MAX_PROCESS_BAR_LEN); |
| 224 |
|
|
| 225 |
// Set socket as non-blocking |
// Set socket as non-blocking |
| 226 |
flags = fcntl(sock, F_GETFL, 0); |
flags_sock = fcntl(sock, F_GETFL, 0); |
| 227 |
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); |
| 228 |
|
|
| 229 |
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
| 230 |
{ |
{ |
| 287 |
return -1; |
return -1; |
| 288 |
} |
} |
| 289 |
|
|
|
fcntl(sock, F_SETFL, flags); /* restore file status flags */ |
|
|
|
|
| 290 |
tos = IPTOS_LOWDELAY; |
tos = IPTOS_LOWDELAY; |
| 291 |
if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) |
if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) |
| 292 |
{ |
{ |
| 297 |
iflush(); |
iflush(); |
| 298 |
log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
| 299 |
|
|
| 300 |
|
// Set STDIN/STDOUT as non-blocking |
| 301 |
|
flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 302 |
|
flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 303 |
|
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); |
| 304 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); |
| 305 |
|
|
| 306 |
BBS_last_access_tm = t_used = time(0); |
BBS_last_access_tm = t_used = time(0); |
| 307 |
loop = 1; |
loop = 1; |
| 308 |
|
|
| 313 |
FD_SET(sock, &read_fds); |
FD_SET(sock, &read_fds); |
| 314 |
|
|
| 315 |
FD_ZERO(&write_fds); |
FD_ZERO(&write_fds); |
| 316 |
FD_SET(STDIN_FILENO, &write_fds); |
FD_SET(STDOUT_FILENO, &write_fds); |
| 317 |
FD_SET(sock, &write_fds); |
FD_SET(sock, &write_fds); |
| 318 |
|
|
| 319 |
timeout.tv_sec = 0; |
timeout.tv_sec = 0; |
| 338 |
} |
} |
| 339 |
else if (ret > 0) |
else if (ret > 0) |
| 340 |
{ |
{ |
| 341 |
if (FD_ISSET(STDIN_FILENO, &read_fds) && FD_ISSET(sock, &write_fds)) |
if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds)) |
| 342 |
{ |
{ |
| 343 |
// Set STDIN as non-blocking |
ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf)); |
| 344 |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
if (ret < 0) |
|
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
|
|
|
|
|
len = read(STDIN_FILENO, buf, sizeof(buf)); |
|
|
if (len < 0) |
|
| 345 |
{ |
{ |
| 346 |
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
| 347 |
{ |
{ |
| 349 |
loop = 0; |
loop = 0; |
| 350 |
} |
} |
| 351 |
} |
} |
| 352 |
else if (len == 0) |
else if (ret == 0) // broken pipe |
| 353 |
{ |
{ |
|
log_error("read(STDIN) reach EOF\n"); |
|
| 354 |
loop = 0; |
loop = 0; |
| 355 |
} |
} |
| 356 |
else |
else |
| 357 |
{ |
{ |
| 358 |
offset = 0; |
input_buf_len = ret; |
| 359 |
do |
input_buf_offset = 0; |
|
{ |
|
|
ret = (int)write(sock, buf + offset, (size_t)(len - offset)); |
|
|
if (ret < 0) |
|
|
{ |
|
|
log_error("write(socket) error (%d)\n", errno); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
offset += ret; |
|
|
} while (offset < len); |
|
| 360 |
|
|
| 361 |
BBS_last_access_tm = time(0); |
BBS_last_access_tm = time(0); |
| 362 |
} |
} |
|
|
|
|
// Restore STDIN flags |
|
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
|
| 363 |
} |
} |
| 364 |
if (FD_ISSET(sock, &read_fds) && FD_ISSET(STDIN_FILENO, &write_fds)) |
|
| 365 |
|
if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds)) |
| 366 |
{ |
{ |
| 367 |
// Set socket as non-blocking |
ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); |
| 368 |
flags = fcntl(sock, F_GETFL, 0); |
if (ret < 0) |
| 369 |
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
{ |
| 370 |
|
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
| 371 |
|
{ |
| 372 |
|
log_error("write(socket) error (%d)\n", errno); |
| 373 |
|
loop = 0; |
| 374 |
|
} |
| 375 |
|
} |
| 376 |
|
else if (ret == 0) // broken pipe |
| 377 |
|
{ |
| 378 |
|
loop = 0; |
| 379 |
|
} |
| 380 |
|
else |
| 381 |
|
{ |
| 382 |
|
input_buf_offset += ret; |
| 383 |
|
} |
| 384 |
|
} |
| 385 |
|
|
| 386 |
len = read(sock, buf, sizeof(buf)); |
if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds)) |
| 387 |
if (len < 0) |
{ |
| 388 |
|
ret = (int)read(sock, output_buf, sizeof(output_buf)); |
| 389 |
|
if (ret < 0) |
| 390 |
{ |
{ |
| 391 |
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
| 392 |
{ |
{ |
| 394 |
loop = 0; |
loop = 0; |
| 395 |
} |
} |
| 396 |
} |
} |
| 397 |
else if (len == 0) |
else if (ret == 0) // broken pipe |
| 398 |
{ |
{ |
|
log_error("read(socket) reach EOF\n"); |
|
| 399 |
loop = 0; |
loop = 0; |
| 400 |
} |
} |
| 401 |
else |
else |
| 402 |
{ |
{ |
| 403 |
offset = 0; |
output_buf_len = ret; |
| 404 |
do |
output_buf_offset = 0; |
|
{ |
|
|
ret = (int)write(STDOUT_FILENO, buf + offset, (size_t)(len - offset)); |
|
|
if (ret < 0) |
|
|
{ |
|
|
log_error("write(STDOUT) error (%d)\n", errno); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
offset += ret; |
|
|
} while (offset < len); |
|
| 405 |
} |
} |
| 406 |
|
} |
| 407 |
|
|
| 408 |
// Restore socket flags |
if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds)) |
| 409 |
fcntl(sock, F_SETFL, flags); |
{ |
| 410 |
|
ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); |
| 411 |
|
if (ret < 0) |
| 412 |
|
{ |
| 413 |
|
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
| 414 |
|
{ |
| 415 |
|
log_error("write(STDOUT) error (%d)\n", errno); |
| 416 |
|
loop = 0; |
| 417 |
|
} |
| 418 |
|
} |
| 419 |
|
else if (ret == 0) // broken pipe |
| 420 |
|
{ |
| 421 |
|
loop = 0; |
| 422 |
|
} |
| 423 |
|
else |
| 424 |
|
{ |
| 425 |
|
output_buf_offset += ret; |
| 426 |
|
} |
| 427 |
} |
} |
| 428 |
} |
} |
| 429 |
} |
} |
| 430 |
|
|
| 431 |
|
// Restore STDIN/STDOUT flags |
| 432 |
|
fcntl(STDIN_FILENO, F_SETFL, flags_stdin); |
| 433 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); |
| 434 |
|
|
| 435 |
|
// Restore socket flags |
| 436 |
|
fcntl(sock, F_SETFL, flags_sock); |
| 437 |
|
|
| 438 |
if (close(sock) == -1) |
if (close(sock) == -1) |
| 439 |
{ |
{ |
| 440 |
log_error("Close socket failed\n"); |
log_error("Close socket failed\n"); |
| 517 |
ch = igetch(0); |
ch = igetch(0); |
| 518 |
switch (ch) |
switch (ch) |
| 519 |
{ |
{ |
| 520 |
case KEY_NULL: // broken pipe |
case KEY_NULL: // broken pipe |
| 521 |
case Ctrl('C'): // user cancel |
case Ctrl('C'): // user cancel |
| 522 |
return 0; |
return 0; |
| 523 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |