/[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.6 by sysadm, Mon Mar 21 18:14:00 2005 UTC Revision 1.10 by sysadm, Tue Mar 22 13:36:13 2005 UTC
# Line 109  load_bbsnet_conf (const char *file_confi Line 109  load_bbsnet_conf (const char *file_confi
109    return 0;    return 0;
110  }  }
111    
 // from Maple-hightman  
 // added by flyriver, 2001.3.3  
 int  
 telnetopt (int fd, char *buf, int max)  
 {  
   unsigned char c, d, e;  
   int pp = 0;  
   unsigned char tmp[30];  
   
   while (pp < max)  
     {  
       c = buf[pp++];  
       if (c == 255)  
         {  
           d = buf[pp++];  
           e = buf[pp++];  
           iflush ();  
           if ((d == 253) && (e == 3 || e == 24))  
             {  
               tmp[0] = 255;  
               tmp[1] = 251;  
               tmp[2] = e;  
               write (fd, tmp, 3);  
               continue;  
             }  
           if ((d == 251 || d == 252) && (e == 1 || e == 3 || e == 24))  
             {  
               tmp[0] = 255;  
               tmp[1] = 253;  
               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;  
 }  
   
112  static void  static void
113  process_bar (int n, int len)  process_bar (int n, int len)
114  {  {
# Line 192  process_bar (int n, int len) Line 119  process_bar (int n, int len)
119    char *ptr3;    char *ptr3;
120    
121    moveto (4, 0);    moveto (4, 0);
122    prints ("┌───────────────┐\n");    prints ("┌───────────────┐\r\n");
123    sprintf (buf2, "            %3d%%              ", n * 100 / len);    sprintf (buf2, "            %3d%%              ", n * 100 / len);
124    ptr = buf;    ptr = buf;
125    ptr2 = buf2;    ptr2 = buf2;
# Line 207  process_bar (int n, int len) Line 134  process_bar (int n, int len)
134    while (*ptr2 != '\0')    while (*ptr2 != '\0')
135      *ptr++ = *ptr2++;      *ptr++ = *ptr2++;
136    *ptr++ = '\0';    *ptr++ = '\0';
137    prints ("│\033[46m%s\033[m│\n", buf);    prints ("│\033[46m%s\033[m│\r\n", buf);
138    prints ("└───────────────┘\n");    prints ("└───────────────┘\r\n");
139      iflush ();
140  }  }
141    
142  int  int
# Line 217  bbsnet_connect (int n) Line 145  bbsnet_connect (int n)
145    int sock, ch, result, len, loop;    int sock, ch, result, len, loop;
146    struct sockaddr_in sin;    struct sockaddr_in sin;
147    char buf[256];    char buf[256];
   time_t now, t_last_action;  
148    fd_set inputs, testfds;    fd_set inputs, testfds;
149    struct timeval timeout;    struct timeval timeout;
150    struct hostent *pHost = NULL;    struct hostent *pHost = NULL;
151    int rc, rv, tos = 020, i;    int rc, rv, tos = 020, i;
152      char remote_addr[256];
153      int remote_port;
154      time_t t_used;
155      struct tm *tm_used;
156    
157      clearscr ();
158    
159    prints ("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\n",    moveto (0, 0);
160      prints ("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
161            bbsnet_conf[n].host1, bbsnet_conf[n].ip);            bbsnet_conf[n].host1, bbsnet_conf[n].ip);
162    prints ("\033[1;32m如果在 %d 秒内无法连上,穿梭程序将放弃连接。\033[m\n",    prints ("\033[1;32m如果在 %d 秒内无法连上,穿梭程序将放弃连接。\033[m\r\n",
163            TIME_OUT);            TIME_OUT);
164      iflush ();
165    
166    pHost = gethostbyname (bbsnet_conf[n].ip);    pHost = gethostbyname (bbsnet_conf[n].ip);
167    
168    if (pHost == NULL)    if (pHost == NULL)
169      {      {
170        prints ("\033[1;31m查找主机名失败!\033[m\n");        prints ("\033[1;31m查找主机名失败!\033[m\r\n");
171        press_any_key ();        press_any_key ();
172        return -1;        return -1;
173      }      }
# Line 241  bbsnet_connect (int n) Line 176  bbsnet_connect (int n)
176    
177    if (sock < 0)    if (sock < 0)
178      {      {
179        prints ("\033[1;31m无法创建socket!\033[m\n");        prints ("\033[1;31m无法创建socket!\033[m\r\n");
180        press_any_key ();        press_any_key ();
181        return -1;        return -1;
182      }      }
# Line 260  bbsnet_connect (int n) Line 195  bbsnet_connect (int n)
195    
196    bzero (&sin, sizeof (sin));    bzero (&sin, sizeof (sin));
197    sin.sin_family = AF_INET;    sin.sin_family = AF_INET;
   sin.sin_port = htons (bbsnet_conf[n].port);  
198    sin.sin_addr = *(struct in_addr *) pHost->h_addr_list[0];    sin.sin_addr = *(struct in_addr *) pHost->h_addr_list[0];
199      sin.sin_port = htons (bbsnet_conf[n].port);
200    
201      strcpy (remote_addr, inet_ntoa (sin.sin_addr));
202      remote_port = ntohs (sin.sin_port);
203    
204    prints ("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\n");    prints ("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\r\n");
205    process_bar (0, MAX_PROCESS_BAR_LEN);    process_bar (0, MAX_PROCESS_BAR_LEN);
206    for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)    for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)
207      {      {
# Line 285  bbsnet_connect (int n) Line 222  bbsnet_connect (int n)
222          break;          break;
223        else        else
224          {          {
225            prints ("\033[1;31m连接失败!\033[m\n");            prints ("\033[1;31m连接失败!\033[m\r\n");
226            press_any_key ();            press_any_key ();
227            return -1;            return -1;
228          }          }
229      }      }
230    if (i == MAX_PROCESS_BAR_LEN)    if (i == MAX_PROCESS_BAR_LEN)
231      {      {
232        prints ("\033[1;31m连接超时!\033[m\n");        prints ("\033[1;31m连接超时!\033[m\r\n");
233        press_any_key ();        press_any_key ();
234        return -1;        return -1;
235      }      }
236    setsockopt (sock, IPPROTO_IP, IP_TOS, &tos, sizeof (int));    setsockopt (sock, IPPROTO_IP, IP_TOS, &tos, sizeof (int));
237    prints ("\033[1;31m连接成功!\033[m\n");  
238      prints ("\033[1;31m连接成功!\033[m\r\n");
239      log_std ("BBSNET connect to %s:%d\n", remote_addr, remote_port);
240    
241    FD_ZERO (&inputs);    FD_ZERO (&inputs);
242    FD_SET (0, &inputs);    FD_SET (0, &inputs);
243    FD_SET (sock, &inputs);    FD_SET (sock, &inputs);
244    
245    t_last_action = time (0);    BBS_last_access_tm = t_used = time (0);
246    
247    loop = 1;    loop = 1;
248    
249    while (loop)    while (loop)
250      {      {
251        testfds = inputs;        testfds = inputs;
252        timeout.tv_sec = 0;        timeout.tv_sec = TIME_OUT;
253        timeout.tv_usec = 100000;        timeout.tv_usec = 0;
254    
255        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,
256                         (fd_set *) NULL, &timeout);                         (fd_set *) NULL, &timeout);
# Line 319  bbsnet_connect (int n) Line 258  bbsnet_connect (int n)
258        switch (result)        switch (result)
259          {          {
260          case 0:          case 0:
261              if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME)
262                {
263                  loop = 0;
264                }
265            break;            break;
266          case -1:          case -1:
267            log_error ("select() error!\n");            log_error ("select() error!\n");
# Line 345  bbsnet_connect (int n) Line 288  bbsnet_connect (int n)
288                write (1, buf, len);                write (1, buf, len);
289              }              }
290            break;            break;
291          }            BBS_last_access_tm = time (0);
       if (time (0) - t_last_action >= 10)  
         {  
           t_last_action = time (0);  
292          }          }
293      }      }
294    
# Line 357  bbsnet_connect (int n) Line 297  bbsnet_connect (int n)
297        log_error ("Close socket failed\n");        log_error ("Close socket failed\n");
298      }      }
299    
300      t_used = time (0) - t_used;
301      tm_used = gmtime (&t_used);
302    
303      log_std ("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
304               tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,
305               tm_used->tm_sec);
306    
307    return 0;    return 0;
308  }  }
309    
# Line 383  bbsnet_refresh () Line 330  bbsnet_refresh ()
330    prints    prints
331      ("╰══════════════════════════════════════╯");      ("╰══════════════════════════════════════╯");
332    moveto (23, 0);    moveto (23, 0);
333    prints    prints (" [\x1b[1;32mCtrl+C\x1b[m]退出");
     (" [\x1b[1;32mCtrl+C\x1b[m]退出");  
