| 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; |
| 192 |
} |
} |
| 193 |
|
|
| 194 |
sin.sin_family = AF_INET; |
sin.sin_family = AF_INET; |
| 195 |
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); |
|
| 196 |
sin.sin_port = 0; |
sin.sin_port = 0; |
| 197 |
|
|
| 198 |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 199 |
{ |
{ |
| 200 |
log_error("Bind address %s:%u failed\n", |
log_error("Bind address %s:%u failed (%d)\n", |
| 201 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
| 202 |
return -2; |
return -2; |
| 203 |
} |
} |
| 204 |
|
|
| 213 |
|
|
| 214 |
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"); |
| 215 |
process_bar(0, MAX_PROCESS_BAR_LEN); |
process_bar(0, MAX_PROCESS_BAR_LEN); |
| 216 |
|
|
| 217 |
|
// Set socket as non-blocking |
| 218 |
|
flags = fcntl(sock, F_GETFL, 0); |
| 219 |
|
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
| 220 |
|
|
| 221 |
|
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
| 222 |
|
{ |
| 223 |
|
if (errno != EINPROGRESS) |
| 224 |
|
{ |
| 225 |
|
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 226 |
|
press_any_key(); |
| 227 |
|
return -1; |
| 228 |
|
} |
| 229 |
|
} |
| 230 |
|
|
| 231 |
for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) |
for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) |
| 232 |
{ |
{ |
| 233 |
ch = igetch(0); // 100 ms |
ch = igetch(0); // 0.1 second |
| 234 |
if (ch == KEY_NULL || ch == Ctrl('C') || SYS_server_exit) |
if (ch == Ctrl('C') || SYS_server_exit) |
| 235 |
{ |
{ |
| 236 |
return 0; |
return 0; |
| 237 |
} |
} |
| 238 |
|
|
| 239 |
rv = NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin), |
FD_ZERO(&read_fds); |
| 240 |
400, (i == 0 ? 1 : 0)); // 400 ms |
FD_SET(sock, &read_fds); |
| 241 |
|
|
| 242 |
if (rv == ERR_TCPLIB_TIMEOUT) |
FD_ZERO(&write_fds); |
| 243 |
|
FD_SET(sock, &write_fds); |
| 244 |
|
|
| 245 |
|
timeout.tv_sec = 0; |
| 246 |
|
timeout.tv_usec = 400 * 1000; // 0.4 second |
| 247 |
|
|
| 248 |
|
ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout); |
| 249 |
|
|
| 250 |
|
if (ret == 0) // Timeout |
| 251 |
{ |
{ |
| 252 |
process_bar(i + 1, MAX_PROCESS_BAR_LEN); |
process_bar(i + 1, MAX_PROCESS_BAR_LEN); |
|
continue; |
|
| 253 |
} |
} |
| 254 |
else if (rv == 0) |
else if (ret < 0) |
| 255 |
{ |
{ |
| 256 |
break; |
if (errno != EINTR) |
| 257 |
|
{ |
| 258 |
|
log_error("select() error (%d) !\n", errno); |
| 259 |
|
return -1; |
| 260 |
|
} |
| 261 |
} |
} |
| 262 |
else |
// ret > 0 |
| 263 |
|
else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds)) |
| 264 |
{ |
{ |
| 265 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
len = sizeof(error); |
| 266 |
press_any_key(); |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
| 267 |
return -1; |
{ |
| 268 |
|
log_error("getsockopt() error (%d) !\n", error); |
| 269 |
|
return -1; |
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
break; // connected |
| 273 |
} |
} |
| 274 |
} |
} |
| 275 |
if (i == MAX_PROCESS_BAR_LEN) |
if (i == MAX_PROCESS_BAR_LEN) |
| 278 |
press_any_key(); |
press_any_key(); |
| 279 |
return -1; |
return -1; |
| 280 |
} |
} |
| 281 |
setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int)); |
|
| 282 |
|
fcntl(sock, F_SETFL, flags); /* restore file status flags */ |
| 283 |
|
|
| 284 |
|
tos = IPTOS_LOWDELAY; |
| 285 |
|
if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) |
| 286 |
|
{ |
| 287 |
|
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
| 288 |
|
} |
| 289 |
|
|
| 290 |
prints("\033[1;31m连接成功!\033[m\r\n"); |
prints("\033[1;31m连接成功!\033[m\r\n"); |
| 291 |
log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
| 296 |
|
|
| 297 |
while (loop && !SYS_server_exit) |
while (loop && !SYS_server_exit) |
| 298 |
{ |
{ |
| 299 |
FD_ZERO(&testfds); |
FD_ZERO(&read_fds); |
| 300 |
FD_SET(STDIN_FILENO, &testfds); |
FD_SET(STDIN_FILENO, &read_fds); |
| 301 |
FD_SET(sock, &testfds); |
FD_SET(sock, &read_fds); |
| 302 |
|
|
| 303 |
timeout.tv_sec = 0; |
timeout.tv_sec = 0; |
| 304 |
timeout.tv_usec = 100 * 1000; // 0.1 second |
timeout.tv_usec = 100 * 1000; // 0.1 second |
| 305 |
|
|
| 306 |
result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL, |
ret = select(sock + 1, &read_fds, NULL, NULL, &timeout); |
|
(fd_set *)NULL, &timeout); |
|
| 307 |
|
|
| 308 |
if (result == 0) |
if (ret == 0) // timeout |
| 309 |
{ |
{ |
| 310 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 311 |
{ |
{ |
| 312 |
loop = 0; |
loop = 0; |
| 313 |
} |
} |
| 314 |
} |
} |
| 315 |
if (result < 0) |
else if (ret < 0) |
| 316 |
{ |
{ |
| 317 |
log_error("select() error (%d) !\n", result); |
if (errno != EINTR) |
| 318 |
loop = 0; |
{ |
| 319 |
|
log_error("select() error (%d) !\n", errno); |
| 320 |
|
loop = 0; |
| 321 |
|
} |
| 322 |
} |
} |
| 323 |
if (result > 0) |
else if (ret > 0) |
| 324 |
{ |
{ |
| 325 |
if (FD_ISSET(STDIN_FILENO, &testfds)) |
if (FD_ISSET(STDIN_FILENO, &read_fds)) |
| 326 |
{ |
{ |
| 327 |
len = read(STDIN_FILENO, buf, sizeof(buf)); |
len = read(STDIN_FILENO, buf, sizeof(buf)); |
| 328 |
if (len == 0) |
if (len == 0) |
| 330 |
loop = 0; |
loop = 0; |
| 331 |
} |
} |
| 332 |
write(sock, buf, (size_t)len); |
write(sock, buf, (size_t)len); |
| 333 |
|
|
| 334 |
|
BBS_last_access_tm = time(0); |
| 335 |
} |
} |
| 336 |
if (FD_ISSET(sock, &testfds)) |
if (FD_ISSET(sock, &read_fds)) |
| 337 |
{ |
{ |
| 338 |
len = read(sock, buf, sizeof(buf)); |
len = read(sock, buf, sizeof(buf)); |
| 339 |
if (len == 0) |
if (len == 0) |
| 342 |
} |
} |
| 343 |
write(STDOUT_FILENO, buf, (size_t)len); |
write(STDOUT_FILENO, buf, (size_t)len); |
| 344 |
} |
} |
|
BBS_last_access_tm = time(0); |
|
| 345 |
} |
} |
| 346 |
} |
} |
| 347 |
|
|
| 427 |
ch = igetch(0); |
ch = igetch(0); |
| 428 |
switch (ch) |
switch (ch) |
| 429 |
{ |
{ |
|
case KEY_NULL: |
|
|
return -1; |
|
| 430 |
case Ctrl('C'): |
case Ctrl('C'): |
| 431 |
return 0; |
return 0; |
| 432 |
|
case KEY_NULL: |
| 433 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 434 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 435 |
{ |
{ |
| 436 |
return 0; |
return 0; |
| 437 |
} |
} |
| 438 |
break; |
continue; |
| 439 |
case CR: |
case CR: |
| 440 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
| 441 |
bbsnet_connect(pos); |
bbsnet_connect(pos); |