/[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.7 by sysadm, Tue Mar 22 08:19:11 2005 UTC Revision 1.13 by sysadm, Sat May 7 12:08:28 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 221  bbsnet_connect (int n) Line 149  bbsnet_connect (int n)
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    prints ("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\n",    clearscr ();
158    
159      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 240  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 259  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 284  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    BBS_last_access_tm = time (0);    BBS_last_access_tm = t_used = time (0);
246    
247    loop = 1;    loop = 1;
248    
# Line 312  bbsnet_connect (int n) Line 252  bbsnet_connect (int n)
252        timeout.tv_sec = TIME_OUT;        timeout.tv_sec = TIME_OUT;
253        timeout.tv_usec = 0;        timeout.tv_usec = 0;
254    
255        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,        result = SignalSafeSelect (FD_SETSIZE, &testfds, (fd_set *) NULL,
256                         (fd_set *) NULL, &timeout);                         (fd_set *) NULL, &timeout);
257    
258        switch (result)        if (result == 0)
259          {          {
         case 0:  
260            if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME)            if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME)
261              {              {
262                loop = 0;                loop = 0;
263              }              }
264            break;          }
265          case -1:        if (result < 0)
266            log_error ("select() error!\n");          {
267            break;            log_error ("select() error (%d) !\n", result);
268          default:            loop = 0;
269            }
270          if (result > 0)
271            {
272            if (FD_ISSET (0, &testfds))            if (FD_ISSET (0, &testfds))
273              {              {
274                len = read (0, buf, 255);                len = read (0, buf, 255);
275                if (len == 0)                if (len == 0)
276                  {                  {
277                    loop = 0;                    loop = 0;
                   break;  
278                  }                  }
279                write (sock, buf, len);                write (sock, buf, len);
280              }              }
# Line 343  bbsnet_connect (int n) Line 284  bbsnet_connect (int n)
284                if (len == 0)                if (len == 0)
285                  {                  {
286                    loop = 0;                    loop = 0;
                   break;  
287                  }                  }
288                write (1, buf, len);                write (1, buf, len);
289              }              }
           break;  
290            BBS_last_access_tm = time (0);            BBS_last_access_tm = time (0);
291          }          }
292      }      }
# Line 357  bbsnet_connect (int n) Line 296  bbsnet_connect (int n)
296        log_error ("Close socket failed\n");        log_error ("Close socket failed\n");
297      }      }
298    
299      t_used = time (0) - t_used;
300      tm_used = gmtime (&t_used);
301    
302      log_std ("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
303               tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,
304               tm_used->tm_sec);
305    
306    return 0;    return 0;
307  }  }
308    
# Line 384  bbsnet_refresh () Line 330  bbsnet_refresh ()
330      ("╰══════════════════════════════════════╯");      ("╰══════════════════════════════════════╯");
331    moveto (23, 0);    moveto (23, 0);
332    prints (" [\x1b[1;32mCtrl+C\x1b[m]退出");    prints (" [\x1b[1;32mCtrl+C\x1b[m]退出");
333    
334    iflush ();    iflush ();
335    
336    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