readwrite.c
上传用户:ig0539
上传日期:2022-05-21
资源大小:181k
文件大小:5k
源码类别:

Ftp客户端

开发平台:

C/C++

  1. /*
  2.  * Part of Very Secure FTPd
  3.  * Licence: GPL v2
  4.  * Author: Chris Evans
  5.  * readwrite.c
  6.  *
  7.  * Routines to encapsulate the underlying read / write mechanism (OpenSSL vs.
  8.  * plain read()/write()).
  9.  */
  10. #include "readwrite.h"
  11. #include "session.h"
  12. #include "netstr.h"
  13. #include "ssl.h"
  14. #include "privsock.h"
  15. #include "defs.h"
  16. #include "sysutil.h"
  17. static int plain_peek_adapter(struct vsf_session* p_sess,
  18.                               char* p_buf,
  19.                               unsigned int len);
  20. static int plain_read_adapter(struct vsf_session* p_sess,
  21.                               char* p_buf,
  22.                               unsigned int len);
  23. static int ssl_peek_adapter(struct vsf_session* p_sess,
  24.                             char* p_buf,
  25.                             unsigned int len);
  26. static int ssl_read_adapter(struct vsf_session* p_sess,
  27.                             char* p_buf,
  28.                             unsigned int len);
  29. int
  30. ftp_write_str(const struct vsf_session* p_sess, const struct mystr* p_str,
  31.               enum EVSFRWTarget target)
  32. {
  33.   if (target == kVSFRWData)
  34.   {
  35.     if (p_sess->data_use_ssl && p_sess->ssl_slave_active)
  36.     {
  37.       int ret = -1;
  38.       int written;
  39.       priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_DO_SSL_WRITE);
  40.       priv_sock_send_str(p_sess->ssl_consumer_fd, p_str);
  41.       written = priv_sock_get_int(p_sess->ssl_consumer_fd);
  42.       if (written > 0 && written == (int) str_getlen(p_str))
  43.       {
  44.         ret = 0;
  45.       }
  46.       return ret;
  47.     }
  48.     else if (p_sess->data_use_ssl)
  49.     {
  50.       return ssl_write_str(p_sess->p_data_ssl, p_str);
  51.     }
  52.     else
  53.     {
  54.       return str_netfd_write(p_str, p_sess->data_fd);
  55.     }
  56.   }
  57.   else
  58.   {
  59.     if (p_sess->control_use_ssl && p_sess->ssl_slave_active)
  60.     {
  61.       priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_WRITE_USER_RESP);
  62.       priv_sock_send_str(p_sess->ssl_consumer_fd, p_str);
  63.       return priv_sock_get_int(p_sess->ssl_consumer_fd);
  64.     }
  65.     else if (p_sess->control_use_ssl)
  66.     {
  67.       return ssl_write_str(p_sess->p_control_ssl, p_str);
  68.     }
  69.     else
  70.     {
  71.       return str_netfd_write(p_str, VSFTP_COMMAND_FD);
  72.     }
  73.   }
  74. }
  75. int
  76. ftp_read_data(struct vsf_session* p_sess, char* p_buf, unsigned int len)
  77. {
  78.   if (p_sess->data_use_ssl && p_sess->ssl_slave_active)
  79.   {
  80.     int ret;
  81.     priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_DO_SSL_READ);
  82.     ret = priv_sock_get_int(p_sess->ssl_consumer_fd);
  83.     priv_sock_recv_buf(p_sess->ssl_consumer_fd, p_buf, len);
  84.     /* Need to do this here too because it is useless in the slave process. */
  85.     vsf_sysutil_check_pending_actions(kVSFSysUtilIO, ret, p_sess->data_fd);
  86.     return ret;
  87.   }
  88.   else if (p_sess->data_use_ssl)
  89.   {
  90.     return ssl_read(p_sess, p_sess->p_data_ssl, p_buf, len);
  91.   }
  92.   else
  93.   {
  94.     return vsf_sysutil_read(p_sess->data_fd, p_buf, len);
  95.   }
  96. }
  97. int
  98. ftp_write_data(const struct vsf_session* p_sess, const char* p_buf,
  99.                unsigned int len)
  100. {
  101.   if (p_sess->data_use_ssl && p_sess->ssl_slave_active)
  102.   {
  103.     int ret;
  104.     priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_DO_SSL_WRITE);
  105.     priv_sock_send_buf(p_sess->ssl_consumer_fd, p_buf, len);
  106.     ret = priv_sock_get_int(p_sess->ssl_consumer_fd);
  107.     /* Need to do this here too because it is useless in the slave process. */
  108.     vsf_sysutil_check_pending_actions(kVSFSysUtilIO, ret, p_sess->data_fd);
  109.     return ret;
  110.   }
  111.   else if (p_sess->data_use_ssl)
  112.   {
  113.     return ssl_write(p_sess->p_data_ssl, p_buf, len);
  114.   }
  115.   else
  116.   {
  117.     return vsf_sysutil_write_loop(p_sess->data_fd, p_buf, len);
  118.   }
  119. }
  120. int
  121. ftp_getline(struct vsf_session* p_sess, struct mystr* p_str, char* p_buf)
  122. {
  123.   if (p_sess->control_use_ssl && p_sess->ssl_slave_active)
  124.   {
  125.     int ret;
  126.     priv_sock_send_cmd(p_sess->ssl_consumer_fd, PRIV_SOCK_GET_USER_CMD);
  127.     ret = priv_sock_get_int(p_sess->ssl_consumer_fd);
  128.     if (ret >= 0)
  129.     {
  130.       priv_sock_get_str(p_sess->ssl_consumer_fd, p_str);
  131.     }
  132.     return ret;
  133.   }
  134.   else
  135.   {
  136.     str_netfd_read_t p_peek = plain_peek_adapter;
  137.     str_netfd_read_t p_read = plain_read_adapter;
  138.     if (p_sess->control_use_ssl)
  139.     {
  140.       p_peek = ssl_peek_adapter;
  141.       p_read = ssl_read_adapter;
  142.     }
  143.     return str_netfd_alloc(p_sess,
  144.                            p_str,
  145.                            'n',
  146.                            p_buf,
  147.                            VSFTP_MAX_COMMAND_LINE,
  148.                            p_peek,
  149.                            p_read);
  150.   }
  151. }
  152. static int
  153. plain_peek_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
  154. {
  155.   (void) p_sess;
  156.   return vsf_sysutil_recv_peek(VSFTP_COMMAND_FD, p_buf, len);
  157. }
  158. static int
  159. plain_read_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
  160. {
  161.   (void) p_sess;
  162.   return vsf_sysutil_read_loop(VSFTP_COMMAND_FD, p_buf, len);
  163. }
  164. static int
  165. ssl_peek_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
  166. {
  167.   return ssl_peek(p_sess, p_sess->p_control_ssl, p_buf, len);
  168. }
  169. static int
  170. ssl_read_adapter(struct vsf_session* p_sess, char* p_buf, unsigned int len)
  171. {
  172.   return ssl_read(p_sess, p_sess->p_control_ssl, p_buf, len);
  173. }