luat_lib_socket.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /*
  2. @module socket
  3. @summary 网络接口
  4. @version 1.0
  5. @date 2022.11.13
  6. @demo socket
  7. @tag LUAT_USE_NETWORK
  8. */
  9. #include "luat_base.h"
  10. #include "luat_malloc.h"
  11. #ifdef LUAT_USE_NETWORK
  12. #include "luat_network_adapter.h"
  13. #include "luat_rtos.h"
  14. #include "luat_zbuff.h"
  15. #define LUAT_LOG_TAG "network"
  16. #include "luat_log.h"
  17. //static const char NW_TYPE[] = "NWA*";
  18. #define LUAT_NW_CTRL_TYPE "NWCTRL*"
  19. typedef struct
  20. {
  21. network_ctrl_t *netc;
  22. int cb_ref; //回调函数
  23. char *task_name;
  24. uint8_t adapter_index;
  25. }luat_socket_ctrl_t;
  26. #ifdef LUAT_USE_LWIP
  27. static int32_t l_socket_callback(lua_State *L, void* ptr)
  28. {
  29. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  30. luat_socket_ctrl_t *l_ctrl =(luat_socket_ctrl_t *)msg->ptr;
  31. if (l_ctrl->netc)
  32. {
  33. if (l_ctrl->cb_ref)
  34. {
  35. lua_geti(L, LUA_REGISTRYINDEX, l_ctrl->cb_ref);
  36. if (lua_isfunction(L, -1)) {
  37. lua_pushlightuserdata(L, l_ctrl);
  38. lua_pushinteger(L, msg->arg1);
  39. lua_pushinteger(L, msg->arg2);
  40. lua_call(L, 3, 0);
  41. }
  42. }
  43. else if (l_ctrl->task_name)
  44. {
  45. lua_getglobal(L, "sys_send");
  46. if (lua_isfunction(L, -1)) {
  47. lua_pushstring(L, l_ctrl->task_name);
  48. lua_pushinteger(L, msg->arg1);
  49. lua_pushinteger(L, msg->arg2);
  50. lua_call(L, 3, 0);
  51. }
  52. }
  53. else
  54. {
  55. lua_getglobal(L, "sys_pub");
  56. if (lua_isfunction(L, -1)) {
  57. lua_pushstring(L, LUAT_NW_CTRL_TYPE);
  58. lua_pushinteger(L, l_ctrl->netc->adapter_index);
  59. lua_pushinteger(L, l_ctrl->netc->socket_id);
  60. lua_pushinteger(L, msg->arg1);
  61. lua_pushinteger(L, msg->arg2);
  62. lua_call(L, 5, 0);
  63. }
  64. }
  65. }
  66. lua_pushinteger(L, 0);
  67. return 1;
  68. }
  69. static int32_t luat_lib_socket_callback(void *data, void *param)
  70. {
  71. OS_EVENT *event = (OS_EVENT *)data;
  72. rtos_msg_t msg;
  73. msg.handler = l_socket_callback;
  74. msg.ptr = param;
  75. msg.arg1 = event->ID & 0x0fffffff;
  76. msg.arg2 = event->Param1;
  77. luat_msgbus_put(&msg, 0);
  78. return 0;
  79. }
  80. static luat_socket_ctrl_t * l_get_ctrl(lua_State *L, int index)
  81. {
  82. if (luaL_testudata(L, 1, LUAT_NW_CTRL_TYPE))
  83. {
  84. return ((luat_socket_ctrl_t *)luaL_checkudata(L, 1, LUAT_NW_CTRL_TYPE));
  85. }
  86. else
  87. {
  88. return ((luat_socket_ctrl_t *)lua_touserdata(L, 1));
  89. }
  90. }
  91. // __gc
  92. static int l_socket_gc(lua_State *L)
  93. {
  94. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  95. if (l_ctrl->netc)
  96. {
  97. network_force_close_socket(l_ctrl->netc);
  98. network_release_ctrl(l_ctrl->netc);
  99. l_ctrl->netc = NULL;
  100. }
  101. if (l_ctrl->cb_ref)
  102. {
  103. luaL_unref(L, LUA_REGISTRYINDEX, l_ctrl->cb_ref);
  104. l_ctrl->cb_ref = 0;
  105. }
  106. if (l_ctrl->task_name)
  107. {
  108. luat_heap_free(l_ctrl->task_name);
  109. l_ctrl->task_name = 0;
  110. }
  111. return 0;
  112. }
  113. /*
  114. 在某个适配的网卡上申请一个socket_ctrl
  115. @api socket.create(adapter, cb)
  116. @int 适配器序号, 只能是socket.ETH0(外置以太网),socket.LWIP_ETH(内置以太网),socket.LWIP_STA(内置WIFI的STA),socket.LWIP_AP(内置WIFI的AP),socket.LWIP_GP(内置蜂窝网络的GPRS),socket.USB(外置USB网卡),如果不填,优先选择soc平台自带能上外网的适配器,若仍然没有,选择最后一个注册的适配器
  117. @string or function string为消息通知的taskName,function则为回调函数,如果固件没有内置sys_wait,则必须是function
  118. 当通过回调函数回调消息时,输入给function一共3个参数:
  119. param1为申请的network_ctrl
  120. param2为具体的消息,只能是socket.RESET, socket.LINK, socket.ON_LINE, socket.TX_OK, socket.RX_NEW, socket.CLOSE等等
  121. param3为消息对应的参数
  122. @return some 成功返回network_ctrl,失败返回nil
  123. @usage
  124. local netc = socket.create(socket.ETH0, socket_cb_fun) --以太网网卡上申请一个network_ctrl,通过socket_cb_fun回调相关消息
  125. local netc = socket.create(socket.ETH0, "IOT_TASK") --以太网网卡上申请一个network_ctrl,通过sendMsg方式通知taskName为"IOT_TASK"回调相关消息
  126. */
  127. static int l_socket_create(lua_State *L)
  128. {
  129. int adapter_index = luaL_optinteger(L, 1, network_get_last_register_adapter());
  130. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY)
  131. {
  132. lua_pushnil(L);
  133. return 1;
  134. }
  135. luat_socket_ctrl_t *l_ctrl = (luat_socket_ctrl_t *)lua_newuserdata(L, sizeof(luat_socket_ctrl_t));
  136. if (!l_ctrl)
  137. {
  138. lua_pushnil(L);
  139. return 1;
  140. }
  141. l_ctrl->adapter_index = adapter_index;
  142. l_ctrl->netc = network_alloc_ctrl(adapter_index);
  143. if (!l_ctrl->netc)
  144. {
  145. LLOGD("create fail");
  146. lua_pushnil(L);
  147. return 1;
  148. }
  149. network_init_ctrl(l_ctrl->netc, NULL, luat_lib_socket_callback, l_ctrl);
  150. if (lua_isfunction(L, 2))
  151. {
  152. lua_pushvalue(L, 2);
  153. l_ctrl->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  154. l_ctrl->task_name = NULL;
  155. }
  156. else if (lua_isstring(L, 2))
  157. {
  158. l_ctrl->cb_ref = 0;
  159. size_t len;
  160. const char *buf;
  161. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  162. l_ctrl->task_name = luat_heap_malloc(len + 1);
  163. memset(l_ctrl->task_name, 0, len + 1);
  164. memcpy(l_ctrl->task_name, buf, len);
  165. }
  166. luaL_setmetatable(L, LUAT_NW_CTRL_TYPE);
  167. return 1;
  168. }
  169. /*
  170. 配置是否打开debug信息
  171. @api socket.debug(ctrl, onoff)
  172. @user_data socket.create得到的ctrl
  173. @boolean true 打开debug开关
  174. @return nil 无返回值
  175. @usage socket.debug(ctrl, true)
  176. */
  177. static int l_socket_set_debug(lua_State *L)
  178. {
  179. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  180. if (lua_isboolean(L, 2))
  181. {
  182. l_ctrl->netc->is_debug = lua_toboolean(L, 2);
  183. }
  184. return 0;
  185. }
  186. /*
  187. 配置network一些信息,
  188. @api socket.config(ctrl, local_port, is_udp, is_tls, keep_idle, keep_interval, keep_cnt, server_cert, client_cert, client_key, client_password)
  189. @user_data socket.create得到的ctrl
  190. @int 本地端口号,小端格式,如果不写,则自动分配一个,如果用户填了端口号则需要小于60000, 默认不写
  191. @boolean 是否是UDP,默认false
  192. @boolean 是否是加密传输,默认false
  193. @int tcp keep live模式下的idle时间(秒),如果留空则表示不启用,如果是不支持标准posix接口的网卡(比如W5500),则为心跳间隔
  194. @int tcp keep live模式下的探测间隔时间(秒)
  195. @int tcp keep live模式下的探测次数
  196. @string TCP模式下的服务器ca证书数据,UDP模式下的PSK,不需要加密传输写nil,后续参数也全部nil
  197. @string TCP模式下的客户端ca证书数据,UDP模式下的PSK-ID,TCP模式下如果不需要验证客户端证书时,忽略,一般不需要验证客户端证书
  198. @string TCP模式下的客户端私钥加密数据
  199. @string TCP模式下的客户端私钥口令数据
  200. @return nil 无返回值
  201. @usage socket.config(ctrl) --最普通的TCP传输
  202. socket.config(ctrl, nil, nil ,true) --最普通的加密TCP传输,证书都不用验证的那种
  203. */
  204. static int l_socket_config(lua_State *L)
  205. {
  206. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  207. uint8_t is_udp = 0;
  208. uint8_t is_tls = 0;
  209. int param_pos = 1;
  210. uint32_t keep_idle, keep_interval, keep_cnt;
  211. const char *server_cert = NULL;
  212. const char *client_cert = NULL;
  213. const char *client_key = NULL;
  214. const char *client_password = NULL;
  215. size_t server_cert_len, client_cert_len, client_key_len, client_password_len;
  216. uint16_t local_port = luaL_optinteger(L, ++param_pos, 0);
  217. if (lua_isboolean(L, ++param_pos))
  218. {
  219. is_udp = lua_toboolean(L, param_pos);
  220. }
  221. if (lua_isboolean(L, ++param_pos))
  222. {
  223. is_tls = lua_toboolean(L, param_pos);
  224. }
  225. keep_idle = luaL_optinteger(L, ++param_pos, 0);
  226. keep_interval = luaL_optinteger(L, ++param_pos, 0);
  227. keep_cnt = luaL_optinteger(L, ++param_pos, 0);
  228. if (lua_isstring(L, ++param_pos))
  229. {
  230. server_cert_len = 0;
  231. server_cert = luaL_checklstring(L, param_pos, &server_cert_len);
  232. }
  233. if (lua_isstring(L, ++param_pos))
  234. {
  235. client_cert_len = 0;
  236. client_cert = luaL_checklstring(L, param_pos, &client_cert_len);
  237. }
  238. if (lua_isstring(L, ++param_pos))
  239. {
  240. client_key_len = 0;
  241. client_key = luaL_checklstring(L, param_pos, &client_key_len);
  242. }
  243. if (lua_isstring(L, ++param_pos))
  244. {
  245. client_password_len = 0;
  246. client_password = luaL_checklstring(L, param_pos, &client_password_len);
  247. }
  248. network_set_base_mode(l_ctrl->netc, !is_udp, 10000, keep_idle, keep_idle, keep_interval, keep_cnt);
  249. network_set_local_port(l_ctrl->netc, local_port);
  250. if (is_tls)
  251. {
  252. network_init_tls(l_ctrl->netc, (server_cert || client_cert)?2:0);
  253. if (is_udp)
  254. {
  255. network_set_psk_info(l_ctrl->netc, (const unsigned char *)server_cert, server_cert_len, (const unsigned char *)client_key, client_key_len);
  256. }
  257. else
  258. {
  259. if (server_cert)
  260. {
  261. network_set_server_cert(l_ctrl->netc, (const unsigned char *)server_cert, server_cert_len);
  262. }
  263. if (client_cert)
  264. {
  265. network_set_client_cert(l_ctrl->netc, (const unsigned char *)client_cert, client_cert_len,
  266. (const unsigned char *)client_key, client_key_len,
  267. (const unsigned char *)client_password, client_password_len);
  268. }
  269. }
  270. }
  271. else
  272. {
  273. network_deinit_tls(l_ctrl->netc);
  274. }
  275. return 0;
  276. }
  277. /*
  278. 等待网卡linkup
  279. @api socket.linkup(ctrl)
  280. @user_data socket.create得到的ctrl
  281. @return boolean true有异常发生,false没有异常,如果有error则不需要看下一个返回值了
  282. @return boolean true已经linkup,false没有linkup,之后需要接收socket.LINK消息
  283. @usage local error, result = socket.linkup(ctrl)
  284. */
  285. static int l_socket_linkup(lua_State *L)
  286. {
  287. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  288. int result = network_wait_link_up(l_ctrl->netc, 0);
  289. lua_pushboolean(L, result < 0);
  290. lua_pushboolean(L, result == 0);
  291. return 2;
  292. }
  293. /*
  294. 作为客户端连接服务器
  295. @api socket.connect(ctrl, ip, remote_port)
  296. @user_data socket.create得到的ctrl
  297. @string or int ip或者域名,如果是IPV4,可以是大端格式的int值
  298. @int 服务器端口号,小端格式
  299. @return boolean true有异常发生,false没有异常,如果有error则不需要看下一个返回值了,如果有异常,后续要close
  300. @return boolean true已经connect,false没有connect,之后需要接收socket.ON_LINE消息
  301. @usage local error, result = socket.connect(ctrl, "xxx.xxx.xxx.xxx", xxxx)
  302. */
  303. static int l_socket_connect(lua_State *L)
  304. {
  305. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  306. luat_ip_addr_t ip_addr;
  307. const char *ip;
  308. size_t ip_len;
  309. ip_addr.type = 0xff;
  310. if (lua_isinteger(L, 2))
  311. {
  312. ip_addr.type = IPADDR_TYPE_V4;
  313. ip_addr.u_addr.ip4.addr = lua_tointeger(L, 2);
  314. ip = NULL;
  315. ip_len = 0;
  316. }
  317. else
  318. {
  319. ip_len = 0;
  320. ip = luaL_checklstring(L, 2, &ip_len);
  321. }
  322. uint16_t remote_port = luaL_checkinteger(L, 3);
  323. int result = network_connect(l_ctrl->netc, ip, ip_len, (ip_addr.type != IPADDR_TYPE_V4)?NULL:&ip_addr, remote_port, 0);
  324. lua_pushboolean(L, result < 0);
  325. lua_pushboolean(L, result == 0);
  326. return 2;
  327. }
  328. /*
  329. 作为客户端断开连接
  330. @api socket.discon(ctrl)
  331. @user_data socket.create得到的ctrl
  332. @return
  333. boolean true有异常发生,false没有异常,如果有error则不需要看下一个返回值了
  334. boolean true已经断开,false没有断开,之后需要接收socket.CLOSED消息
  335. @usage local error, result = socket.discon(ctrl)
  336. */
  337. static int l_socket_disconnect(lua_State *L)
  338. {
  339. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  340. int result = network_close(l_ctrl->netc, 0);
  341. lua_pushboolean(L, result < 0);
  342. lua_pushboolean(L, result == 0);
  343. return 2;
  344. }
  345. /*
  346. 强制关闭socket
  347. @api socket.close(ctrl)
  348. @user_data socket.create得到的ctrl
  349. */
  350. static int l_socket_close(lua_State *L)
  351. {
  352. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  353. network_force_close_socket(l_ctrl->netc);
  354. return 0;
  355. }
  356. /*
  357. 发送数据给对端,UDP单次发送不要超过1460字节,否则很容易失败
  358. @api socket.tx(ctrl, data, ip, port, flag)
  359. @user_data socket.create得到的ctrl
  360. @string or user_data zbuff 要发送的数据
  361. @string or int 对端IP,如果是TCP应用则忽略,如果是UDP,如果留空则用connect时候的参数,如果是IPV4,可以是大端格式的int值
  362. @int 对端端口号,小端格式,如果是TCP应用则忽略,如果是UDP,如果留空则用connect时候的参数
  363. @int 发送参数,目前预留,不起作用
  364. @return boolean true有异常发生,false没有异常,如果有error则不需要看下一个返回值了,如果有异常,后续要close
  365. @return boolean true缓冲区满了,false没有异常,如果true,则需要等待一段时间或者等到socket.TX_OK消息后再尝试发送,同时忽略下一个返回值
  366. @return boolean true已经收到应答,false没有收到应答,之后需要接收socket.TX_OK消息, 也可以忽略继续发送,直到full==true
  367. @usage local error, full, result = socket.tx(ctrl, "123456", "xxx.xxx.xxx.xxx", xxxx)
  368. */
  369. static int l_socket_tx(lua_State *L)
  370. {
  371. char ip_buf[68] = {0};
  372. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  373. luat_ip_addr_t ip_addr = {0};
  374. luat_zbuff_t *buff = NULL;
  375. const char *ip = NULL;
  376. const char *data = NULL;
  377. size_t ip_len = 0, data_len = 0;
  378. ip_addr.type = 0xff;
  379. if (lua_isstring(L, 2))
  380. {
  381. data_len = 0;
  382. data = luaL_checklstring(L, 2, &data_len);
  383. }
  384. else
  385. {
  386. buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  387. data = (const char*)buff->addr;
  388. data_len = buff->used;
  389. }
  390. if (lua_isinteger(L, 3))
  391. {
  392. ip_addr.type = 0;
  393. ip_addr.u_addr.ip4.addr = lua_tointeger(L, 3);
  394. }
  395. else if (lua_isstring(L, 3))
  396. {
  397. ip_len = 0;
  398. ip = luaL_checklstring(L, 3, &ip_len);
  399. memcpy(ip_buf, ip, ip_len);
  400. ip_buf[ip_len] = 0;
  401. ipaddr_aton(ip_buf, &ip_addr);
  402. }
  403. uint32_t tx_len;
  404. int result = network_tx(l_ctrl->netc, data, data_len, luaL_optinteger(L, 5, 0), (ip_addr.type != 0xff)?&ip_addr:NULL, luaL_optinteger(L, 4, 0), &tx_len, 0);
  405. lua_pushboolean(L, result < 0);
  406. lua_pushboolean(L, tx_len != data_len);
  407. lua_pushboolean(L, result == 0);
  408. return 3;
  409. }
  410. /*
  411. 接收对端发出的数据,注意数据已经缓存在底层,使用本函数只是提取出来,UDP模式下一次只会取出一个数据包
  412. @api socket.rx(ctrl, buff, flag)
  413. @user_data socket.create得到的ctrl
  414. @user_data zbuff 存放接收的数据,如果缓冲区不够大会自动扩容
  415. @int 接收参数,目前预留,不起作用
  416. @return boolean true有异常发生,false没有异常,如果有异常,后续要close
  417. @return int 本次接收到数据长度
  418. @return string 对端IP,只有UDP模式下才有意义,TCP模式返回nil,注意返回的格式,如果是IPV4,1byte 0x00 + 4byte地址 如果是IPV6,1byte 0x01 + 16byte地址
  419. @return int 对端port,只有UDP模式下才有意义,TCP模式返回0
  420. @usage local error, data_len, ip, port = socket.rx(ctrl, buff)
  421. */
  422. static int l_socket_rx(lua_State *L)
  423. {
  424. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  425. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  426. luat_ip_addr_t ip_addr;
  427. uint8_t ip[17];
  428. uint16_t port;
  429. uint8_t new_flag = 0;
  430. int rx_len;
  431. int total_len;
  432. int result = network_rx(l_ctrl->netc, NULL, 0, 0, NULL, NULL, &total_len);
  433. if (result < 0)
  434. {
  435. lua_pushboolean(L, 1);
  436. lua_pushinteger(L, 0);
  437. lua_pushnil(L);
  438. lua_pushnil(L);
  439. }
  440. else if (!total_len)
  441. {
  442. lua_pushboolean(L, 0);
  443. lua_pushinteger(L, 0);
  444. lua_pushnil(L);
  445. lua_pushnil(L);
  446. }
  447. else
  448. {
  449. if ((buff->len - buff->used) < total_len)
  450. {
  451. __zbuff_resize(buff, total_len + buff->used);
  452. }
  453. result = network_rx(l_ctrl->netc, buff->addr + buff->used, total_len, 0, &ip_addr, &port, &rx_len);
  454. if (result < 0)
  455. {
  456. lua_pushboolean(L, 1);
  457. lua_pushinteger(L, 0);
  458. lua_pushnil(L);
  459. lua_pushnil(L);
  460. }
  461. else if (!rx_len)
  462. {
  463. lua_pushboolean(L, 0);
  464. lua_pushinteger(L, 0);
  465. lua_pushnil(L);
  466. lua_pushnil(L);
  467. }
  468. else
  469. {
  470. buff->used += rx_len;
  471. lua_pushboolean(L, 0);
  472. lua_pushinteger(L, rx_len);
  473. if (l_ctrl->netc->is_tcp)
  474. {
  475. lua_pushnil(L);
  476. lua_pushnil(L);
  477. }
  478. else
  479. {
  480. if (IPADDR_TYPE_V4 == ip_addr.type)
  481. {
  482. ip[0] = 0;
  483. memcpy(ip + 1, &ip_addr.u_addr.ip4.addr, 4);
  484. lua_pushlstring(L, ip, 5);
  485. }
  486. else
  487. {
  488. ip[0] = 1;
  489. memcpy(ip + 1, ip_addr.u_addr.ip6.addr, 16);
  490. lua_pushlstring(L, ip, 17);
  491. }
  492. lua_pushinteger(L, port);
  493. }
  494. }
  495. }
  496. return 4;
  497. }
  498. /*
  499. 等待新的socket消息,在连接成功和发送数据成功后,使用一次将network状态转换到接收新数据
  500. @api socket.wait(ctrl)
  501. @user_data socket.create得到的ctrl
  502. @return boolean true有异常发生,false没有异常,如果有异常,后续要close
  503. @return boolean true有新的数据需要接收,false没有数据,之后需要接收socket.EVENT消息
  504. @usage local error, result = socket.wait(ctrl)
  505. */
  506. static int l_socket_wait(lua_State *L)
  507. {
  508. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  509. int result = network_wait_event(l_ctrl->netc, NULL, 0, NULL);
  510. lua_pushboolean(L, result < 0);
  511. lua_pushboolean(L, result == 0);
  512. return 2;
  513. }
  514. /*
  515. 作为服务端开始监听
  516. @api socket.listen(ctrl)
  517. @user_data socket.create得到的ctrl
  518. @return boolean true有异常发生,false没有异常,如果有error则不需要看下一个返回值了,如果有异常,后续要close
  519. @return boolean true已经connect,false没有connect,之后需要接收socket.ON_LINE消息
  520. @usage local error, result = socket.listen(ctrl)
  521. */
  522. static int l_socket_listen(lua_State *L)
  523. {
  524. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  525. int result = network_listen(l_ctrl->netc, 0);
  526. lua_pushboolean(L, result < 0);
  527. lua_pushboolean(L, result == 0);
  528. return 2;
  529. }
  530. /*
  531. 作为服务端接收到一个新的客户端,注意,如果是类似W5500的硬件协议栈不支持1对多,则不需要第二个参数
  532. @api socket.accept(ctrl)
  533. @user_data socket.create得到的ctrl,这里是服务器端
  534. @string or function or nil string为消息通知的taskName,function则为回调函数,和socket.create参数一致
  535. @return boolean true有异常发生,false没有异常,如果有error则不需要看下一个返回值了,如果有异常,后续要close
  536. @return user_data or nil 如果支持1对多,则会返回新的ctrl,自动create,如果不支持则返回nil
  537. @usage local error, new_netc = socket.listen(ctrl, cb)
  538. */
  539. static int l_socket_accept(lua_State *L)
  540. {
  541. luat_socket_ctrl_t *old_ctrl = l_get_ctrl(L, 1);
  542. if (network_accept_enable(old_ctrl->netc))
  543. {
  544. luat_socket_ctrl_t *new_ctrl = (luat_socket_ctrl_t *)lua_newuserdata(L, sizeof(luat_socket_ctrl_t));
  545. if (!new_ctrl)
  546. {
  547. lua_pushboolean(L, 0);
  548. lua_pushnil(L);
  549. return 2;
  550. }
  551. new_ctrl->adapter_index = old_ctrl->adapter_index;
  552. new_ctrl->netc = network_alloc_ctrl(old_ctrl->adapter_index);
  553. if (!new_ctrl->netc)
  554. {
  555. LLOGD("create fail");
  556. lua_pushboolean(L, 0);
  557. lua_pushnil(L);
  558. return 2;
  559. }
  560. network_init_ctrl(new_ctrl->netc, NULL, luat_lib_socket_callback, new_ctrl);
  561. if (lua_isfunction(L, 2))
  562. {
  563. lua_pushvalue(L, 2);
  564. new_ctrl->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  565. new_ctrl->task_name = NULL;
  566. }
  567. else if (lua_isstring(L, 2))
  568. {
  569. new_ctrl->cb_ref = 0;
  570. size_t len;
  571. const char *buf;
  572. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  573. new_ctrl->task_name = luat_heap_malloc(len + 1);
  574. memset(new_ctrl->task_name, 0, len + 1);
  575. memcpy(new_ctrl->task_name, buf, len);
  576. }
  577. if (network_socket_accept(old_ctrl, new_ctrl))
  578. {
  579. lua_pushboolean(L, 0);
  580. lua_pushnil(L);
  581. return 2;
  582. }
  583. else
  584. {
  585. lua_pushboolean(L, 1);
  586. luaL_setmetatable(L, LUAT_NW_CTRL_TYPE);
  587. return 2;
  588. }
  589. }
  590. else
  591. {
  592. lua_pushboolean(L, !network_socket_accept(old_ctrl->netc, NULL));
  593. lua_pushnil(L);
  594. return 2;
  595. }
  596. }
  597. /*
  598. 主动释放掉network_ctrl
  599. @api socket.release(ctrl)
  600. @user_data socket.create得到的ctrl
  601. @usage socket.release(ctrl)
  602. */
  603. static int l_socket_release(lua_State *L)
  604. {
  605. return l_socket_gc(L);
  606. }
  607. /*
  608. 设置DNS服务器
  609. @api socket.setDNS(adapter_index, dns_index, ip)
  610. @int 适配器序号, 只能是socket.ETH0,socket.STA,socket.AP,如果不填,会选择最后一个注册的适配器
  611. @int dns服务器序号,从1开始
  612. @string or int dns,如果是IPV4,可以是大端格式的int值
  613. @return boolean 成功返回true,失败返回false
  614. @usage socket.setDNS(socket.ETH0, 1, "114.114.114.114")
  615. */
  616. static int l_socket_set_dns(lua_State *L)
  617. {
  618. char ip_buf[68];
  619. int adapter_index = luaL_optinteger(L, 1, network_get_last_register_adapter());
  620. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY)
  621. {
  622. lua_pushboolean(L, 0);
  623. return 1;
  624. }
  625. int dns_index = luaL_optinteger(L, 2, 1);
  626. luat_ip_addr_t ip_addr;
  627. const char *ip;
  628. size_t ip_len;
  629. ip_addr.type = 0xff;
  630. if (lua_isinteger(L, 3))
  631. {
  632. ip_addr.type = 0;
  633. ip_addr.u_addr.ip4.addr = lua_tointeger(L, 3);
  634. ip = NULL;
  635. ip_len = 0;
  636. }
  637. else
  638. {
  639. ip_len = 0;
  640. ip = luaL_checklstring(L, 3, &ip_len);
  641. memcpy(ip_buf, ip, ip_len);
  642. ip_buf[ip_len] = 0;
  643. ipaddr_aton(ip_buf, &ip_addr);
  644. }
  645. network_set_dns_server(adapter_index, dns_index - 1, &ip_addr);
  646. lua_pushboolean(L, 1);
  647. return 1;
  648. }
  649. /*
  650. 设置SSL的log
  651. @api socket.sslLog(log_level)
  652. @int mbedtls log等级,<=2基本不打印,不要超过9
  653. @usage socket.sslLog(3)
  654. */
  655. static int l_socket_set_ssl_log(lua_State *L)
  656. {
  657. mbedtls_debug_set_threshold(luaL_optinteger(L, 1, 1));
  658. return 0;
  659. }
  660. #include "rotable2.h"
  661. static const rotable_Reg_t reg_socket_adapter[] =
  662. {
  663. {"create", ROREG_FUNC(l_socket_create)},
  664. {"debug", ROREG_FUNC(l_socket_set_debug)},
  665. {"config", ROREG_FUNC(l_socket_config)},
  666. {"linkup", ROREG_FUNC(l_socket_linkup)},
  667. {"connect", ROREG_FUNC(l_socket_connect)},
  668. {"listen", ROREG_FUNC(l_socket_listen)},
  669. {"accept", ROREG_FUNC(l_socket_accept)},
  670. {"discon", ROREG_FUNC(l_socket_disconnect)},
  671. {"close", ROREG_FUNC(l_socket_close)},
  672. {"tx", ROREG_FUNC(l_socket_tx)},
  673. {"rx", ROREG_FUNC(l_socket_rx)},
  674. {"wait", ROREG_FUNC(l_socket_wait)},
  675. //{"listen", ROREG_FUNC(l_socket_listen)},
  676. //{"accept", ROREG_FUNC(l_socket_accept)},
  677. {"release", ROREG_FUNC(l_socket_release)},
  678. { "setDNS", ROREG_FUNC(l_socket_set_dns)},
  679. { "sslLog", ROREG_FUNC(l_socket_set_ssl_log)},
  680. //@const ETH0 number 带硬件协议栈的ETH0
  681. { "ETH0", ROREG_INT(NW_ADAPTER_INDEX_ETH0)},
  682. //@const LWIP_ETH number 使用LWIP协议栈的以太网卡
  683. { "LWIP_ETH", ROREG_INT(NW_ADAPTER_INDEX_LWIP_ETH)},
  684. //@const LWIP_STA number 使用LWIP协议栈的WIFI STA
  685. { "LWIP_STA", ROREG_INT(NW_ADAPTER_INDEX_LWIP_WIFI_STA)},
  686. //@const LWIP_AP number 使用LWIP协议栈的WIFI AP
  687. { "LWIP_AP", ROREG_INT(NW_ADAPTER_INDEX_LWIP_WIFI_AP)},
  688. //@const LWIP_GP number 使用LWIP协议栈的移动蜂窝模块
  689. { "LWIP_GP", ROREG_INT(NW_ADAPTER_INDEX_LWIP_GPRS)},
  690. //@const USB number 使用LWIP协议栈的USB网卡
  691. { "USB", ROREG_INT(NW_ADAPTER_INDEX_USB)},
  692. //@const LINK number LINK事件
  693. { "LINK", ROREG_INT(EV_NW_RESULT_LINK & 0x0fffffff)},
  694. //@const ON_LINE number ON_LINE事件
  695. { "ON_LINE", ROREG_INT(EV_NW_RESULT_CONNECT & 0x0fffffff)},
  696. //@const EVENT number EVENT事件
  697. { "EVENT", ROREG_INT(EV_NW_RESULT_EVENT & 0x0fffffff)},
  698. //@const TX_OK number TX_OK事件
  699. { "TX_OK", ROREG_INT(EV_NW_RESULT_TX & 0x0fffffff)},
  700. //@const CLOSED number CLOSED事件
  701. { "CLOSED", ROREG_INT(EV_NW_RESULT_CLOSE & 0x0fffffff)},
  702. { NULL, ROREG_INT(0)}
  703. };
  704. #else
  705. static int32_t l_socket_callback(lua_State *L, void* ptr)
  706. {
  707. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  708. luat_socket_ctrl_t *l_ctrl =(luat_socket_ctrl_t *)msg->ptr;
  709. if (l_ctrl->netc)
  710. {
  711. if (l_ctrl->cb_ref)
  712. {
  713. lua_geti(L, LUA_REGISTRYINDEX, l_ctrl->cb_ref);
  714. if (lua_isfunction(L, -1)) {
  715. lua_pushlightuserdata(L, l_ctrl);
  716. lua_pushinteger(L, msg->arg1);
  717. lua_pushinteger(L, msg->arg2);
  718. lua_call(L, 3, 0);
  719. }
  720. }
  721. else if (l_ctrl->task_name)
  722. {
  723. lua_getglobal(L, "sys_send");
  724. if (lua_isfunction(L, -1)) {
  725. lua_pushstring(L, l_ctrl->task_name);
  726. lua_pushinteger(L, msg->arg1);
  727. lua_pushinteger(L, msg->arg2);
  728. lua_call(L, 3, 0);
  729. }
  730. }
  731. else
  732. {
  733. lua_getglobal(L, "sys_pub");
  734. if (lua_isfunction(L, -1)) {
  735. lua_pushstring(L, LUAT_NW_CTRL_TYPE);
  736. lua_pushinteger(L, l_ctrl->netc->adapter_index);
  737. lua_pushinteger(L, l_ctrl->netc->socket_id);
  738. lua_pushinteger(L, msg->arg1);
  739. lua_pushinteger(L, msg->arg2);
  740. lua_call(L, 5, 0);
  741. }
  742. }
  743. }
  744. lua_pushinteger(L, 0);
  745. return 1;
  746. }
  747. static int32_t luat_lib_socket_callback(void *data, void *param)
  748. {
  749. OS_EVENT *event = (OS_EVENT *)data;
  750. rtos_msg_t msg;
  751. msg.handler = l_socket_callback;
  752. msg.ptr = param;
  753. msg.arg1 = event->ID & 0x0fffffff;
  754. msg.arg2 = event->Param1;
  755. luat_msgbus_put(&msg, 0);
  756. return 0;
  757. }
  758. static luat_socket_ctrl_t * l_get_ctrl(lua_State *L, int index)
  759. {
  760. if (luaL_testudata(L, 1, LUAT_NW_CTRL_TYPE))
  761. {
  762. return ((luat_socket_ctrl_t *)luaL_checkudata(L, 1, LUAT_NW_CTRL_TYPE));
  763. }
  764. else
  765. {
  766. return ((luat_socket_ctrl_t *)lua_touserdata(L, 1));
  767. }
  768. }
  769. // __gc
  770. static int l_socket_gc(lua_State *L)
  771. {
  772. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  773. if (l_ctrl->netc)
  774. {
  775. network_force_close_socket(l_ctrl->netc);
  776. network_release_ctrl(l_ctrl->netc);
  777. l_ctrl->netc = NULL;
  778. }
  779. if (l_ctrl->cb_ref)
  780. {
  781. luaL_unref(L, LUA_REGISTRYINDEX, l_ctrl->cb_ref);
  782. l_ctrl->cb_ref = 0;
  783. }
  784. if (l_ctrl->task_name)
  785. {
  786. luat_heap_free(l_ctrl->task_name);
  787. l_ctrl->task_name = 0;
  788. }
  789. return 0;
  790. }
  791. static int l_socket_create(lua_State *L)
  792. {
  793. int adapter_index = luaL_optinteger(L, 1, network_get_last_register_adapter());
  794. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY)
  795. {
  796. lua_pushnil(L);
  797. return 1;
  798. }
  799. luat_socket_ctrl_t *l_ctrl = (luat_socket_ctrl_t *)lua_newuserdata(L, sizeof(luat_socket_ctrl_t));
  800. if (!l_ctrl)
  801. {
  802. lua_pushnil(L);
  803. return 1;
  804. }
  805. l_ctrl->adapter_index = adapter_index;
  806. l_ctrl->netc = network_alloc_ctrl(adapter_index);
  807. if (!l_ctrl->netc)
  808. {
  809. LLOGD("create fail");
  810. lua_pushnil(L);
  811. return 1;
  812. }
  813. network_init_ctrl(l_ctrl->netc, NULL, luat_lib_socket_callback, l_ctrl);
  814. if (lua_isfunction(L, 2))
  815. {
  816. lua_pushvalue(L, 2);
  817. l_ctrl->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  818. l_ctrl->task_name = NULL;
  819. }
  820. else if (lua_isstring(L, 2))
  821. {
  822. l_ctrl->cb_ref = 0;
  823. size_t len;
  824. const char *buf;
  825. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  826. l_ctrl->task_name = luat_heap_malloc(len + 1);
  827. memset(l_ctrl->task_name, 0, len + 1);
  828. memcpy(l_ctrl->task_name, buf, len);
  829. }
  830. luaL_setmetatable(L, LUAT_NW_CTRL_TYPE);
  831. return 1;
  832. }
  833. static int l_socket_set_debug(lua_State *L)
  834. {
  835. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  836. if (lua_isboolean(L, 2))
  837. {
  838. l_ctrl->netc->is_debug = lua_toboolean(L, 2);
  839. }
  840. return 0;
  841. }
  842. static int l_socket_config(lua_State *L)
  843. {
  844. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  845. uint8_t is_udp = 0;
  846. uint8_t is_tls = 0;
  847. int param_pos = 1;
  848. uint32_t keep_idle, keep_interval, keep_cnt;
  849. const char *server_cert = NULL;
  850. const char *client_cert = NULL;
  851. const char *client_key = NULL;
  852. const char *client_password = NULL;
  853. size_t server_cert_len, client_cert_len, client_key_len, client_password_len;
  854. uint16_t local_port = luaL_optinteger(L, ++param_pos, 0);
  855. if (lua_isboolean(L, ++param_pos))
  856. {
  857. is_udp = lua_toboolean(L, param_pos);
  858. }
  859. if (lua_isboolean(L, ++param_pos))
  860. {
  861. is_tls = lua_toboolean(L, param_pos);
  862. }
  863. keep_idle = luaL_optinteger(L, ++param_pos, 0);
  864. keep_interval = luaL_optinteger(L, ++param_pos, 0);
  865. keep_cnt = luaL_optinteger(L, ++param_pos, 0);
  866. if (lua_isstring(L, ++param_pos))
  867. {
  868. server_cert_len = 0;
  869. server_cert = luaL_checklstring(L, param_pos, &server_cert_len);
  870. }
  871. if (lua_isstring(L, ++param_pos))
  872. {
  873. client_cert_len = 0;
  874. client_cert = luaL_checklstring(L, param_pos, &client_cert_len);
  875. }
  876. if (lua_isstring(L, ++param_pos))
  877. {
  878. client_key_len = 0;
  879. client_key = luaL_checklstring(L, param_pos, &client_key_len);
  880. }
  881. if (lua_isstring(L, ++param_pos))
  882. {
  883. client_password_len = 0;
  884. client_password = luaL_checklstring(L, param_pos, &client_password_len);
  885. }
  886. network_set_base_mode(l_ctrl->netc, !is_udp, 10000, keep_idle, keep_idle, keep_interval, keep_cnt);
  887. network_set_local_port(l_ctrl->netc, local_port);
  888. if (is_tls)
  889. {
  890. network_init_tls(l_ctrl->netc, (server_cert || client_cert)?2:0);
  891. if (is_udp)
  892. {
  893. network_set_psk_info(l_ctrl->netc, (const unsigned char *)server_cert, server_cert_len, (const unsigned char *)client_key, client_key_len);
  894. }
  895. else
  896. {
  897. if (server_cert)
  898. {
  899. network_set_server_cert(l_ctrl->netc, (const unsigned char *)server_cert, server_cert_len);
  900. }
  901. if (client_cert)
  902. {
  903. network_set_client_cert(l_ctrl->netc, (const unsigned char *)client_cert, client_cert_len,
  904. (const unsigned char *)client_key, client_key_len,
  905. (const unsigned char *)client_password, client_password_len);
  906. }
  907. }
  908. }
  909. else
  910. {
  911. network_deinit_tls(l_ctrl->netc);
  912. }
  913. return 0;
  914. }
  915. static int l_socket_linkup(lua_State *L)
  916. {
  917. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  918. int result = network_wait_link_up(l_ctrl->netc, 0);
  919. lua_pushboolean(L, result < 0);
  920. lua_pushboolean(L, result == 0);
  921. return 2;
  922. }
  923. static int l_socket_connect(lua_State *L)
  924. {
  925. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  926. luat_ip_addr_t ip_addr;
  927. const char *ip;
  928. size_t ip_len;
  929. ip_addr.is_ipv6 = 0xff;
  930. if (lua_isinteger(L, 2))
  931. {
  932. ip_addr.is_ipv6 = 0;
  933. ip_addr.ipv4 = lua_tointeger(L, 2);
  934. ip = NULL;
  935. ip_len = 0;
  936. }
  937. else
  938. {
  939. ip_len = 0;
  940. ip = luaL_checklstring(L, 2, &ip_len);
  941. }
  942. uint16_t remote_port = luaL_checkinteger(L, 3);
  943. int result = network_connect(l_ctrl->netc, ip, ip_len, ip_addr.is_ipv6?NULL:&ip_addr, remote_port, 0);
  944. lua_pushboolean(L, result < 0);
  945. lua_pushboolean(L, result == 0);
  946. return 2;
  947. }
  948. static int l_socket_disconnect(lua_State *L)
  949. {
  950. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  951. int result = network_close(l_ctrl->netc, 0);
  952. lua_pushboolean(L, result < 0);
  953. lua_pushboolean(L, result == 0);
  954. return 2;
  955. }
  956. static int l_socket_close(lua_State *L)
  957. {
  958. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  959. network_force_close_socket(l_ctrl->netc);
  960. return 0;
  961. }
  962. static int l_socket_tx(lua_State *L)
  963. {
  964. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  965. luat_ip_addr_t ip_addr = {0};
  966. luat_zbuff_t *buff = NULL;
  967. const char *ip = NULL;
  968. const char *data = NULL;
  969. size_t ip_len = 0, data_len = 0;
  970. ip_addr.is_ipv6 = 0xff;
  971. if (lua_isstring(L, 2))
  972. {
  973. data_len = 0;
  974. data = luaL_checklstring(L, 2, &data_len);
  975. }
  976. else
  977. {
  978. buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  979. data = (const char *)buff->addr;
  980. data_len = buff->used;
  981. }
  982. if (lua_isinteger(L, 3))
  983. {
  984. ip_addr.is_ipv6 = 0;
  985. ip_addr.ipv4 = lua_tointeger(L, 3);
  986. }
  987. else if (lua_isstring(L, 3))
  988. {
  989. ip_len = 0;
  990. ip = luaL_checklstring(L, 3, &ip_len);
  991. if (network_string_is_ipv4(ip, ip_len))
  992. {
  993. ip_addr.is_ipv6 = 0;
  994. ip_addr.ipv4 = network_string_to_ipv4(ip, ip_len);
  995. }
  996. else
  997. {
  998. char *name = luat_heap_malloc(ip_len + 1);
  999. memcpy(name, ip, ip_len);
  1000. name[ip_len] = 0;
  1001. network_string_to_ipv6(name, &ip_addr);
  1002. free(name);
  1003. }
  1004. }
  1005. uint32_t tx_len;
  1006. int result = network_tx(l_ctrl->netc, (const uint8_t *)data, data_len, luaL_optinteger(L, 5, 0), (ip_addr.is_ipv6 != 0xff)?&ip_addr:NULL, luaL_optinteger(L, 4, 0), &tx_len, 0);
  1007. lua_pushboolean(L, result < 0);
  1008. lua_pushboolean(L, tx_len != data_len);
  1009. lua_pushboolean(L, result == 0);
  1010. return 3;
  1011. }
  1012. static int l_socket_rx(lua_State *L)
  1013. {
  1014. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  1015. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  1016. luat_ip_addr_t ip_addr;
  1017. uint8_t ip[17];
  1018. uint16_t port;
  1019. uint8_t new_flag = 0;
  1020. int rx_len;
  1021. int total_len;
  1022. int result = network_rx(l_ctrl->netc, NULL, 0, 0, NULL, NULL, &total_len);
  1023. if (result < 0)
  1024. {
  1025. lua_pushboolean(L, 1);
  1026. lua_pushinteger(L, 0);
  1027. lua_pushnil(L);
  1028. lua_pushnil(L);
  1029. }
  1030. else if (!total_len)
  1031. {
  1032. lua_pushboolean(L, 0);
  1033. lua_pushinteger(L, 0);
  1034. lua_pushnil(L);
  1035. lua_pushnil(L);
  1036. }
  1037. else
  1038. {
  1039. if ((buff->len - buff->used) < total_len)
  1040. {
  1041. __zbuff_resize(buff, total_len + buff->used);
  1042. }
  1043. result = network_rx(l_ctrl->netc, buff->addr + buff->used, total_len, 0, &ip_addr, &port, &rx_len);
  1044. if (result < 0)
  1045. {
  1046. lua_pushboolean(L, 1);
  1047. lua_pushinteger(L, 0);
  1048. lua_pushnil(L);
  1049. lua_pushnil(L);
  1050. }
  1051. else if (!rx_len)
  1052. {
  1053. lua_pushboolean(L, 0);
  1054. lua_pushinteger(L, 0);
  1055. lua_pushnil(L);
  1056. lua_pushnil(L);
  1057. }
  1058. else
  1059. {
  1060. buff->used += rx_len;
  1061. lua_pushboolean(L, 0);
  1062. lua_pushinteger(L, rx_len);
  1063. if (l_ctrl->netc->is_tcp)
  1064. {
  1065. lua_pushnil(L);
  1066. lua_pushnil(L);
  1067. }
  1068. else
  1069. {
  1070. if (!ip_addr.is_ipv6)
  1071. {
  1072. ip[0] = 0;
  1073. memcpy(ip + 1, &ip_addr.ipv4, 4);
  1074. lua_pushlstring(L, (const char*)ip, 5);
  1075. }
  1076. else
  1077. {
  1078. ip[0] = 1;
  1079. memcpy(ip + 1, &ip_addr.ipv6_u8_addr, 16);
  1080. lua_pushlstring(L, (const char*)ip, 17);
  1081. }
  1082. lua_pushinteger(L, port);
  1083. }
  1084. }
  1085. }
  1086. return 4;
  1087. }
  1088. static int l_socket_wait(lua_State *L)
  1089. {
  1090. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  1091. int result = network_wait_event(l_ctrl->netc, NULL, 0, NULL);
  1092. lua_pushboolean(L, result < 0);
  1093. lua_pushboolean(L, result == 0);
  1094. return 2;
  1095. }
  1096. static int l_socket_listen(lua_State *L)
  1097. {
  1098. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  1099. int result = network_listen(l_ctrl->netc, 0);
  1100. lua_pushboolean(L, result < 0);
  1101. lua_pushboolean(L, result == 0);
  1102. return 2;
  1103. }
  1104. static int l_socket_accept(lua_State *L)
  1105. {
  1106. luat_socket_ctrl_t *old_ctrl = l_get_ctrl(L, 1);
  1107. if (network_accept_enable(old_ctrl->netc))
  1108. {
  1109. luat_socket_ctrl_t *new_ctrl = (luat_socket_ctrl_t *)lua_newuserdata(L, sizeof(luat_socket_ctrl_t));
  1110. if (!new_ctrl)
  1111. {
  1112. lua_pushboolean(L, 0);
  1113. lua_pushnil(L);
  1114. return 2;
  1115. }
  1116. new_ctrl->adapter_index = old_ctrl->adapter_index;
  1117. new_ctrl->netc = network_alloc_ctrl(old_ctrl->adapter_index);
  1118. if (!new_ctrl->netc)
  1119. {
  1120. LLOGD("create fail");
  1121. lua_pushboolean(L, 0);
  1122. lua_pushnil(L);
  1123. return 2;
  1124. }
  1125. network_init_ctrl(new_ctrl->netc, NULL, luat_lib_socket_callback, new_ctrl);
  1126. if (lua_isfunction(L, 2))
  1127. {
  1128. lua_pushvalue(L, 2);
  1129. new_ctrl->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1130. new_ctrl->task_name = NULL;
  1131. }
  1132. else if (lua_isstring(L, 2))
  1133. {
  1134. new_ctrl->cb_ref = 0;
  1135. size_t len;
  1136. const char *buf;
  1137. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  1138. new_ctrl->task_name = luat_heap_malloc(len + 1);
  1139. memset(new_ctrl->task_name, 0, len + 1);
  1140. memcpy(new_ctrl->task_name, buf, len);
  1141. }
  1142. if (network_socket_accept(old_ctrl, new_ctrl))
  1143. {
  1144. lua_pushboolean(L, 0);
  1145. lua_pushnil(L);
  1146. return 2;
  1147. }
  1148. else
  1149. {
  1150. lua_pushboolean(L, 1);
  1151. luaL_setmetatable(L, LUAT_NW_CTRL_TYPE);
  1152. return 2;
  1153. }
  1154. }
  1155. else
  1156. {
  1157. lua_pushboolean(L, !network_socket_accept(old_ctrl->netc, NULL));
  1158. lua_pushnil(L);
  1159. return 2;
  1160. }
  1161. }
  1162. static int l_socket_release(lua_State *L)
  1163. {
  1164. return l_socket_gc(L);
  1165. }
  1166. static int l_socket_set_dns(lua_State *L)
  1167. {
  1168. int adapter_index = luaL_optinteger(L, 1, network_get_last_register_adapter());
  1169. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY)
  1170. {
  1171. lua_pushboolean(L, 0);
  1172. return 1;
  1173. }
  1174. int dns_index = luaL_optinteger(L, 2, 1);
  1175. luat_ip_addr_t ip_addr;
  1176. const char *ip;
  1177. size_t ip_len;
  1178. ip_addr.is_ipv6 = 0xff;
  1179. if (lua_isinteger(L, 3))
  1180. {
  1181. ip_addr.is_ipv6 = 0;
  1182. ip_addr.ipv4 = lua_tointeger(L, 3);
  1183. ip = NULL;
  1184. ip_len = 0;
  1185. }
  1186. else
  1187. {
  1188. ip_len = 0;
  1189. ip = luaL_checklstring(L, 3, &ip_len);
  1190. ip_addr.is_ipv6 = !network_string_is_ipv4(ip, ip_len);
  1191. if (ip_addr.is_ipv6)
  1192. {
  1193. char *temp = luat_heap_malloc(ip_len + 1);
  1194. memcpy(temp, ip, ip_len);
  1195. temp[ip_len] = 0;
  1196. network_string_to_ipv6(temp, &ip_addr);
  1197. luat_heap_free(temp);
  1198. }
  1199. else
  1200. {
  1201. ip_addr.ipv4 = network_string_to_ipv4(ip, ip_len);
  1202. }
  1203. }
  1204. network_set_dns_server(adapter_index, dns_index - 1, &ip_addr);
  1205. lua_pushboolean(L, 1);
  1206. return 1;
  1207. }
  1208. static int l_socket_set_ssl_log(lua_State *L)
  1209. {
  1210. #if defined(MBEDTLS_DEBUG_C)
  1211. mbedtls_debug_set_threshold(luaL_optinteger(L, 1, 1));
  1212. #endif
  1213. return 0;
  1214. }
  1215. #include "rotable2.h"
  1216. static const rotable_Reg_t reg_socket_adapter[] =
  1217. {
  1218. {"create", ROREG_FUNC(l_socket_create)},
  1219. {"debug", ROREG_FUNC(l_socket_set_debug)},
  1220. {"config", ROREG_FUNC(l_socket_config)},
  1221. {"linkup", ROREG_FUNC(l_socket_linkup)},
  1222. {"connect", ROREG_FUNC(l_socket_connect)},
  1223. {"listen", ROREG_FUNC(l_socket_listen)},
  1224. {"accept", ROREG_FUNC(l_socket_accept)},
  1225. {"discon", ROREG_FUNC(l_socket_disconnect)},
  1226. {"close", ROREG_FUNC(l_socket_close)},
  1227. {"tx", ROREG_FUNC(l_socket_tx)},
  1228. {"rx", ROREG_FUNC(l_socket_rx)},
  1229. {"wait", ROREG_FUNC(l_socket_wait)},
  1230. //{"listen", ROREG_FUNC(l_socket_listen)},
  1231. //{"accept", ROREG_FUNC(l_socket_accept)},
  1232. {"release", ROREG_FUNC(l_socket_release)},
  1233. { "setDNS", ROREG_FUNC(l_socket_set_dns)},
  1234. { "sslLog", ROREG_FUNC(l_socket_set_ssl_log)},
  1235. { "ETH0", ROREG_INT(NW_ADAPTER_INDEX_ETH0)},
  1236. { "LINK", ROREG_INT(EV_NW_RESULT_LINK & 0x0fffffff)},
  1237. { "ON_LINE", ROREG_INT(EV_NW_RESULT_CONNECT & 0x0fffffff)},
  1238. { "EVENT", ROREG_INT(EV_NW_RESULT_EVENT & 0x0fffffff)},
  1239. { "TX_OK", ROREG_INT(EV_NW_RESULT_TX & 0x0fffffff)},
  1240. { "CLOSED", ROREG_INT(EV_NW_RESULT_CLOSE & 0x0fffffff)},
  1241. { NULL, ROREG_INT(0)}
  1242. };
  1243. #endif
  1244. LUAMOD_API int luaopen_socket_adapter( lua_State *L ) {
  1245. luat_newlib2(L, reg_socket_adapter);
  1246. luaL_newmetatable(L, LUAT_NW_CTRL_TYPE); /* create metatable for file handles */
  1247. lua_pushcfunction(L, l_socket_gc);
  1248. lua_setfield(L, -2, "__gc");
  1249. lua_pop(L, 1); /* pop new metatable */
  1250. return 1;
  1251. }
  1252. #endif