ftp.h 2.4 KB

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