/[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.59 by sysadm, Sat Oct 4 10:11:44 2025 UTC Revision 1.63 by sysadm, Fri Oct 10 14:02:41 2025 UTC
# Line 31  Line 31 
31  #include <time.h>  #include <time.h>
32  #include <unistd.h>  #include <unistd.h>
33  #include <arpa/inet.h>  #include <arpa/inet.h>
34    #include <iconv.h>
35  #include <libssh/libssh.h>  #include <libssh/libssh.h>
36  #include <libssh/server.h>  #include <libssh/server.h>
37  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
# Line 47  Line 48 
48  #define MAXSTATION 26 * 2  #define MAXSTATION 26 * 2
49  #define STATION_PER_LINE 4  #define STATION_PER_LINE 4
50    
51    #define BBS_NET_DEFAULT_CHARSET "UTF-8"
52    
53  struct _bbsnet_conf  struct _bbsnet_conf
54  {  {
55          char host1[20];          char host1[20];
56          char host2[40];          char host2[40];
57          char ip[40];          char ip[40];
58          in_port_t port;          in_port_t port;
59            char charset[20];
60  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
61    
62  MENU_SET bbsnet_menu;  MENU_SET bbsnet_menu;
# Line 63  int load_bbsnet_conf(const char *file_co Line 67  int load_bbsnet_conf(const char *file_co
67          MENU *p_menu;          MENU *p_menu;
68          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
69          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
70          char t[256], *t1, *t2, *t3, *t4, *saveptr;          char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr;
71    
72          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
73          if (fp == NULL)          if (fp == NULL)
# Line 101  int load_bbsnet_conf(const char *file_co Line 105  int load_bbsnet_conf(const char *file_co
105                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
106                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
107                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
108                    t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
109    
110                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*')
111                  {                  {
112                          continue;                          continue;
113                  }                  }
# Line 114  int load_bbsnet_conf(const char *file_co Line 119  int load_bbsnet_conf(const char *file_co
119                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);
120                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';
121                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);
122                    strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
123                    bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
124    
125                  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);
126                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
# Line 188  void process_bar(int n, int len) Line 195  void process_bar(int n, int len)
195          iflush();          iflush();
196  }  }
197    
198    int bbsnet_io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len)
199    {
200            char *in_buf;
201            char *out_buf;
202            size_t in_bytes;
203            size_t out_bytes;
204            int ret;
205    
206            in_buf = p_buf + *p_buf_offset;
207            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
208            out_buf = p_conv + *p_conv_len;
209            out_bytes = conv_size - (size_t)(*p_conv_len);
210    
211            while (in_bytes > 0)
212            {
213                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
214                    if (ret == -1)
215                    {
216                            if (errno == EINVAL) // Incomplete
217                            {
218    #ifdef _DEBUG
219                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes);
220    #endif
221                                    *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
222                                    *p_buf_offset = 0;
223                                    *p_conv_len = (int)(conv_size - out_bytes);
224                                    memmove(p_buf, in_buf, (size_t)(*p_buf_len));
225    
226                                    break;
227                            }
228                            else if (errno == E2BIG)
229                            {
230                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
231                                    return -1;
232                            }
233                            else if (errno == EILSEQ)
234                            {
235                                    if (in_bytes > out_bytes || out_bytes <= 0)
236                                    {
237                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
238                                            return -2;
239                                    }
240    
241                                    *out_buf = *in_buf;
242                                    in_buf++;
243                                    out_buf++;
244                                    in_bytes--;
245                                    out_bytes--;
246    
247                                    continue;
248                            }
249                    }
250                    else
251                    {
252                            *p_buf_len = 0;
253                            *p_buf_offset = 0;
254                            *p_conv_len = (int)(conv_size - out_bytes);
255    
256                            break;
257                    }
258            }
259    
260            return 0;
261    }
262    
263  int bbsnet_connect(int n)  int bbsnet_connect(int n)
264  {  {
265          int sock, ret, loop, error;          int sock, ret, loop, error;
# Line 203  int bbsnet_connect(int n) Line 275  int bbsnet_connect(int n)
275          int output_buf_len = 0;          int output_buf_len = 0;
276          int input_buf_offset = 0;          int input_buf_offset = 0;
277          int output_buf_offset = 0;          int output_buf_offset = 0;
278            iconv_t input_cd = NULL;
279            char input_conv[LINE_BUFFER_LEN * 2];
280            char output_conv[LINE_BUFFER_LEN * 2];
281            int input_conv_len = 0;
282            int output_conv_len = 0;
283            int input_conv_offset = 0;
284            int output_conv_offset = 0;
285            iconv_t output_cd = NULL;
286          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
287          int nfds, epollfd;          int nfds, epollfd;
288          int stdin_read_wait = 0;          int stdin_read_wait = 0;
# Line 213  int bbsnet_connect(int n) Line 293  int bbsnet_connect(int n)
293          int tos;          int tos;
294          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
295          int remote_port;          int remote_port;
296            char local_addr[IP_ADDR_LEN];
297            int local_port;
298            socklen_t sock_len;
299          time_t t_used = time(NULL);          time_t t_used = time(NULL);
300          struct tm *tm_used;          struct tm *tm_used;
301          int ch;          int ch;
# Line 392  int bbsnet_connect(int n) Line 475  int bbsnet_connect(int n)
475                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
476          }          }
477    
478            sock_len = sizeof(sin);
479            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
480            {
481                    log_error("getsockname() error: %d", errno);
482                    goto cleanup;
483            }
484    
485            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
486            local_addr[sizeof(local_addr) - 1] = '\0';
487            local_port = ntohs(sin.sin_port);
488    
489          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
490          iflush();          iflush();
491          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",
492                               remote_addr, remote_port, local_addr, local_port, BBS_username);
493    
494            input_cd = iconv_open(bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET);
495            if (input_cd == (iconv_t)(-1))
496            {
497                    log_error("iconv_open(%s->%s) error: %d\n", BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset, errno);
498                    goto cleanup;
499            }
500            output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset);
501            if (input_cd == (iconv_t)(-1))
502            {
503                    log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET, errno);
504                    iconv_close(input_cd);
505                    goto cleanup;
506            }
507    
508          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
509          ev.data.fd = sock;          ev.data.fd = sock;
# Line 542  int bbsnet_connect(int n) Line 651  int bbsnet_connect(int n)
651    
652                  if (sock_write_wait)                  if (sock_write_wait)
653                  {                  {
654                          while (input_buf_offset < input_buf_len && !SYS_server_exit)                          if (input_buf_offset < input_buf_len)
655                          {                          {
656                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                  ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
657                                    if (ret < 0)
658                                    {
659                                            log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
660                                    }
661                            }
662    
663                            while (input_conv_offset < input_conv_len && !SYS_server_exit)
664                            {
665                                    ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
666                                  if (ret < 0)                                  if (ret < 0)
667                                  {                                  {
668                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 574  int bbsnet_connect(int n) Line 692  int bbsnet_connect(int n)
692                                  }                                  }
693                                  else                                  else
694                                  {                                  {
695                                          input_buf_offset += ret;                                          input_conv_offset += ret;
696                                          if (input_buf_offset >= input_buf_len) // Output buffer complete                                          if (input_conv_offset >= input_conv_len) // Output buffer complete
697                                          {                                          {
698                                                  input_buf_offset = 0;                                                  input_conv_offset = 0;
699                                                  input_buf_len = 0;                                                  input_conv_len = 0;
700                                                  break;                                                  break;
701                                          }                                          }
702                                          continue;                                          continue;
# Line 628  int bbsnet_connect(int n) Line 746  int bbsnet_connect(int n)
746    
747                  if (stdout_write_wait)                  if (stdout_write_wait)
748                  {                  {
749                          while (output_buf_offset < output_buf_len && !SYS_server_exit)                          if (output_buf_offset < output_buf_len)
750                            {
751                                    ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
752                                    if (ret < 0)
753                                    {
754                                            log_error("bbsnet_io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
755                                    }
756                            }
757    
758                            while (output_conv_offset < output_conv_len && !SYS_server_exit)
759                          {                          {
760                                  if (SSH_v2)                                  if (SSH_v2)
761                                  {                                  {
762                                          ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
763                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
764                                          {                                          {
765                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
# Line 642  int bbsnet_connect(int n) Line 769  int bbsnet_connect(int n)
769                                  }                                  }
770                                  else                                  else
771                                  {                                  {
772                                          ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                          ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
773                                  }                                  }
774                                  if (ret < 0)                                  if (ret < 0)
775                                  {                                  {
# Line 673  int bbsnet_connect(int n) Line 800  int bbsnet_connect(int n)
800                                  }                                  }
801                                  else                                  else
802                                  {                                  {
803                                          output_buf_offset += ret;                                          output_conv_offset += ret;
804                                          if (output_buf_offset >= output_buf_len) // Output buffer complete                                          if (output_conv_offset >= output_conv_len) // Output buffer complete
805                                          {                                          {
806                                                  output_buf_offset = 0;                                                  output_conv_offset = 0;
807                                                  output_buf_len = 0;                                                  output_conv_len = 0;
808                                                  break;                                                  break;
809                                          }                                          }
810                                          continue;                                          continue;
# Line 686  int bbsnet_connect(int n) Line 813  int bbsnet_connect(int n)
813                  }                  }
814          }          }
815    
816            iconv_close(input_cd);
817            iconv_close(output_cd);
818    
819  cleanup:  cleanup:
820          if (close(epollfd) < 0)          if (close(epollfd) < 0)
821          {          {


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

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