ftp.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef HV_FTP_H_
  2. #define HV_FTP_H_
  3. #include "hexport.h"
  4. #define FTP_COMMAND_PORT 21
  5. #define FTP_DATA_PORT 20
  6. // ftp_command
  7. // X(name)
  8. #define FTP_COMMAND_MAP(X) \
  9. X(HELP) \
  10. X(USER) \
  11. X(PASS) \
  12. X(PWD) \
  13. X(CWD) \
  14. X(CDUP) \
  15. X(MKD) \
  16. X(RMD) \
  17. X(STAT) \
  18. X(SIZE) \
  19. X(DELE) \
  20. X(RNFR) \
  21. X(RNTO) \
  22. X(PORT) \
  23. X(PASV) \
  24. X(LIST) \
  25. X(NLST) \
  26. X(APPE) \
  27. X(RETR) \
  28. X(STOR) \
  29. X(QUIT) \
  30. enum ftp_command {
  31. #define X(name) FTP_##name,
  32. FTP_COMMAND_MAP(X)
  33. #undef X
  34. };
  35. // ftp_status
  36. // XXX(code, name, string)
  37. #define FTP_STATUS_MAP(XXX) \
  38. XXX(220, READY, Ready) \
  39. XXX(221, BYE, Bye) \
  40. XXX(226, TRANSFER_COMPLETE, Transfer complete) \
  41. XXX(227, PASV, Entering Passive Mode) \
  42. XXX(331, PASS, Password required) \
  43. XXX(230, LOGIN_OK, Login OK) \
  44. XXX(250, OK, OK) \
  45. XXX(500, BAD_SYNTAX, Bad syntax) \
  46. XXX(530, NOT_LOGIN, Not login) \
  47. enum ftp_status {
  48. #define XXX(code, name, string) FTP_STATUS_##name = code,
  49. FTP_STATUS_MAP(XXX)
  50. #undef XXX
  51. };
  52. // more friendly macros
  53. #define FTP_MKDIR FTP_MKD
  54. #define FTP_RMDIR FTP_RMD
  55. #define FTP_APPEND FTP_APPE
  56. #define FTP_REMOVE FTP_DELE
  57. #define FTP_DOWNLOAD FTP_RETR
  58. #define FTP_UPLOAD FTP_STOR
  59. #define FTP_RECV_BUFSIZE 8192
  60. typedef struct ftp_handle_s {
  61. int sockfd;
  62. char recvbuf[FTP_RECV_BUFSIZE];
  63. void* userdata;
  64. } ftp_handle_t;
  65. BEGIN_EXTERN_C
  66. HV_EXPORT const char* ftp_command_str(enum ftp_command cmd);
  67. HV_EXPORT const char* ftp_status_str(enum ftp_status status);
  68. HV_EXPORT int ftp_connect(ftp_handle_t* hftp, const char* host, int port);
  69. HV_EXPORT int ftp_login(ftp_handle_t* hftp, const char* username, const char* password);
  70. HV_EXPORT int ftp_quit(ftp_handle_t* hftp);
  71. HV_EXPORT int ftp_exec(ftp_handle_t* hftp, const char* cmd, const char* param);
  72. // local => remote
  73. HV_EXPORT int ftp_upload(ftp_handle_t* hftp, const char* local_filepath, const char* remote_filepath);
  74. // remote => local
  75. HV_EXPORT int ftp_download(ftp_handle_t* hftp, const char* remote_filepath, const char* local_filepath);
  76. typedef int (*ftp_download_cb)(ftp_handle_t* hftp, char* buf, int len);
  77. HV_EXPORT int ftp_download_with_cb(ftp_handle_t* hftp, const char* filepath, ftp_download_cb cb);
  78. END_EXTERN_C
  79. #endif // HV_FTP_H_