334    iflush ();    iflush ();
335    
336    return 0;    return 0;
# Line 415  bbsnet_selchange (int new_pos) Line 361  bbsnet_selchange (int new_pos)
361  int  int
362  bbs_net ()  bbs_net ()
363  {  {
364    int ch, result, pos, i;    int ch, pos, i;
365    char file_config[256];    char file_config[256];
   time_t t_last_action;  
   fd_set inputs, testfds;  
   struct timeval timeout;  
366    
367    strcpy (file_config, app_home_dir);    strcpy (file_config, app_home_dir);
368    strcat (file_config, "conf/bbsnet.conf");    strcat (file_config, "conf/bbsnet.conf");
369    
370    load_bbsnet_conf (file_config);    load_bbsnet_conf (file_config);
371    
372    FD_ZERO (&inputs);    BBS_last_access_tm = time (0);
   FD_SET (0, &inputs);  
   
   t_last_action = time (0);  
373    
374    clearscr ();    clearscr ();
375    bbsnet_refresh ();    bbsnet_refresh ();
# Line 439  bbs_net () Line 379  bbs_net ()
379    
380    while (1)    while (1)
381      {      {
382        testfds = inputs;        ch = igetch ();
383        timeout.tv_sec = 0;        switch (ch)
       timeout.tv_usec = 100000;  
   
       result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,  
                        (fd_set *) NULL, &timeout);  
       switch (result)  
384          {          {
385          case 0:          case KEY_NULL:
386            case Ctrl ('C'):
387              return 0;
388            case KEY_TIMEOUT:
389              if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME)
390                {
391                  return -1;
392                }
393              continue;
394            case CR:
395              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
396              bbsnet_connect (pos);
397              bbsnet_refresh ();
398              display_current_menu (&bbsnet_menu);
399              bbsnet_selchange (pos);
400            break;            break;
401          case -1:          case KEY_UP:
402            log_error ("select() error!\n");            for (i = 0; i < STATION_PER_LINE; i++)
403                menu_control (&bbsnet_menu, KEY_UP);
404              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
405              bbsnet_selchange (pos);
406              break;
407            case KEY_DOWN:
408              for (i = 0; i < STATION_PER_LINE; i++)
409                menu_control (&bbsnet_menu, KEY_DOWN);
410              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
411              bbsnet_selchange (pos);
412              break;
413            case KEY_LEFT:
414              menu_control (&bbsnet_menu, KEY_UP);
415              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
416              bbsnet_selchange (pos);
417              break;
418            case KEY_RIGHT:
419              menu_control (&bbsnet_menu, KEY_DOWN);
420              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
421              bbsnet_selchange (pos);
422            break;            break;
423          default:          default:
424            if (FD_ISSET (0, &testfds))            menu_control (&bbsnet_menu, ch);
425              {            pos = bbsnet_menu.p_menu[0]->item_cur_pos;
426                ch = igetch ();            bbsnet_selchange (pos);
               switch (ch)  
                 {  
                 case KEY_NULL:  
                 case Ctrl ('C'):  
                   return 0;  
                 case CR:  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_connect (pos);  
                   bbsnet_refresh ();  
                   display_current_menu (&bbsnet_menu);  
                   bbsnet_selchange (pos);  
                   break;  
                 case KEY_UP:  
                   for (i = 0; i < STATION_PER_LINE; i++)  
                     menu_control (&bbsnet_menu, KEY_UP);  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_selchange (pos);  
                   break;  
                 case KEY_DOWN:  
                   for (i = 0; i < STATION_PER_LINE; i++)  
                     menu_control (&bbsnet_menu, KEY_DOWN);  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_selchange (pos);  
                   break;  
                 case KEY_LEFT:  
                   menu_control (&bbsnet_menu, KEY_UP);  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_selchange (pos);  
                   break;  
                 case KEY_RIGHT:  
                   menu_control (&bbsnet_menu, KEY_DOWN);  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_selchange (pos);  
                   break;  
                 default:  
                   menu_control (&bbsnet_menu, ch);  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_selchange (pos);  
                   break;  
                 }  
             }  
427            break;            break;
428          }          }
429        if (time (0) - t_last_action >= 10)        BBS_last_access_tm = time (0);
         {  
           t_last_action = time (0);  
         }  
430      }      }
431    
432    return 0;    return 0;


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

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