/[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.29 by sysadm, Sat May 10 11:09:02 2025 UTC Revision 1.36 by sysadm, Sun May 11 12:47:32 2025 UTC
# Line 33  Line 33 
33  #include <sys/ioctl.h>  #include <sys/ioctl.h>
34  #include <sys/socket.h>  #include <sys/socket.h>
35  #include <netinet/in.h>  #include <netinet/in.h>
36    #include <netinet/ip.h>
37  #include <arpa/inet.h>  #include <arpa/inet.h>
38    
39  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
# Line 140  static void process_bar(int n, int len) Line 141  static void process_bar(int n, int len)
141          moveto(4, 0);          moveto(4, 0);
142          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
143          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
144          strncpy(buf2, buf, (size_t) n);          strncpy(buf2, buf, (size_t)n);
145          buf2[n] = '\0';          buf2[n] = '\0';
146          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
147          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
# Line 149  static void process_bar(int n, int len) Line 150  static void process_bar(int n, int len)
150    
151  int bbsnet_connect(int n)  int bbsnet_connect(int n)
152  {  {
153          int sock, flags, ret, loop, error;          int sock, ret, loop, error;
154          ssize_t len;          int flags_sock;
155            int flags_stdin;
156            int flags_stdout;
157            int len;
158          struct sockaddr_in sin;          struct sockaddr_in sin;
159          char buf[LINE_BUFFER_LEN];          char input_buf[LINE_BUFFER_LEN];
160            char output_buf[LINE_BUFFER_LEN];
161            int input_buf_len = 0;
162            int output_buf_len = 0;
163            int input_buf_offset = 0;
164            int output_buf_offset = 0;
165          fd_set read_fds;          fd_set read_fds;
166          fd_set write_fds;          fd_set write_fds;
167          struct timeval timeout;          struct timeval timeout;
168          struct hostent *p_host = NULL;          struct hostent *p_host = NULL;
169          int tos = 020, i;          int tos;
170            int i;
171          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
172          int remote_port;          int remote_port;
173          time_t t_used;          time_t t_used;
# Line 190  int bbsnet_connect(int n) Line 200  int bbsnet_connect(int n)
200          }          }
201    
202          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
203          sin.sin_addr.s_addr =          sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY);
                 (strnlen(hostaddr_server, sizeof(hostaddr_server)) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY);  
