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

Diff of /lbbs/src/bbs_net.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.38 by sysadm, Tue May 13 02:21:39 2025 UTC Revision 1.58 by sysadm, Wed Oct 1 02:20:50 2025 UTC
# Line 16  Line 16 
16    
17  #include "bbs.h"  #include "bbs.h"
18  #include "common.h"  #include "common.h"
 #include "log.h"  
19  #include "io.h"  #include "io.h"
20  #include "screen.h"  #include "log.h"
21    #include "login.h"
22  #include "menu.h"  #include "menu.h"
23  #include <stdio.h>  #include "screen.h"
 #include <stdarg.h>  
24  #include <errno.h>  #include <errno.h>
 #include <string.h>  
 #include <stdlib.h>  
25  #include <fcntl.h>  #include <fcntl.h>
26    #include <netdb.h>
27    #include <stdarg.h>
28    #include <stdio.h>
29    #include <stdlib.h>
30    #include <string.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 <libssh/libssh.h>
35    #include <libssh/server.h>
36    #include <libssh/callbacks.h>
37    #include <netinet/in.h>
38    #include <netinet/ip.h>
39  #include <sys/select.h>  #include <sys/select.h>
40  #include <sys/ioctl.h>  #include <sys/ioctl.h>
41  #include <sys/socket.h>  #include <sys/socket.h>
42  #include <sys/epoll.h>  #include <sys/epoll.h>
 #include <netinet/in.h>  
 #include <netinet/ip.h>  
 #include <arpa/inet.h>  
43    
44  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
45    
# Line 57  int load_bbsnet_conf(const char *file_co Line 61  int load_bbsnet_conf(const char *file_co
61  {  {
62          FILE *fp;          FILE *fp;
63          MENU *p_menu;          MENU *p_menu;
64          MENU_ITEM *p_menuitem;          MENU_ITEM *p_menu_item;
65            MENU_ITEM_ID menu_item_id;
66          char t[256], *t1, *t2, *t3, *t4, *saveptr;          char t[256], *t1, *t2, *t3, *t4, *saveptr;
         int item_count = 0;  
67    
68          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
69          if (fp == NULL)          if (fp == NULL)
# Line 67  int load_bbsnet_conf(const char *file_co Line 71  int load_bbsnet_conf(const char *file_co
71                  return -1;                  return -1;
72          }          }
73    
74          p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU));          bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));
75            if (bbsnet_menu.p_menu_pool == NULL)
76            {
77                    log_error("calloc(p_menu_pool) error\n");
78                    return -3;
79            }
80            bbsnet_menu.menu_count = 1;
81    
82            bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));
83            if (bbsnet_menu.p_menu_item_pool == NULL)
84            {
85                    log_error("calloc(p_menu_item_pool) error\n");
86                    return -3;
87            }
88            bbsnet_menu.menu_item_count = MAXSTATION;
89    
90            p_menu = (MENU *)get_menu_by_id(&bbsnet_menu, 0);
91    
92          strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);          strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
93          p_menu->name[sizeof(p_menu->name) - 1] = '\0';          p_menu->name[sizeof(p_menu->name) - 1] = '\0';
94          p_menu->title.show = 0;          p_menu->title.show = 0;
95          p_menu->screen.show = 0;          p_menu->screen_show = 0;
96    
97          while (fgets(t, 255, fp) && item_count < MAXSTATION)          menu_item_id = 0;
98            while (fgets(t, 255, fp) && menu_item_id < MAXSTATION)
99          {          {
100                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);
101                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
# Line 85  int load_bbsnet_conf(const char *file_co Line 107  int load_bbsnet_conf(const char *file_co
107                          continue;                          continue;
108                  }                  }
109    
110                  strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1) - 1);                  strncpy(bbsnet_conf[menu_item_id].host1, t2, sizeof(bbsnet_conf[menu_item_id].host1) - 1);
111                  bbsnet_conf[item_count].host1[sizeof(bbsnet_conf[item_count].host1) - 1] = '\0';                  bbsnet_conf[menu_item_id].host1[sizeof(bbsnet_conf[menu_item_id].host1) - 1] = '\0';
112                  strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2) - 1);                  strncpy(bbsnet_conf[menu_item_id].host2, t1, sizeof(bbsnet_conf[menu_item_id].host2) - 1);
113                  bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0';                  bbsnet_conf[menu_item_id].host2[sizeof(bbsnet_conf[menu_item_id].host2) - 1] = '\0';
114                  strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip) - 1);                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);
115                  bbsnet_conf[item_count].ip[sizeof(bbsnet_conf[item_count].ip) - 1] = '\0';                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';
116                  bbsnet_conf[item_count].port = (in_port_t)(t4 ? atoi(t4) : 23);                  bbsnet_conf[menu_item_id].port = (in_port_t)(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;  
                 snprintf(p_menuitem->action, sizeof(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] =  
                         (char)(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);  
                 p_menuitem->name[1] = '\0';  
                 snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s",  
                                  p_menuitem->name[0], bbsnet_conf[item_count].host1);  
117    
118                  item_count++;                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
119          }                  if (p_menu_item == NULL)
120          fclose(fp);                  {
121                            log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);
122                            return -1;
123                    }
124    
125          p_menu->item_count = item_count;                  p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE);
126          p_menu->item_cur_pos = 0;                  p_menu_item->col = (int16_t)(5 + menu_item_id % STATION_PER_LINE * 20);
127                    snprintf(p_menu_item->action, sizeof(p_menu_item->action), "%d", (int16_t)menu_item_id);
128                    p_menu_item->submenu = 0;
129                    p_menu_item->priv = 0;
130                    p_menu_item->level = 0;
131                    p_menu_item->name[0] =
132                            (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id);
133                    p_menu_item->name[1] = '\0';
134                    snprintf(p_menu_item->text, sizeof(p_menu_item->text), "%c. %s",
135                                     p_menu_item->name[0], bbsnet_conf[menu_item_id].host1);
136    
137                    p_menu->items[p_menu->item_count] = menu_item_id;
138                    p_menu->item_count++;
139                    menu_item_id++;
140            }
141    
142            bbsnet_menu.menu_item_count = (int16_t)menu_item_id;
143            bbsnet_menu.menu_id_path[0] = 0;
144            bbsnet_menu.menu_item_pos[0] = 0;
145            bbsnet_menu.choose_step = 0;
146    
147          bbsnet_menu.menu_count = 1;          fclose(fp);
         bbsnet_menu.menu_select_depth = 0;  
         bbsnet_menu.p_menu_select[0] = bbsnet_menu.p_menu[0];  
