| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
#include "screen.h" |
#include "screen.h" |
| 22 |
#include "menu.h" |
#include "menu.h" |
|
#include "tcplib.h" |
|
| 23 |
#include <stdio.h> |
#include <stdio.h> |
| 24 |
#include <stdarg.h> |
#include <stdarg.h> |
| 25 |
|
#include <errno.h> |
| 26 |
|
#include <string.h> |
| 27 |
|
#include <stdlib.h> |
| 28 |
|
#include <fcntl.h> |
| 29 |
|
#include <time.h> |
| 30 |
|
#include <unistd.h> |
| 31 |
|
#include <netdb.h> |
| 32 |
|
#include <sys/select.h> |
| 33 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 34 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 35 |
#include <netinet/in.h> |
#include <netinet/in.h> |
| 36 |
|
#include <netinet/ip.h> |
| 37 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
|
#include <time.h> |
|
|
#include <unistd.h> |
|
|
#include <netdb.h> |
|
| 38 |
|
|
| 39 |
#define MENU_CONF_DELIM " \t\r\n" |
#define MENU_CONF_DELIM " \t\r\n" |
| 40 |
|
|
| 141 |
moveto(4, 0); |
moveto(4, 0); |
| 142 |
prints(" ------------------------------ \r\n"); |
prints(" ------------------------------ \r\n"); |
| 143 |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
| 144 |
strncpy(buf2, buf, (size_t) n); |
strncpy(buf2, buf, (size_t)n); |
| 145 |
buf2[n] = '\0'; |
buf2[n] = '\0'; |
| 146 |
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); |
| 147 |
prints(" ------------------------------ \r\n"); |
prints(" ------------------------------ \r\n"); |
| 150 |
|
|
| 151 |
int bbsnet_connect(int n) |
int bbsnet_connect(int n) |
| 152 |
{ |
{ |
| 153 |
int sock, result, loop; |
int sock, flags, ret, loop, error; |
| 154 |
ssize_t len; |
ssize_t len; |
| 155 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 156 |
char buf[LINE_BUFFER_LEN]; |
char buf[LINE_BUFFER_LEN]; |
| 157 |
fd_set testfds; |
fd_set read_fds; |
| 158 |
|
fd_set write_fds; |
| 159 |
struct timeval timeout; |
struct timeval timeout; |
| 160 |
struct hostent *p_host = NULL; |
struct hostent *p_host = NULL; |
| 161 |
int rv, tos = 020, i; |
int tos; |
| 162 |
|
int i; |
| 163 |
char remote_addr[IP_ADDR_LEN]; |
char remote_addr[IP_ADDR_LEN]; |
| 164 |
int remote_port; |
int remote_port; |
| 165 |
time_t t_used; |
time_t t_used; |
| 166 |
struct tm *tm_used; |
struct tm *tm_used; |
| 167 |
int ch; |
int ch; |
| 168 |
|
int offset; |
| 169 |
|
|
| 170 |
clearscr(); |
clearscr(); |
| 171 |
|
|
| 193 |
} |
} |
| 194 |
|
|
| 195 |
sin.sin_family = AF_INET; |
sin.sin_family = AF_INET; |
| 196 |
sin.sin_addr.s_addr = |
sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY); |
|
(strnlen(hostaddr_server, sizeof(hostaddr_server)) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY); |
|
| 197 |
sin.sin_port = 0; |
sin.sin_port = 0; |
| 198 |
|
|
| 199 |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 200 |
{ |
{ |
| 201 |
log_error("Bind address %s:%u failed\n", |
log_error("Bind address %s:%u failed (%d)\n", |
| 202 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
| 203 |
return -2; |
return -2; |
| 204 |
} |
} |
| 205 |
|
|
| 214 |
|
|
| 215 |
prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); |
prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); |
| 216 |
process_bar(0, MAX_PROCESS_BAR_LEN); |
process_bar(0, MAX_PROCESS_BAR_LEN); |
| 217 |
|
|
| 218 |
|
// Set socket as non-blocking |
| 219 |
|
flags = fcntl(sock, F_GETFL, 0); |
| 220 |
|
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
| 221 |
|
|
| 222 |
|
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
| 223 |
|
{ |
| 224 |
|
if (errno != EINPROGRESS) |
| 225 |
|
{ |
| 226 |
|
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 227 |
|
press_any_key(); |
| 228 |
|
return -1; |
| 229 |
|
} |
| 230 |
|
} |
| 231 |
|
|
| 232 |
for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) |
for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) |
| 233 |
{ |
{ |
| 234 |
ch = igetch(0); // 100 ms |
ch = igetch(0); // 0.1 second |
| 235 |
if (ch == KEY_NULL || ch == Ctrl('C') || SYS_server_exit) |
if (ch == Ctrl('C') || SYS_server_exit) |
| 236 |
{ |
{ |
| 237 |
return 0; |
return 0; |
| 238 |
} |
} |
| 239 |
|
|
| 240 |
rv = NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin), |
FD_ZERO(&read_fds); |
| 241 |
400, (i == 0 ? 1 : 0)); // 400 ms |
FD_SET(sock, &read_fds); |
| 242 |
|
|
| 243 |
if (rv == ERR_TCPLIB_TIMEOUT) |
FD_ZERO(&write_fds); |
| 244 |
|
FD_SET(sock, &write_fds); |
| 245 |
|
|
| 246 |
|
timeout.tv_sec = 0; |
| 247 |
|
timeout.tv_usec = 400 * 1000; // 0.4 second |
| 248 |
|
|
| 249 |
|
ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout); |
| 250 |
|
|
| 251 |
|
if (ret == 0) // Timeout |
| 252 |
{ |
{ |
| 253 |
process_bar(i + 1, MAX_PROCESS_BAR_LEN); |
process_bar(i + 1, MAX_PROCESS_BAR_LEN); |
|
continue; |
|
| 254 |
} |
} |
| 255 |
else if (rv == 0) |
else if (ret < 0) |
| 256 |
{ |
{ |
| 257 |
break; |
if (errno != EINTR) |
| 258 |
|
{ |
| 259 |
|
log_error("select() error (%d) !\n", errno); |
| 260 |
|
return -1; |
| 261 |
|
} |
| 262 |
} |
} |
| 263 |
else |
// ret > 0 |
| 264 |
|
else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds)) |
| 265 |
{ |
{ |
| 266 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
len = sizeof(error); |
| 267 |
press_any_key(); |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
| 268 |
return -1; |
{ |
| 269 |
|
log_error("getsockopt() error (%d) !\n", error); |
| 270 |
|
return -1; |
| 271 |
|
} |
| 272 |
|
|
| 273 |
|
break; // connected |
| 274 |
} |
} |
| 275 |
} |
} |
| 276 |
if (i == MAX_PROCESS_BAR_LEN) |
if (i == MAX_PROCESS_BAR_LEN) |
| 279 |
press_any_key(); |
press_any_key(); |
| 280 |
return -1; |
return -1; |
| 281 |
} |
} |
| 282 |
setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int)); |
|
| 283 |
|
fcntl(sock, F_SETFL, flags); /* restore file status flags */ |
| 284 |
|
|
| 285 |
|
tos = IPTOS_LOWDELAY; |
| 286 |
|
if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) |
| 287 |
|
{ |
| 288 |
|
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
| 289 |
|
} |
| 290 |
|
|
| 291 |
prints("\033[1;31m连接成功!\033[m\r\n"); |
prints("\033[1;31m连接成功!\033[m\r\n"); |
| 292 |
|
iflush(); |
| 293 |
log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
| 294 |
|
|
| 295 |
BBS_last_access_tm = t_used = time(0); |
BBS_last_access_tm = t_used = time(0); |
|
|
|
| 296 |
loop = 1; |
loop = 1; |
| 297 |
|
|
| 298 |
while (loop && !SYS_server_exit) |
while (loop && !SYS_server_exit) |
| 299 |
{ |
{ |
| 300 |
FD_ZERO(&testfds); |
FD_ZERO(&read_fds); |
| 301 |
FD_SET(STDIN_FILENO, &testfds); |
FD_SET(STDIN_FILENO, &read_fds); |
| 302 |
FD_SET(sock, &testfds); |
FD_SET(sock, &read_fds); |
| 303 |
|
|
| 304 |
|
FD_ZERO(&write_fds); |
| 305 |
|
FD_SET(STDIN_FILENO, &write_fds); |
| 306 |
|
FD_SET(sock, &write_fds); |
| 307 |
|
|
| 308 |
timeout.tv_sec = 0; |
timeout.tv_sec = 0; |
| 309 |
timeout.tv_usec = 100 * 1000; // 0.1 second |
timeout.tv_usec = 100 * 1000; // 0.1 second |
| 310 |
|
|
| 311 |
result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL, |
ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout); |
|
(fd_set *)NULL, &timeout); |
|
| 312 |
|
|
| 313 |
if (result == 0) |
if (ret == 0) // timeout |
| 314 |
{ |
{ |
| 315 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 316 |
{ |
{ |
| 317 |
loop = 0; |
loop = 0; |
| 318 |
} |
} |
| 319 |
} |
} |
| 320 |
if (result < 0) |
else if (ret < 0) |
| 321 |
{ |
{ |
| 322 |
log_error("select() error (%d) !\n", result); |
if (errno != EINTR) |
| 323 |
loop = 0; |
{ |
| 324 |
|
log_error("select() error (%d) !\n", errno); |
| 325 |
|
loop = 0; |
| 326 |
|
} |
| 327 |
} |
} |
| 328 |
if (result > 0) |
else if (ret > 0) |
| 329 |
{ |
{ |
| 330 |
if (FD_ISSET(STDIN_FILENO, &testfds)) |
if (FD_ISSET(STDIN_FILENO, &read_fds) && FD_ISSET(sock, &write_fds)) |
| 331 |
{ |
{ |
| 332 |
|
// Set STDIN as non-blocking |
| 333 |
|
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 334 |
|
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
| 335 |
|
|
| 336 |
len = read(STDIN_FILENO, buf, sizeof(buf)); |
len = read(STDIN_FILENO, buf, sizeof(buf)); |
| 337 |
if (len == 0) |
if (len < 0) |
| 338 |
{ |
{ |
| 339 |
|
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
| 340 |
|
{ |
| 341 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 342 |
|
loop = 0; |
| 343 |
|
} |
| 344 |
|
} |
| 345 |
|
else if (len == 0) |
| 346 |
|
{ |
| 347 |
|
log_error("read(STDIN) reach EOF\n"); |
| 348 |
loop = 0; |
loop = 0; |
| 349 |
} |
} |
| 350 |
write(sock, buf, (size_t)len); |
else |
| 351 |
|
{ |
| 352 |
|
offset = 0; |
| 353 |
|
do |
| 354 |
|
{ |
| 355 |
|
ret = (int)write(sock, buf + offset, (size_t)(len - offset)); |
| 356 |
|
if (ret < 0) |
| 357 |
|
{ |
| 358 |
|
log_error("write(socket) error (%d)\n", errno); |
| 359 |
|
loop = 0; |
| 360 |
|
break; |
| 361 |
|
} |
| 362 |
|
offset += ret; |
| 363 |
|
} while (offset < len); |
| 364 |
|
|
| 365 |
|
BBS_last_access_tm = time(0); |
| 366 |
|
} |
| 367 |
|
|
| 368 |
|
// Restore STDIN flags |
| 369 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 370 |
} |
} |
| 371 |
if (FD_ISSET(sock, &testfds)) |
if (FD_ISSET(sock, &read_fds) && FD_ISSET(STDIN_FILENO, &write_fds)) |
| 372 |
{ |
{ |
| 373 |
|
// Set socket as non-blocking |
| 374 |
|
flags = fcntl(sock, F_GETFL, 0); |
| 375 |
|
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
| 376 |
|
|
| 377 |
len = read(sock, buf, sizeof(buf)); |
len = read(sock, buf, sizeof(buf)); |
| 378 |
if (len == 0) |
if (len < 0) |
| 379 |
{ |
{ |
| 380 |
|
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
| 381 |
|
{ |
| 382 |
|
log_error("read(socket) error (%d)\n", errno); |
| 383 |
|
loop = 0; |
| 384 |
|
} |
| 385 |
|
} |
| 386 |
|
else if (len == 0) |
| 387 |
|
{ |
| 388 |
|
log_error("read(socket) reach EOF\n"); |
| 389 |
loop = 0; |
loop = 0; |
| 390 |
} |
} |
| 391 |
write(STDOUT_FILENO, buf, (size_t)len); |
else |
| 392 |
|
{ |
| 393 |
|
offset = 0; |
| 394 |
|
do |
| 395 |
|
{ |
| 396 |
|
ret = (int)write(STDOUT_FILENO, buf + offset, (size_t)(len - offset)); |
| 397 |
|
if (ret < 0) |
| 398 |
|
{ |
| 399 |
|
log_error("write(STDOUT) error (%d)\n", errno); |
| 400 |
|
loop = 0; |
| 401 |
|
break; |
| 402 |
|
} |
| 403 |
|
offset += ret; |
| 404 |
|
} while (offset < len); |
| 405 |
|
} |
| 406 |
|
|
| 407 |
|
// Restore socket flags |
| 408 |
|
fcntl(sock, F_SETFL, flags); |
| 409 |
} |
} |
|
BBS_last_access_tm = time(0); |
|
| 410 |
} |
} |
| 411 |
} |
} |
| 412 |
|
|
| 492 |
ch = igetch(0); |
ch = igetch(0); |
| 493 |
switch (ch) |
switch (ch) |
| 494 |
{ |
{ |
| 495 |
case KEY_NULL: |
case KEY_NULL: // broken pipe |
| 496 |
return -1; |
case Ctrl('C'): // user cancel |
|
case Ctrl('C'): |
|
| 497 |
return 0; |
return 0; |
| 498 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 499 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 500 |
{ |
{ |
| 501 |
return 0; |
return 0; |
| 502 |
} |
} |
| 503 |
break; |
continue; |
| 504 |
case CR: |
case CR: |
| 505 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
| 506 |
bbsnet_connect(pos); |
bbsnet_connect(pos); |