luat_lib_ftp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "luat_base.h"
  2. #include "luat_network_adapter.h"
  3. #include "luat_msgbus.h"
  4. #include "luat_mem.h"
  5. #include "luat_ftp.h"
  6. #define LUAT_LOG_TAG "ftp"
  7. #include "luat_log.h"
  8. static uint64_t ftp_idp = 0;
  9. static int32_t l_ftp_callback(lua_State *L, void* ptr){
  10. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  11. luat_ftp_ctrl_t *luat_ftp_ctrl = (luat_ftp_ctrl_t *)msg->ptr;
  12. // LLOGD("l_ftp_callback arg1:%d arg2:%d idp:%lld",msg->arg1,msg->arg2,ftp_idp);
  13. if (ftp_idp){
  14. if (msg->arg1 == FTP_ERROR){
  15. lua_pushboolean(L, 0);
  16. }else if (msg->arg1 == FTP_SUCCESS_DATE){
  17. lua_pushlstring(L,(const char *)(luat_ftp_ctrl->result_buffer.Data),luat_ftp_ctrl->result_buffer.Pos);
  18. }else{
  19. lua_pushboolean(L, 1);
  20. }
  21. luat_cbcwait(L, ftp_idp, 1);
  22. ftp_idp = 0;
  23. }
  24. OS_DeInitBuffer(&luat_ftp_ctrl->result_buffer);
  25. return 0;
  26. }
  27. static void luat_ftp_cb(luat_ftp_ctrl_t *luat_ftp_ctrl, FTP_SUCCESS_STATE_e event){
  28. rtos_msg_t msg = {0};
  29. msg.handler = l_ftp_callback;
  30. msg.ptr = luat_ftp_ctrl;
  31. msg.arg1 = event;
  32. luat_msgbus_put(&msg, 0);
  33. }
  34. /*
  35. FTP客户端
  36. @api ftp.login(adapter,ip_addr,port,username,password)
  37. @int 适配器序号, 只能是socket.ETH0, socket.STA, socket.AP,如果不填,会选择平台自带的方式,然后是最后一个注册的适配器
  38. @string ip_addr 地址
  39. @string port 端口,默认21
  40. @string username 用户名
  41. @string password 密码
  42. @bool/table 是否为ssl加密连接,默认不加密,true为无证书最简单的加密,table为有证书的加密 <br>server_cert 服务器ca证书数据 <br>client_cert 客户端ca证书数据 <br>client_key 客户端私钥加密数据 <br>client_password 客户端私钥口令数据
  43. @return bool/string 成功返回true 失败返回string
  44. @usage
  45. ftp_login = ftp.login(nil,"xxx")
  46. */
  47. static int l_ftp_login(lua_State *L) {
  48. int result = 0;
  49. size_t len = 0;
  50. luat_ftp_tls_t* luat_ftp_tls = NULL;
  51. uint8_t adapter = luaL_optinteger(L, 1, network_get_last_register_adapter());
  52. const char *ip_addr = luaL_checklstring(L, 2, &len);
  53. uint16_t port = luaL_optinteger(L, 3, 21);
  54. const char *username = luaL_optlstring(L, 4, "",&len);
  55. const char *password = luaL_optlstring(L, 5, "",&len);
  56. // 加密相关
  57. if (lua_isboolean(L, 6)){
  58. if (lua_toboolean(L, 6)){
  59. luat_ftp_tls = (luat_ftp_tls_t *)luat_heap_malloc(sizeof(luat_ftp_tls_t));
  60. memset(luat_ftp_tls, 0, sizeof(luat_ftp_tls_t));
  61. }
  62. }else if (lua_istable(L, 6)){
  63. luat_ftp_tls = (luat_ftp_tls_t *)luat_heap_malloc(sizeof(luat_ftp_tls_t));
  64. memset(luat_ftp_tls, 0, sizeof(luat_ftp_tls_t));
  65. lua_pushstring(L, "server_cert");
  66. if (LUA_TSTRING == lua_gettable(L, 6)) {
  67. luat_ftp_tls->server_cert = luaL_checklstring(L, -1, &len);
  68. }
  69. lua_pop(L, 1);
  70. lua_pushstring(L, "client_cert");
  71. if (LUA_TSTRING == lua_gettable(L, 6)) {
  72. luat_ftp_tls->client_cert = luaL_checklstring(L, -1, &len);
  73. }
  74. lua_pop(L, 1);
  75. lua_pushstring(L, "client_key");
  76. if (LUA_TSTRING == lua_gettable(L, 6)) {
  77. luat_ftp_tls->client_key = luaL_checklstring(L, -1, &len);
  78. }
  79. lua_pop(L, 1);
  80. lua_pushstring(L, "client_password");
  81. if (LUA_TSTRING == lua_gettable(L, 6)) {
  82. luat_ftp_tls->client_password = luaL_checklstring(L, -1, &len);
  83. }
  84. lua_pop(L, 1);
  85. }
  86. if (luat_ftp_tls!=NULL){
  87. if (lua_isstring(L, 6)){
  88. luat_ftp_tls->server_cert = luaL_checklstring(L, 6, &len);
  89. }
  90. if (lua_isstring(L, 7)){
  91. luat_ftp_tls->client_cert = luaL_checklstring(L, 7, &len);
  92. }
  93. if (lua_isstring(L, 8)){
  94. luat_ftp_tls->client_key = luaL_checklstring(L, 8, &len);
  95. }
  96. if (lua_isstring(L, 9)){
  97. luat_ftp_tls->client_password = luaL_checklstring(L, 9, &len);
  98. }
  99. }
  100. if (0!=(result = luat_ftp_login(adapter,ip_addr,port,username,password,luat_ftp_tls,luat_ftp_cb))){
  101. LLOGE("ftp login fail");
  102. luat_ftp_release();
  103. lua_pushinteger(L,result);
  104. luat_pushcwait_error(L,1);
  105. }else{
  106. ftp_idp = luat_pushcwait(L);
  107. }
  108. if (luat_ftp_tls){
  109. luat_heap_free(luat_ftp_tls);
  110. }
  111. return 1;
  112. }
  113. /*
  114. FTP命令
  115. @api ftp.command(cmd)
  116. @string cmd 命令 目前支持:NOOP SYST TYPE PWD MKD CWD CDUP RMD DELE LIST
  117. @return string 成功返回true 失败返回string
  118. @usage
  119. // 空操作,防止连接断掉
  120. print(ftp.command("NOOP").wait())
  121. // 报告远程系统的操作系统类型
  122. print(ftp.command("SYST").wait())
  123. // 指定文件类型
  124. print(ftp.command("TYPE I").wait())
  125. // 显示当前工作目录名
  126. print(ftp.command("PWD").wait())
  127. // 创建目录
  128. print(ftp.command("MKD QWER").wait())
  129. // 改变当前工作目录
  130. print(ftp.command("CWD /QWER").wait())
  131. // 返回上一层目录
  132. print(ftp.command("CDUP").wait())
  133. // 删除目录
  134. print(ftp.command("RMD QWER").wait())
  135. // 获取当前工作目录下的文件名列表
  136. print(ftp.command("LIST").wait())
  137. // 删除文件
  138. print(ftp.command("DELE /1/12222.txt").wait())
  139. */
  140. static int l_ftp_command(lua_State *L) {
  141. size_t len;
  142. const char * command = luaL_optlstring(L, 1, "",&len);
  143. if (luat_ftp_command(command)){
  144. LLOGE("ftp command fail");
  145. lua_pushinteger(L,FTP_ERROR_FILE);
  146. luat_pushcwait_error(L,1);
  147. }else{
  148. ftp_idp = luat_pushcwait(L);
  149. }
  150. return 1;
  151. }
  152. /*
  153. FTP文件下载
  154. @api ftp.pull(local_name,remote_name)
  155. @string local_name 本地文件
  156. @string remote_name 服务器文件
  157. @return bool/string 成功返回true 失败返回string
  158. @usage
  159. ftp.pull("/1222.txt","/1222.txt").wait()
  160. */
  161. static int l_ftp_pull(lua_State *L) {
  162. size_t len;
  163. const char * local_name = luaL_optlstring(L, 1, "",&len);
  164. const char * remote_name = luaL_optlstring(L, 2, "",&len);
  165. if (luat_ftp_pull(local_name,remote_name)){
  166. LLOGE("ftp pull fail");
  167. lua_pushinteger(L,FTP_ERROR_FILE);
  168. luat_pushcwait_error(L,1);
  169. }else{
  170. ftp_idp = luat_pushcwait(L);
  171. }
  172. return 1;
  173. }
  174. /*
  175. FTP文件上传
  176. @api ftp.push(local_name,remote_name)
  177. @string local_name 本地文件
  178. @string remote_name 服务器文件
  179. @return bool/string 成功返回true 失败返回string
  180. @usage
  181. ftp.push("/1222.txt","/1222.txt").wait()
  182. */
  183. static int l_ftp_push(lua_State *L) {
  184. size_t len;
  185. const char * local_name = luaL_optlstring(L, 1, "",&len);
  186. const char * remote_name = luaL_optlstring(L, 2, "",&len);
  187. if (luat_ftp_push(local_name,remote_name)){
  188. LLOGE("ftp push fail");
  189. lua_pushinteger(L,FTP_ERROR_CONNECT);
  190. luat_pushcwait_error(L,1);
  191. }else{
  192. ftp_idp = luat_pushcwait(L);
  193. }
  194. return 1;
  195. }
  196. /*
  197. FTP客户端关闭
  198. @api ftp.close()
  199. @return bool/string 成功返回true 失败返回string
  200. @usage
  201. ftp.close().wait()
  202. */
  203. static int l_ftp_close(lua_State *L) {
  204. if (luat_ftp_close()){
  205. lua_pushinteger(L,FTP_ERROR_CONNECT);
  206. luat_pushcwait_error(L,1);
  207. }else{
  208. ftp_idp = luat_pushcwait(L);
  209. }
  210. return 1;
  211. }
  212. /*
  213. 配置是否打开debug信息
  214. @api ftp.debug(onoff)
  215. @boolean 是否打开debug开关
  216. @return nil 无返回值
  217. @usage ftp.debug(true)
  218. */
  219. static int l_ftp_set_debug(lua_State *L){
  220. if (lua_isboolean(L, 1)){
  221. luat_ftp_debug(lua_toboolean(L, 1));
  222. }
  223. return 0;
  224. }
  225. #include "rotable2.h"
  226. #ifdef LUAT_USE_NETWORK
  227. static const rotable_Reg_t reg_ftp[] =
  228. {
  229. {"login", ROREG_FUNC(l_ftp_login)},
  230. {"command", ROREG_FUNC(l_ftp_command)},
  231. {"pull", ROREG_FUNC(l_ftp_pull)},
  232. {"push", ROREG_FUNC(l_ftp_push)},
  233. {"close", ROREG_FUNC(l_ftp_close)},
  234. {"debug", ROREG_FUNC(l_ftp_set_debug)},
  235. { NULL, ROREG_INT(0)}
  236. };
  237. #else
  238. static const rotable_Reg_t reg_ftp_emtry[] =
  239. {
  240. { NULL, ROREG_INT(0)}
  241. };
  242. #endif
  243. LUAMOD_API int luaopen_ftp( lua_State *L ) {
  244. #ifdef LUAT_USE_NETWORK
  245. luat_newlib2(L, reg_ftp);
  246. #else
  247. luat_newlib2(L, reg_ftp_emtry);
  248. LLOGE("ftp require network enable!!");
  249. #endif
  250. return 1;
  251. }