/[LeafOK_CVS]/lbbs/src/bbs_net.c
ViewVC logotype

Contents of /lbbs/src/bbs_net.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.91 - (show annotations)
Wed Dec 17 03:47:00 2025 UTC (2 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.90: +2 -0 lines
Content type: text/x-csrc
Refine debug log

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * bbs_net
4 * - user interactive feature of site shuttle
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "bbs.h"
14 #include "bbs_net.h"
15 #include "common.h"
16 #include "io.h"
17 #include "log.h"
18 #include "login.h"
19 #include "menu.h"
20 #include "screen.h"
21 #include "str_process.h"
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <netdb.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <arpa/inet.h>
32 #include <ctype.h>
33 #include <libssh/libssh.h>
34 #include <libssh/server.h>
35 #include <libssh/callbacks.h>
36 #include <netinet/in.h>
37 #include <netinet/ip.h>
38 #include <sys/select.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41
42 #ifdef HAVE_SYS_EPOLL_H
43 #include <sys/epoll.h>
44 #else
45 #include <poll.h>
46 #endif
47
48 static const char MENU_CONF_DELIM[] = " \t\r\n";
49
50 enum _bbs_net_constant_t
51 {
52 MAX_PROCESS_BAR_LEN = 30,
53 MAXSTATION = 26 * 2,
54 STATION_PER_LINE = 4,
55 USERNAME_MAX_LEN = 20,
56 PASSWORD_MAX_LEN = 20,
57 SSH_CONNECT_TIMEOUT = 5, // seconds
58 };
59
60 struct _bbsnet_conf
61 {
62 char org_name[40];
63 char site_name[40];
64 char host_name[IP_ADDR_LEN];
65 in_port_t port;
66 int8_t use_ssh;
67 char charset[CHARSET_MAX_LEN + 1];
68 } bbsnet_conf[MAXSTATION];
69
70 static MENU_SET bbsnet_menu;
71
72 static void unload_bbsnet_conf(void);
73
74 static int load_bbsnet_conf(const char *file_config)
75 {
76 FILE *fp;
77 MENU *p_menu;
78 MENU_ITEM *p_menu_item;
79 MENU_ITEM_ID menu_item_id;
80 char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;
81 int port;
82
83 unload_bbsnet_conf();
84
85 bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));
86 if (bbsnet_menu.p_menu_pool == NULL)
87 {
88 log_error("calloc(p_menu_pool) error\n");
89 return -1;
90 }
91 bbsnet_menu.menu_count = 1;
92
93 bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));
94 if (bbsnet_menu.p_menu_item_pool == NULL)
95 {
96 log_error("calloc(p_menu_item_pool) error\n");
97 unload_bbsnet_conf();
98 return -1;
99 }
100 bbsnet_menu.menu_item_count = MAXSTATION;
101
102 p_menu = (MENU *)get_menu_by_id(&bbsnet_menu, 0);
103
104 strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
105 p_menu->name[sizeof(p_menu->name) - 1] = '\0';
106 p_menu->title.show = 0;
107 p_menu->screen_show = 0;
108
109 fp = fopen(file_config, "r");
110 if (fp == NULL)
111 {
112 unload_bbsnet_conf();
113 return -2;
114 }
115
116 menu_item_id = 0;
117 while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION)
118 {
119 t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr);
120 t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
121 t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
122 t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
123 t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
124 t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
125
126 if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||
127 t5 == NULL || t6 == NULL || line[0] == '#' || line[0] == '*')
128 {
129 continue;
130 }
131
132 strncpy(bbsnet_conf[menu_item_id].site_name, t2, sizeof(bbsnet_conf[menu_item_id].site_name) - 1);
133 bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0';
134 strncpy(bbsnet_conf[menu_item_id].org_name, t1, sizeof(bbsnet_conf[menu_item_id].org_name) - 1);
135 bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0';
136 strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);
137 bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0';
138 port = atoi(t4);
139 if (port <= 0 || port > 65535)
140 {
141 log_error("Invalid port value %d of menu item %d\n", port, menu_item_id);
142 fclose(fp);
143 unload_bbsnet_conf();
144 return -3;
145 }
146 bbsnet_conf[menu_item_id].port = (in_port_t)port;
147 bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');
148 strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
149 bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
150
151 p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
152 if (p_menu_item == NULL)
153 {
154 log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);
155 fclose(fp);
156 unload_bbsnet_conf();
157 return -3;
158 }
159
160 p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE);
161 p_menu_item->col = (int16_t)(5 + menu_item_id % STATION_PER_LINE * 20);
162 snprintf(p_menu_item->action, sizeof(p_menu_item->action), "%d", (int16_t)menu_item_id);
163 p_menu_item->submenu = 0;
164 p_menu_item->priv = 0;
165 p_menu_item->level = 0;
166 p_menu_item->name[0] =
167 (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id);
168 p_menu_item->name[1] = '\0';
169 snprintf(p_menu_item->text, sizeof(p_menu_item->text), "\033[1;36m%c.\033[m %s",
170 p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name);
171
172 p_menu->items[p_menu->item_count] = menu_item_id;
173 p_menu->item_count++;
174 menu_item_id++;
175 }
176
177 bbsnet_menu.menu_item_count = (int16_t)menu_item_id;
178 bbsnet_menu.menu_id_path[0] = 0;
179 bbsnet_menu.menu_item_pos[0] = 0;
180 bbsnet_menu.choose_step = 0;
181
182 fclose(fp);
183
184 return 0;
185 }
186
187 static void unload_bbsnet_conf(void)
188 {
189 bbsnet_menu.menu_count = 0;
190 bbsnet_menu.menu_item_count = 0;
191
192 if (bbsnet_menu.p_menu_pool)
193 {
194 free(bbsnet_menu.p_menu_pool);
195 bbsnet_menu.p_menu_pool = NULL;
196 }
197
198 if (bbsnet_menu.p_menu_item_pool)
199 {
200 free(bbsnet_menu.p_menu_item_pool);
201 bbsnet_menu.p_menu_item_pool = NULL;
202 }
203 }
204
205 static void process_bar(int n, int len)
206 {
207 char buf[LINE_BUFFER_LEN];
208 char buf2[LINE_BUFFER_LEN];
209
210 if (len <= 0)
211 {
212 len = 1;
213 }
214 else if (len > LINE_BUFFER_LEN)
215 {
216 len = LINE_BUFFER_LEN - 1;
217 }
218 if (n < 0)
219 {
220 n = 0;
221 }
222 else if (n > len)
223 {
224 n = len;
225 }
226
227 moveto(4, 1);
228 prints(" ------------------------------ \r\n");
229 snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len);
230 memcpy(buf2, buf, (size_t)n);
231 buf2[n] = '\0';
232 prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
233 prints(" ------------------------------ \r\n");
234 iflush();
235 }
236
237 static int bbsnet_connect(int n)
238 {
239 int sock = -1;
240 int ret;
241 int loop;
242 int error;
243 int sock_connected = 0;
244 int flags_sock = -1;
245 int flags_stdin = -1;
246 int flags_stdout = -1;
247 struct sockaddr_in sin;
248 char input_buf[LINE_BUFFER_LEN];
249 char output_buf[LINE_BUFFER_LEN];
250 int input_buf_len = 0;
251 int output_buf_len = 0;
252 int input_buf_offset = 0;
253 int output_buf_offset = 0;
254 char input_conv[LINE_BUFFER_LEN * 2];
255 char output_conv[LINE_BUFFER_LEN * 2];
256 int input_conv_len = 0;
257 int output_conv_len = 0;
258 int input_conv_offset = 0;
259 int output_conv_offset = 0;
260 iconv_t input_cd = (iconv_t)(-1);
261 iconv_t output_cd = (iconv_t)(-1);
262 char tocode[CHARSET_MAX_LEN + 20];
263
264 #ifdef HAVE_SYS_EPOLL_H
265 struct epoll_event ev, events[MAX_EVENTS];
266 int epollfd = -1;
267 #else
268 struct pollfd pfds[3];
269 #endif
270
271 int nfds;
272 int stdin_read_wait = 0;
273 int stdout_write_wait = 0;
274 int sock_read_wait = 0;
275 int sock_write_wait = 0;
276 struct hostent *p_host = NULL;
277 int tos;
278 char remote_addr[IP_ADDR_LEN];
279 int remote_port;
280 char local_addr[IP_ADDR_LEN];
281 int local_port;
282 socklen_t sock_len;
283 time_t t_begin;
284 time_t t_used = time(NULL);
285 struct tm *tm_used;
286 int ch;
287 char remote_user[USERNAME_MAX_LEN + 1];
288 char remote_pass[PASSWORD_MAX_LEN + 1];
289 ssh_session session = NULL;
290 ssh_channel channel = NULL;
291 int ssh_process_config = 0;
292 int ssh_log_level = SSH_LOG_NOLOG;
293
294 if (user_online_update("BBS_NET") < 0)
295 {
296 log_error("user_online_update(BBS_NET) error\n");
297 }
298
299 if (bbsnet_conf[n].use_ssh)
300 {
301 clearscr();
302
303 if (!SSH_v2)
304 {
305 moveto(1, 1);
306 prints("只有在以SSH方式登陆本站时,才能使用SSH站点穿梭。");
307 press_any_key();
308 return 0;
309 }
310
311 moveto(1, 1);
312 prints("通过SSH方式连接[%s]...", bbsnet_conf[n].site_name);
313 moveto(2, 1);
314 prints("请输入用户名: ");
315 iflush();
316 if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)
317 {
318 return -1;
319 }
320 if (remote_user[0] == '\0')
321 {
322 return 0;
323 }
324
325 moveto(3, 1);
326 prints("请输入密码: ");
327 iflush();
328 if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)
329 {
330 return -1;
331 }
332 if (remote_pass[0] == '\0')
333 {
334 return 0;
335 }
336 }
337
338 clearscr();
339
340 moveto(1, 1);
341 prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
342 bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);
343 iflush();
344
345 p_host = gethostbyname(bbsnet_conf[n].host_name);
346
347 if (p_host == NULL)
348 {
349 prints("\033[1;31m查找主机名失败!\033[m\r\n");
350 press_any_key();
351 goto cleanup;
352 }
353
354 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
355
356 if (sock < 0)
357 {
358 prints("\033[1;31m无法创建socket!\033[m\r\n");
359 press_any_key();
360 goto cleanup;
361 }
362
363 sin.sin_family = AF_INET;
364 sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY);
365 sin.sin_port = 0;
366
367 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
368 {
369 log_error("Bind address %s:%u failed (%d)\n",
370 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
371 goto cleanup;
372 }
373
374 memset(&sin, 0, sizeof(sin));
375 sin.sin_family = AF_INET;
376 sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
377 sin.sin_port = htons(bbsnet_conf[n].port);
378
379 strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);
380 remote_addr[sizeof(remote_addr) - 1] = '\0';
381 remote_port = ntohs(sin.sin_port);
382
383 prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
384 process_bar(0, MAX_PROCESS_BAR_LEN);
385
386 // Set socket as non-blocking
387 if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)
388 {
389 log_error("fcntl(F_GETFL) error (%d)\n", errno);
390 goto cleanup;
391 }
392 if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)
393 {
394 log_error("fcntl(F_SETFL) error (%d)\n", errno);
395 goto cleanup;
396 }
397
398 // Set STDIN/STDOUT as non-blocking
399 if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
400 {
401 log_error("fcntl(F_GETFL) error (%d)\n", errno);
402 goto cleanup;
403 }
404 if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
405 {
406 log_error("fcntl(F_GETFL) error (%d)\n", errno);
407 goto cleanup;
408 }
409 if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)
410 {
411 log_error("fcntl(F_SETFL) error (%d)\n", errno);
412 goto cleanup;
413 }
414 if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)
415 {
416 log_error("fcntl(F_SETFL) error (%d)\n", errno);
417 goto cleanup;
418 }
419
420 #ifdef HAVE_SYS_EPOLL_H
421 epollfd = epoll_create1(0);
422 if (epollfd < 0)
423 {
424 log_error("epoll_create1() error (%d)\n", errno);
425 goto cleanup;
426 }
427
428 ev.events = EPOLLOUT | EPOLLET;
429 ev.data.fd = sock;
430 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
431 {
432 log_error("epoll_ctl(socket) error (%d)\n", errno);
433 goto cleanup;
434 }
435
436 ev.events = EPOLLIN | EPOLLET;
437 ev.data.fd = STDIN_FILENO;
438 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
439 {
440 log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
441 goto cleanup;
442 }
443 #endif
444
445 while (!SYS_server_exit)
446 {
447 if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
448 {
449 if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
450 {
451 // Use select / epoll to check writability of the socket,
452 // then use getsockopt to check the status of the socket.
453 // See man connect(2)
454 break;
455 }
456 else if (errno == EINTR)
457 {
458 continue;
459 }
460 else
461 {
462 log_error("connect(socket) error (%d)\n", errno);
463
464 prints("\033[1;31m连接失败!\033[m\r\n");
465 press_any_key();
466
467 goto cleanup;
468 }
469 }
470 }
471
472 for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)
473 {
474 #ifdef HAVE_SYS_EPOLL_H
475 nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
476 ret = nfds;
477 #else
478 pfds[0].fd = sock;
479 pfds[0].events = POLLOUT;
480 pfds[1].fd = STDIN_FILENO;
481 pfds[1].events = POLLIN;
482 nfds = 2;
483 ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second
484 #endif
485
486 if (ret < 0)
487 {
488 if (errno != EINTR)
489 {
490 #ifdef HAVE_SYS_EPOLL_H
491 log_error("epoll_wait() error (%d)\n", errno);
492 #else
493 log_error("poll() error (%d)\n", errno);
494 #endif
495 break;
496 }
497 }
498 else if (ret == 0) // timeout
499 {
500 process_bar(j + 1, MAX_PROCESS_BAR_LEN);
501 }
502 else // ret > 0
503 {
504 for (int i = 0; i < nfds; i++)
505 {
506 #ifdef HAVE_SYS_EPOLL_H
507 if (events[i].data.fd == sock)
508 #else
509 if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT))
510 #endif
511 {
512 socklen_t len = sizeof(error);
513 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
514 {
515 log_error("getsockopt() error (%d) !\n", errno);
516 goto cleanup;
517 }
518 if (error == 0)
519 {
520 sock_connected = 1;
521 }
522 }
523 #ifdef HAVE_SYS_EPOLL_H
524 else if (events[i].data.fd == STDIN_FILENO)
525 #else
526 else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
527 #endif
528 {
529 do
530 {
531 ch = igetch(0);
532 } while (ch == 0);
533 if (ch == Ctrl('C') || ch == KEY_ESC)
534 {
535 goto cleanup;
536 }
537 }
538 }
539 }
540 }
541 if (SYS_server_exit)
542 {
543 goto cleanup;
544 }
545 if (!sock_connected)
546 {
547 prints("\033[1;31m连接失败!\033[m\r\n");
548 press_any_key();
549
550 goto cleanup;
551 }
552
553 tos = IPTOS_LOWDELAY;
554 if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
555 {
556 log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
557 }
558
559 sock_len = sizeof(sin);
560 if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
561 {
562 log_error("getsockname() error: %d", errno);
563 goto cleanup;
564 }
565
566 strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
567 local_addr[sizeof(local_addr) - 1] = '\0';
568 local_port = ntohs(sin.sin_port);
569
570 if (bbsnet_conf[n].use_ssh)
571 {
572 session = ssh_new();
573 if (session == NULL)
574 {
575 log_error("ssh_new() error\n");
576 goto cleanup;
577 }
578
579 if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 ||
580 ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 ||
581 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 ||
582 ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 ||
583 ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 ||
584 ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 ||
585 ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
586 {
587 log_error("Error setting SSH options: %s\n", ssh_get_error(session));
588 goto cleanup;
589 }
590
591 ssh_set_blocking(session, 0);
592
593 t_begin = time(NULL);
594 ret = SSH_ERROR;
595 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
596 {
597 ret = ssh_connect(session);
598 if (ret == SSH_OK)
599 {
600 break;
601 }
602 else if (ret == SSH_AGAIN)
603 {
604 // log_error("ssh_connect() error: SSH_AGAIN\n");
605 }
606 else // if (ret == SSH_ERROR)
607 {
608 log_error("ssh_connect() error: SSH_ERROR\n");
609 goto cleanup;
610 }
611 }
612 if (ret != SSH_OK)
613 {
614 prints("\033[1;31m连接超时!\033[m\r\n");
615 press_any_key();
616 goto cleanup;
617 }
618
619 ret = ssh_session_is_known_server(session);
620 switch (ret)
621 {
622 case SSH_KNOWN_HOSTS_NOT_FOUND:
623 case SSH_KNOWN_HOSTS_UNKNOWN:
624 if (ssh_session_update_known_hosts(session) != SSH_OK)
625 {
626 log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name);
627 prints("\033[1;31m无法添加服务器证书\033[m\r\n");
628 press_any_key();
629 goto cleanup;
630 }
631 log_common("SSH key of (%s) is added into %s\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE);
632 case SSH_KNOWN_HOSTS_OK:
633 break;
634 case SSH_KNOWN_HOSTS_CHANGED:
635 case SSH_KNOWN_HOSTS_OTHER:
636 log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret);
637 prints("\033[1;31m服务器证书已变更\033[m\r\n");
638 press_any_key();
639 goto cleanup;
640 }
641
642 ret = SSH_AUTH_ERROR;
643 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
644 {
645 ret = ssh_userauth_password(session, NULL, remote_pass);
646 if (ret == SSH_AUTH_SUCCESS)
647 {
648 break;
649 }
650 else if (ret == SSH_AUTH_AGAIN)
651 {
652 // log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");
653 }
654 else if (ret == SSH_AUTH_ERROR)
655 {
656 log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n");
657 goto cleanup;
658 }
659 else // if (ret == SSH_AUTH_DENIED)
660 {
661 log_error("ssh_userauth_password() error: SSH_AUTH_DENIED\n");
662 prints("\033[1;31m身份验证失败!\033[m\r\n");
663 press_any_key();
664 goto cleanup;
665 }
666 }
667 if (ret != SSH_AUTH_SUCCESS)
668 {
669 prints("\033[1;31m连接超时!\033[m\r\n");
670 press_any_key();
671 goto cleanup;
672 }
673
674 channel = ssh_channel_new(session);
675 if (channel == NULL)
676 {
677 log_error("ssh_channel_new() error\n");
678 goto cleanup;
679 }
680
681 ret = SSH_ERROR;
682 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
683 {
684 ret = ssh_channel_open_session(channel);
685 if (ret == SSH_OK)
686 {
687 break;
688 }
689 else if (ret == SSH_AGAIN)
690 {
691 // log_error("ssh_channel_open_session() error: SSH_AGAIN\n");
692 }
693 else // if (ret == SSH_ERROR)
694 {
695 log_error("ssh_channel_open_session() error: SSH_ERROR\n");
696 goto cleanup;
697 }
698 }
699 if (ret != SSH_OK)
700 {
701 prints("\033[1;31m连接超时!\033[m\r\n");
702 press_any_key();
703 goto cleanup;
704 }
705
706 ret = SSH_ERROR;
707 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
708 {
709 ret = ssh_channel_request_pty(channel);
710 if (ret == SSH_OK)
711 {
712 break;
713 }
714 else if (ret == SSH_AGAIN)
715 {
716 // log_error("ssh_channel_request_pty() error: SSH_AGAIN\n");
717 }
718 else // if (ret == SSH_ERROR)
719 {
720 log_error("ssh_channel_request_pty() error: SSH_ERROR\n");
721 goto cleanup;
722 }
723 }
724 if (ret != SSH_OK)
725 {
726 prints("\033[1;31m连接超时!\033[m\r\n");
727 press_any_key();
728 goto cleanup;
729 }
730
731 ret = SSH_ERROR;
732 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
733 {
734 ret = ssh_channel_request_shell(channel);
735 if (ret == SSH_OK)
736 {
737 break;
738 }
739 else if (ret == SSH_AGAIN)
740 {
741 // log_error("ssh_channel_request_shell() error: SSH_AGAIN\n");
742 }
743 else // if (ret == SSH_ERROR)
744 {
745 log_error("ssh_channel_request_shell() error: SSH_ERROR\n");
746 goto cleanup;
747 }
748 }
749 if (ret != SSH_OK)
750 {
751 prints("\033[1;31m连接超时!\033[m\r\n");
752 press_any_key();
753 goto cleanup;
754 }
755 }
756
757 prints("\033[1;31m连接成功!\033[m\r\n");
758 iflush();
759 log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",
760 remote_addr, remote_port, local_addr, local_port, BBS_username);
761
762 snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
763 (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
764 input_cd = iconv_open(tocode, stdio_charset);
765 if (input_cd == (iconv_t)(-1))
766 {
767 log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
768 goto cleanup;
769 }
770
771 snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
772 (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
773 output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
774 if (output_cd == (iconv_t)(-1))
775 {
776 log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
777 goto cleanup;
778 }
779
780 #ifdef HAVE_SYS_EPOLL_H
781 ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
782 ev.data.fd = sock;
783 if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
784 {
785 log_error("epoll_ctl(socket) error (%d)\n", errno);
786 goto cleanup;
787 }
788
789 ev.events = EPOLLOUT | EPOLLET;
790 ev.data.fd = STDOUT_FILENO;
791 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
792 {
793 log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
794 goto cleanup;
795 }
796 #endif
797
798 BBS_last_access_tm = t_used = time(NULL);
799 loop = 1;
800
801 while (loop && !SYS_server_exit)
802 {
803 if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
804 {
805 log_error("SSH channel is closed\n");
806 loop = 0;
807 break;
808 }
809
810 if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel))
811 {
812 log_error("Remote SSH channel is closed\n");
813 loop = 0;
814 break;
815 }
816
817 #ifdef HAVE_SYS_EPOLL_H
818 nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
819 ret = nfds;
820 #else
821 pfds[0].fd = STDIN_FILENO;
822 pfds[0].events = POLLIN;
823 pfds[1].fd = sock;
824 pfds[1].events = POLLIN | POLLOUT;
825 pfds[2].fd = STDOUT_FILENO;
826 pfds[2].events = POLLOUT;
827 nfds = 3;
828 ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
829 #endif
830
831 if (ret < 0)
832 {
833 if (errno != EINTR)
834 {
835 #ifdef HAVE_SYS_EPOLL_H
836 log_error("epoll_wait() error (%d)\n", errno);
837 #else
838 log_error("poll() error (%d)\n", errno);
839 #endif
840 break;
841 }
842 continue;
843 }
844 else if (ret == 0) // timeout
845 {
846 if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
847 {
848 break;
849 }
850 }
851
852 for (int i = 0; i < nfds; i++)
853 {
854 #ifdef HAVE_SYS_EPOLL_H
855 if (events[i].data.fd == STDIN_FILENO)
856 #else
857 if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
858 #endif
859 {
860 stdin_read_wait = 1;
861 }
862
863 #ifdef HAVE_SYS_EPOLL_H
864 if (events[i].data.fd == sock)
865 #else
866 if (pfds[i].fd == sock)
867 #endif
868 {
869 #ifdef HAVE_SYS_EPOLL_H
870 if (events[i].events & EPOLLIN)
871 #else
872 if (pfds[i].revents & POLLIN)
873 #endif
874 {
875 sock_read_wait = 1;
876 }
877
878 #ifdef HAVE_SYS_EPOLL_H
879 if (events[i].events & EPOLLOUT)
880 #else
881 if (pfds[i].revents & POLLOUT)
882 #endif
883 {
884 sock_write_wait = 1;
885 }
886 }
887
888 #ifdef HAVE_SYS_EPOLL_H
889 if (events[i].data.fd == STDOUT_FILENO)
890 #else
891 if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
892 #endif
893 {
894 stdout_write_wait = 1;
895 }
896 }
897
898 if (stdin_read_wait)
899 {
900 while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
901 {
902 if (SSH_v2)
903 {
904 ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
905 if (ret == SSH_ERROR)
906 {
907 log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
908 loop = 0;
909 break;
910 }
911 else if (ret == SSH_EOF)
912 {
913 stdin_read_wait = 0;
914 loop = 0;
915 break;
916 }
917 else if (ret == 0)
918 {
919 // Send NO-OP to remote server
920 input_buf[input_buf_len] = '\0';
921 input_buf_len++;
922 BBS_last_access_tm = time(NULL);
923
924 stdin_read_wait = 0;
925 break; // Check whether channel is still open
926 }
927 }
928 else
929 {
930 ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
931 }
932 if (ret < 0)
933 {
934 if (errno == EAGAIN || errno == EWOULDBLOCK)
935 {
936 stdin_read_wait = 0;
937 break;
938 }
939 else if (errno == EINTR)
940 {
941 continue;
942 }
943 else
944 {
945 log_error("read(STDIN) error (%d)\n", errno);
946 loop = 0;
947 break;
948 }
949 }
950 else if (ret == 0) // broken pipe
951 {
952 #ifdef _DEBUG
953 log_error("read(STDIN) EOF\n");
954 #endif
955 stdin_read_wait = 0;
956 loop = 0;
957 break;
958 }
959 else
960 {
961 input_buf_len += ret;
962 BBS_last_access_tm = time(NULL);
963
964 // Refresh current action while user input
965 if (user_online_update("BBS_NET") < 0)
966 {
967 log_error("user_online_update(BBS_NET) error\n");
968 }
969
970 continue;
971 }
972 }
973 }
974
975 if (sock_write_wait)
976 {
977 if (input_buf_offset < input_buf_len)
978 {
979 // For debug
980 #ifdef _DEBUG
981 for (int j = input_buf_offset; j < input_buf_len; j++)
982 {
983 log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
984 }
985 #endif
986
987 ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
988 if (ret < 0)
989 {
990 log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
991 input_buf_len = input_buf_offset; // Discard invalid sequence
992 }
993
994 // For debug
995 #ifdef _DEBUG
996 for (int j = input_conv_offset; j < input_conv_len; j++)
997 {
998 log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);
999 }
1000 #endif
1001 }
1002
1003 while (input_conv_offset < input_conv_len && !SYS_server_exit)
1004 {
1005 if (bbsnet_conf[n].use_ssh)
1006 {
1007 ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));
1008 if (ret == SSH_ERROR)
1009 {
1010 log_error("ssh_channel_write() error: %s\n", ssh_get_error(session));
1011 loop = 0;
1012 break;
1013 }
1014 }
1015 else
1016 {
1017 ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
1018 }
1019 if (ret < 0)
1020 {
1021 if (errno == EAGAIN || errno == EWOULDBLOCK)
1022 {
1023 sock_write_wait = 0;
1024 break;
1025 }
1026 else if (errno == EINTR)
1027 {
1028 continue;
1029 }
1030 else
1031 {
1032 log_error("write(socket) error (%d)\n", errno);
1033 loop = 0;
1034 break;
1035 }
1036 }
1037 else if (ret == 0) // broken pipe
1038 {
1039 #ifdef _DEBUG
1040 log_error("write(socket) EOF\n");
1041 #endif
1042 sock_write_wait = 0;
1043 loop = 0;
1044 break;
1045 }
1046 else
1047 {
1048 input_conv_offset += ret;
1049 if (input_conv_offset >= input_conv_len) // Output buffer complete
1050 {
1051 input_conv_offset = 0;
1052 input_conv_len = 0;
1053 break;
1054 }
1055 continue;
1056 }
1057 }
1058 }
1059
1060 if (sock_read_wait)
1061 {
1062 while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
1063 {
1064 if (bbsnet_conf[n].use_ssh)
1065 {
1066 ret = ssh_channel_read_nonblocking(channel, output_buf + output_buf_len,
1067 (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
1068 if (ret == SSH_ERROR)
1069 {
1070 log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session));
1071 loop = 0;
1072 break;
1073 }
1074 else if (ret == SSH_EOF)
1075 {
1076 sock_read_wait = 0;
1077 loop = 0;
1078 break;
1079 }
1080 else if (ret == 0)
1081 {
1082 sock_read_wait = 0;
1083 break;
1084 }
1085 }
1086 else
1087 {
1088 ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
1089 }
1090 if (ret < 0)
1091 {
1092 if (errno == EAGAIN || errno == EWOULDBLOCK)
1093 {
1094 sock_read_wait = 0;
1095 break;
1096 }
1097 else if (errno == EINTR)
1098 {
1099 continue;
1100 }
1101 else
1102 {
1103 log_error("read(socket) error (%d)\n", errno);
1104 loop = 0;
1105 break;
1106 }
1107 }
1108 else if (ret == 0) // broken pipe
1109 {
1110 #ifdef _DEBUG
1111 log_error("read(socket) EOF\n");
1112 #endif
1113 sock_read_wait = 0;
1114 loop = 0;
1115 break;
1116 }
1117 else
1118 {
1119 output_buf_len += ret;
1120 continue;
1121 }
1122 }
1123 }
1124
1125 if (stdout_write_wait)
1126 {
1127 if (output_buf_offset < output_buf_len)
1128 {
1129 ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
1130 if (ret < 0)
1131 {
1132 log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
1133 output_buf_len = output_buf_offset; // Discard invalid sequence
1134 }
1135 }
1136
1137 while (output_conv_offset < output_conv_len && !SYS_server_exit)
1138 {
1139 if (SSH_v2)
1140 {
1141 ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
1142 if (ret == SSH_ERROR)
1143 {
1144 log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
1145 loop = 0;
1146 break;
1147 }
1148 }
1149 else
1150 {
1151 ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
1152 }
1153 if (ret < 0)
1154 {
1155 if (errno == EAGAIN || errno == EWOULDBLOCK)
1156 {
1157 stdout_write_wait = 0;
1158 break;
1159 }
1160 else if (errno == EINTR)
1161 {
1162 continue;
1163 }
1164 else
1165 {
1166 log_error("write(STDOUT) error (%d)\n", errno);
1167 loop = 0;
1168 break;
1169 }
1170 }
1171 else if (ret == 0) // broken pipe
1172 {
1173 #ifdef _DEBUG
1174 log_error("write(STDOUT) EOF\n");
1175 #endif
1176 stdout_write_wait = 0;
1177 loop = 0;
1178 break;
1179 }
1180 else
1181 {
1182 output_conv_offset += ret;
1183 if (output_conv_offset >= output_conv_len) // Output buffer complete
1184 {
1185 output_conv_offset = 0;
1186 output_conv_len = 0;
1187 break;
1188 }
1189 continue;
1190 }
1191 }
1192 }
1193 }
1194
1195 cleanup:
1196 if (input_cd != (iconv_t)(-1))
1197 {
1198 iconv_close(input_cd);
1199 }
1200 if (output_cd != (iconv_t)(-1))
1201 {
1202 iconv_close(output_cd);
1203 }
1204
1205 #ifdef HAVE_SYS_EPOLL_H
1206 if (epollfd != -1 && close(epollfd) < 0)
1207 {
1208 log_error("close(epoll) error (%d)\n");
1209 }
1210 #endif
1211
1212 if (bbsnet_conf[n].use_ssh)
1213 {
1214 if (channel != NULL)
1215 {
1216 ssh_channel_free(channel);
1217 }
1218 if (session != NULL)
1219 {
1220 ssh_disconnect(session);
1221 ssh_free(session);
1222 }
1223 }
1224
1225 // Restore STDIN/STDOUT flags
1226 if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)
1227 {
1228 log_error("fcntl(F_SETFL) error (%d)\n", errno);
1229 }
1230 if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)
1231 {
1232 log_error("fcntl(F_SETFL) error (%d)\n", errno);
1233 }
1234
1235 if (sock != -1 && close(sock) == -1)
1236 {
1237 log_error("Close socket failed\n");
1238 }
1239
1240 t_used = time(NULL) - t_used;
1241 tm_used = gmtime(&t_used);
1242
1243 log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
1244 tm_used->tm_yday, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec);
1245
1246 BBS_last_access_tm = time(NULL);
1247
1248 return 0;
1249 }
1250
1251 static int bbsnet_refresh()
1252 {
1253 clearscr();
1254
1255 moveto(1, 1);
1256 prints(" ------------------------------------------------------------------------------ ");
1257 for (int i = 2; i < 19; i++)
1258 {
1259 moveto(i, 1);
1260 prints("|");
1261 moveto(i, 80);
1262 prints("|");
1263 }
1264 moveto(19, 1);
1265 prints("|------------------------------------------------------------------------------|");
1266 moveto(22, 1);
1267 prints(" ------------------------------------------------------------------------------ ");
1268 moveto(23, 1);
1269 prints(" [\033[1;32mCtrl+C\033[m]退出");
1270
1271 iflush();
1272
1273 return 0;
1274 }
1275
1276 static int bbsnet_selchange()
1277 {
1278 int i = bbsnet_menu.menu_item_pos[0];
1279
1280 moveto(20, 1);
1281 clrtoeol();
1282 prints("|\033[1m单位: \033[1;33m%s\033[m%*s 站名: \033[1;33m%s\033[m%*s 类型: \033[1;33m%s\033[m",
1283 bbsnet_conf[i].org_name, 20 - str_length(bbsnet_conf[i].org_name, 1), "",
1284 bbsnet_conf[i].site_name, 20 - str_length(bbsnet_conf[i].site_name, 1), "",
1285 (bbsnet_conf[i].use_ssh ? "SSH" : "Telnet"));
1286 moveto(20, 80);
1287 prints("|");
1288 moveto(21, 1);
1289 clrtoeol();
1290 prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5d\033[m 编码: \033[1;33m%s\033[m",
1291 bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset);
1292 moveto(21, 80);
1293 prints("|");
1294 iflush();
1295
1296 return 0;
1297 }
1298
1299 int bbs_net()
1300 {
1301 int ch, i;
1302
1303 if (load_bbsnet_conf(CONF_BBSNET) < 0)
1304 {
1305 clearscr();
1306 moveto(1, 1);
1307 prints("加载穿梭配置失败!");
1308 press_any_key();
1309 return -1;
1310 }
1311
1312 bbsnet_refresh();
1313 display_menu(&bbsnet_menu);
1314 bbsnet_selchange();
1315
1316 while (!SYS_server_exit)
1317 {
1318 ch = igetch(100);
1319
1320 if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1321 {
1322 BBS_last_access_tm = time(NULL);
1323 }
1324
1325 switch (ch)
1326 {
1327 case KEY_NULL: // broken pipe
1328 #ifdef _DEBUG
1329 log_error("KEY_NULL\n");
1330 #endif
1331 goto cleanup;
1332 case KEY_TIMEOUT:
1333 if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1334 {
1335 log_error("User input timeout\n");
1336 goto cleanup;
1337 }
1338 continue;
1339 case KEY_ESC:
1340 case Ctrl('C'): // user cancel
1341 goto cleanup;
1342 case CR:
1343 bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
1344 // Force cleanup anything remaining in the output buffer
1345 clearscr();
1346 iflush();
1347 // Clear screen and redraw menu
1348 bbsnet_refresh();
1349 display_menu(&bbsnet_menu);
1350 bbsnet_selchange();
1351 break;
1352 case KEY_UP:
1353 for (i = 0; i < STATION_PER_LINE; i++)
1354 {
1355 menu_control(&bbsnet_menu, KEY_UP);
1356 }
1357 bbsnet_selchange();
1358 break;
1359 case KEY_DOWN:
1360 for (i = 0; i < STATION_PER_LINE; i++)
1361 {
1362 menu_control(&bbsnet_menu, KEY_DOWN);
1363 }
1364 bbsnet_selchange();
1365 break;
1366 case KEY_LEFT:
1367 menu_control(&bbsnet_menu, KEY_UP);
1368 bbsnet_selchange();
1369 break;
1370 case KEY_RIGHT:
1371 menu_control(&bbsnet_menu, KEY_DOWN);
1372 bbsnet_selchange();
1373 break;
1374 case KEY_HOME:
1375 case KEY_PGUP:
1376 menu_control(&bbsnet_menu, KEY_PGUP);
1377 bbsnet_selchange();
1378 break;
1379 case KEY_END:
1380 case KEY_PGDN:
1381 menu_control(&bbsnet_menu, KEY_PGDN);
1382 bbsnet_selchange();
1383 break;
1384 default:
1385 menu_control(&bbsnet_menu, ch);
1386 bbsnet_selchange();
1387 break;
1388 }
1389 }
1390
1391 cleanup:
1392 unload_bbsnet_conf();
1393
1394 return 0;
1395 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1