/[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.108 by sysadm, Sat Dec 20 10:46:42 2025 UTC Revision 1.109 by sysadm, Mon Dec 22 06:54:10 2025 UTC
# Line 45  Line 45 
45  #include <poll.h>  #include <poll.h>
46  #endif  #endif
47    
 static const char MENU_CONF_DELIM[] = " \t\r\n";  
   
48  enum _bbs_net_constant_t  enum _bbs_net_constant_t
49  {  {
50          MAXSTATION = 26 * 2,          MAXSTATION = 26 * 2,
51          STATION_PER_LINE = 4,          STATION_PER_LINE = 4,
52            ORG_NAME_MAX_LEN = 40,
53            SITE_NAME_MAX_LEN = 40,
54            HOSTNAME_MAX_LEN = 253,
55            PORT_STR_MAX_LEN = 5,
56          USERNAME_MAX_LEN = 20,          USERNAME_MAX_LEN = 20,
57          PASSWORD_MAX_LEN = 20,          PASSWORD_MAX_LEN = 20,
58          REMOTE_CONNECT_TIMEOUT = 10, // seconds          REMOTE_CONNECT_TIMEOUT = 10, // seconds
# Line 60  enum _bbs_net_constant_t Line 62  enum _bbs_net_constant_t
62    
63  struct _bbsnet_conf  struct _bbsnet_conf
64  {  {
65          char org_name[40];          char org_name[ORG_NAME_MAX_LEN + 1];
66          char site_name[40];          char site_name[SITE_NAME_MAX_LEN + 1];
67          char host_name[IP_ADDR_LEN];          char host_name[HOSTNAME_MAX_LEN + 1];
68          char port[6];          char port[PORT_STR_MAX_LEN + 1];
69          int8_t use_ssh;          int8_t use_ssh;
70          char charset[CHARSET_MAX_LEN + 1];          char charset[CHARSET_MAX_LEN + 1];
71  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
72    
73    static const char MENU_CONF_DELIM[] = " \t\r\n";
74    
75  static MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
76    
77  static void unload_bbsnet_conf(void);  static void unload_bbsnet_conf(void);
78    
79  static int load_bbsnet_conf(const char *file_config)  static int load_bbsnet_conf(const char *file_config)
80  {  {
81          FILE *fp;          FILE *fin;
82            int fin_line = 0;
83          MENU *p_menu;          MENU *p_menu;
84          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
85          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
86          char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;          char line[LINE_BUFFER_LEN];
87            char *p = NULL;
88            char *saveptr = NULL;
89          long port;          long port;
90          char *endptr;          char *endptr;
91    
# Line 108  static int load_bbsnet_conf(const char * Line 115  static int load_bbsnet_conf(const char *
115          p_menu->title.show = 0;          p_menu->title.show = 0;
116          p_menu->screen_show = 0;          p_menu->screen_show = 0;
117    
118          fp = fopen(file_config, "r");          fin = fopen(file_config, "r");
119          if (fp == NULL)          if (fin == NULL)
120          {          {
121                  unload_bbsnet_conf();                  unload_bbsnet_conf();
122                  return -2;                  return -2;
123          }          }
124    
125          menu_item_id = 0;          menu_item_id = 0;
126          while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION)          while (fgets(line, sizeof(line), fin) && menu_item_id < MAXSTATION)
127          {          {
128                  t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr);                  fin_line++;
                 t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
                 t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
                 t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
                 t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
                 t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
129    
130                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||                  p = strtok_r(line, MENU_CONF_DELIM, &saveptr);
131                          t5 == NULL || t6 == NULL || t1[0] == '#')                  if (p == NULL) // Blank line
132                  {                  {
133                          continue;                          continue;
134                  }                  }
135    
136                  strncpy(bbsnet_conf[menu_item_id].site_name, t2, sizeof(bbsnet_conf[menu_item_id].site_name) - 1);                  if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
137                  bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0';                  {
138                  strncpy(bbsnet_conf[menu_item_id].org_name, t1, sizeof(bbsnet_conf[menu_item_id].org_name) - 1);                          continue;
139                    }
140    
141                    if (strlen(p) > sizeof(bbsnet_conf[menu_item_id].org_name) - 1)
142                    {
143                            log_error("Error org_name in BBSNET config line %d", fin_line);
144                            continue;
145                    }
146                    strncpy(bbsnet_conf[menu_item_id].org_name, p, sizeof(bbsnet_conf[menu_item_id].org_name) - 1);
147                  bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0';                  bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0';
148                  strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);  
149                    p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
150                    if (p == NULL || strlen(p) > sizeof(bbsnet_conf[menu_item_id].site_name) - 1)
151                    {
152                            log_error("Error site_name in BBSNET config line %d", fin_line);
153                            continue;
154                    }
155                    strncpy(bbsnet_conf[menu_item_id].site_name, p, sizeof(bbsnet_conf[menu_item_id].site_name) - 1);
156                    bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0';
157    
158                    p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
159                    if (p == NULL || strlen(p) > sizeof(bbsnet_conf[menu_item_id].host_name) - 1)
160                    {
161                            log_error("Error host_name in BBSNET config line %d", fin_line);
162                            continue;
163                    }
164                    strncpy(bbsnet_conf[menu_item_id].host_name, p, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);
165                  bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0';                  bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0';
166                  port = strtol(t4, &endptr, 10);  
167                    p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
168                    if (p == NULL || strlen(p) > sizeof(bbsnet_conf[menu_item_id].port) - 1)
169                    {
170                            log_error("Error port in BBSNET config line %d", fin_line);
171                            continue;
172                    }
173                    port = strtol(p, &endptr, 10);
174                  if (*endptr != '\0' || port <= 0 || port > 65535)                  if (*endptr != '\0' || port <= 0 || port > 65535)
175                  {                  {
176                          log_error("Invalid port value %ld of menu item %d", port, menu_item_id);                          log_error("Invalid port %ld in BBSNET config line %d", port, fin_line);
177                          fclose(fp);                          continue;
                         unload_bbsnet_conf();  
                         return -3;  
178                  }                  }
179                  strncpy(bbsnet_conf[menu_item_id].port, t4, sizeof(bbsnet_conf[menu_item_id].port) - 1);                  strncpy(bbsnet_conf[menu_item_id].port, p, sizeof(bbsnet_conf[menu_item_id].port) - 1);
180                  bbsnet_conf[menu_item_id].port[sizeof(bbsnet_conf[menu_item_id].port) - 1] = '\0';                  bbsnet_conf[menu_item_id].port[sizeof(bbsnet_conf[menu_item_id].port) - 1] = '\0';
181                  bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');  
182                  strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);                  p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
183                    if (p == NULL || strlen(p) != 1)
184                    {
185                            log_error("Error use_ssh in BBSNET config line %d", fin_line);
186                            continue;
187                    }
188                    bbsnet_conf[menu_item_id].use_ssh = (toupper(p[0]) == 'Y');
189    
190                    p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
191                    if (p == NULL || strlen(p) > sizeof(bbsnet_conf[menu_item_id].charset) - 1)
192                    {
193                            log_error("Error charset in BBSNET config line %d", fin_line);
194                            continue;
195                    }
196                    strncpy(bbsnet_conf[menu_item_id].charset, p, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
197                  bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';                  bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
198    
199                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
200                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
201                  {                  {
202                          log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);                          log_error("get_menu_item_by_id(%d) error: NULL pointer", menu_item_id);
203                          fclose(fp);                          fclose(fin);
204                          unload_bbsnet_conf();                          unload_bbsnet_conf();
205                          return -3;                          return -3;
206                  }                  }
# Line 182  static int load_bbsnet_conf(const char * Line 227  static int load_bbsnet_conf(const char *
227          bbsnet_menu.menu_item_pos[0] = 0;          bbsnet_menu.menu_item_pos[0] = 0;
228          bbsnet_menu.choose_step = 0;          bbsnet_menu.choose_step = 0;
229    
230          fclose(fp);          fclose(fin);
231    
232          return 0;          return 0;
233  }  }
# Line 359  static int bbsnet_connect(int n) Line 404  static int bbsnet_connect(int n)
404                  }                  }
405    
406                  moveto(1, 1);                  moveto(1, 1);
407                  prints("通过SSH方式连接[%s]...", bbsnet_conf[n].site_name);                  prints("\033[1;32m正在准备往 %s (%s:%s) 的%s连接... \033[m\r\n",
408                  moveto(2, 1);                             bbsnet_conf[n].site_name, bbsnet_conf[n].host_name, bbsnet_conf[n].port,
409                               (bbsnet_conf[n].use_ssh ? "SSH" : "Telnet"));
410                  prints("请输入用户名: ");                  prints("请输入用户名: ");
411                  iflush();                  iflush();
412                  if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)                  if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)
# Line 388  static int bbsnet_connect(int n) Line 434  static int bbsnet_connect(int n)
434          clearscr();          clearscr();
435    
436          moveto(1, 1);          moveto(1, 1);
437          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s:%s) 的%s连接,请稍候... \033[m\r\n",
438                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name, bbsnet_conf[n].port,
439                       (bbsnet_conf[n].use_ssh ? "SSH" : "Telnet"));
440          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");
441          progress_bar(0, PROGRESS_BAR_LEN);          progress_bar(0, PROGRESS_BAR_LEN);
442    


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

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