204          sin.sin_port = 0;          sin.sin_port = 0;
205    
206          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
207          {          {
208                  log_error("Bind address %s:%u failed\n",                  log_error("Bind address %s:%u failed (%d)\n",
209                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
210                  return -2;                  return -2;
211          }          }
212    
# Line 214  int bbsnet_connect(int n) Line 223  int bbsnet_connect(int n)
223          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
224    
225          // Set socket as non-blocking          // Set socket as non-blocking
226          flags = fcntl(sock, F_GETFL, 0);          flags_sock = fcntl(sock, F_GETFL, 0);
227          fcntl(sock, F_SETFL, flags | O_NONBLOCK);          fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);
228    
229          if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)          if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
230          {          {
# Line 230  int bbsnet_connect(int n) Line 239  int bbsnet_connect(int n)
239          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)
240          {          {
241                  ch = igetch(0); // 0.1 second                  ch = igetch(0); // 0.1 second
242                  if (ch == KEY_NULL || ch == Ctrl('C') || SYS_server_exit)                  if (ch == Ctrl('C') || SYS_server_exit)
243                  {                  {
244                          return 0;                          return 0;
245                  }                  }
# Line 243  int bbsnet_connect(int n) Line 252  int bbsnet_connect(int n)
252    
253                  timeout.tv_sec = 0;                  timeout.tv_sec = 0;
254                  timeout.tv_usec = 400 * 1000; // 0.4 second                  timeout.tv_usec = 400 * 1000; // 0.4 second
255            
256                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);
257    
258                  if (ret == 0) // Timeout                  if (ret == 0) // Timeout
# Line 278  int bbsnet_connect(int n) Line 287  int bbsnet_connect(int n)
287                  return -1;                  return -1;
288          }          }
289    
290          fcntl(sock, F_SETFL, flags); /* restore file status flags */          tos = IPTOS_LOWDELAY;
291          setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int));          if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
292            {
293                    log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
294            }
295    
296          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
297            iflush();
298          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);
299    
300          BBS_last_access_tm = t_used = time(0);          // Set STDIN/STDOUT as non-blocking
301            flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);
302            flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);
303            fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
304            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
305    
306            BBS_last_access_tm = t_used = time(0);
307          loop = 1;          loop = 1;
308    
309          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
# Line 293  int bbsnet_connect(int n) Line 311  int bbsnet_connect(int n)
311                  FD_ZERO(&read_fds);                  FD_ZERO(&read_fds);
312                  FD_SET(STDIN_FILENO, &read_fds);                  FD_SET(STDIN_FILENO, &read_fds);
313                  FD_SET(sock, &read_fds);                  FD_SET(sock, &read_fds);
314            
315                    FD_ZERO(&write_fds);
316                    FD_SET(STDOUT_FILENO, &write_fds);
317                    FD_SET(sock, &write_fds);
318    
319                  timeout.tv_sec = 0;                  timeout.tv_sec = 0;
320                  timeout.tv_usec = 100 * 1000; // 0.1 second                  timeout.tv_usec = 100 * 1000; // 0.1 second
321    
322                  ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout);                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);
323    
324                  if (ret == 0) // timeout                  if (ret == 0) // timeout
325                  {                  {
# Line 316  int bbsnet_connect(int n) Line 338  int bbsnet_connect(int n)
338                  }                  }
339                  else if (ret > 0)                  else if (ret > 0)
340                  {                  {
341                          if (FD_ISSET(STDIN_FILENO, &read_fds))                          if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds))
342                            {
343                                    ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf));
344                                    if (ret < 0)
345                                    {
346                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
347                                            {
348                                                    log_error("read(STDIN) error (%d)\n", errno);
349                                                    loop = 0;
350                                            }
351                                    }
352                                    else if (ret == 0) // broken pipe
353                                    {
354                                            loop = 0;
355                                    }
356                                    else
357                                    {
358                                            input_buf_len = ret;
359                                            input_buf_offset = 0;
360    
361                                            BBS_last_access_tm = time(0);
362                                    }
363                            }
364    
365                            if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds))
366                          {                          {
367                                  len = read(STDIN_FILENO, buf, sizeof(buf));                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));
368                                  if (len == 0)                                  if (ret < 0)
369                                    {
370                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
371                                            {
372                                                    log_error("write(socket) error (%d)\n", errno);
373                                                    loop = 0;
374                                            }
375                                    }
376                                    else if (ret == 0) // broken pipe
377                                  {                                  {
378                                          loop = 0;                                          loop = 0;
379                                  }                                  }
380                                  write(sock, buf, (size_t)len);                                  else
381                                    {
382                                            input_buf_offset += ret;
383                                    }
384                            }
385    
386                                  BBS_last_access_tm = time(0);                          if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds))
387                            {
388                                    ret = (int)read(sock, output_buf, sizeof(output_buf));
389                                    if (ret < 0)
390                                    {
391                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
392                                            {
393                                                    log_error("read(socket) error (%d)\n", errno);
394                                                    loop = 0;
395                                            }
396                                    }
397                                    else if (ret == 0) // broken pipe
398                                    {
399                                            loop = 0;
400                                    }
401                                    else
402                                    {
403                                            output_buf_len = ret;
404                                            output_buf_offset = 0;
405                                    }
406                          }                          }
407                          if (FD_ISSET(sock, &read_fds))  
408                            if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds))
409                          {                          {
410                                  len = read(sock, buf, sizeof(buf));                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
411                                  if (len == 0)                                  if (ret < 0)
412                                    {
413                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
414                                            {
415                                                    log_error("write(STDOUT) error (%d)\n", errno);
416                                                    loop = 0;
417                                            }
418                                    }
419                                    else if (ret == 0) // broken pipe
420                                  {                                  {
421                                          loop = 0;                                          loop = 0;
422                                  }                                  }
423                                  write(STDOUT_FILENO, buf, (size_t)len);                                  else
424                                    {
425                                            output_buf_offset += ret;
426                                    }
427                          }                          }
428                  }                  }
429          }          }
430    
431            // Restore STDIN/STDOUT flags
432            fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
433            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
434    
435            // Restore socket flags
436            fcntl(sock, F_SETFL, flags_sock);
437    
438          if (close(sock) == -1)          if (close(sock) == -1)
439          {          {
440                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
# Line 421  int bbs_net() Line 517  int bbs_net()
517                  ch = igetch(0);                  ch = igetch(0);
518                  switch (ch)                  switch (ch)
519                  {                  {
520                  case KEY_NULL:                  case KEY_NULL:  // broken pipe
521                          return -1;                  case Ctrl('C'): // user cancel
                 case Ctrl('C'):  
522                          return 0;                          return 0;
523                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
524                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)


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

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