net.c 51 KB

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