148    
149          return 0;          return 0;
150  }  }
151    
152  static void process_bar(int n, int len)  void unload_bbsnet_conf(void)
153    {
154            bbsnet_menu.menu_count = 0;
155            bbsnet_menu.menu_item_count = 0;
156    
157            free(bbsnet_menu.p_menu_pool);
158            bbsnet_menu.p_menu_pool = NULL;
159            free(bbsnet_menu.p_menu_item_pool);
160            bbsnet_menu.p_menu_item_pool = NULL;
161    }
162    
163    void process_bar(int n, int len)
164  {  {
165          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
166          char buf2[LINE_BUFFER_LEN];          char buf2[LINE_BUFFER_LEN];
# Line 174  int bbsnet_connect(int n) Line 213  int bbsnet_connect(int n)
213          int tos;          int tos;
214          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
215          int remote_port;          int remote_port;
216          time_t t_used;          time_t t_used = time(NULL);
217          struct tm *tm_used;          struct tm *tm_used;
218          int ch;          int ch;
219    
220            if (user_online_update("BBS_NET") < 0)
221            {
222                    log_error("user_online_update(BBS_NET) error\n");
223            }
224    
225          clearscr();          clearscr();
226    
227          moveto(0, 0);          moveto(0, 0);
228          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m姝e湪娴嬭瘯寰 %s (%s) 鐨勮繛鎺ワ紝璇风◢鍊... \033[m\r\n",
229                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);
230          iflush();          iflush();
231    
# Line 189  int bbsnet_connect(int n) Line 233  int bbsnet_connect(int n)
233    
234          if (p_host == NULL)          if (p_host == NULL)
235          {          {
236                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  prints("\033[1;31m鏌ユ壘涓绘満鍚嶅け璐ワ紒\033[m\r\n");
237                  press_any_key();                  press_any_key();
238                  return -1;                  return -1;
239          }          }
# Line 198  int bbsnet_connect(int n) Line 242  int bbsnet_connect(int n)
242    
243          if (sock < 0)          if (sock < 0)
244          {          {
245                  prints("\033[1;31m无法创建socket!\033[m\r\n");                  prints("\033[1;31m鏃犳硶鍒涘缓socket锛乗033[m\r\n");
246                  press_any_key();                  press_any_key();
247                  return -1;                  return -1;
248          }          }
249    
250          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
251          sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY);          sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY);
252          sin.sin_port = 0;          sin.sin_port = 0;
253    
254          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
# Line 214  int bbsnet_connect(int n) Line 258  int bbsnet_connect(int n)
258                  return -2;                  return -2;
259          }          }
260    
261          bzero(&sin, sizeof(sin));          memset(&sin, 0, sizeof(sin));
262          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
263          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
264          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
# Line 223  int bbsnet_connect(int n) Line 267  int bbsnet_connect(int n)
267          remote_addr[sizeof(remote_addr) - 1] = '\0';          remote_addr[sizeof(remote_addr) - 1] = '\0';
268          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
269    
270          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");
271          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
272    
273            // Set socket as non-blocking
274            flags_sock = fcntl(sock, F_GETFL, 0);
275            fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);
276    
277            // Set STDIN/STDOUT as non-blocking
278            flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);
279            flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);
280            fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
281            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
282    
283          epollfd = epoll_create1(0);          epollfd = epoll_create1(0);
284          if (epollfd < 0)          if (epollfd < 0)
285          {          {
# Line 238  int bbsnet_connect(int n) Line 292  int bbsnet_connect(int n)
292          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
293          {          {
294                  log_error("epoll_ctl(socket) error (%d)\n", errno);                  log_error("epoll_ctl(socket) error (%d)\n", errno);
295                  return -1;                  goto cleanup;
296          }          }
297    
298          ev.events = EPOLLIN;          ev.events = EPOLLIN;
# Line 246  int bbsnet_connect(int n) Line 300  int bbsnet_connect(int n)
300          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
301          {          {
302                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
303                  return -1;                  goto cleanup;
304          }          }
305    
         // Set socket as non-blocking  
         flags_sock = fcntl(sock, F_GETFL, 0);  
         fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);  
   
306          while (!SYS_server_exit)          while (!SYS_server_exit)
307          {          {
308                  if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)                  if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
309                  {                  {
310                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
311                          {                          {
                                 log_std("Debug: %d\n", errno);  
312                                  // Use select / epoll to check writability of the socket,                                  // Use select / epoll to check writability of the socket,
313                                  // then use getsockopt to check the status of the socket.                                  // then use getsockopt to check the status of the socket.
314                                  // See man connect(2)                                  // See man connect(2)
# Line 272  int bbsnet_connect(int n) Line 321  int bbsnet_connect(int n)
321                          else                          else
322                          {                          {
323                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)\n", errno);
324                                  prints("\033[1;31m连接失败!\033[m\r\n");  
325                                    prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n");
326                                  press_any_key();                                  press_any_key();
327                                  return -1;  
328                                    goto cleanup;
329                          }                          }
330                  }                  }
331          }          }
# Line 288  int bbsnet_connect(int n) Line 339  int bbsnet_connect(int n)
339                          if (errno != EINTR)                          if (errno != EINTR)
340                          {                          {
341                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
342                                  return -1;                                  break;
343                          }                          }
344                  }                  }
345                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
# Line 305  int bbsnet_connect(int n) Line 356  int bbsnet_connect(int n)
356                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
357                                          {                                          {
358                                                  log_error("getsockopt() error (%d) !\n", error);                                                  log_error("getsockopt() error (%d) !\n", error);
359                                                  return -1;                                                  goto cleanup;
360                                          }                                          }
361                                          if (error == 0)                                          if (error == 0)
362                                          {                                          {
# Line 315  int bbsnet_connect(int n) Line 366  int bbsnet_connect(int n)
366                                  else if (events[i].data.fd == STDIN_FILENO)                                  else if (events[i].data.fd == STDIN_FILENO)
367                                  {                                  {
368                                          ch = igetch(0);                                          ch = igetch(0);
369                                          if (ch == Ctrl('C'))                                          if (ch == Ctrl('C') || ch == KEY_ESC)
370                                          {                                          {
371                                                  return 0;                                                  goto cleanup;
372                                          }                                          }
373                                  }                                  }
374                          }                          }
# Line 325  int bbsnet_connect(int n) Line 376  int bbsnet_connect(int n)
376          }          }
377          if (SYS_server_exit)          if (SYS_server_exit)
378          {          {
379                  return 0;                  goto cleanup;
380          }          }
381          if (!sock_connected)          if (!sock_connected)
382          {          {
383                  prints("\033[1;31m连接超时!\033[m\r\n");                  prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n");
384                  press_any_key();                  press_any_key();
385                  return -1;  
386                    goto cleanup;
387          }          }
388    
389          tos = IPTOS_LOWDELAY;          tos = IPTOS_LOWDELAY;
# Line 340  int bbsnet_connect(int n) Line 392  int bbsnet_connect(int n)
392                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
393          }          }
394    
395          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m杩炴帴鎴愬姛锛乗033[m\r\n");
396          iflush();          iflush();
397          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);
398    
399          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
400          ev.data.fd = sock;          ev.data.fd = sock;
401          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
402          {          {
403                  log_error("epoll_ctl(socket) error (%d)\n", errno);                  log_error("epoll_ctl(socket) error (%d)\n", errno);
404                  return -1;                  goto cleanup;
405          }          }
406    
407          ev.events = EPOLLOUT;          ev.events = EPOLLOUT;
# Line 357  int bbsnet_connect(int n) Line 409  int bbsnet_connect(int n)
409          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
410          {          {
411                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
412                  return -1;                  goto cleanup;
413          }          }
414    
415          // Set STDIN/STDOUT as non-blocking          BBS_last_access_tm = t_used = time(NULL);
         flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);  
         flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);  
         fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);  
         fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);  
   
         BBS_last_access_tm = t_used = time(0);  
