| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
bbs_net.c - description |
bbs_net.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 17 |
#include "bbs.h" |
#include "bbs.h" |
| 18 |
#include "common.h" |
#include "common.h" |
| 19 |
#include "io.h" |
#include "io.h" |
| 20 |
|
#include "log.h" |
| 21 |
|
#include "login.h" |
| 22 |
#include "menu.h" |
#include "menu.h" |
| 23 |
#include "tcplib.h" |
#include "screen.h" |
| 24 |
#include <stdio.h> |
#include <errno.h> |
| 25 |
|
#include <fcntl.h> |
| 26 |
|
#include <netdb.h> |
| 27 |
#include <stdarg.h> |
#include <stdarg.h> |
| 28 |
#include <sys/ioctl.h> |
#include <stdio.h> |
| 29 |
#include <sys/socket.h> |
#include <stdlib.h> |
| 30 |
#include <netinet/in.h> |
#include <string.h> |
|
#include <arpa/inet.h> |
|
| 31 |
#include <time.h> |
#include <time.h> |
| 32 |
#include <unistd.h> |
#include <unistd.h> |
| 33 |
#include <netdb.h> |
#include <arpa/inet.h> |
| 34 |
|
#include <iconv.h> |
| 35 |
|
#include <libssh/libssh.h> |
| 36 |
|
#include <libssh/server.h> |
| 37 |
|
#include <libssh/callbacks.h> |
| 38 |
|
#include <netinet/in.h> |
| 39 |
|
#include <netinet/ip.h> |
| 40 |
|
#include <sys/select.h> |
| 41 |
|
#include <sys/ioctl.h> |
| 42 |
|
#include <sys/socket.h> |
| 43 |
|
#include <sys/epoll.h> |
| 44 |
|
|
| 45 |
|
#define MENU_CONF_DELIM " \t\r\n" |
| 46 |
|
|
| 47 |
#define TIME_OUT 15 |
#define MAX_PROCESS_BAR_LEN 30 |
| 48 |
#define MAX_PROCESS_BAR_LEN 30 |
#define MAXSTATION 26 * 2 |
| 49 |
#define MAXSTATION 26*2 |
#define STATION_PER_LINE 4 |
|
#define STATION_PER_LINE 4 |
|
| 50 |
|
|
| 51 |
struct _bbsnet_conf |
struct _bbsnet_conf |
| 52 |
{ |
{ |
| 53 |
char host1[20]; |
char host1[20]; |
| 54 |
char host2[40]; |
char host2[40]; |
| 55 |
char ip[40]; |
char ip[40]; |
| 56 |
int port; |
in_port_t port; |
| 57 |
|
char charset[20]; |
| 58 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 59 |
|
|
| 60 |
MENU_SET bbsnet_menu; |
MENU_SET bbsnet_menu; |
| 61 |
|
|
| 62 |
int |
int load_bbsnet_conf(const char *file_config) |
|
load_bbsnet_conf (const char *file_config) |
|
| 63 |
{ |
{ |
| 64 |
FILE *fp; |
FILE *fp; |
| 65 |
MENU *p_menu; |
MENU *p_menu; |
| 66 |
MENU_ITEM *p_menuitem; |
MENU_ITEM *p_menu_item; |
| 67 |
char t[256], *t1, *t2, *t3, *t4; |
MENU_ITEM_ID menu_item_id; |
| 68 |
int item_count = 0; |
char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr; |
| 69 |
|
|
| 70 |
fp = fopen (file_config, "r"); |
fp = fopen(file_config, "r"); |
| 71 |
if (fp == NULL) |
if (fp == NULL) |
| 72 |
return -1; |
{ |
| 73 |
|
return -1; |
| 74 |
p_menu = bbsnet_menu.p_menu[0] = malloc (sizeof (MENU)); |
} |
| 75 |
strcpy (p_menu->name, "BBSNET"); |
|
| 76 |
p_menu->title.show = 0; |
bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU)); |
| 77 |
p_menu->screen.show = 0; |
if (bbsnet_menu.p_menu_pool == NULL) |
| 78 |
|
{ |
| 79 |
while (fgets (t, 255, fp) && item_count < MAXSTATION) |
log_error("calloc(p_menu_pool) error\n"); |
| 80 |
{ |
return -3; |
| 81 |
t1 = strtok (t, " \t"); |
} |
| 82 |
t2 = strtok (NULL, " \t\n"); |
bbsnet_menu.menu_count = 1; |
| 83 |
t3 = strtok (NULL, " \t\n"); |
|
| 84 |
t4 = strtok (NULL, " \t\n"); |
bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM)); |
| 85 |
|
if (bbsnet_menu.p_menu_item_pool == NULL) |
| 86 |
if (t1[0] == '#' || t1[0] == '*' || t1 == NULL || t2 == NULL |
{ |
| 87 |
|| t3 == NULL) |
log_error("calloc(p_menu_item_pool) error\n"); |
| 88 |
continue; |
return -3; |
| 89 |
strncpy (bbsnet_conf[item_count].host1, t2, 18); |
} |
| 90 |
bbsnet_conf[item_count].host1[18] = 0; |
bbsnet_menu.menu_item_count = MAXSTATION; |
|
strncpy (bbsnet_conf[item_count].host2, t1, 36); |
|
|
bbsnet_conf[item_count].host2[36] = 0; |
|
|
strncpy (bbsnet_conf[item_count].ip, t3, 36); |
|
|
bbsnet_conf[item_count].ip[36] = 0; |
|
|
bbsnet_conf[item_count].port = t4 ? atoi (t4) : 23; |
|
|
|
|
|
p_menuitem = p_menu->items[item_count] = malloc (sizeof (MENU_ITEM)); |
|
|
p_menuitem->row = 2 + item_count / STATION_PER_LINE; |
|
|
p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20; |
|
|
sprintf (p_menuitem->action, "%d", item_count); |
|
|
p_menuitem->submenu = 0; |
|
|
p_menuitem->priv = 0; |
|
|
p_menuitem->level = 0; |
|
|
p_menuitem->display = 0; |
|
|
p_menuitem->name[0] = |
|
|
(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count); |
|
|
p_menuitem->name[1] = '\0'; |
|
|
sprintf (p_menuitem->text, "[1;36m%c.[m %s", |
|
|
p_menuitem->name[0], bbsnet_conf[item_count].host1); |
|
|
|
|
|
item_count++; |
|
|
} |
|
|
fclose (fp); |
|
|
|
|
|
p_menu->item_count = item_count; |
|
|
p_menu->item_cur_pos = 0; |
|
|
|
|
|
bbsnet_menu.menu_count = 1; |
|
|
bbsnet_menu.menu_select_depth = 0; |
|
|
bbsnet_menu.p_menu_select[0] = bbsnet_menu.p_menu[0]; |
|
| 91 |
|
|
| 92 |
return 0; |
p_menu = (MENU *)get_menu_by_id(&bbsnet_menu, 0); |
| 93 |
|
|
| 94 |
|
strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1); |
| 95 |
|
p_menu->name[sizeof(p_menu->name) - 1] = '\0'; |
| 96 |
|
p_menu->title.show = 0; |
| 97 |
|
p_menu->screen_show = 0; |
| 98 |
|
|
| 99 |
|
menu_item_id = 0; |
| 100 |
|
while (fgets(t, 255, fp) && menu_item_id < MAXSTATION) |
| 101 |
|
{ |
| 102 |
|
t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr); |
| 103 |
|
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 104 |
|
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 105 |
|
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 106 |
|
t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 107 |
|
|
| 108 |
|
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*') |
| 109 |
|
{ |
| 110 |
|
continue; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
strncpy(bbsnet_conf[menu_item_id].host1, t2, sizeof(bbsnet_conf[menu_item_id].host1) - 1); |
| 114 |
|
bbsnet_conf[menu_item_id].host1[sizeof(bbsnet_conf[menu_item_id].host1) - 1] = '\0'; |
| 115 |
|
strncpy(bbsnet_conf[menu_item_id].host2, t1, sizeof(bbsnet_conf[menu_item_id].host2) - 1); |
| 116 |
|
bbsnet_conf[menu_item_id].host2[sizeof(bbsnet_conf[menu_item_id].host2) - 1] = '\0'; |
| 117 |
|
strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1); |
| 118 |
|
bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0'; |
| 119 |
|
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); |
| 120 |
|
strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1); |
| 121 |
|
bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0'; |
| 122 |
|
|
| 123 |
|
p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); |
| 124 |
|
if (p_menu_item == NULL) |
| 125 |
|
{ |
| 126 |
|
log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); |
| 127 |
|
return -1; |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE); |
| 131 |
|
p_menu_item->col = (int16_t)(5 + menu_item_id % STATION_PER_LINE * 20); |
| 132 |
|
snprintf(p_menu_item->action, sizeof(p_menu_item->action), "%d", (int16_t)menu_item_id); |
| 133 |
|
p_menu_item->submenu = 0; |
| 134 |
|
p_menu_item->priv = 0; |
| 135 |
|
p_menu_item->level = 0; |
| 136 |
|
p_menu_item->name[0] = |
| 137 |
|
(char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id); |
| 138 |
|
p_menu_item->name[1] = '\0'; |
| 139 |
|
snprintf(p_menu_item->text, sizeof(p_menu_item->text), "[1;36m%c.[m %s", |
| 140 |
|
p_menu_item->name[0], bbsnet_conf[menu_item_id].host1); |
| 141 |
|
|
| 142 |
|
p_menu->items[p_menu->item_count] = menu_item_id; |
| 143 |
|
p_menu->item_count++; |
| 144 |
|
menu_item_id++; |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
bbsnet_menu.menu_item_count = (int16_t)menu_item_id; |
| 148 |
|
bbsnet_menu.menu_id_path[0] = 0; |
| 149 |
|
bbsnet_menu.menu_item_pos[0] = 0; |
| 150 |
|
bbsnet_menu.choose_step = 0; |
| 151 |
|
|
| 152 |
|
fclose(fp); |
| 153 |
|
|
| 154 |
|
return 0; |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
void unload_bbsnet_conf(void) |
| 158 |
|
{ |
| 159 |
|
bbsnet_menu.menu_count = 0; |
| 160 |
|
bbsnet_menu.menu_item_count = 0; |
| 161 |
|
|
| 162 |
|
free(bbsnet_menu.p_menu_pool); |
| 163 |
|
bbsnet_menu.p_menu_pool = NULL; |
| 164 |
|
free(bbsnet_menu.p_menu_item_pool); |
| 165 |
|
bbsnet_menu.p_menu_item_pool = NULL; |
| 166 |
} |
} |
| 167 |
|
|
| 168 |
// from Maple-hightman |
void process_bar(int n, int len) |
|
// added by flyriver, 2001.3.3 |
|
|
int |
|
|
telnetopt (int fd, char *buf, int max) |
|
| 169 |
{ |
{ |
| 170 |
unsigned char c, d, e; |
char buf[LINE_BUFFER_LEN]; |
| 171 |
int pp = 0; |
char buf2[LINE_BUFFER_LEN]; |
| 172 |
unsigned char tmp[30]; |
|
| 173 |
|
if (len > LINE_BUFFER_LEN) |
| 174 |
while (pp < max) |
{ |
| 175 |
{ |
len = LINE_BUFFER_LEN - 1; |
| 176 |
c = buf[pp++]; |
} |
| 177 |
if (c == 255) |
if (n < 0) |
| 178 |
{ |
{ |
| 179 |
d = buf[pp++]; |
n = 0; |
| 180 |
e = buf[pp++]; |
} |
| 181 |
iflush (); |
else if (n > len) |
| 182 |
if ((d == 253) && (e == 3 || e == 24)) |
{ |
| 183 |
{ |
n = len; |
| 184 |
tmp[0] = 255; |
} |
| 185 |
tmp[1] = 251; |
|
| 186 |
tmp[2] = e; |
moveto(4, 0); |
| 187 |
write (fd, tmp, 3); |
prints(" ------------------------------ \r\n"); |
| 188 |
continue; |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
| 189 |
} |
strncpy(buf2, buf, (size_t)n); |
| 190 |
if ((d == 251 || d == 252) && (e == 1 || e == 3 || e == 24)) |
buf2[n] = '\0'; |
| 191 |
{ |
prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n); |
| 192 |
tmp[0] = 255; |
prints(" ------------------------------ \r\n"); |
| 193 |
tmp[1] = 253; |
iflush(); |
|
tmp[2] = e; |
|
|
write (fd, tmp, 3); |
|
|
continue; |
|
|
} |
|
|
if (d == 251 || d == 252) |
|
|
{ |
|
|
tmp[0] = 255; |
|
|
tmp[1] = 254; |
|
|
tmp[2] = e; |
|
|
write (fd, tmp, 3); |
|
|
continue; |
|
|
} |
|
|
if (d == 253 || d == 254) |
|
|
{ |
|
|
tmp[0] = 255; |
|
|
tmp[1] = 252; |
|
|
tmp[2] = e; |
|
|
write (fd, tmp, 3); |
|
|
continue; |
|
|
} |
|
|
if (d == 250) |
|
|
{ |
|
|
while (e != 240 && pp < max) |
|
|
e = buf[pp++]; |
|
|
tmp[0] = 255; |
|
|
tmp[1] = 250; |
|
|
tmp[2] = 24; |
|
|
tmp[3] = 0; |
|
|
tmp[4] = 65; |
|
|
tmp[5] = 78; |
|
|
tmp[6] = 83; |
|
|
tmp[7] = 73; |
|
|
tmp[8] = 255; |
|
|
tmp[9] = 240; |
|
|
write (fd, tmp, 10); |
|
|
} |
|
|
} |
|
|
else |
|
|
outc (c); |
|
|
} |
|
|
iflush (); |
|
|
return 0; |
|
| 194 |
} |
} |
| 195 |
|
|
| 196 |
static void |
int bbsnet_io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len) |
|
process_bar (int n, int len) |
|
| 197 |
{ |
{ |
| 198 |
char buf[256]; |
char *in_buf; |
| 199 |
char buf2[256]; |
char *out_buf; |
| 200 |
char *ptr; |
size_t in_bytes; |
| 201 |
char *ptr2; |
size_t out_bytes; |
| 202 |
char *ptr3; |
int ret; |
| 203 |
|
|
| 204 |
moveto (4, 0); |
in_buf = p_buf + *p_buf_offset; |
| 205 |
prints ("\n"); |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 206 |
sprintf (buf2, " %3d%% ", n * 100 / len); |
out_buf = p_conv + *p_conv_len; |
| 207 |
ptr = buf; |
out_bytes = conv_size - (size_t)(*p_conv_len); |
| 208 |
ptr2 = buf2; |
|
| 209 |
ptr3 = buf + n; |
while (in_bytes > 0) |
| 210 |
while (ptr != ptr3) |
{ |
| 211 |
*ptr++ = *ptr2++; |
ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes); |
| 212 |
*ptr++ = '\x1b'; |
if (ret == -1) |
| 213 |
*ptr++ = '['; |
{ |
| 214 |
*ptr++ = '4'; |
if (errno == EINVAL) // Incomplete |
| 215 |
*ptr++ = '4'; |
{ |
| 216 |
*ptr++ = 'm'; |
#ifdef _DEBUG |
| 217 |
while (*ptr2 != '\0') |
log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes); |
| 218 |
*ptr++ = *ptr2++; |
#endif |
| 219 |
*ptr++ = '\0'; |
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf); |
| 220 |
prints ("\033[46m%s\033[m\n", buf); |
*p_buf_offset = 0; |
| 221 |
prints ("\n"); |
*p_conv_len = (int)(conv_size - out_bytes); |
| 222 |
|
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
| 223 |
|
|
| 224 |
|
break; |
| 225 |
|
} |
| 226 |
|
else if (errno == E2BIG) |
| 227 |
|
{ |
| 228 |
|
log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); |
| 229 |
|
return -1; |
| 230 |
|
} |
| 231 |
|
else if (errno == EILSEQ) |
| 232 |
|
{ |
| 233 |
|
if (in_bytes > out_bytes || out_bytes <= 0) |
| 234 |
|
{ |
| 235 |
|
log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); |
| 236 |
|
return -2; |
| 237 |
|
} |
| 238 |
|
|
| 239 |
|
*out_buf = *in_buf; |
| 240 |
|
in_buf++; |
| 241 |
|
out_buf++; |
| 242 |
|
in_bytes--; |
| 243 |
|
out_bytes--; |
| 244 |
|
|
| 245 |
|
continue; |
| 246 |
|
} |
| 247 |
|
} |
| 248 |
|
else |
| 249 |
|
{ |
| 250 |
|
*p_buf_len = 0; |
| 251 |
|
*p_buf_offset = 0; |
| 252 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 253 |
|
|
| 254 |
|
break; |
| 255 |
|
} |
| 256 |
|
} |
| 257 |
|
|
| 258 |
|
return 0; |
| 259 |
} |
} |
| 260 |
|
|
| 261 |
int |
int bbsnet_connect(int n) |
|
bbsnet_connect (int n) |
|
| 262 |
{ |
{ |
| 263 |
int sock, ch, result, len, loop; |
int sock, ret, loop, error; |
| 264 |
struct sockaddr_in sin; |
int sock_connected = 0; |
| 265 |
char buf[256]; |
int flags_sock; |
| 266 |
fd_set inputs, testfds; |
int flags_stdin; |
| 267 |
struct timeval timeout; |
int flags_stdout; |
| 268 |
struct hostent *pHost = NULL; |
int len; |
| 269 |
int rc, rv, tos = 020, i; |
struct sockaddr_in sin; |
| 270 |
|
char input_buf[LINE_BUFFER_LEN]; |
| 271 |
prints ("\033[1;32mڲ %s (%s) ӣԺ... \033[m\n", |
char output_buf[LINE_BUFFER_LEN]; |
| 272 |
bbsnet_conf[n].host1, bbsnet_conf[n].ip); |
int input_buf_len = 0; |
| 273 |
prints ("\033[1;32m %d ϣӡ\033[m\n", |
int output_buf_len = 0; |
| 274 |
TIME_OUT); |
int input_buf_offset = 0; |
| 275 |
|
int output_buf_offset = 0; |
| 276 |
pHost = gethostbyname (bbsnet_conf[n].ip); |
iconv_t input_cd = NULL; |
| 277 |
|
char input_conv[LINE_BUFFER_LEN * 2]; |
| 278 |
if (pHost == NULL) |
char output_conv[LINE_BUFFER_LEN * 2]; |
| 279 |
{ |
int input_conv_len = 0; |
| 280 |
prints ("\033[1;31mʧܣ\033[m\n"); |
int output_conv_len = 0; |
| 281 |
press_any_key (); |
int input_conv_offset = 0; |
| 282 |
return -1; |
int output_conv_offset = 0; |
| 283 |
} |
iconv_t output_cd = NULL; |
| 284 |
|
struct epoll_event ev, events[MAX_EVENTS]; |
| 285 |
sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
int nfds, epollfd; |
| 286 |
|
int stdin_read_wait = 0; |
| 287 |
if (sock < 0) |
int stdout_write_wait = 0; |
| 288 |
{ |
int sock_read_wait = 0; |
| 289 |
prints ("\033[1;31msocket\033[m\n"); |
int sock_write_wait = 0; |
| 290 |
press_any_key (); |
struct hostent *p_host = NULL; |
| 291 |
return -1; |
int tos; |
| 292 |
} |
char remote_addr[IP_ADDR_LEN]; |
| 293 |
|
int remote_port; |
| 294 |
sin.sin_family = AF_INET; |
time_t t_used = time(NULL); |
| 295 |
sin.sin_addr.s_addr = |
struct tm *tm_used; |
| 296 |
(strlen (hostaddr_server) > 0 ? inet_addr (hostaddr_server) : INADDR_ANY); |
int ch; |
| 297 |
sin.sin_port = 0; |
|
| 298 |
|
if (user_online_update("BBS_NET") < 0) |
| 299 |
if (bind (sock, (struct sockaddr *) &sin, sizeof (sin)) < 0) |
{ |
| 300 |
{ |
log_error("user_online_update(BBS_NET) error\n"); |
| 301 |
log_error ("Bind address %s:%u failed\n", |
} |
| 302 |
inet_ntoa (sin.sin_addr), ntohs (sin.sin_port)); |
|
| 303 |
return -2; |
clearscr(); |
| 304 |
} |
|
| 305 |
|
moveto(0, 0); |
| 306 |
bzero (&sin, sizeof (sin)); |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
| 307 |
sin.sin_family = AF_INET; |
bbsnet_conf[n].host1, bbsnet_conf[n].ip); |
| 308 |
sin.sin_port = htons (bbsnet_conf[n].port); |
iflush(); |
| 309 |
sin.sin_addr = *(struct in_addr *) pHost->h_addr_list[0]; |
|
| 310 |
|
p_host = gethostbyname(bbsnet_conf[n].ip); |
| 311 |
|
|
| 312 |
prints ("\033[1;32mʾǰʹõʱ䡣\033[m\n"); |
if (p_host == NULL) |
| 313 |
process_bar (0, MAX_PROCESS_BAR_LEN); |
{ |
| 314 |
for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) |
prints("\033[1;31m查找主机名失败!\033[m\r\n"); |
| 315 |
{ |
press_any_key(); |
| 316 |
if (i == 0) |
return -1; |
| 317 |
rv = |
} |
| 318 |
NonBlockConnectEx (sock, (struct sockaddr *) &sin, sizeof (sin), |
|
| 319 |
500, 1); |
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 320 |
else |
|
| 321 |
rv = |
if (sock < 0) |
| 322 |
NonBlockConnectEx (sock, (struct sockaddr *) &sin, sizeof (sin), |
{ |
| 323 |
500, 0); |
prints("\033[1;31m无法创建socket!\033[m\r\n"); |
| 324 |
if (rv == ERR_TCPLIB_TIMEOUT) |
press_any_key(); |
| 325 |
{ |
return -1; |
| 326 |
process_bar (i + 1, MAX_PROCESS_BAR_LEN); |
} |
| 327 |
continue; |
|
| 328 |
} |
sin.sin_family = AF_INET; |
| 329 |
else if (rv == 0) |
sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY); |
| 330 |
break; |
sin.sin_port = 0; |
| 331 |
else |
|
| 332 |
{ |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 333 |
prints ("\033[1;31mʧܣ\033[m\n"); |
{ |
| 334 |
press_any_key (); |
log_error("Bind address %s:%u failed (%d)\n", |
| 335 |
return -1; |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
| 336 |
} |
return -2; |
| 337 |
} |
} |
| 338 |
if (i == MAX_PROCESS_BAR_LEN) |
|
| 339 |
{ |
memset(&sin, 0, sizeof(sin)); |
| 340 |
prints ("\033[1;31mӳʱ\033[m\n"); |
sin.sin_family = AF_INET; |
| 341 |
press_any_key (); |
sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0]; |
| 342 |
return -1; |
sin.sin_port = htons(bbsnet_conf[n].port); |
| 343 |
} |
|
| 344 |
setsockopt (sock, IPPROTO_IP, IP_TOS, &tos, sizeof (int)); |
strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1); |
| 345 |
prints ("\033[1;31mӳɹ\033[m\n"); |
remote_addr[sizeof(remote_addr) - 1] = '\0'; |
| 346 |
|
remote_port = ntohs(sin.sin_port); |
| 347 |
FD_ZERO (&inputs); |
|
| 348 |
FD_SET (0, &inputs); |
prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); |
| 349 |
FD_SET (sock, &inputs); |
process_bar(0, MAX_PROCESS_BAR_LEN); |
| 350 |
|
|
| 351 |
BBS_last_access_tm = time (0); |
// Set socket as non-blocking |
| 352 |
|
flags_sock = fcntl(sock, F_GETFL, 0); |
| 353 |
loop = 1; |
fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); |
| 354 |
|
|
| 355 |
while (loop) |
// Set STDIN/STDOUT as non-blocking |
| 356 |
{ |
flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 357 |
testfds = inputs; |
flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 358 |
timeout.tv_sec = TIME_OUT; |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); |
| 359 |
timeout.tv_usec = 0; |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); |
| 360 |
|
|
| 361 |
result = select (FD_SETSIZE, &testfds, (fd_set *) NULL, |
epollfd = epoll_create1(0); |
| 362 |
(fd_set *) NULL, &timeout); |
if (epollfd < 0) |
| 363 |
|
{ |
| 364 |
switch (result) |
log_error("epoll_create1() error (%d)\n", errno); |
| 365 |
{ |
return -1; |
| 366 |
case 0: |
} |
| 367 |
if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
|
| 368 |
{ |
ev.events = EPOLLOUT | EPOLLET; |
| 369 |
loop = 0; |
ev.data.fd = sock; |
| 370 |
} |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) |
| 371 |
break; |
{ |
| 372 |
case -1: |
log_error("epoll_ctl(socket) error (%d)\n", errno); |
| 373 |
log_error ("select() error!\n"); |
goto cleanup; |
| 374 |
break; |
} |
| 375 |
default: |
|
| 376 |
if (FD_ISSET (0, &testfds)) |
ev.events = EPOLLIN | EPOLLET; |
| 377 |
{ |
ev.data.fd = STDIN_FILENO; |
| 378 |
len = read (0, buf, 255); |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 379 |
if (len == 0) |
{ |
| 380 |
{ |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 381 |
loop = 0; |
goto cleanup; |
| 382 |
break; |
} |
|
} |
|
|
write (sock, buf, len); |
|
|
} |
|
|
if (FD_ISSET (sock, &testfds)) |
|
|
{ |
|
|
len = read (sock, buf, 255); |
|
|
if (len == 0) |
|
|
{ |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
write (1, buf, len); |
|
|
} |
|
|
break; |
|
|
BBS_last_access_tm = time (0); |
|
|
} |
|
|
} |
|
|
|
|
|
if (close (sock) == -1) |
|
|
{ |
|
|
log_error ("Close socket failed\n"); |
|
|
} |
|
| 383 |
|
|
| 384 |
return 0; |
while (!SYS_server_exit) |
| 385 |
|
{ |
| 386 |
|
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
| 387 |
|
{ |
| 388 |
|
if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS) |
| 389 |
|
{ |
| 390 |
|
// Use select / epoll to check writability of the socket, |
| 391 |
|
// then use getsockopt to check the status of the socket. |
| 392 |
|
// See man connect(2) |
| 393 |
|
break; |
| 394 |
|
} |
| 395 |
|
else if (errno == EINTR) |
| 396 |
|
{ |
| 397 |
|
continue; |
| 398 |
|
} |
| 399 |
|
else |
| 400 |
|
{ |
| 401 |
|
log_error("connect(socket) error (%d)\n", errno); |
| 402 |
|
|
| 403 |
|
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 404 |
|
press_any_key(); |
| 405 |
|
|
| 406 |
|
goto cleanup; |
| 407 |
|
} |
| 408 |
|
} |
| 409 |
|
} |
| 410 |
|
|
| 411 |
|
for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) |
| 412 |
|
{ |
| 413 |
|
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second |
| 414 |
|
|
| 415 |
|
if (nfds < 0) |
| 416 |
|
{ |
| 417 |
|
if (errno != EINTR) |
| 418 |
|
{ |
| 419 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 420 |
|
break; |
| 421 |
|
} |
| 422 |
|
} |
| 423 |
|
else if (nfds == 0) // timeout |
| 424 |
|
{ |
| 425 |
|
process_bar(j + 1, MAX_PROCESS_BAR_LEN); |
| 426 |
|
} |
| 427 |
|
else // ret > 0 |
| 428 |
|
{ |
| 429 |
|
for (int i = 0; i < nfds; i++) |
| 430 |
|
{ |
| 431 |
|
if (events[i].data.fd == sock) |
| 432 |
|
{ |
| 433 |
|
len = sizeof(error); |
| 434 |
|
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
| 435 |
|
{ |
| 436 |
|
log_error("getsockopt() error (%d) !\n", error); |
| 437 |
|
goto cleanup; |
| 438 |
|
} |
| 439 |
|
if (error == 0) |
| 440 |
|
{ |
| 441 |
|
sock_connected = 1; |
| 442 |
|
} |
| 443 |
|
} |
| 444 |
|
else if (events[i].data.fd == STDIN_FILENO) |
| 445 |
|
{ |
| 446 |
|
ch = igetch(0); |
| 447 |
|
if (ch == Ctrl('C') || ch == KEY_ESC) |
| 448 |
|
{ |
| 449 |
|
goto cleanup; |
| 450 |
|
} |
| 451 |
|
} |
| 452 |
|
} |
| 453 |
|
} |
| 454 |
|
} |
| 455 |
|
if (SYS_server_exit) |
| 456 |
|
{ |
| 457 |
|
goto cleanup; |
| 458 |
|
} |
| 459 |
|
if (!sock_connected) |
| 460 |
|
{ |
| 461 |
|
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 462 |
|
press_any_key(); |
| 463 |
|
|
| 464 |
|
goto cleanup; |
| 465 |
|
} |
| 466 |
|
|
| 467 |
|
tos = IPTOS_LOWDELAY; |
| 468 |
|
if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) |
| 469 |
|
{ |
| 470 |
|
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
| 471 |
|
} |
| 472 |
|
|
| 473 |
|
prints("\033[1;31m连接成功!\033[m\r\n"); |
| 474 |
|
iflush(); |
| 475 |
|
log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
| 476 |
|
|
| 477 |
|
input_cd = iconv_open(bbsnet_conf[n].charset, "UTF-8"); |
| 478 |
|
if (input_cd == (iconv_t)(-1)) |
| 479 |
|
{ |
| 480 |
|
log_error("iconv_open(UTF8->GBK) error: %d\n", errno); |
| 481 |
|
goto cleanup; |
| 482 |
|
} |
| 483 |
|
output_cd = iconv_open("UTF-8", bbsnet_conf[n].charset); |
| 484 |
|
if (input_cd == (iconv_t)(-1)) |
| 485 |
|
{ |
| 486 |
|
log_error("iconv_open(GBK->UTF-8) error: %d\n", errno); |
| 487 |
|
iconv_close(input_cd); |
| 488 |
|
goto cleanup; |
| 489 |
|
} |
| 490 |
|
|
| 491 |
|
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
| 492 |
|
ev.data.fd = sock; |
| 493 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
| 494 |
|
{ |
| 495 |
|
log_error("epoll_ctl(socket) error (%d)\n", errno); |
| 496 |
|
goto cleanup; |
| 497 |
|
} |
| 498 |
|
|
| 499 |
|
ev.events = EPOLLOUT | EPOLLET; |
| 500 |
|
ev.data.fd = STDOUT_FILENO; |
| 501 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 502 |
|
{ |
| 503 |
|
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 504 |
|
goto cleanup; |
| 505 |
|
} |
| 506 |
|
|
| 507 |
|
BBS_last_access_tm = t_used = time(NULL); |
| 508 |
|
loop = 1; |
| 509 |
|
|
| 510 |
|
while (loop && !SYS_server_exit) |
| 511 |
|
{ |
| 512 |
|
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 513 |
|
{ |
| 514 |
|
log_error("SSH channel is closed\n"); |
| 515 |
|
loop = 0; |
| 516 |
|
break; |
| 517 |
|
} |
| 518 |
|
|
| 519 |
|
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 520 |
|
|
| 521 |
|
if (nfds < 0) |
| 522 |
|
{ |
| 523 |
|
if (errno != EINTR) |
| 524 |
|
{ |
| 525 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 526 |
|
break; |
| 527 |
|
} |
| 528 |
|
continue; |
| 529 |
|
} |
| 530 |
|
else if (nfds == 0) // timeout |
| 531 |
|
{ |
| 532 |
|
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 533 |
|
{ |
| 534 |
|
break; |
| 535 |
|
} |
| 536 |
|
} |
| 537 |
|
|
| 538 |
|
for (int i = 0; i < nfds; i++) |
| 539 |
|
{ |
| 540 |
|
if (events[i].data.fd == STDIN_FILENO) |
| 541 |
|
{ |
| 542 |
|
stdin_read_wait = 1; |
| 543 |
|
} |
| 544 |
|
|
| 545 |
|
if (events[i].data.fd == sock) |
| 546 |
|
{ |
| 547 |
|
if (events[i].events & EPOLLIN) |
| 548 |
|
{ |
| 549 |
|
sock_read_wait = 1; |
| 550 |
|
} |
| 551 |
|
if (events[i].events & EPOLLOUT) |
| 552 |
|
{ |
| 553 |
|
sock_write_wait = 1; |
| 554 |
|
} |
| 555 |
|
} |
| 556 |
|
|
| 557 |
|
if (events[i].data.fd == STDOUT_FILENO) |
| 558 |
|
{ |
| 559 |
|
stdout_write_wait = 1; |
| 560 |
|
} |
| 561 |
|
} |
| 562 |
|
|
| 563 |
|
if (stdin_read_wait) |
| 564 |
|
{ |
| 565 |
|
while (input_buf_len < sizeof(input_buf) && !SYS_server_exit) |
| 566 |
|
{ |
| 567 |
|
if (SSH_v2) |
| 568 |
|
{ |
| 569 |
|
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); |
| 570 |
|
if (ret == SSH_ERROR) |
| 571 |
|
{ |
| 572 |
|
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 573 |
|
loop = 0; |
| 574 |
|
break; |
| 575 |
|
} |
| 576 |
|
else if (ret == SSH_EOF) |
| 577 |
|
{ |
| 578 |
|
stdin_read_wait = 0; |
| 579 |
|
loop = 0; |
| 580 |
|
break; |
| 581 |
|
} |
| 582 |
|
else if (ret == 0) |
| 583 |
|
{ |
| 584 |
|
stdin_read_wait = 0; |
| 585 |
|
break; // Check whether channel is still open |
| 586 |
|
} |
| 587 |
|
} |
| 588 |
|
else |
| 589 |
|
{ |
| 590 |
|
ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len); |
| 591 |
|
} |
| 592 |
|
if (ret < 0) |
| 593 |
|
{ |
| 594 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 595 |
|
{ |
| 596 |
|
stdin_read_wait = 0; |
| 597 |
|
break; |
| 598 |
|
} |
| 599 |
|
else if (errno == EINTR) |
| 600 |
|
{ |
| 601 |
|
continue; |
| 602 |
|
} |
| 603 |
|
else |
| 604 |
|
{ |
| 605 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 606 |
|
loop = 0; |
| 607 |
|
break; |
| 608 |
|
} |
| 609 |
|
} |
| 610 |
|
else if (ret == 0) // broken pipe |
| 611 |
|
{ |
| 612 |
|
#ifdef _DEBUG |
| 613 |
|
log_error("read(STDIN) EOF\n"); |
| 614 |
|
#endif |
| 615 |
|
stdin_read_wait = 0; |
| 616 |
|
loop = 0; |
| 617 |
|
break; |
| 618 |
|
} |
| 619 |
|
else |
| 620 |
|
{ |
| 621 |
|
input_buf_len += ret; |
| 622 |
|
BBS_last_access_tm = time(NULL); |
| 623 |
|
|
| 624 |
|
// Refresh current action while user input |
| 625 |
|
if (user_online_update("BBS_NET") < 0) |
| 626 |
|
{ |
| 627 |
|
log_error("user_online_update(BBS_NET) error\n"); |
| 628 |
|
} |
| 629 |
|
|
| 630 |
|
continue; |
| 631 |
|
} |
| 632 |
|
} |
| 633 |
|
} |
| 634 |
|
|
| 635 |
|
if (sock_write_wait) |
| 636 |
|
{ |
| 637 |
|
if (input_buf_offset < input_buf_len) |
| 638 |
|
{ |
| 639 |
|
ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); |
| 640 |
|
if (ret < 0) |
| 641 |
|
{ |
| 642 |
|
log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); |
| 643 |
|
} |
| 644 |
|
} |
| 645 |
|
|
| 646 |
|
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
| 647 |
|
{ |
| 648 |
|
ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset)); |
| 649 |
|
if (ret < 0) |
| 650 |
|
{ |
| 651 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 652 |
|
{ |
| 653 |
|
sock_write_wait = 0; |
| 654 |
|
break; |
| 655 |
|
} |
| 656 |
|
else if (errno == EINTR) |
| 657 |
|
{ |
| 658 |
|
continue; |
| 659 |
|
} |
| 660 |
|
else |
| 661 |
|
{ |
| 662 |
|
log_error("write(socket) error (%d)\n", errno); |
| 663 |
|
loop = 0; |
| 664 |
|
break; |
| 665 |
|
} |
| 666 |
|
} |
| 667 |
|
else if (ret == 0) // broken pipe |
| 668 |
|
{ |
| 669 |
|
#ifdef _DEBUG |
| 670 |
|
log_error("write(socket) EOF\n"); |
| 671 |
|
#endif |
| 672 |
|
sock_write_wait = 0; |
| 673 |
|
loop = 0; |
| 674 |
|
break; |
| 675 |
|
} |
| 676 |
|
else |
| 677 |
|
{ |
| 678 |
|
input_conv_offset += ret; |
| 679 |
|
if (input_conv_offset >= input_conv_len) // Output buffer complete |
| 680 |
|
{ |
| 681 |
|
input_conv_offset = 0; |
| 682 |
|
input_conv_len = 0; |
| 683 |
|
break; |
| 684 |
|
} |
| 685 |
|
continue; |
| 686 |
|
} |
| 687 |
|
} |
| 688 |
|
} |
| 689 |
|
|
| 690 |
|
if (sock_read_wait) |
| 691 |
|
{ |
| 692 |
|
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) |
| 693 |
|
{ |
| 694 |
|
ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); |
| 695 |
|
if (ret < 0) |
| 696 |
|
{ |
| 697 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 698 |
|
{ |
| 699 |
|
sock_read_wait = 0; |
| 700 |
|
break; |
| 701 |
|
} |
| 702 |
|
else if (errno == EINTR) |
| 703 |
|
{ |
| 704 |
|
continue; |
| 705 |
|
} |
| 706 |
|
else |
| 707 |
|
{ |
| 708 |
|
log_error("read(socket) error (%d)\n", errno); |
| 709 |
|
loop = 0; |
| 710 |
|
break; |
| 711 |
|
} |
| 712 |
|
} |
| 713 |
|
else if (ret == 0) // broken pipe |
| 714 |
|
{ |
| 715 |
|
#ifdef _DEBUG |
| 716 |
|
log_error("read(socket) EOF\n"); |
| 717 |
|
#endif |
| 718 |
|
sock_read_wait = 0; |
| 719 |
|
loop = 0; |
| 720 |
|
break; |
| 721 |
|
} |
| 722 |
|
else |
| 723 |
|
{ |
| 724 |
|
output_buf_len += ret; |
| 725 |
|
continue; |
| 726 |
|
} |
| 727 |
|
} |
| 728 |
|
} |
| 729 |
|
|
| 730 |
|
if (stdout_write_wait) |
| 731 |
|
{ |
| 732 |
|
if (output_buf_offset < output_buf_len) |
| 733 |
|
{ |
| 734 |
|
ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len); |
| 735 |
|
if (ret < 0) |
| 736 |
|
{ |
| 737 |
|
log_error("bbsnet_io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); |
| 738 |
|
} |
| 739 |
|
} |
| 740 |
|
|
| 741 |
|
while (output_conv_offset < output_conv_len && !SYS_server_exit) |
| 742 |
|
{ |
| 743 |
|
if (SSH_v2) |
| 744 |
|
{ |
| 745 |
|
ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); |
| 746 |
|
if (ret == SSH_ERROR) |
| 747 |
|
{ |
| 748 |
|
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
| 749 |
|
loop = 0; |
| 750 |
|
break; |
| 751 |
|
} |
| 752 |
|
} |
| 753 |
|
else |
| 754 |
|
{ |
| 755 |
|
ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset)); |
| 756 |
|
} |
| 757 |
|
if (ret < 0) |
| 758 |
|
{ |
| 759 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 760 |
|
{ |
| 761 |
|
stdout_write_wait = 0; |
| 762 |
|
break; |
| 763 |
|
} |
| 764 |
|
else if (errno == EINTR) |
| 765 |
|
{ |
| 766 |
|
continue; |
| 767 |
|
} |
| 768 |
|
else |
| 769 |
|
{ |
| 770 |
|
log_error("write(STDOUT) error (%d)\n", errno); |
| 771 |
|
loop = 0; |
| 772 |
|
break; |
| 773 |
|
} |
| 774 |
|
} |
| 775 |
|
else if (ret == 0) // broken pipe |
| 776 |
|
{ |
| 777 |
|
#ifdef _DEBUG |
| 778 |
|
log_error("write(STDOUT) EOF\n"); |
| 779 |
|
#endif |
| 780 |
|
stdout_write_wait = 0; |
| 781 |
|
loop = 0; |
| 782 |
|
break; |
| 783 |
|
} |
| 784 |
|
else |
| 785 |
|
{ |
| 786 |
|
output_conv_offset += ret; |
| 787 |
|
if (output_conv_offset >= output_conv_len) // Output buffer complete |
| 788 |
|
{ |
| 789 |
|
output_conv_offset = 0; |
| 790 |
|
output_conv_len = 0; |
| 791 |
|
break; |
| 792 |
|
} |
| 793 |
|
continue; |
| 794 |
|
} |
| 795 |
|
} |
| 796 |
|
} |
| 797 |
|
} |
| 798 |
|
|
| 799 |
|
iconv_close(input_cd); |
| 800 |
|
iconv_close(output_cd); |
| 801 |
|
|
| 802 |
|
cleanup: |
| 803 |
|
if (close(epollfd) < 0) |
| 804 |
|
{ |
| 805 |
|
log_error("close(epoll) error (%d)\n"); |
| 806 |
|
} |
| 807 |
|
|
| 808 |
|
// Restore STDIN/STDOUT flags |
| 809 |
|
fcntl(STDIN_FILENO, F_SETFL, flags_stdin); |
| 810 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); |
| 811 |
|
|
| 812 |
|
// Restore socket flags |
| 813 |
|
fcntl(sock, F_SETFL, flags_sock); |
| 814 |
|
|
| 815 |
|
if (close(sock) == -1) |
| 816 |
|
{ |
| 817 |
|
log_error("Close socket failed\n"); |
| 818 |
|
} |
| 819 |
|
|
| 820 |
|
t_used = time(NULL) - t_used; |
| 821 |
|
tm_used = gmtime(&t_used); |
| 822 |
|
|
| 823 |
|
log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n", |
| 824 |
|
tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, |
| 825 |
|
tm_used->tm_sec); |
| 826 |
|
|
| 827 |
|
return 0; |
| 828 |
} |
} |
| 829 |
|
|
| 830 |
static int |
static int |
| 831 |
bbsnet_refresh () |
bbsnet_refresh() |
| 832 |
{ |
{ |
| 833 |
int i; |
clearscr(); |
| 834 |
|
moveto(1, 0); |
| 835 |
|
prints(" ----------------------------------------------------------------------------- "); |
| 836 |
|
for (int i = 2; i < 19; i++) |
| 837 |
|
{ |
| 838 |
|
moveto(i, 0); |
| 839 |
|
prints("|"); |
| 840 |
|
moveto(i, 79); |
| 841 |
|
prints("|"); |
| 842 |
|
} |
| 843 |
|
moveto(19, 0); |
| 844 |
|
prints("|-----------------------------------------------------------------------------|"); |
| 845 |
|
moveto(22, 0); |
| 846 |
|
prints(" ----------------------------------------------------------------------------- "); |
| 847 |
|
moveto(23, 0); |
| 848 |
|
prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); |
| 849 |
|
|
| 850 |
clearscr (); |
iflush(); |
|
moveto (1, 0); |
|
|
prints |
|
|
("qTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTr"); |
|
|
for (i = 2; i < 19; i++) |
|
|
{ |
|
|
moveto (i, 0); |
|
|
prints ("U"); |
|
|
moveto (i, 79); |
|
|
prints ("U"); |
|
|
} |
|
|
moveto (19, 0); |
|
|
prints |
|
|
("UU"); |
|
|
moveto (22, 0); |
|
|
prints |
|
|
("tTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTs"); |
|
|
moveto (23, 0); |
|
|
prints (" [\x1b[1;32mCtrl+C\x1b[m]˳"); |
|
|
iflush (); |
|
| 851 |
|
|
| 852 |
return 0; |
return 0; |
| 853 |
} |
} |
| 854 |
|
|
| 855 |
int |
int bbsnet_selchange() |
|
bbsnet_selchange (int new_pos) |
|
| 856 |
{ |
{ |
| 857 |
moveto (20, 0); |
int i = bbsnet_menu.menu_item_pos[0]; |
|
clrtoeol (); |
|
|
prints ("U\x1b[1mλ:\x1b[1;33m%-18s\x1b[m վ:\x1b[1;33m%s\x1b[m", |
|
|
bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1); |
|
|
moveto (20, 79); |
|
|
prints ("U"); |
|
|
moveto (21, 0); |
|
|
clrtoeol (); |
|
|
prints ("U\x1b[1m:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip); |
|
|
if (bbsnet_conf[new_pos].port != 23) |
|
|
prints (" %d", bbsnet_conf[new_pos].port); |
|
|
prints ("\x1b[m"); |
|
|
moveto (21, 79); |
|
|
prints ("U"); |
|
|
iflush (); |
|
| 858 |
|
|
| 859 |
return 0; |
moveto(20, 0); |
| 860 |
|
clrtoeol(); |
| 861 |
|
prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m 站名:\x1b[1;33m%s\x1b[m", |
| 862 |
|
bbsnet_conf[i].host2, bbsnet_conf[i].host1); |
| 863 |
|
moveto(20, 79); |
| 864 |
|
prints("|"); |
| 865 |
|
moveto(21, 0); |
| 866 |
|
clrtoeol(); |
| 867 |
|
prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip); |
| 868 |
|
if (bbsnet_conf[i].port != 23) |
| 869 |
|
{ |
| 870 |
|
prints(" %d", bbsnet_conf[i].port); |
| 871 |
|
} |
| 872 |
|
prints("\x1b[m"); |
| 873 |
|
moveto(21, 79); |
| 874 |
|
prints("|"); |
| 875 |
|
iflush(); |
| 876 |
|
|
| 877 |
|
return 0; |
| 878 |
} |
} |
| 879 |
|
|
| 880 |
int |
int bbs_net() |
|
bbs_net () |
|
| 881 |
{ |
{ |
| 882 |
int ch, pos, i; |
int ch, i; |
| 883 |
char file_config[256]; |
|
| 884 |
|
load_bbsnet_conf(CONF_BBSNET); |
| 885 |
|
|
| 886 |
strcpy (file_config, app_home_dir); |
BBS_last_access_tm = time(NULL); |
|
strcat (file_config, "conf/bbsnet.conf"); |
|
| 887 |
|
|
| 888 |
load_bbsnet_conf (file_config); |
clearscr(); |
| 889 |
|
bbsnet_refresh(); |
| 890 |
|
display_menu(&bbsnet_menu); |
| 891 |
|
bbsnet_selchange(); |
| 892 |
|
|
| 893 |
BBS_last_access_tm = time (0); |
while (!SYS_server_exit) |
| 894 |
|
{ |
| 895 |
clearscr (); |
ch = igetch(100); |
| 896 |
bbsnet_refresh (); |
|
| 897 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
switch (ch) |
| 898 |
display_menu (get_menu (&bbsnet_menu, "BBSNET")); |
{ |
| 899 |
bbsnet_selchange (pos); |
case KEY_NULL: // broken pipe |
| 900 |
|
case KEY_ESC: |
| 901 |
while (1) |
case Ctrl('C'): // user cancel |
| 902 |
{ |
goto cleanup; |
| 903 |
ch = igetch (); |
case KEY_TIMEOUT: |
| 904 |
switch (ch) |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 905 |
{ |
{ |
| 906 |
case KEY_NULL: |
goto cleanup; |
| 907 |
case Ctrl ('C'): |
} |
| 908 |
return 0; |
continue; |
| 909 |
case KEY_TIMEOUT: |
case CR: |
| 910 |
if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
igetch_reset(); |
| 911 |
{ |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]); |
| 912 |
return -1; |
bbsnet_refresh(); |
| 913 |
} |
display_menu(&bbsnet_menu); |
| 914 |
continue; |
bbsnet_selchange(); |
| 915 |
case CR: |
break; |
| 916 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
case KEY_UP: |
| 917 |
bbsnet_connect (pos); |
for (i = 0; i < STATION_PER_LINE; i++) |
| 918 |
bbsnet_refresh (); |
{ |
| 919 |
display_current_menu (&bbsnet_menu); |
menu_control(&bbsnet_menu, KEY_UP); |
| 920 |
bbsnet_selchange (pos); |
} |
| 921 |
break; |
bbsnet_selchange(); |
| 922 |
case KEY_UP: |
break; |
| 923 |
for (i = 0; i < STATION_PER_LINE; i++) |
case KEY_DOWN: |
| 924 |
menu_control (&bbsnet_menu, KEY_UP); |
for (i = 0; i < STATION_PER_LINE; i++) |
| 925 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
{ |
| 926 |
bbsnet_selchange (pos); |
menu_control(&bbsnet_menu, KEY_DOWN); |
| 927 |
break; |
} |
| 928 |
case KEY_DOWN: |
bbsnet_selchange(); |
| 929 |
for (i = 0; i < STATION_PER_LINE; i++) |
break; |
| 930 |
menu_control (&bbsnet_menu, KEY_DOWN); |
case KEY_LEFT: |
| 931 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
menu_control(&bbsnet_menu, KEY_UP); |
| 932 |
bbsnet_selchange (pos); |
bbsnet_selchange(); |
| 933 |
break; |
break; |
| 934 |
case KEY_LEFT: |
case KEY_RIGHT: |
| 935 |
menu_control (&bbsnet_menu, KEY_UP); |
menu_control(&bbsnet_menu, KEY_DOWN); |
| 936 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
bbsnet_selchange(); |
| 937 |
bbsnet_selchange (pos); |
break; |
| 938 |
break; |
case KEY_HOME: |
| 939 |
case KEY_RIGHT: |
case KEY_PGUP: |
| 940 |
menu_control (&bbsnet_menu, KEY_DOWN); |
menu_control(&bbsnet_menu, KEY_PGUP); |
| 941 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
bbsnet_selchange(); |
| 942 |
bbsnet_selchange (pos); |
break; |
| 943 |
break; |
case KEY_END: |
| 944 |
default: |
case KEY_PGDN: |
| 945 |
menu_control (&bbsnet_menu, ch); |
menu_control(&bbsnet_menu, KEY_PGDN); |
| 946 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
bbsnet_selchange(); |
| 947 |
bbsnet_selchange (pos); |
break; |
| 948 |
break; |
default: |
| 949 |
|
menu_control(&bbsnet_menu, ch); |
| 950 |
|
bbsnet_selchange(); |
| 951 |
|
break; |
| 952 |
|
} |
| 953 |
|
BBS_last_access_tm = time(NULL); |
| 954 |
} |
} |
|
BBS_last_access_tm = time (0); |
|
|
} |
|
| 955 |
|
|
| 956 |
return 0; |
cleanup: |
| 957 |
|
unload_bbsnet_conf(); |
| 958 |
|
|
| 959 |
|
return 0; |
| 960 |
} |
} |