net.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. // Module for network
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #include "lmem.h"
  6. #include "c_string.h"
  7. #include "c_stdlib.h"
  8. #include "c_types.h"
  9. #include "mem.h"
  10. #include "lwip/ip_addr.h"
  11. #include "espconn.h"
  12. #include "lwip/dns.h"
  13. #define TCP ESPCONN_TCP
  14. #define UDP ESPCONN_UDP
  15. static ip_addr_t host_ip; // for dns
  16. #ifdef HAVE_SSL_SERVER_CRT
  17. #include HAVE_SSL_SERVER_CRT
  18. #else
  19. __attribute__((section(".servercert.flash"))) unsigned char net_server_cert_area[INTERNAL_FLASH_SECTOR_SIZE];
  20. #endif
  21. __attribute__((section(".clientcert.flash"))) unsigned char net_client_cert_area[INTERNAL_FLASH_SECTOR_SIZE];
  22. #if 0
  23. static int expose_array(lua_State* L, char *array, unsigned short len);
  24. #endif
  25. #define MAX_SOCKET 5
  26. static int socket_num = 0;
  27. static int socket[MAX_SOCKET];
  28. static int tcpserver_cb_connect_ref = LUA_NOREF; // for tcp server connected callback
  29. static uint16_t tcp_server_timeover = 30;
  30. static struct espconn *pTcpServer = NULL;
  31. static struct espconn *pUdpServer = NULL;
  32. typedef struct lnet_userdata
  33. {
  34. struct espconn *pesp_conn;
  35. int self_ref;
  36. int cb_connect_ref;
  37. int cb_reconnect_ref;
  38. int cb_disconnect_ref;
  39. int cb_receive_ref;
  40. int cb_send_ref;
  41. int cb_dns_found_ref;
  42. #ifdef CLIENT_SSL_ENABLE
  43. uint8_t secure;
  44. #endif
  45. }lnet_userdata;
  46. static void net_server_disconnected(void *arg) // for tcp server only
  47. {
  48. NODE_DBG("net_server_disconnected is called.\n");
  49. struct espconn *pesp_conn = arg;
  50. if(pesp_conn == NULL)
  51. return;
  52. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  53. if(nud == NULL)
  54. return;
  55. lua_State *L = lua_getstate();
  56. #if 0
  57. char temp[20] = {0};
  58. c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
  59. NODE_DBG("remote ");
  60. NODE_DBG(temp);
  61. NODE_DBG(":");
  62. NODE_DBG("%d",pesp_conn->proto.tcp->remote_port);
  63. NODE_DBG(" disconnected.\n");
  64. #endif
  65. if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
  66. {
  67. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
  68. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
  69. lua_call(L, 1, 0);
  70. }
  71. int i;
  72. lua_gc(L, LUA_GCSTOP, 0);
  73. for(i=0;i<MAX_SOCKET;i++){
  74. if( (LUA_NOREF!=socket[i]) && (socket[i] == nud->self_ref) ){
  75. // found the saved client
  76. nud->pesp_conn->reverse = NULL;
  77. nud->pesp_conn = NULL; // the espconn is made by low level sdk, do not need to free, delete() will not free it.
  78. nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
  79. luaL_unref(L, LUA_REGISTRYINDEX, socket[i]);
  80. socket[i] = LUA_NOREF;
  81. socket_num--;
  82. break;
  83. }
  84. }
  85. lua_gc(L, LUA_GCRESTART, 0);
  86. }
  87. static void net_socket_disconnected(void *arg) // tcp only
  88. {
  89. NODE_DBG("net_socket_disconnected is called.\n");
  90. struct espconn *pesp_conn = arg;
  91. if(pesp_conn == NULL)
  92. return;
  93. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  94. if(nud == NULL)
  95. return;
  96. lua_State *L = lua_getstate();
  97. if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
  98. {
  99. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
  100. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
  101. lua_call(L, 1, 0);
  102. }
  103. if(pesp_conn->proto.tcp)
  104. c_free(pesp_conn->proto.tcp);
  105. pesp_conn->proto.tcp = NULL;
  106. if(nud->pesp_conn)
  107. c_free(nud->pesp_conn);
  108. nud->pesp_conn = NULL; // espconn is already disconnected
  109. lua_gc(L, LUA_GCSTOP, 0);
  110. if(nud->self_ref != LUA_NOREF){
  111. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  112. nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
  113. }
  114. lua_gc(L, LUA_GCRESTART, 0);
  115. }
  116. static void net_server_reconnected(void *arg, sint8_t err)
  117. {
  118. NODE_DBG("net_server_reconnected is called.\n");
  119. net_server_disconnected(arg);
  120. }
  121. static void net_socket_reconnected(void *arg, sint8_t err)
  122. {
  123. NODE_DBG("net_socket_reconnected is called.\n");
  124. net_socket_disconnected(arg);
  125. }
  126. static void net_socket_received(void *arg, char *pdata, unsigned short len)
  127. {
  128. NODE_DBG("net_socket_received is called.\n");
  129. struct espconn *pesp_conn = arg;
  130. if(pesp_conn == NULL)
  131. return;
  132. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  133. if(nud == NULL)
  134. return;
  135. if(nud->cb_receive_ref == LUA_NOREF)
  136. return;
  137. if(nud->self_ref == LUA_NOREF)
  138. return;
  139. lua_State *L = lua_getstate();
  140. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_receive_ref);
  141. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
  142. // expose_array(L, pdata, len);
  143. // *(pdata+len) = 0;
  144. // NODE_DBG(pdata);
  145. // NODE_DBG("\n");
  146. lua_pushlstring(L, pdata, len);
  147. // lua_pushinteger(L, len);
  148. lua_call(L, 2, 0);
  149. }
  150. static void net_socket_sent(void *arg)
  151. {
  152. // NODE_DBG("net_socket_sent is called.\n");
  153. struct espconn *pesp_conn = arg;
  154. if(pesp_conn == NULL)
  155. return;
  156. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  157. if(nud == NULL)
  158. return;
  159. if(nud->cb_send_ref == LUA_NOREF)
  160. return;
  161. if(nud->self_ref == LUA_NOREF)
  162. return;
  163. lua_State *L = lua_getstate();
  164. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
  165. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
  166. lua_call(L, 1, 0);
  167. }
  168. static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  169. {
  170. NODE_DBG("net_dns_found is called.\n");
  171. struct espconn *pesp_conn = arg;
  172. if(pesp_conn == NULL){
  173. NODE_DBG("pesp_conn null.\n");
  174. return;
  175. }
  176. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  177. if(nud == NULL){
  178. NODE_DBG("nud null.\n");
  179. return;
  180. }
  181. if(nud->cb_dns_found_ref == LUA_NOREF){
  182. NODE_DBG("cb_dns_found_ref null.\n");
  183. return;
  184. }
  185. if(nud->self_ref == LUA_NOREF){
  186. NODE_DBG("self_ref null.\n");
  187. return;
  188. }
  189. lua_State *L = lua_getstate();
  190. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
  191. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
  192. if(ipaddr == NULL)
  193. {
  194. NODE_DBG( "DNS Fail!\n" );
  195. lua_pushnil(L);
  196. }else{
  197. // ipaddr->addr is a uint32_t ip
  198. char ip_str[20];
  199. c_memset(ip_str, 0, sizeof(ip_str));
  200. if(ipaddr->addr != 0)
  201. {
  202. c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr)));
  203. }
  204. lua_pushstring(L, ip_str); // the ip para
  205. }
  206. // "enhanced" end
  207. lua_call(L, 2, 0);
  208. end:
  209. if((pesp_conn->type == ESPCONN_TCP && pesp_conn->proto.tcp->remote_port == 0)
  210. || (pesp_conn->type == ESPCONN_UDP && pesp_conn->proto.udp->remote_port == 0) ){
  211. lua_gc(L, LUA_GCSTOP, 0);
  212. if(nud->self_ref != LUA_NOREF){
  213. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  214. nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
  215. }
  216. lua_gc(L, LUA_GCRESTART, 0);
  217. }
  218. }
  219. static void net_server_connected(void *arg) // for tcp only
  220. {
  221. NODE_DBG("net_server_connected is called.\n");
  222. struct espconn *pesp_conn = arg;
  223. int i = 0;
  224. lnet_userdata *skt = NULL;
  225. if(pesp_conn == NULL)
  226. return;
  227. #if 0
  228. char temp[20] = {0};
  229. c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
  230. NODE_DBG("remote ");
  231. NODE_DBG(temp);
  232. NODE_DBG(":");
  233. NODE_DBG("%d",pesp_conn->proto.tcp->remote_port);
  234. NODE_DBG(" connected.\n");
  235. #endif
  236. for(i=0;i<MAX_SOCKET;i++){
  237. if(socket[i] == LUA_NOREF) // found empty slot
  238. {
  239. break;
  240. }
  241. }
  242. if(i>=MAX_SOCKET) // can't create more socket
  243. {
  244. NODE_ERR("MAX_SOCKET\n");
  245. pesp_conn->reverse = NULL; // not accept this conn
  246. if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port)
  247. espconn_disconnect(pesp_conn);
  248. return;
  249. }
  250. if(tcpserver_cb_connect_ref == LUA_NOREF)
  251. return;
  252. lua_State *L = lua_getstate();
  253. lua_rawgeti(L, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref); // get function
  254. // create a new client object
  255. skt = (lnet_userdata *)lua_newuserdata(L, sizeof(lnet_userdata));
  256. if(!skt){
  257. NODE_ERR("can't newudata\n");
  258. lua_pop(L, 1);
  259. return;
  260. }
  261. // set its metatable
  262. luaL_getmetatable(L, "net.socket");
  263. lua_setmetatable(L, -2);
  264. // pre-initialize it, in case of errors
  265. skt->self_ref = LUA_NOREF;
  266. lua_pushvalue(L, -1); // copy the top of stack
  267. skt->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); // ref to it self, for module api to find the userdata
  268. socket[i] = skt->self_ref; // save to socket array
  269. socket_num++;
  270. skt->cb_connect_ref = LUA_NOREF; // this socket already connected
  271. skt->cb_reconnect_ref = LUA_NOREF;
  272. skt->cb_disconnect_ref = LUA_NOREF;
  273. skt->cb_receive_ref = LUA_NOREF;
  274. skt->cb_send_ref = LUA_NOREF;
  275. skt->cb_dns_found_ref = LUA_NOREF;
  276. #ifdef CLIENT_SSL_ENABLE
  277. skt->secure = 0; // as a server SSL is not supported.
  278. #endif
  279. skt->pesp_conn = pesp_conn; // point to the espconn made by low level sdk
  280. pesp_conn->reverse = skt; // let espcon carray the info of this userdata(net.socket)
  281. espconn_regist_recvcb(pesp_conn, net_socket_received);
  282. espconn_regist_sentcb(pesp_conn, net_socket_sent);
  283. espconn_regist_disconcb(pesp_conn, net_server_disconnected);
  284. espconn_regist_reconcb(pesp_conn, net_server_reconnected);
  285. // now socket[i] has the client ref, and stack top has the userdata
  286. lua_call(L, 1, 0); // function(conn)
  287. }
  288. static void net_socket_connected(void *arg)
  289. {
  290. NODE_DBG("net_socket_connected is called.\n");
  291. struct espconn *pesp_conn = arg;
  292. if(pesp_conn == NULL)
  293. return;
  294. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  295. if(nud == NULL)
  296. return;
  297. // can receive and send data, even if there is no connected callback in lua.
  298. espconn_regist_recvcb(pesp_conn, net_socket_received);
  299. espconn_regist_sentcb(pesp_conn, net_socket_sent);
  300. espconn_regist_disconcb(pesp_conn, net_socket_disconnected);
  301. if(nud->cb_connect_ref == LUA_NOREF)
  302. return;
  303. if(nud->self_ref == LUA_NOREF)
  304. return;
  305. lua_State *L = lua_getstate();
  306. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
  307. lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
  308. lua_call(L, 1, 0);
  309. }
  310. // Lua: s = net.create(type, secure/timeout, function(conn))
  311. static int net_create( lua_State* L, const char* mt )
  312. {
  313. NODE_DBG("net_create is called.\n");
  314. struct espconn *pesp_conn = NULL;
  315. lnet_userdata *nud, *temp = NULL;
  316. unsigned type;
  317. #ifdef CLIENT_SSL_ENABLE
  318. unsigned secure = 0;
  319. #endif
  320. uint8_t stack = 1;
  321. bool isserver = false;
  322. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  323. isserver = true;
  324. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  325. isserver = false;
  326. else
  327. {
  328. NODE_DBG("wrong metatable for net_create.\n");
  329. return 0;
  330. }
  331. type = luaL_checkinteger( L, stack );
  332. if ( type != ESPCONN_TCP && type != ESPCONN_UDP )
  333. return luaL_error( L, "wrong arg type" );
  334. stack++;
  335. #ifdef CLIENT_SSL_ENABLE
  336. if(!isserver){
  337. if ( lua_isnumber(L, stack) )
  338. {
  339. secure = lua_tointeger(L, stack);
  340. stack++;
  341. if ( secure != 0 && secure != 1 ){
  342. return luaL_error( L, "wrong arg type" );
  343. }
  344. } else {
  345. secure = 0; // default to 0
  346. }
  347. }
  348. #endif
  349. if(isserver && type == ESPCONN_TCP){
  350. if ( lua_isnumber(L, stack) )
  351. {
  352. unsigned to = lua_tointeger(L, stack);
  353. stack++;
  354. if ( to < 1 || to > 28800 ){
  355. return luaL_error( L, "wrong arg type" );
  356. }
  357. tcp_server_timeover = (uint16_t)to;
  358. } else {
  359. tcp_server_timeover = 30; // default to 30
  360. }
  361. }
  362. // create a object
  363. nud = (lnet_userdata *)lua_newuserdata(L, sizeof(lnet_userdata));
  364. // pre-initialize it, in case of errors
  365. nud->self_ref = LUA_NOREF;
  366. nud->cb_connect_ref = LUA_NOREF;
  367. nud->cb_reconnect_ref = LUA_NOREF;
  368. nud->cb_disconnect_ref = LUA_NOREF;
  369. nud->cb_receive_ref = LUA_NOREF;
  370. nud->cb_send_ref = LUA_NOREF;
  371. nud->cb_dns_found_ref = LUA_NOREF;
  372. nud->pesp_conn = NULL;
  373. #ifdef CLIENT_SSL_ENABLE
  374. nud->secure = secure;
  375. #endif
  376. // set its metatable
  377. luaL_getmetatable(L, mt);
  378. lua_setmetatable(L, -2);
  379. // create the espconn struct
  380. if(isserver && type==ESPCONN_TCP && pTcpServer){
  381. if(tcpserver_cb_connect_ref != LUA_NOREF){ // self_ref should be unref in close()
  382. lua_pop(L,1);
  383. return luaL_error(L, "only one tcp server allowed");
  384. }
  385. pesp_conn = nud->pesp_conn = pTcpServer;
  386. } else if(isserver && type==ESPCONN_UDP && pUdpServer){
  387. temp = (lnet_userdata *)pUdpServer->reverse;
  388. if(temp && temp->self_ref != LUA_NOREF){
  389. lua_pop(L,1);
  390. return luaL_error(L, "only one udp server allowed");
  391. }
  392. pesp_conn = nud->pesp_conn = pUdpServer;
  393. } else {
  394. pesp_conn = nud->pesp_conn = (struct espconn *)c_zalloc(sizeof(struct espconn));
  395. if(!pesp_conn)
  396. return luaL_error(L, "not enough memory");
  397. pesp_conn->proto.tcp = NULL;
  398. pesp_conn->proto.udp = NULL;
  399. pesp_conn->reverse = NULL;
  400. if( type==ESPCONN_TCP )
  401. {
  402. pesp_conn->proto.tcp = (esp_tcp *)c_zalloc(sizeof(esp_tcp));
  403. if(!pesp_conn->proto.tcp){
  404. c_free(pesp_conn);
  405. pesp_conn = nud->pesp_conn = NULL;
  406. return luaL_error(L, "not enough memory");
  407. }
  408. NODE_DBG("TCP server/socket is set.\n");
  409. }
  410. else if( type==ESPCONN_UDP )
  411. {
  412. pesp_conn->proto.udp = (esp_udp *)c_zalloc(sizeof(esp_udp));
  413. if(!pesp_conn->proto.udp){
  414. c_free(pesp_conn);
  415. pesp_conn = nud->pesp_conn = NULL;
  416. return luaL_error(L, "not enough memory");
  417. }
  418. NODE_DBG("UDP server/socket is set.\n");
  419. }
  420. }
  421. pesp_conn->type = type;
  422. pesp_conn->state = ESPCONN_NONE;
  423. // reverse is for the callback function
  424. pesp_conn->reverse = nud;
  425. if(isserver && type==ESPCONN_TCP && pTcpServer==NULL){
  426. pTcpServer = pesp_conn;
  427. } else if(isserver && type==ESPCONN_UDP && pUdpServer==NULL){
  428. pUdpServer = pesp_conn;
  429. }
  430. // if call back function is specified, call it with para userdata
  431. // luaL_checkanyfunction(L, 2);
  432. if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
  433. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  434. lua_pushvalue(L, -2); // copy the self_ref(userdata) to the top
  435. lua_call(L, 1, 0);
  436. }
  437. return 1;
  438. }
  439. // static int net_close( lua_State* L, const char* mt );
  440. // Lua: net.delete( socket/server )
  441. // call close() first
  442. // server: disconnect server, unref everything
  443. // socket: unref everything
  444. static int net_delete( lua_State* L, const char* mt )
  445. {
  446. NODE_DBG("net_delete is called.\n");
  447. bool isserver = false;
  448. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  449. isserver = true;
  450. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  451. isserver = false;
  452. else
  453. {
  454. NODE_DBG("wrong metatable for net_delete.\n");
  455. return 0;
  456. }
  457. // net_close( L, mt ); // close it first
  458. lnet_userdata *nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  459. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  460. if(nud==NULL){
  461. NODE_DBG("userdata is nil.\n");
  462. return 0;
  463. }
  464. if(nud->pesp_conn){ // for client connected to tcp server, this should set NULL in disconnect cb
  465. nud->pesp_conn->reverse = NULL;
  466. if(!isserver) // socket is freed here
  467. {
  468. if(nud->pesp_conn->type == ESPCONN_UDP){
  469. if(nud->pesp_conn->proto.udp)
  470. c_free(nud->pesp_conn->proto.udp);
  471. nud->pesp_conn->proto.udp = NULL;
  472. } else if (nud->pesp_conn->type == ESPCONN_TCP) {
  473. if(nud->pesp_conn->proto.tcp)
  474. c_free(nud->pesp_conn->proto.tcp);
  475. nud->pesp_conn->proto.tcp = NULL;
  476. }
  477. c_free(nud->pesp_conn);
  478. }
  479. nud->pesp_conn = NULL; // for socket, it will free this when disconnected
  480. }
  481. // free (unref) callback ref
  482. if(LUA_NOREF!=nud->cb_connect_ref){
  483. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
  484. nud->cb_connect_ref = LUA_NOREF;
  485. }
  486. if(LUA_NOREF!=nud->cb_reconnect_ref){
  487. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_reconnect_ref);
  488. nud->cb_reconnect_ref = LUA_NOREF;
  489. }
  490. if(LUA_NOREF!=nud->cb_disconnect_ref){
  491. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
  492. nud->cb_disconnect_ref = LUA_NOREF;
  493. }
  494. if(LUA_NOREF!=nud->cb_receive_ref){
  495. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_receive_ref);
  496. nud->cb_receive_ref = LUA_NOREF;
  497. }
  498. if(LUA_NOREF!=nud->cb_send_ref){
  499. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
  500. nud->cb_send_ref = LUA_NOREF;
  501. }
  502. if(LUA_NOREF!=nud->cb_dns_found_ref){
  503. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
  504. nud->cb_dns_found_ref = LUA_NOREF;
  505. }
  506. lua_gc(L, LUA_GCSTOP, 0);
  507. if(LUA_NOREF!=nud->self_ref){
  508. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  509. nud->self_ref = LUA_NOREF;
  510. }
  511. lua_gc(L, LUA_GCRESTART, 0);
  512. return 0;
  513. }
  514. static void socket_connect(struct espconn *pesp_conn)
  515. {
  516. if(pesp_conn == NULL)
  517. return;
  518. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  519. if(nud == NULL)
  520. return;
  521. if( pesp_conn->type == ESPCONN_TCP )
  522. {
  523. #ifdef CLIENT_SSL_ENABLE
  524. if(nud->secure){
  525. espconn_secure_set_size(ESPCONN_CLIENT, 5120); /* set SSL buffer size */
  526. espconn_secure_connect(pesp_conn);
  527. }
  528. else
  529. #endif
  530. {
  531. espconn_connect(pesp_conn);
  532. }
  533. }
  534. else if (pesp_conn->type == ESPCONN_UDP)
  535. {
  536. espconn_create(pesp_conn);
  537. }
  538. NODE_DBG("socket_connect is called.\n");
  539. }
  540. static void socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg);
  541. static int dns_reconn_count = 0;
  542. static void socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  543. {
  544. NODE_DBG("socket_dns_found is called.\n");
  545. struct espconn *pesp_conn = arg;
  546. if(pesp_conn == NULL){
  547. NODE_DBG("pesp_conn null.\n");
  548. return;
  549. }
  550. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  551. if(nud == NULL)
  552. return;
  553. lua_State *L = lua_getstate();
  554. if(ipaddr == NULL)
  555. {
  556. dns_reconn_count++;
  557. if( dns_reconn_count >= 5 ){
  558. NODE_ERR( "DNS Fail!\n" );
  559. lua_gc(L, LUA_GCSTOP, 0);
  560. if(nud->self_ref != LUA_NOREF){
  561. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  562. nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
  563. }
  564. lua_gc(L, LUA_GCRESTART, 0);
  565. return;
  566. }
  567. NODE_ERR( "DNS retry %d!\n", dns_reconn_count );
  568. host_ip.addr = 0;
  569. espconn_gethostbyname(pesp_conn, name, &host_ip, socket_dns_found);
  570. return;
  571. }
  572. // ipaddr->addr is a uint32_t ip
  573. if(ipaddr->addr != 0)
  574. {
  575. dns_reconn_count = 0;
  576. if( pesp_conn->type == ESPCONN_TCP )
  577. {
  578. c_memcpy(pesp_conn->proto.tcp->remote_ip, &(ipaddr->addr), 4);
  579. NODE_DBG("TCP ip is set: ");
  580. NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr)));
  581. NODE_DBG("\n");
  582. }
  583. else if (pesp_conn->type == ESPCONN_UDP)
  584. {
  585. c_memcpy(pesp_conn->proto.udp->remote_ip, &(ipaddr->addr), 4);
  586. NODE_DBG("UDP ip is set: ");
  587. NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr)));
  588. NODE_DBG("\n");
  589. }
  590. socket_connect(pesp_conn);
  591. }
  592. }
  593. // Lua: server:listen( port, ip, function(con) )
  594. // Lua: socket:connect( port, ip, function(con) )
  595. static int net_start( lua_State* L, const char* mt )
  596. {
  597. NODE_DBG("net_start is called.\n");
  598. struct espconn *pesp_conn = NULL;
  599. lnet_userdata *nud;
  600. unsigned port;
  601. size_t il;
  602. bool isserver = false;
  603. ip_addr_t ipaddr;
  604. const char *domain;
  605. uint8_t stack = 1;
  606. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  607. isserver = true;
  608. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  609. isserver = false;
  610. else
  611. {
  612. NODE_DBG("wrong metatable for net_start.\n");
  613. return 0;
  614. }
  615. nud = (lnet_userdata *)luaL_checkudata(L, stack, mt);
  616. luaL_argcheck(L, nud, stack, "Server/Socket expected");
  617. stack++;
  618. if(nud==NULL){
  619. NODE_DBG("userdata is nil.\n");
  620. return 0;
  621. }
  622. if(nud->pesp_conn == NULL){
  623. NODE_DBG("nud->pesp_conn is NULL.\n");
  624. return 0;
  625. }
  626. pesp_conn = nud->pesp_conn;
  627. port = luaL_checkinteger( L, stack );
  628. stack++;
  629. if( pesp_conn->type == ESPCONN_TCP )
  630. {
  631. if(isserver)
  632. pesp_conn->proto.tcp->local_port = port;
  633. else{
  634. pesp_conn->proto.tcp->remote_port = port;
  635. pesp_conn->proto.tcp->local_port = espconn_port();
  636. }
  637. NODE_DBG("TCP port is set: %d.\n", port);
  638. }
  639. else if (pesp_conn->type == ESPCONN_UDP)
  640. {
  641. if(isserver)
  642. pesp_conn->proto.udp->local_port = port;
  643. else{
  644. pesp_conn->proto.udp->remote_port = port;
  645. pesp_conn->proto.udp->local_port = espconn_port();
  646. }
  647. NODE_DBG("UDP port is set: %d.\n", port);
  648. }
  649. if( lua_isstring(L,stack) ) // deal with the domain string
  650. {
  651. domain = luaL_checklstring( L, stack, &il );
  652. stack++;
  653. if (domain == NULL)
  654. {
  655. if(isserver)
  656. domain = "0.0.0.0";
  657. else
  658. domain = "127.0.0.1";
  659. }
  660. ipaddr.addr = ipaddr_addr(domain);
  661. if( pesp_conn->type == ESPCONN_TCP )
  662. {
  663. if(isserver)
  664. c_memcpy(pesp_conn->proto.tcp->local_ip, &ipaddr.addr, 4);
  665. else
  666. c_memcpy(pesp_conn->proto.tcp->remote_ip, &ipaddr.addr, 4);
  667. NODE_DBG("TCP ip is set: ");
  668. NODE_DBG(IPSTR, IP2STR(&ipaddr.addr));
  669. NODE_DBG("\n");
  670. }
  671. else if (pesp_conn->type == ESPCONN_UDP)
  672. {
  673. if(isserver)
  674. c_memcpy(pesp_conn->proto.udp->local_ip, &ipaddr.addr, 4);
  675. else
  676. c_memcpy(pesp_conn->proto.udp->remote_ip, &ipaddr.addr, 4);
  677. NODE_DBG("UDP ip is set: ");
  678. NODE_DBG(IPSTR, IP2STR(&ipaddr.addr));
  679. NODE_DBG("\n");
  680. }
  681. }
  682. // call back function when a connection is obtained, tcp only
  683. if ( pesp_conn->type == ESPCONN_TCP ) {
  684. if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
  685. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  686. if(isserver) // for tcp server connected callback
  687. {
  688. if(tcpserver_cb_connect_ref != LUA_NOREF)
  689. luaL_unref(L, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref);
  690. tcpserver_cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  691. }
  692. else
  693. {
  694. if(nud->cb_connect_ref != LUA_NOREF)
  695. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
  696. nud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  697. }
  698. }
  699. }
  700. if(!isserver || pesp_conn->type == ESPCONN_UDP){ // self_ref is only needed by socket userdata, or udp server
  701. lua_pushvalue(L, 1); // copy to the top of stack
  702. if(nud->self_ref != LUA_NOREF)
  703. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  704. nud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  705. }
  706. if( pesp_conn->type == ESPCONN_TCP )
  707. {
  708. if(isserver){ // no secure server support for now
  709. espconn_regist_connectcb(pesp_conn, net_server_connected);
  710. // tcp server, SSL is not supported
  711. #ifdef CLIENT_SSL_ENABLE
  712. // if(nud->secure)
  713. // espconn_secure_accept(pesp_conn);
  714. // else
  715. #endif
  716. espconn_accept(pesp_conn); // if it's a server, no need to dns.
  717. espconn_regist_time(pesp_conn, tcp_server_timeover, 0);
  718. }
  719. else{
  720. espconn_regist_connectcb(pesp_conn, net_socket_connected);
  721. espconn_regist_reconcb(pesp_conn, net_socket_reconnected);
  722. #ifdef CLIENT_SSL_ENABLE
  723. if(nud->secure){
  724. if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port)
  725. espconn_secure_disconnect(pesp_conn);
  726. // espconn_secure_connect(pesp_conn);
  727. }
  728. else
  729. #endif
  730. {
  731. if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port)
  732. espconn_disconnect(pesp_conn);
  733. // espconn_connect(pesp_conn);
  734. }
  735. }
  736. }
  737. else if (pesp_conn->type == ESPCONN_UDP)
  738. {
  739. espconn_regist_recvcb(pesp_conn, net_socket_received);
  740. espconn_regist_sentcb(pesp_conn, net_socket_sent);
  741. if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port)
  742. espconn_delete(pesp_conn);
  743. if(isserver)
  744. espconn_create(pesp_conn); // if it's a server, no need to dns.
  745. }
  746. if(!isserver){
  747. if((ipaddr.addr == IPADDR_NONE) && (c_memcmp(domain,"255.255.255.255",16) != 0))
  748. {
  749. host_ip.addr = 0;
  750. dns_reconn_count = 0;
  751. if(ESPCONN_OK == espconn_gethostbyname(pesp_conn, domain, &host_ip, socket_dns_found)){
  752. socket_dns_found(domain, &host_ip, pesp_conn); // ip is returned in host_ip.
  753. }
  754. }
  755. else
  756. {
  757. socket_connect(pesp_conn);
  758. }
  759. }
  760. return 0;
  761. }
  762. // Lua: server/socket:close()
  763. // server disconnect everything, unref everything
  764. // client disconnect and unref itself
  765. static int net_close( lua_State* L, const char* mt )
  766. {
  767. NODE_DBG("net_close is called.\n");
  768. bool isserver = false;
  769. int i = 0;
  770. lnet_userdata *nud = NULL, *skt = NULL;
  771. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  772. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  773. if(nud == NULL)
  774. return 0;
  775. if(nud->pesp_conn == NULL)
  776. return 0;
  777. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  778. isserver = true;
  779. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  780. isserver = false;
  781. else
  782. {
  783. NODE_DBG("wrong metatable for net_close.\n");
  784. return 0;
  785. }
  786. if(isserver && nud->pesp_conn->type == ESPCONN_TCP && tcpserver_cb_connect_ref != LUA_NOREF){
  787. luaL_unref(L, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref);
  788. tcpserver_cb_connect_ref = LUA_NOREF;
  789. }
  790. int n = lua_gettop(L);
  791. skt = nud;
  792. do{
  793. if(isserver && skt == NULL){
  794. if(socket[i] != LUA_NOREF){ // there is client socket exists
  795. lua_rawgeti(L, LUA_REGISTRYINDEX, socket[i]); // get the referenced user_data to stack top
  796. #if 0
  797. socket[i] = LUA_NOREF;
  798. socket_num--;
  799. #endif // do this in net_server_disconnected
  800. i++;
  801. if(lua_isuserdata(L,-1)){
  802. skt = lua_touserdata(L,-1);
  803. } else {
  804. lua_pop(L, 1);
  805. continue;
  806. }
  807. }else{
  808. // skip LUA_NOREF
  809. i++;
  810. continue;
  811. }
  812. }
  813. if(skt==NULL){
  814. NODE_DBG("userdata is nil.\n");
  815. continue;
  816. }
  817. if(skt->pesp_conn) // disconnect the connection
  818. {
  819. if(skt->pesp_conn->type == ESPCONN_TCP)
  820. {
  821. #ifdef CLIENT_SSL_ENABLE
  822. if(skt->secure){
  823. if(skt->pesp_conn->proto.tcp->remote_port || skt->pesp_conn->proto.tcp->local_port)
  824. espconn_secure_disconnect(skt->pesp_conn);
  825. }
  826. else
  827. #endif
  828. {
  829. if(skt->pesp_conn->proto.tcp->remote_port || skt->pesp_conn->proto.tcp->local_port)
  830. espconn_disconnect(skt->pesp_conn);
  831. }
  832. }else if(skt->pesp_conn->type == ESPCONN_UDP)
  833. {
  834. if(skt->pesp_conn->proto.tcp->remote_port || skt->pesp_conn->proto.tcp->local_port)
  835. espconn_delete(skt->pesp_conn);
  836. // a udp server/socket unref it self here. not in disconnect.
  837. if(LUA_NOREF!=skt->self_ref){ // for a udp self_ref is NOREF
  838. luaL_unref(L, LUA_REGISTRYINDEX, skt->self_ref);
  839. skt->self_ref = LUA_NOREF; // for a socket, now only var in lua is ref to the userdata
  840. }
  841. }
  842. }
  843. #if 0
  844. // unref the self_ref
  845. if(LUA_NOREF!=skt->self_ref){ // for a server self_ref is NOREF
  846. luaL_unref(L, LUA_REGISTRYINDEX, skt->self_ref);
  847. skt->self_ref = LUA_NOREF; // for a socket, now only var in lua is ref to the userdata
  848. }
  849. #endif
  850. lua_settop(L, n); // reset the stack top
  851. skt = NULL;
  852. } while( isserver && i<MAX_SOCKET);
  853. #if 0
  854. // unref the self_ref, for both socket and server
  855. if(LUA_NOREF!=nud->self_ref){ // for a server self_ref is NOREF
  856. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  857. nud->self_ref = LUA_NOREF; // now only var in lua is ref to the userdata
  858. }
  859. #endif
  860. return 0;
  861. }
  862. // Lua: socket/udpserver:on( "method", function(s) )
  863. static int net_on( lua_State* L, const char* mt )
  864. {
  865. NODE_DBG("net_on is called.\n");
  866. bool isserver = false;
  867. lnet_userdata *nud;
  868. size_t sl;
  869. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  870. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  871. if(nud==NULL){
  872. NODE_DBG("userdata is nil.\n");
  873. return 0;
  874. }
  875. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  876. isserver = true;
  877. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  878. isserver = false;
  879. else
  880. {
  881. NODE_DBG("wrong metatable for net_on.\n");
  882. return 0;
  883. }
  884. const char *method = luaL_checklstring( L, 2, &sl );
  885. if (method == NULL)
  886. return luaL_error( L, "wrong arg type" );
  887. luaL_checkanyfunction(L, 3);
  888. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  889. if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 10 && c_strcmp(method, "connection") == 0){
  890. if(nud->cb_connect_ref != LUA_NOREF)
  891. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
  892. nud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  893. }else if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 12 && c_strcmp(method, "reconnection") == 0){
  894. if(nud->cb_reconnect_ref != LUA_NOREF)
  895. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_reconnect_ref);
  896. nud->cb_reconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  897. }else if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 13 && c_strcmp(method, "disconnection") == 0){
  898. if(nud->cb_disconnect_ref != LUA_NOREF)
  899. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
  900. nud->cb_disconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  901. }else if((!isserver || nud->pesp_conn->type == ESPCONN_UDP) && sl == 7 && c_strcmp(method, "receive") == 0){
  902. if(nud->cb_receive_ref != LUA_NOREF)
  903. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_receive_ref);
  904. nud->cb_receive_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  905. }else if((!isserver || nud->pesp_conn->type == ESPCONN_UDP) && sl == 4 && c_strcmp(method, "sent") == 0){
  906. if(nud->cb_send_ref != LUA_NOREF)
  907. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
  908. nud->cb_send_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  909. }else if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 3 && c_strcmp(method, "dns") == 0){
  910. if(nud->cb_dns_found_ref != LUA_NOREF)
  911. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
  912. nud->cb_dns_found_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  913. }else{
  914. lua_pop(L, 1);
  915. return luaL_error( L, "method not supported" );
  916. }
  917. return 0;
  918. }
  919. // Lua: server/socket:send( string, function(sent) )
  920. static int net_send( lua_State* L, const char* mt )
  921. {
  922. // NODE_DBG("net_send is called.\n");
  923. bool isserver = false;
  924. struct espconn *pesp_conn = NULL;
  925. lnet_userdata *nud;
  926. size_t l;
  927. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  928. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  929. if(nud==NULL){
  930. NODE_DBG("userdata is nil.\n");
  931. return 0;
  932. }
  933. if(nud->pesp_conn == NULL){
  934. NODE_DBG("nud->pesp_conn is NULL.\n");
  935. return 0;
  936. }
  937. pesp_conn = nud->pesp_conn;
  938. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  939. isserver = true;
  940. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  941. isserver = false;
  942. else
  943. {
  944. NODE_DBG("wrong metatable for net_send.\n");
  945. return 0;
  946. }
  947. if(isserver && nud->pesp_conn->type == ESPCONN_TCP){
  948. return luaL_error( L, "tcp server send not supported" );
  949. }
  950. #if 0
  951. char temp[20] = {0};
  952. c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
  953. NODE_DBG("remote ");
  954. NODE_DBG(temp);
  955. NODE_DBG(":");
  956. NODE_DBG("%d",pesp_conn->proto.tcp->remote_port);
  957. NODE_DBG(" sending data.\n");
  958. #endif
  959. const char *payload = luaL_checklstring( L, 2, &l );
  960. if (l>1460 || payload == NULL)
  961. return luaL_error( L, "need <1460 payload" );
  962. if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION){
  963. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  964. if(nud->cb_send_ref != LUA_NOREF)
  965. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
  966. nud->cb_send_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  967. }
  968. // SDK 1.4.0 changed behaviour, for UDP server need to look up remote ip/port
  969. if (isserver && pesp_conn->type == ESPCONN_UDP)
  970. {
  971. remot_info *pr = 0;
  972. if (espconn_get_connection_info (pesp_conn, &pr, 0) != ESPCONN_OK)
  973. return luaL_error (L, "remote ip/port unavailable");
  974. pesp_conn->proto.udp->remote_port = pr->remote_port;
  975. os_memmove (pesp_conn->proto.udp->remote_ip, pr->remote_ip, 4);
  976. // The remot_info apparently should *not* be os_free()d, fyi
  977. }
  978. #ifdef CLIENT_SSL_ENABLE
  979. if(nud->secure)
  980. espconn_secure_sent(pesp_conn, (unsigned char *)payload, l);
  981. else
  982. #endif
  983. espconn_sent(pesp_conn, (unsigned char *)payload, l);
  984. return 0;
  985. }
  986. // Lua: socket:dns( string, function(socket, ip) )
  987. static int net_dns( lua_State* L, const char* mt )
  988. {
  989. NODE_DBG("net_dns is called.\n");
  990. bool isserver = false;
  991. struct espconn *pesp_conn = NULL;
  992. lnet_userdata *nud;
  993. size_t l;
  994. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  995. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  996. if(nud==NULL){
  997. NODE_DBG("userdata is nil.\n");
  998. return 0;
  999. }
  1000. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  1001. isserver = true;
  1002. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  1003. isserver = false;
  1004. else
  1005. {
  1006. NODE_DBG("wrong metatable for net_send.\n");
  1007. return 0;
  1008. }
  1009. if(nud->pesp_conn == NULL){
  1010. NODE_DBG("nud->pesp_conn is NULL.\n");
  1011. return 0;
  1012. }
  1013. pesp_conn = nud->pesp_conn;
  1014. if(!isserver || pesp_conn->type == ESPCONN_UDP){ // self_ref is only needed by socket userdata, or udp server
  1015. lua_pushvalue(L, 1); // copy to the top of stack
  1016. if(nud->self_ref != LUA_NOREF)
  1017. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  1018. nud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1019. }
  1020. const char *domain = luaL_checklstring( L, 2, &l );
  1021. if (l>128 || domain == NULL)
  1022. return luaL_error( L, "need <128 domain" );
  1023. if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION){
  1024. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  1025. if(nud->cb_dns_found_ref != LUA_NOREF)
  1026. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
  1027. nud->cb_dns_found_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1028. }
  1029. host_ip.addr = 0;
  1030. if(ESPCONN_OK == espconn_gethostbyname(pesp_conn, domain, &host_ip, net_dns_found))
  1031. net_dns_found(domain, &host_ip, pesp_conn); // ip is returned in host_ip.
  1032. return 0;
  1033. }
  1034. // Lua: net.dns.resolve( domain, function(ip) )
  1035. static int net_dns_static( lua_State* L )
  1036. {
  1037. const char *mt = "net.socket";
  1038. if (!lua_isstring( L, 1 ))
  1039. return luaL_error( L, "wrong parameter type (domain)" );
  1040. int rfunc = LUA_NOREF; //save reference to func
  1041. if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION){
  1042. rfunc = luaL_ref(L, LUA_REGISTRYINDEX);
  1043. }
  1044. int rdom = luaL_ref(L, LUA_REGISTRYINDEX); //save reference to domain
  1045. lua_settop(L,0); //empty stack
  1046. lua_getfield(L, LUA_GLOBALSINDEX, "net");
  1047. lua_getfield(L, -1, "createConnection");
  1048. lua_remove(L, -2); //remove "net" from stack
  1049. lua_pushinteger(L, UDP); // we are going to create a new dummy UDP socket
  1050. lua_call(L,1,1);// after this the stack should have a socket
  1051. lua_rawgeti(L, LUA_REGISTRYINDEX, rdom); // load domain back to the stack
  1052. lua_rawgeti(L, LUA_REGISTRYINDEX, rfunc); // load the callback function back to the stack
  1053. luaL_unref(L, LUA_REGISTRYINDEX, rdom); //free reference
  1054. luaL_unref(L, LUA_REGISTRYINDEX, rfunc); //free reference
  1055. bool isserver = false;
  1056. struct espconn *pesp_conn = NULL;
  1057. lnet_userdata *nud;
  1058. size_t l;
  1059. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  1060. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  1061. if(nud==NULL){
  1062. NODE_DBG("userdata is nil.\n");
  1063. return 0;
  1064. }
  1065. if(nud->pesp_conn == NULL){
  1066. NODE_DBG("nud->pesp_conn is NULL.\n");
  1067. return 0;
  1068. }
  1069. pesp_conn = nud->pesp_conn;
  1070. lua_pushvalue(L, 1); // copy to the top of stack
  1071. if(nud->self_ref != LUA_NOREF)
  1072. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  1073. nud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1074. const char *domain = luaL_checklstring( L, 2, &l );
  1075. if (l>128 || domain == NULL)
  1076. return luaL_error( L, "need <128 domain" );
  1077. if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION){
  1078. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  1079. if(nud->cb_dns_found_ref != LUA_NOREF)
  1080. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
  1081. nud->cb_dns_found_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1082. }
  1083. host_ip.addr = 0;
  1084. if(ESPCONN_OK == espconn_gethostbyname(pesp_conn, domain, &host_ip, net_dns_found))
  1085. net_dns_found(domain, &host_ip, pesp_conn); // ip is returned in host_ip.
  1086. return 0;
  1087. }
  1088. // Lua: s = net.createServer(type, function(server))
  1089. static int net_createServer( lua_State* L )
  1090. {
  1091. const char *mt = "net.server";
  1092. return net_create(L, mt);
  1093. }
  1094. // Lua: server:delete()
  1095. static int net_server_delete( lua_State* L )
  1096. {
  1097. const char *mt = "net.server";
  1098. return net_delete(L, mt);
  1099. }
  1100. // Lua: server:listen( port, ip )
  1101. static int net_server_listen( lua_State* L )
  1102. {
  1103. const char *mt = "net.server";
  1104. return net_start(L, mt);
  1105. }
  1106. // Lua: server:close()
  1107. static int net_server_close( lua_State* L )
  1108. {
  1109. const char *mt = "net.server";
  1110. return net_close(L, mt);
  1111. }
  1112. // Lua: udpserver:on( "method", function(udpserver) )
  1113. static int net_udpserver_on( lua_State* L )
  1114. {
  1115. const char *mt = "net.server";
  1116. return net_on(L, mt);
  1117. }
  1118. // Lua: udpserver:send(string, function() )
  1119. static int net_udpserver_send( lua_State* L )
  1120. {
  1121. const char *mt = "net.server";
  1122. return net_send(L, mt);;
  1123. }
  1124. // Lua: s = net.createConnection(type, function(conn))
  1125. static int net_createConnection( lua_State* L )
  1126. {
  1127. const char *mt = "net.socket";
  1128. return net_create(L, mt);
  1129. }
  1130. // Lua: socket:delete()
  1131. static int net_socket_delete( lua_State* L )
  1132. {
  1133. const char *mt = "net.socket";
  1134. return net_delete(L, mt);
  1135. }
  1136. // Lua: socket:connect( port, ip )
  1137. static int net_socket_connect( lua_State* L )
  1138. {
  1139. const char *mt = "net.socket";
  1140. return net_start(L, mt);
  1141. }
  1142. // Lua: socket:close()
  1143. static int net_socket_close( lua_State* L )
  1144. {
  1145. const char *mt = "net.socket";
  1146. return net_close(L, mt);
  1147. }
  1148. // Lua: socket:on( "method", function(socket) )
  1149. static int net_socket_on( lua_State* L )
  1150. {
  1151. const char *mt = "net.socket";
  1152. return net_on(L, mt);
  1153. }
  1154. // Lua: socket:send( string, function() )
  1155. static int net_socket_send( lua_State* L )
  1156. {
  1157. const char *mt = "net.socket";
  1158. return net_send(L, mt);
  1159. }
  1160. static int net_socket_hold( lua_State* L )
  1161. {
  1162. const char *mt = "net.socket";
  1163. struct espconn *pesp_conn = NULL;
  1164. lnet_userdata *nud;
  1165. size_t l;
  1166. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  1167. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  1168. if(nud==NULL){
  1169. NODE_DBG("userdata is nil.\n");
  1170. return 0;
  1171. }
  1172. if(nud->pesp_conn == NULL){
  1173. NODE_DBG("nud->pesp_conn is NULL.\n");
  1174. return 0;
  1175. }
  1176. pesp_conn = nud->pesp_conn;
  1177. espconn_recv_hold(pesp_conn);
  1178. return 0;
  1179. }
  1180. static int net_socket_unhold( lua_State* L )
  1181. {
  1182. const char *mt = "net.socket";
  1183. struct espconn *pesp_conn = NULL;
  1184. lnet_userdata *nud;
  1185. size_t l;
  1186. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  1187. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  1188. if(nud==NULL){
  1189. NODE_DBG("userdata is nil.\n");
  1190. return 0;
  1191. }
  1192. if(nud->pesp_conn == NULL){
  1193. NODE_DBG("nud->pesp_conn is NULL.\n");
  1194. return 0;
  1195. }
  1196. pesp_conn = nud->pesp_conn;
  1197. espconn_recv_unhold(pesp_conn);
  1198. return 0;
  1199. }
  1200. // Lua: ip,port = sk:getpeer()
  1201. static int net_socket_getpeer( lua_State* L )
  1202. {
  1203. lnet_userdata *nud;
  1204. const char *mt = "net.socket";
  1205. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  1206. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  1207. if(nud!=NULL && nud->pesp_conn!=NULL ){
  1208. char temp[20] = {0};
  1209. c_sprintf(temp, IPSTR, IP2STR( &(nud->pesp_conn->proto.tcp->remote_ip) ) );
  1210. if ( nud->pesp_conn->proto.tcp->remote_port != 0 ) {
  1211. lua_pushstring( L, temp );
  1212. lua_pushinteger( L, nud->pesp_conn->proto.tcp->remote_port );
  1213. } else {
  1214. lua_pushnil( L );
  1215. lua_pushnil( L );
  1216. }
  1217. } else {
  1218. lua_pushnil( L );
  1219. lua_pushnil( L );
  1220. }
  1221. return 2;
  1222. }
  1223. // Lua: socket:dns( string, function(ip) )
  1224. static int net_socket_dns( lua_State* L )
  1225. {
  1226. const char *mt = "net.socket";
  1227. return net_dns(L, mt);
  1228. }
  1229. static int net_multicastJoinLeave( lua_State *L, int join)
  1230. {
  1231. size_t il;
  1232. ip_addr_t multicast_addr;
  1233. ip_addr_t if_addr;
  1234. const char *multicast_ip;
  1235. const char *if_ip;
  1236. NODE_DBG("net_multicastJoin is called.\n");
  1237. if(! lua_isstring(L,1) ) return luaL_error( L, "wrong arg type" );
  1238. if_ip = luaL_checklstring( L, 1, &il );
  1239. if (if_ip != NULL)
  1240. if ( if_ip[0] == '\0' || stricmp(if_ip,"any") == 0)
  1241. {
  1242. if_ip = "0.0.0.0";
  1243. il = 7;
  1244. }
  1245. if (if_ip == NULL || il > 15 || il < 7) return luaL_error( L, "invalid if ip" );
  1246. if_addr.addr = ipaddr_addr(if_ip);
  1247. if(! lua_isstring(L,2) ) return luaL_error( L, "wrong arg type" );
  1248. multicast_ip = luaL_checklstring( L, 2, &il );
  1249. if (multicast_ip == NULL || il > 15 || il < 7) return luaL_error( L, "invalid multicast ip" );
  1250. multicast_addr.addr = ipaddr_addr(multicast_ip);
  1251. if (join)
  1252. {
  1253. espconn_igmp_join(&if_addr, &multicast_addr);
  1254. }
  1255. else
  1256. {
  1257. espconn_igmp_leave(&if_addr, &multicast_addr);
  1258. }
  1259. return 0;
  1260. }
  1261. // Lua: net.multicastJoin(ifip, multicastip)
  1262. // if ifip "" or "any" all interfaces are affected
  1263. static int net_multicastJoin( lua_State* L )
  1264. {
  1265. return net_multicastJoinLeave(L,1);
  1266. }
  1267. // Lua: net.multicastLeave(ifip, multicastip)
  1268. // if ifip "" or "any" all interfaces are affected
  1269. static int net_multicastLeave( lua_State* L )
  1270. {
  1271. return net_multicastJoinLeave(L,0);
  1272. }
  1273. // Returns NULL on success, error message otherwise
  1274. static const char *append_pem_blob(const char *pem, const char *type, uint8_t **buffer_p, uint8_t *buffer_limit, const char *name) {
  1275. char unb64[256];
  1276. memset(unb64, 0xff, sizeof(unb64));
  1277. int i;
  1278. for (i = 0; i < 64; i++) {
  1279. unb64["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
  1280. }
  1281. if (!pem) {
  1282. return "No PEM blob";
  1283. }
  1284. // Scan for -----BEGIN CERT
  1285. pem = strstr(pem, "-----BEGIN ");
  1286. if (!pem) {
  1287. return "No PEM header";
  1288. }
  1289. if (strncmp(pem + 11, type, strlen(type))) {
  1290. return "Wrong PEM type";
  1291. }
  1292. pem = strchr(pem, '\n');
  1293. if (!pem) {
  1294. return "Incorrect PEM format";
  1295. }
  1296. //
  1297. // Base64 encoded data starts here
  1298. // Get all the base64 data into a single buffer....
  1299. // We will use the back end of the buffer....
  1300. //
  1301. uint8_t *buffer = *buffer_p;
  1302. uint8_t *dest = buffer + 32 + 2; // Leave space for name and length
  1303. int bitcount = 0;
  1304. int accumulator = 0;
  1305. for (; *pem && dest < buffer_limit; pem++) {
  1306. int val = unb64[*(uint8_t*) pem];
  1307. if (val & 0xC0) {
  1308. // not a base64 character
  1309. if (isspace(*(uint8_t*) pem)) {
  1310. continue;
  1311. }
  1312. if (*pem == '=') {
  1313. // just ignore -- at the end
  1314. bitcount = 0;
  1315. continue;
  1316. }
  1317. if (*pem == '-') {
  1318. break;
  1319. }
  1320. return "Invalid character in PEM";
  1321. } else {
  1322. bitcount += 6;
  1323. accumulator = (accumulator << 6) + val;
  1324. if (bitcount >= 8) {
  1325. bitcount -= 8;
  1326. *dest++ = accumulator >> bitcount;
  1327. }
  1328. }
  1329. }
  1330. if (dest >= buffer_limit || strncmp(pem, "-----END ", 9) || strncmp(pem + 9, type, strlen(type)) || bitcount) {
  1331. return "Invalid PEM format data";
  1332. }
  1333. size_t len = dest - (buffer + 32 + 2);
  1334. memset(buffer, 0, 32);
  1335. strcpy(buffer, name);
  1336. buffer[32] = len & 0xff;
  1337. buffer[33] = (len >> 8) & 0xff;
  1338. *buffer_p = dest;
  1339. return NULL;
  1340. }
  1341. static const char *fill_page_with_pem(lua_State *L, const unsigned char *flash_memory, int flash_offset, const char **types, const char **names)
  1342. {
  1343. uint8_t *buffer = luaM_malloc(L, INTERNAL_FLASH_SECTOR_SIZE);
  1344. uint8_t *buffer_base = buffer;
  1345. uint8_t *buffer_limit = buffer + INTERNAL_FLASH_SECTOR_SIZE;
  1346. int argno;
  1347. for (argno = 1; argno <= lua_gettop(L) && types[argno - 1]; argno++) {
  1348. const char *pem = lua_tostring(L, argno);
  1349. const char *error = append_pem_blob(pem, types[argno - 1], &buffer, buffer_limit, names[argno - 1]);
  1350. if (error) {
  1351. luaM_free(L, buffer_base);
  1352. return error;
  1353. }
  1354. }
  1355. memset(buffer, 0xff, buffer_limit - buffer);
  1356. // Lets see if it matches what is already there....
  1357. if (c_memcmp(buffer_base, flash_memory, INTERNAL_FLASH_SECTOR_SIZE) != 0) {
  1358. // Starts being dangerous
  1359. if (platform_flash_erase_sector(flash_offset / INTERNAL_FLASH_SECTOR_SIZE) != PLATFORM_OK) {
  1360. luaM_free(L, buffer_base);
  1361. return "Failed to erase sector";
  1362. }
  1363. if (platform_s_flash_write(buffer_base, flash_offset, INTERNAL_FLASH_SECTOR_SIZE) != INTERNAL_FLASH_SECTOR_SIZE) {
  1364. luaM_free(L, buffer_base);
  1365. return "Failed to write sector";
  1366. }
  1367. // ends being dangerous
  1368. }
  1369. luaM_free(L, buffer_base);
  1370. return NULL;
  1371. }
  1372. // Lua: net.cert.auth(true / false | PEM data [, PEM data] )
  1373. static int net_cert_auth(lua_State *L)
  1374. {
  1375. int enable;
  1376. uint32_t flash_offset = platform_flash_mapped2phys((uint32_t) &net_client_cert_area[0]);
  1377. if ((flash_offset & 0xfff) || flash_offset > 0xff000 || INTERNAL_FLASH_SECTOR_SIZE != 0x1000) {
  1378. // THis should never happen
  1379. return luaL_error( L, "bad offset" );
  1380. }
  1381. if (lua_type(L, 1) == LUA_TSTRING) {
  1382. const char *types[3] = { "CERTIFICATE", "RSA PRIVATE KEY", NULL };
  1383. const char *names[2] = { "certificate", "private_key" };
  1384. const char *error = fill_page_with_pem(L, &net_client_cert_area[0], flash_offset, types, names);
  1385. if (error) {
  1386. return luaL_error(L, error);
  1387. }
  1388. enable = 1;
  1389. } else {
  1390. enable = lua_toboolean(L, 1);
  1391. }
  1392. bool rc;
  1393. if (enable) {
  1394. // See if there is a cert there
  1395. if (net_client_cert_area[0] == 0x00 || net_client_cert_area[0] == 0xff) {
  1396. return luaL_error( L, "no certificates found" );
  1397. }
  1398. rc = espconn_secure_cert_req_enable(1, flash_offset / INTERNAL_FLASH_SECTOR_SIZE);
  1399. } else {
  1400. rc = espconn_secure_cert_req_disable(1);
  1401. }
  1402. lua_pushboolean(L, rc);
  1403. return 1;
  1404. }
  1405. // Lua: net.cert.verify(true / false | PEM data [, PEM data] )
  1406. static int net_cert_verify(lua_State *L)
  1407. {
  1408. int enable;
  1409. uint32_t flash_offset = platform_flash_mapped2phys((uint32_t) &net_server_cert_area[0]);
  1410. if ((flash_offset & 0xfff) || flash_offset > 0xff000 || INTERNAL_FLASH_SECTOR_SIZE != 0x1000) {
  1411. // THis should never happen
  1412. return luaL_error( L, "bad offset" );
  1413. }
  1414. if (lua_type(L, 1) == LUA_TSTRING) {
  1415. const char *types[2] = { "CERTIFICATE", NULL };
  1416. const char *names[1] = { "certificate" };
  1417. const char *error = fill_page_with_pem(L, &net_server_cert_area[0], flash_offset, types, names);
  1418. if (error) {
  1419. return luaL_error(L, error);
  1420. }
  1421. enable = 1;
  1422. } else {
  1423. enable = lua_toboolean(L, 1);
  1424. }
  1425. bool rc;
  1426. if (enable) {
  1427. // See if there is a cert there
  1428. if (net_server_cert_area[0] == 0x00 || net_server_cert_area[0] == 0xff) {
  1429. return luaL_error( L, "no certificates found" );
  1430. }
  1431. rc = espconn_secure_ca_enable(1, flash_offset / INTERNAL_FLASH_SECTOR_SIZE);
  1432. } else {
  1433. rc = espconn_secure_ca_disable(1);
  1434. }
  1435. lua_pushboolean(L, rc);
  1436. return 1;
  1437. }
  1438. // Lua: s = net.dns.setdnsserver(ip_addr, [index])
  1439. static int net_setdnsserver( lua_State* L )
  1440. {
  1441. size_t l;
  1442. u32_t ip32;
  1443. const char *server = luaL_checklstring( L, 1, &l );
  1444. if (l>16 || server == NULL || (ip32 = ipaddr_addr(server)) == IPADDR_NONE || ip32 == IPADDR_ANY)
  1445. return luaL_error( L, "invalid dns server ip" );
  1446. int numdns = luaL_optint(L, 2, 0);
  1447. if (numdns >= DNS_MAX_SERVERS)
  1448. return luaL_error( L, "server index out of range [0-%d]", DNS_MAX_SERVERS - 1);
  1449. ip_addr_t ipaddr;
  1450. ip4_addr_set_u32(&ipaddr, ip32);
  1451. dns_setserver(numdns,&ipaddr);
  1452. return 0;
  1453. }
  1454. // Lua: s = net.dns.getdnsserver([index])
  1455. static int net_getdnsserver( lua_State* L )
  1456. {
  1457. int numdns = luaL_optint(L, 1, 0);
  1458. if (numdns >= DNS_MAX_SERVERS)
  1459. return luaL_error( L, "server index out of range [0-%d]", DNS_MAX_SERVERS - 1);
  1460. // ip_addr_t ipaddr;
  1461. // dns_getserver(numdns,&ipaddr);
  1462. // Bug fix by @md5crypt https://github.com/nodemcu/nodemcu-firmware/pull/500
  1463. ip_addr_t ipaddr = dns_getserver(numdns);
  1464. if ( ip_addr_isany(&ipaddr) ) {
  1465. lua_pushnil( L );
  1466. } else {
  1467. char temp[20] = {0};
  1468. c_sprintf(temp, IPSTR, IP2STR( &ipaddr.addr ) );
  1469. lua_pushstring( L, temp );
  1470. }
  1471. return 1;
  1472. }
  1473. #if 0
  1474. static int net_array_index( lua_State* L )
  1475. {
  1476. char** parray = luaL_checkudata(L, 1, "net.array");
  1477. int index = luaL_checkint(L, 2);
  1478. lua_pushnumber(L, (*parray)[index-1]);
  1479. return 1;
  1480. }
  1481. static int net_array_newindex( lua_State* L )
  1482. {
  1483. char** parray = luaL_checkudata(L, 1, "net.array");
  1484. int index = luaL_checkint(L, 2);
  1485. int value = luaL_checkint(L, 3);
  1486. (*parray)[index-1] = value;
  1487. return 0;
  1488. }
  1489. // expose an array to lua, by storing it in a userdata with the array metatable
  1490. static int expose_array(lua_State* L, char *array, unsigned short len) {
  1491. char** parray = lua_newuserdata(L, len);
  1492. *parray = array;
  1493. luaL_getmetatable(L, "net.array");
  1494. lua_setmetatable(L, -2);
  1495. return 1;
  1496. }
  1497. #endif
  1498. // Module function map
  1499. static const LUA_REG_TYPE net_server_map[] = {
  1500. { LSTRKEY( "listen" ), LFUNCVAL( net_server_listen ) },
  1501. { LSTRKEY( "close" ), LFUNCVAL( net_server_close ) },
  1502. { LSTRKEY( "on" ), LFUNCVAL( net_udpserver_on ) },
  1503. { LSTRKEY( "send" ), LFUNCVAL( net_udpserver_send ) },
  1504. //{ LSTRKEY( "delete" ), LFUNCVAL( net_server_delete ) },
  1505. { LSTRKEY( "__gc" ), LFUNCVAL( net_server_delete ) },
  1506. { LSTRKEY( "__index" ), LROVAL( net_server_map ) },
  1507. { LNILKEY, LNILVAL }
  1508. };
  1509. static const LUA_REG_TYPE net_socket_map[] = {
  1510. { LSTRKEY( "connect" ), LFUNCVAL( net_socket_connect ) },
  1511. { LSTRKEY( "close" ), LFUNCVAL( net_socket_close ) },
  1512. { LSTRKEY( "on" ), LFUNCVAL( net_socket_on ) },
  1513. { LSTRKEY( "send" ), LFUNCVAL( net_socket_send ) },
  1514. { LSTRKEY( "hold" ), LFUNCVAL( net_socket_hold ) },
  1515. { LSTRKEY( "unhold" ), LFUNCVAL( net_socket_unhold ) },
  1516. { LSTRKEY( "dns" ), LFUNCVAL( net_socket_dns ) },
  1517. { LSTRKEY( "getpeer" ), LFUNCVAL( net_socket_getpeer ) },
  1518. //{ LSTRKEY( "delete" ), LFUNCVAL( net_socket_delete ) },
  1519. { LSTRKEY( "__gc" ), LFUNCVAL( net_socket_delete ) },
  1520. { LSTRKEY( "__index" ), LROVAL( net_socket_map ) },
  1521. { LNILKEY, LNILVAL }
  1522. };
  1523. #if 0
  1524. static const LUA_REG_TYPE net_array_map[] = {
  1525. { LSTRKEY( "__index" ), LFUNCVAL( net_array_index ) },
  1526. { LSTRKEY( "__newindex" ), LFUNCVAL( net_array_newindex ) },
  1527. { LNILKEY, LNILVAL }
  1528. };
  1529. #endif
  1530. static const LUA_REG_TYPE net_cert_map[] = {
  1531. { LSTRKEY( "verify" ), LFUNCVAL( net_cert_verify ) },
  1532. #ifdef CLIENT_SSL_CERT_AUTH_ENABLE
  1533. { LSTRKEY( "auth" ), LFUNCVAL( net_cert_auth ) },
  1534. #endif
  1535. { LNILKEY, LNILVAL }
  1536. };
  1537. static const LUA_REG_TYPE net_dns_map[] = {
  1538. { LSTRKEY( "setdnsserver" ), LFUNCVAL( net_setdnsserver ) },
  1539. { LSTRKEY( "getdnsserver" ), LFUNCVAL( net_getdnsserver ) },
  1540. { LSTRKEY( "resolve" ), LFUNCVAL( net_dns_static ) },
  1541. { LNILKEY, LNILVAL }
  1542. };
  1543. static const LUA_REG_TYPE net_map[] = {
  1544. { LSTRKEY( "createServer" ), LFUNCVAL( net_createServer ) },
  1545. { LSTRKEY( "createConnection" ), LFUNCVAL( net_createConnection ) },
  1546. { LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) },
  1547. { LSTRKEY( "multicastLeave"), LFUNCVAL( net_multicastLeave ) },
  1548. { LSTRKEY( "dns" ), LROVAL( net_dns_map ) },
  1549. #ifdef CLIENT_SSL_ENABLE
  1550. { LSTRKEY( "cert" ), LROVAL(net_cert_map) },
  1551. #endif
  1552. { LSTRKEY( "TCP" ), LNUMVAL( TCP ) },
  1553. { LSTRKEY( "UDP" ), LNUMVAL( UDP ) },
  1554. { LSTRKEY( "__metatable" ), LROVAL( net_map ) },
  1555. { LNILKEY, LNILVAL }
  1556. };
  1557. int luaopen_net( lua_State *L ) {
  1558. int i;
  1559. for(i=0;i<MAX_SOCKET;i++)
  1560. {
  1561. socket[i] = LUA_NOREF;
  1562. }
  1563. luaL_rometatable(L, "net.server", (void *)net_server_map); // create metatable for net.server
  1564. luaL_rometatable(L, "net.socket", (void *)net_socket_map); // create metatable for net.socket
  1565. #if 0
  1566. luaL_rometatable(L, "net.array", (void *)net_array_map); // create metatable for net.array
  1567. #endif
  1568. return 0;
  1569. }
  1570. NODEMCU_MODULE(NET, "net", net_map, luaopen_net);