416          loop = 1;          loop = 1;
417    
418          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
419          {          {
420                    if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
421                    {
422                            log_error("SSH channel is closed\n");
423                            loop = 0;
424                            break;
425                    }
426    
427                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
428    
429                  if (nfds < 0)                  if (nfds < 0)
# Line 384  int bbsnet_connect(int n) Line 437  int bbsnet_connect(int n)
437                  }                  }
438                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
439                  {                  {
440                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
441                          {                          {
442                                  break;                                  break;
443                          }                          }
# Line 398  int bbsnet_connect(int n) Line 451  int bbsnet_connect(int n)
451                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
452                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
453                                  {                                  {
454                                          ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                          if (SSH_v2)
455                                            {
456                                                    ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
457                                                    if (ret == SSH_ERROR)
458                                                    {
459                                                            log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
460                                                            loop = 0;
461                                                            break;
462                                                    }
463                                                    else if (ret == SSH_EOF)
464                                                    {
465                                                            stdin_read_wait = 0;
466                                                            loop = 0;
467                                                            break;
468                                                    }
469                                                    else if (ret == 0)
470                                                    {
471                                                            stdin_read_wait = 0;
472                                                            break; // Check whether channel is still open
473                                                    }
474                                            }
475                                            else
476                                            {
477                                                    ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
478                                            }
479                                          if (ret < 0)                                          if (ret < 0)
480                                          {                                          {
481                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 419  int bbsnet_connect(int n) Line 496  int bbsnet_connect(int n)
496                                          }                                          }
497                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
498                                          {                                          {
499                                                  log_std("read(STDIN) EOF\n");  #ifdef _DEBUG
500                                                    log_error("read(STDIN) EOF\n");
501    #endif
502                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
503                                                  loop = 0;                                                  loop = 0;
504                                                  break;                                                  break;
# Line 427  int bbsnet_connect(int n) Line 506  int bbsnet_connect(int n)
506                                          else                                          else
507                                          {                                          {
508                                                  input_buf_len += ret;                                                  input_buf_len += ret;
509                                                  BBS_last_access_tm = time(0);                                                  BBS_last_access_tm = time(NULL);
510    
511                                                    // Refresh current action while user input
512                                                    if (user_online_update("BBS_NET") < 0)
513                                                    {
514                                                            log_error("user_online_update(BBS_NET) error\n");
515                                                    }
516    
517                                                  continue;                                                  continue;
518                                          }                                          }
519                                  }                                  }
# Line 459  int bbsnet_connect(int n) Line 545  int bbsnet_connect(int n)
545                                          }                                          }
546                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
547                                          {                                          {
548                                                  log_std("write(socket) EOF\n");  #ifdef _DEBUG
549                                                    log_error("write(socket) EOF\n");
550    #endif
551                                                  sock_write_wait = 0;                                                  sock_write_wait = 0;
552                                                  loop = 0;                                                  loop = 0;
553                                                  break;                                                  break;
# Line 504  int bbsnet_connect(int n) Line 592  int bbsnet_connect(int n)
592                                          }                                          }
593                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
594                                          {                                          {
595                                                  log_std("read(socket) EOF\n");  #ifdef _DEBUG
596                                                    log_error("read(socket) EOF\n");
597    #endif
598                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
599                                                  loop = 0;                                                  loop = 0;
600                                                  break;                                                  break;
# Line 522  int bbsnet_connect(int n) Line 612  int bbsnet_connect(int n)
612                                  stdout_write_wait = 1;                                  stdout_write_wait = 1;
613                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)
614                                  {                                  {
615                                          ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                          if (SSH_v2)
616                                            {
617                                                    ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));
618                                                    if (ret == SSH_ERROR)
619                                                    {
620                                                            log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
621                                                            loop = 0;
622                                                            break;
623                                                    }
624                                            }
625                                            else
626                                            {
627                                                    ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
628                                            }
629                                          if (ret < 0)                                          if (ret < 0)
630                                          {                                          {
631                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 543  int bbsnet_connect(int n) Line 646  int bbsnet_connect(int n)
646                                          }                                          }
647                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
648                                          {                                          {
649                                                  log_std("write(STDOUT) EOF\n");  #ifdef _DEBUG
650                                                    log_error("write(STDOUT) EOF\n");
651    #endif
652                                                  stdout_write_wait = 0;                                                  stdout_write_wait = 0;
653                                                  loop = 0;                                                  loop = 0;
654                                                  break;                                                  break;
# Line 564  int bbsnet_connect(int n) Line 669  int bbsnet_connect(int n)
669                  }                  }
670          }          }
671    
672    cleanup:
673            if (close(epollfd) < 0)
674            {
675                    log_error("close(epoll) error (%d)\n");
676            }
677    
678          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
679          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
680          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
# Line 576  int bbsnet_connect(int n) Line 687  int bbsnet_connect(int n)
687                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
688          }          }
689    
690          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
691          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
692    
693          log_std("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
694                          tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,                             tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,
695                          tm_used->tm_sec);                             tm_used->tm_sec);
696    
697          return 0;          return 0;
698  }  }
# Line 604  bbsnet_refresh() Line 715  bbsnet_refresh()
715          moveto(22, 0);          moveto(22, 0);
716          prints(" ----------------------------------------------------------------------------- ");          prints(" ----------------------------------------------------------------------------- ");
717          moveto(23, 0);          moveto(23, 0);
718          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]閫鍑");
719    
720          iflush();          iflush();
721    
722          return 0;          return 0;
723  }  }
724    
725  int bbsnet_selchange(int new_pos)  int bbsnet_selchange()
726  {  {
727            int i = bbsnet_menu.menu_item_pos[0];
728    
729          moveto(20, 0);          moveto(20, 0);
730          clrtoeol();          clrtoeol();
731          prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m  站名:\x1b[1;33m%s\x1b[m",          prints("|\x1b[1m鍗曚綅:\x1b[1;33m%-18s\x1b[m  绔欏悕:\x1b[1;33m%s\x1b[m",
732                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
733          moveto(20, 79);          moveto(20, 79);
734          prints("|");          prints("|");
735          moveto(21, 0);          moveto(21, 0);
736          clrtoeol();          clrtoeol();
737          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m杩炲線:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
738          if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[i].port != 23)
739          {          {
740                  prints("  %d", bbsnet_conf[new_pos].port);                  prints("  %d", bbsnet_conf[i].port);
741          }          }
742          prints("\x1b[m");          prints("\x1b[m");
743          moveto(21, 79);          moveto(21, 79);
# Line 636  int bbsnet_selchange(int new_pos) Line 749  int bbsnet_selchange(int new_pos)
749    
750  int bbs_net()  int bbs_net()
751  {  {
752          int ch, pos, i;          int ch, i;
753    
754          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
755    
756          BBS_last_access_tm = time(0);          BBS_last_access_tm = time(NULL);
757    
758          clearscr();          clearscr();
759          bbsnet_refresh();          bbsnet_refresh();
760          pos = bbsnet_menu.p_menu[0]->item_cur_pos;          display_menu(&bbsnet_menu);
761          display_menu(get_menu(&bbsnet_menu, "BBSNET"));          bbsnet_selchange();
         bbsnet_selchange(pos);  
762    
763          while (!SYS_server_exit)          while (!SYS_server_exit)
764          {          {
765                  ch = igetch(0);                  ch = igetch(100);
766    
767                  switch (ch)                  switch (ch)
768                  {                  {
769                  case KEY_NULL:  // broken pipe                  case KEY_NULL: // broken pipe
770                    case KEY_ESC:
771                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
772                          return 0;                          goto cleanup;
773                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
774                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
775                          {                          {
776                                  return 0;                                  goto cleanup;
777                          }                          }
778                          continue;                          continue;
779                  case CR:                  case CR:
780                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          igetch_reset();
781                          bbsnet_connect(pos);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
782                          bbsnet_refresh();                          bbsnet_refresh();
783                          display_current_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
784                          bbsnet_selchange(pos);                          bbsnet_selchange();
785                          break;                          break;
786                  case KEY_UP:                  case KEY_UP:
787                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
788                          {                          {
789                                  menu_control(&bbsnet_menu, KEY_UP);                                  menu_control(&bbsnet_menu, KEY_UP);
790                          }                          }
791                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
792                          break;                          break;
793                  case KEY_DOWN:                  case KEY_DOWN:
794                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
795                          {                          {
796                                  menu_control(&bbsnet_menu, KEY_DOWN);                                  menu_control(&bbsnet_menu, KEY_DOWN);
797                          }                          }
798                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
799                          break;                          break;
800                  case KEY_LEFT:                  case KEY_LEFT:
801                          menu_control(&bbsnet_menu, KEY_UP);                          menu_control(&bbsnet_menu, KEY_UP);
802                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
803                          break;                          break;
804                  case KEY_RIGHT:                  case KEY_RIGHT:
805                          menu_control(&bbsnet_menu, KEY_DOWN);                          menu_control(&bbsnet_menu, KEY_DOWN);
806                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
807                          bbsnet_selchange(pos);                          break;
808                    case KEY_HOME:
809                    case KEY_PGUP:
810                            menu_control(&bbsnet_menu, KEY_PGUP);
811                            bbsnet_selchange();
812                            break;
813                    case KEY_END:
814                    case KEY_PGDN:
815                            menu_control(&bbsnet_menu, KEY_PGDN);
816                            bbsnet_selchange();
817                          break;                          break;
818                  default:                  default:
819                          menu_control(&bbsnet_menu, ch);                          menu_control(&bbsnet_menu, ch);
820                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
821                          break;                          break;
822                  }                  }
823                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(NULL);
824          }          }
825    
826    cleanup:
827            unload_bbsnet_conf();
828    
829          return 0;          return 0;
830  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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