luat_lib_ulwip.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. @module ulwip
  3. @summary 用户空间的lwip集成(开发中)
  4. @version 1.0
  5. @date 2024.1.22
  6. @auther wendal
  7. @tag LUAT_USE_ULWIP
  8. @usage
  9. --[[
  10. 注意: 本库处于开发中, 接口随时可能变化
  11. 用户空间的LWIP集成, 用于支持lwip的netif的网络集成, 实现在lua代码中直接控制MAC包/IP包的收发
  12. 总体数据路径如下
  13. lua代码 -> ulwip.input -> lwip(netif->input) -> lwip处理逻辑 -> luatos socket框架
  14. lua代码 <- ulwip回调函数 <- lwip(netif->low_level_output) <- lwip处理逻辑 <- luatos socket框架
  15. 应用示例:
  16. 1. Air601的wifi模块作为被控端, 通过UART/SPI收发MAC包, 实现Air780E/Air780EP集成wifi模块的功能
  17. 2. 使用W5500/CH395/ENC28J60等以太网模块, 在用户lua代码中控制其mac包收发, 并集成到luatos socket框架中
  18. 3. 通过蓝牙模块,集成lowpan6
  19. ]]
  20. */
  21. #include "luat_base.h"
  22. #include "luat_ulwip.h"
  23. #include "luat_crypto.h"
  24. #define LUAT_LOG_TAG "ulwip"
  25. #include "luat_log.h"
  26. static ulwip_ctx_t nets[USERLWIP_NET_COUNT];
  27. extern struct netif *netif_default;
  28. // 搜索adpater_index对应的netif
  29. static struct netif* find_netif(uint8_t adapter_index) {
  30. struct netif *netif = NULL;
  31. for (size_t i = 0; i < USERLWIP_NET_COUNT; i++)
  32. {
  33. if (nets[i].adapter_index == adapter_index)
  34. {
  35. netif = nets[i].netif;
  36. break;
  37. }
  38. }
  39. return netif;
  40. }
  41. static int find_index(uint8_t adapter_index) {
  42. for (size_t i = 0; i < USERLWIP_NET_COUNT; i++)
  43. {
  44. if (nets[i].adapter_index == adapter_index)
  45. {
  46. return i;
  47. }
  48. }
  49. return -1;
  50. }
  51. static int netif_ip_event_cb(lua_State *L, void* ptr) {
  52. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  53. lua_getglobal(L, "sys_pub");
  54. char buff[32] = {0};
  55. ulwip_ctx_t* ctx = (ulwip_ctx_t*)ptr;
  56. if (lua_isfunction(L, -1)) {
  57. if (msg->arg2) {
  58. lua_pushstring(L, "IP_READY");
  59. ipaddr_ntoa_r(&ctx->netif->ip_addr, buff, 32);
  60. LLOGD("IP_READY %d %s", ctx->adapter_index, buff);
  61. lua_pushstring(L, buff);
  62. lua_pushinteger(L, ctx->adapter_index);
  63. lua_call(L, 3, 0);
  64. }
  65. else {
  66. lua_pushstring(L, "IP_LOSE");
  67. LLOGD("IP_LOSE %d", ctx->adapter_index);
  68. lua_pushinteger(L, ctx->adapter_index);
  69. lua_call(L, 2, 0);
  70. }
  71. }
  72. return 0;
  73. }
  74. int ulwip_netif_ip_event(ulwip_ctx_t* ctx) {
  75. struct netif* netif = ctx->netif;
  76. int ready_now = !ip_addr_isany(&netif->ip_addr);
  77. ready_now &= netif_is_link_up(netif);
  78. ready_now &= netif_is_up(netif);
  79. net_lwip2_set_link_state(ctx->adapter_index, ready_now);
  80. if (ctx->ip_ready == ready_now) {
  81. return 0;
  82. }
  83. ctx->ip_ready = ready_now;
  84. rtos_msg_t msg = {0};
  85. msg.arg1 = ctx->adapter_index;
  86. msg.arg2 = ready_now;
  87. msg.ptr = ctx;
  88. msg.handler = netif_ip_event_cb;
  89. luat_msgbus_put(&msg, 0);
  90. return 0;
  91. }
  92. // 回调函数, 用于lwip的netif输出数据
  93. static int netif_output_cb(lua_State *L, void* ptr) {
  94. // LLOGD("netif_output_cb");
  95. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  96. int idx = find_index(msg->arg2);
  97. if (idx < 0 || nets[idx].netif == NULL) {
  98. LLOGE("非法的适配器索引号 %d", msg->arg2);
  99. return 0;
  100. }
  101. lua_geti(L, LUA_REGISTRYINDEX, nets[idx].output_lua_ref);
  102. if (lua_isfunction(L, -1)) {
  103. lua_pushinteger(L, msg->arg2);
  104. if (nets[idx].use_zbuff_out) {
  105. luat_zbuff_t* buff = lua_newuserdata(L, sizeof(luat_zbuff_t));
  106. if (buff == NULL)
  107. {
  108. LLOGE("malloc failed for netif_output_cb");
  109. return 0;
  110. }
  111. memset(buff, 0, sizeof(luat_zbuff_t));
  112. buff->addr = ptr;
  113. buff->len = msg->arg1;
  114. buff->used = msg->arg1;
  115. luaL_setmetatable(L, LUAT_ZBUFF_TYPE);
  116. }
  117. else {
  118. lua_pushlstring(L, (const char*)ptr, msg->arg1);
  119. luat_heap_free(ptr);
  120. }
  121. lua_call(L, 2, 0);
  122. }
  123. else {
  124. // LLOGD("不是回调函数 %d", nets[msg->arg2].output_lua_ref);
  125. luat_heap_free(ptr);
  126. }
  127. return 0;
  128. }
  129. static err_t netif_output(struct netif *netif, struct pbuf *p) {
  130. // LLOGD("lwip待发送数据 %p %d", p, p->tot_len);
  131. rtos_msg_t msg = {0};
  132. msg.handler = netif_output_cb;
  133. msg.arg1 = p->tot_len;
  134. msg.arg2 = -1;
  135. for (size_t i = 0; i < USERLWIP_NET_COUNT; i++)
  136. {
  137. if (nets[i].netif == netif)
  138. {
  139. msg.arg2 = nets[i].adapter_index;
  140. break;
  141. }
  142. }
  143. if (msg.arg2 < 0) {
  144. LLOGE("netif_output %p not found", netif);
  145. return ERR_IF;
  146. }
  147. msg.ptr = luat_heap_malloc(p->tot_len);
  148. if (msg.ptr == NULL)
  149. {
  150. LLOGE("malloc %d failed for netif_output", p->tot_len);
  151. return ERR_MEM;
  152. }
  153. size_t offset = 0;
  154. do {
  155. memcpy((char*)msg.ptr + offset, p->payload, p->len);
  156. offset += p->len;
  157. p = p->next;
  158. } while (p);
  159. luat_msgbus_put(&msg, 0);
  160. return 0;
  161. }
  162. static err_t luat_netif_init(struct netif *netif) {
  163. for (size_t i = 0; i < USERLWIP_NET_COUNT; i++)
  164. {
  165. if (nets[i].netif == netif)
  166. {
  167. LLOGD("netif init %d %p", nets[i].adapter_index, netif);
  168. netif->linkoutput = netif_output;
  169. netif->output = ulwip_etharp_output;
  170. #if LWIP_IPV6
  171. netif->output_ip6 = ethip6_output;
  172. #endif
  173. netif->mtu = nets[i].mtu;
  174. netif->flags = nets[i].flags;
  175. memcpy(netif->hwaddr, nets[i].hwaddr, ETH_HWADDR_LEN);
  176. netif->hwaddr_len = ETH_HWADDR_LEN;
  177. return 0;
  178. }
  179. }
  180. return ERR_IF;
  181. }
  182. /*
  183. 初始化lwip netif
  184. @api ulwip.setup(adapter_index, mac, output_lua_ref, opts)
  185. @int adapter_index 适配器编号
  186. @string mac 网卡mac地址
  187. @function output_lua_ref 回调函数, 参数为(adapter_index, data)
  188. @table 额外参数, 例如 {mtu=1500, flags=(ulwip.FLAG_BROADCAST | ulwip.FLAG_ETHARP)}
  189. @return boolean 成功与否
  190. @usage
  191. -- 初始化一个适配器, 并设置回调函数
  192. ulwip.setup(socket.LWIP_STA, string.fromHex("18fe34a27b69"), function(adapter_index, data)
  193. log.info("ulwip", "output_lua_ref", adapter_index, data:toHex())
  194. end)
  195. -- 注意, setup之后, netif的状态是down, 调用ulwip.updown(adapter_index, true)后, 才能正常收发数据
  196. -- 额外参数配置table可选值
  197. -- mtu, 默认1460
  198. -- flags, 默认 ulwip.FLAG_BROADCAST | ulwip.FLAG_ETHARP | ulwip.FLAG_ETHERNET | ulwip.FLAG_IGMP | ulwip.FLAG_MLD6
  199. -- zbuff_out 回调函数接受zbuff作为参数, 默认false
  200. -- reverse 本地lwip设备,翻转调用逻辑, 默认false, 这个参数是为了拦截当前设备的硬件联网数据所设计的
  201. */
  202. static int l_ulwip_setup(lua_State *L) {
  203. // 必须有适配器编号
  204. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  205. // 设置MAC地址,必须的
  206. const char* mac = luaL_checkstring(L, 2);
  207. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY)
  208. {
  209. LLOGE("非法的adapter_index %d", adapter_index);
  210. return 0;
  211. }
  212. if (!lua_isfunction(L, 3)) {
  213. LLOGE("output_lua_ref must be a function");
  214. return 0;
  215. }
  216. uint16_t mtu = 1460;
  217. uint8_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
  218. uint16_t zbuff_out = 0;
  219. uint8_t reverse = 0;
  220. if (lua_istable(L, 4)) {
  221. lua_getfield(L, 4, "mtu");
  222. if (lua_isinteger(L, -1)) {
  223. mtu = (uint16_t)luaL_checkinteger(L, -1);
  224. }
  225. lua_pop(L, 1);
  226. lua_getfield(L, 4, "flags");
  227. if (lua_isinteger(L, -1)) {
  228. flags = (uint8_t)luaL_checkinteger(L, -1);
  229. }
  230. lua_pop(L, 1);
  231. lua_getfield(L, 4, "zbuff_out");
  232. if (lua_isboolean(L, -1)) {
  233. zbuff_out = lua_toboolean(L, -1);
  234. if (zbuff_out) {
  235. LLOGD("使用zbuff作为netif out的回调函数");
  236. }
  237. }
  238. lua_pop(L, 1);
  239. lua_getfield(L, 4, "reverse");
  240. if (lua_isinteger(L, -1)) {
  241. reverse = (uint8_t)luaL_checkinteger(L, -1);
  242. }
  243. lua_pop(L, 1);
  244. }
  245. struct netif *netif = NULL;
  246. struct netif *tmp = NULL;
  247. for (size_t i = 0; i < USERLWIP_NET_COUNT; i++)
  248. {
  249. if (nets[i].netif == NULL)
  250. {
  251. if (reverse) {
  252. #if defined(CHIP_EC718) || defined(CHIP_EC618) || defined(CHIP_EC716)
  253. extern struct netif * net_lwip_get_netif(uint8_t adapter_index);
  254. netif = net_lwip_get_netif(adapter_index);
  255. #else
  256. netif = netif_default;
  257. #endif
  258. if (netif == NULL) {
  259. LLOGE("当前设备的netif不存在,无法启用reverse参数!!");
  260. return 0;
  261. }
  262. nets[i].adapter_index = adapter_index;
  263. nets[i].netif = netif;
  264. nets[i].mtu = netif->mtu; // 其实没有意义
  265. nets[i].flags = netif->flags;
  266. nets[i].use_zbuff_out = zbuff_out;
  267. nets[i].reverse = reverse;
  268. lua_pushvalue(L, 3);
  269. memcpy(nets[i].hwaddr, netif->hwaddr, ETH_HWADDR_LEN);
  270. nets[i].output_lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  271. lua_pushboolean(L, 1);
  272. return 1;
  273. }
  274. netif = luat_heap_malloc(sizeof(struct netif));
  275. if (netif) {
  276. memset(netif, 0, sizeof(struct netif));
  277. nets[i].adapter_index = adapter_index;
  278. nets[i].netif = netif;
  279. nets[i].mtu = mtu;
  280. nets[i].flags = flags;
  281. nets[i].use_zbuff_out = zbuff_out;
  282. nets[i].reverse = reverse;
  283. lua_pushvalue(L, 3);
  284. memcpy(nets[i].hwaddr, mac, ETH_HWADDR_LEN);
  285. nets[i].output_lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  286. break;
  287. }
  288. }
  289. }
  290. if (netif == NULL)
  291. {
  292. LLOGE("没有空余的netif了");
  293. return 0;
  294. }
  295. // 已经分配netif, 继续初始化
  296. tmp = netif_add(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4, NULL, luat_netif_init, netif_input);
  297. netif->name[0] = 'u';
  298. netif->name[1] = 's';
  299. if (NULL == tmp) {
  300. LLOGE("netif_add 返回异常!!!");
  301. }
  302. #if LWIP_IPV6
  303. netif_create_ip6_linklocal_address(netif, 1);
  304. netif->ip6_autoconfig_enabled = 1;
  305. #endif
  306. net_lwip2_set_netif(adapter_index, netif);
  307. lua_pushboolean(L, 1);
  308. return 1;
  309. }
  310. /*
  311. 设置netif的状态
  312. @api ulwip.updown(adapter_index, up)
  313. @int adapter_index 适配器编号
  314. @boolean up true为up, false为down
  315. @return boolean 成功与否
  316. @usage
  317. -- 参考ulwip.setup
  318. */
  319. static int l_ulwip_updown(lua_State *L) {
  320. // 必须有适配器编号
  321. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  322. int idx = find_index(adapter_index);
  323. if (idx < 0) {
  324. LLOGE("没有找到netif");
  325. return 0;
  326. }
  327. if (lua_isboolean(L, 2)) {
  328. if (lua_toboolean(L, 2)) {
  329. netif_set_up(nets[idx].netif);
  330. }
  331. else {
  332. netif_set_down(nets[idx].netif);
  333. }
  334. ulwip_netif_ip_event(&nets[idx]);
  335. }
  336. lua_pushboolean(L, netif_is_up(nets[idx].netif));
  337. return 1;
  338. }
  339. /*
  340. 设置netif的物理链路状态
  341. @api ulwip.link(adapter_index, up)
  342. @int adapter_index 适配器编号
  343. @boolean up true为up, false为down
  344. @return boolean 当前状态
  345. @usage
  346. -- 参考ulwip.setup
  347. */
  348. static int l_ulwip_link(lua_State *L) {
  349. // 必须有适配器编号
  350. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  351. int idx = find_index(adapter_index);
  352. if (idx < 0) {
  353. LLOGE("没有找到netif");
  354. return 0;
  355. }
  356. if (lua_isboolean(L, 2))
  357. {
  358. if (lua_toboolean(L, 2))
  359. {
  360. netif_set_link_up(nets[idx].netif);
  361. }
  362. else {
  363. netif_set_link_down(nets[idx].netif);
  364. }
  365. ulwip_netif_ip_event(&nets[idx]);
  366. }
  367. lua_pushboolean(L, netif_is_link_up(nets[idx].netif));
  368. return 1;
  369. }
  370. static void netif_input_cb(void *ptr) {
  371. netif_cb_ctx_t* cb_ctx = (netif_cb_ctx_t*)ptr;
  372. if (cb_ctx->ctx->reverse) {
  373. if (ERR_OK != cb_ctx->ctx->netif->output(cb_ctx->ctx->netif, cb_ctx->p, NULL)) {
  374. LLOGW("ctx->netif->input 失败 %d", cb_ctx->p->tot_len);
  375. pbuf_free(cb_ctx->p);
  376. }
  377. }
  378. else {
  379. if (ERR_OK != cb_ctx->ctx->netif->input(cb_ctx->p, cb_ctx->ctx->netif)) {
  380. LLOGW("ctx->netif->input 失败 %d", cb_ctx->p->tot_len);
  381. pbuf_free(cb_ctx->p);
  382. }
  383. }
  384. luat_heap_free(cb_ctx);
  385. }
  386. /*
  387. 往netif输入数据
  388. @api ulwip.input(adapter_index, data)
  389. @int adapter_index 适配器编号
  390. @string data 输入的数据
  391. @return boolean 成功与否
  392. @usage
  393. -- 参考ulwip.setup
  394. */
  395. static int l_ulwip_input(lua_State *L) {
  396. // 必须有适配器编号
  397. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  398. int ret = 0;
  399. struct pbuf *q = NULL;
  400. const char* data = NULL;
  401. size_t len = 0;
  402. size_t offset = 0;
  403. int idx = find_index(adapter_index);
  404. if (idx < 0) {
  405. LLOGE("没有找到netif %d", adapter_index);
  406. return 0;
  407. }
  408. if (lua_type(L, 2) == LUA_TSTRING)
  409. {
  410. data = luaL_checklstring(L, 2, &len);
  411. }
  412. else if (lua_type(L, 2) == LUA_TUSERDATA) {
  413. luat_zbuff_t* zb = (luat_zbuff_t*)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE);
  414. data = (const char*)zb->addr;
  415. if (lua_isinteger(L, 3)) {
  416. len = luaL_checkinteger(L, 3);
  417. offset = luaL_checkinteger(L, 4);
  418. data += offset;
  419. }
  420. else {
  421. len = zb->used;
  422. }
  423. }
  424. else {
  425. LLOGE("未知的数据格式, 当前仅支持zbuff和string");
  426. return 0;
  427. }
  428. // LLOGD("输入的mac帧 %d %02X:%02X:%02X:%02X:%02X:%02X", len, data[0], data[1], data[2], data[3], data[4], data[5]);
  429. struct pbuf *p = pbuf_alloc(PBUF_RAW, (uint16_t)len, PBUF_RAM);
  430. if (p == NULL) {
  431. LLOGE("pbuf_alloc failed");
  432. return 0;
  433. }
  434. for (q = p; q != NULL; q = q->next) {
  435. memcpy(q->payload, data, q->len);
  436. data += q->len;
  437. }
  438. #if NO_SYS
  439. if (nets[idx].reverse) {
  440. ret = nets[idx].netif->output(p, nets[idx].netif, NULL);
  441. }
  442. else {
  443. ret = nets[idx].netif->input(p, nets[idx].netif);
  444. }
  445. #else
  446. netif_cb_ctx_t* cb_ctx = (netif_cb_ctx_t*)luat_heap_malloc(sizeof(netif_cb_ctx_t));
  447. if (cb_ctx == NULL) {
  448. LLOGE("netif->input ret %d", ret);
  449. LWIP_DEBUGF(NETIF_DEBUG, ("l_ulwip_input: IP input error\n"));
  450. pbuf_free(p);
  451. return 0;
  452. }
  453. memset(cb_ctx, 0, sizeof(netif_cb_ctx_t));
  454. cb_ctx->ctx = &nets[idx];
  455. cb_ctx->p = p;
  456. ret = tcpip_callback(netif_input_cb, cb_ctx);
  457. if(ret != ERR_OK) {
  458. luat_heap_free(cb_ctx);
  459. cb_ctx = NULL;
  460. }
  461. #endif
  462. if(ret != ERR_OK) {
  463. LLOGE("netif->input ret %d", ret);
  464. LWIP_DEBUGF(NETIF_DEBUG, ("l_ulwip_input: IP input error\n"));
  465. pbuf_free(p);
  466. return 0;
  467. }
  468. lua_pushboolean(L, 1);
  469. return 1;
  470. }
  471. /*
  472. 启动或关闭dhcp
  473. @api ulwip.dhcp(adapter_index, up)
  474. @int adapter_index 适配器编号
  475. @boolean up true为启动, false为关闭
  476. @return boolean 当前状态
  477. @usage
  478. -- 参考ulwip.setup
  479. */
  480. static int l_ulwip_dhcp(lua_State *L) {
  481. // 必须有适配器编号
  482. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  483. struct netif* netif = find_netif(adapter_index);
  484. if (netif == NULL) {
  485. LLOGE("没有找到netif");
  486. return 0;
  487. }
  488. int dhcp_enable = lua_toboolean(L, 2);
  489. int i = find_index(adapter_index);
  490. if (i < 0)
  491. {
  492. LLOGE("没有找到adapter_index %d", adapter_index);
  493. return 0;
  494. }
  495. nets[i].dhcp_enable = dhcp_enable;
  496. if (dhcp_enable) {
  497. nets[i].ip_static = 0;
  498. ulwip_dhcp_client_start(&nets[i]);
  499. }
  500. else {
  501. ulwip_dhcp_client_stop(&nets[i]);
  502. }
  503. lua_pushboolean(L, 1);
  504. return 1;
  505. }
  506. /*
  507. 设置或获取ip信息
  508. @api ulwip.ip(adapter_index, ip, netmask, gw)
  509. @int adapter_index 适配器编号
  510. @string ip IP地址, 仅获取时可以不填
  511. @string netmask 子网掩码, 仅获取时可以不填
  512. @string gw 网关地址, 仅获取时可以不填
  513. @return string ip地址, 子网掩码, 网关地址
  514. @usage
  515. -- 获取现有值
  516. local ip, netmask, gw = ulwip.ip(socket.LWIP_STA)
  517. -- 设置新值
  518. ulwip.ip(socket.LWIP_STA, "192.168.0.1", "255.255.255.0", "192.168.0.1")
  519. */
  520. static int l_ulwip_ip(lua_State *L) {
  521. const char* tmp = NULL;
  522. // 必须有适配器编号
  523. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  524. struct netif* netif = find_netif(adapter_index);
  525. if (netif == NULL) {
  526. LLOGE("没有找到netif %d", adapter_index);
  527. return 0;
  528. }
  529. if (lua_type(L, 2) == LUA_TSTRING)
  530. {
  531. tmp = luaL_checkstring(L, 2);
  532. ipaddr_aton(tmp, &netif->ip_addr);
  533. tmp = luaL_checkstring(L, 3);
  534. ipaddr_aton(tmp, &netif->netmask);
  535. tmp = luaL_checkstring(L, 4);
  536. ipaddr_aton(tmp, &netif->gw);
  537. int idx = find_index(adapter_index);
  538. nets[idx].ip_static = !ip_addr_isany(&netif->ip_addr);
  539. }
  540. // 反馈IP信息
  541. tmp = ip_ntoa(&netif->ip_addr);
  542. lua_pushstring(L, tmp);
  543. tmp = ip_ntoa(&netif->netmask);
  544. lua_pushstring(L, tmp);
  545. tmp = ip_ntoa(&netif->gw);
  546. lua_pushstring(L, tmp);
  547. return 3;
  548. }
  549. /*
  550. 将netif注册到luatos socket中
  551. @api ulwip.reg(adapter_index)
  552. @int adapter_index 适配器编号
  553. @return boolean 成功与否
  554. @usage
  555. -- 参考ulwip.setup
  556. */
  557. static int l_ulwip_reg(lua_State *L) {
  558. // 必须有适配器编号
  559. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  560. struct netif* netif = find_netif(adapter_index);
  561. if (netif == NULL) {
  562. LLOGE("没有找到netif %d", adapter_index);
  563. return 0;
  564. }
  565. net_lwip2_register_adapter(adapter_index);
  566. lua_pushboolean(L, 1);
  567. return 1;
  568. }
  569. extern struct netif * net_lwip_get_netif(uint8_t adapter_index);
  570. #include "rotable2.h"
  571. static const rotable_Reg_t reg_ulwip[] =
  572. {
  573. { "input" , ROREG_FUNC(l_ulwip_input)},
  574. { "setup" , ROREG_FUNC(l_ulwip_setup)},
  575. { "updown" , ROREG_FUNC(l_ulwip_updown)},
  576. { "link" , ROREG_FUNC(l_ulwip_link)},
  577. { "dhcp" , ROREG_FUNC(l_ulwip_dhcp)},
  578. { "ip" , ROREG_FUNC(l_ulwip_ip)},
  579. { "reg" , ROREG_FUNC(l_ulwip_reg)},
  580. // 网卡FLAGS,默认
  581. // NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6
  582. // @const FLAG_BROADCAST number 支持广播
  583. { "FLAG_BROADCAST", ROREG_INT(NETIF_FLAG_BROADCAST)},
  584. // @const FLAG_ETHARP number 支持ARP
  585. { "FLAG_ETHARP", ROREG_INT(NETIF_FLAG_ETHARP)},
  586. // @const FLAG_ETHERNET number 以太网模式
  587. { "FLAG_ETHERNET", ROREG_INT(NETIF_FLAG_ETHERNET)},
  588. // @const FLAG_IGMP number 支持IGMP
  589. { "FLAG_IGMP", ROREG_INT(NETIF_FLAG_IGMP)},
  590. // @const FLAG_MLD6 number 支持_MLD6
  591. { "FLAG_MLD6", ROREG_INT(NETIF_FLAG_MLD6)},
  592. { NULL, ROREG_INT(0)}
  593. };
  594. LUAMOD_API int luaopen_ulwip( lua_State *L ) {
  595. luat_newlib2(L, reg_ulwip);
  596. return 1;
  597. }
  598. // 针对EC618/EC7xx平台的IP包输入回调
  599. err_t ulwip_ip_input_cb(struct pbuf *p, struct netif *inp) {
  600. for (size_t i = 0; i < NW_ADAPTER_INDEX_LWIP_NETIF_QTY; i++)
  601. {
  602. if (nets[i].netif == inp)
  603. {
  604. // TODO 过滤一下当前能处理的包
  605. // 回调到lua中
  606. netif_output(inp, p);
  607. break;
  608. }
  609. }
  610. // LLOGD("收到IP数据包(len=%d)", p->tot_len);
  611. // u8_t ipVersion;
  612. // ipVersion = IP_HDR_GET_VERSION(p->payload);
  613. // if (ipVersion == 4) {
  614. // // 解析出里面的协议内容
  615. // struct ip_hdr *ip = (struct ip_hdr *)p->payload;
  616. // u16_t udpPort = 0;
  617. // if (ip->_proto == IP_PROTO_UDP) {
  618. // // UDP协议
  619. // struct udp_hdr *udp = (struct udp_hdr *)((char*)p->payload + sizeof(struct ip_hdr));
  620. // udpPort = lwip_ntohs(udp->dest);
  621. // }
  622. // else if (ip->_proto == IP_PROTO_TCP) {
  623. // // TCP协议
  624. // struct tcp_hdr *tcp = (struct tcp_hdr *)((char*)p->payload + sizeof(struct ip_hdr));
  625. // udpPort = lwip_ntohs(tcp->dest);
  626. // }
  627. // // else {
  628. // // // 其他协议
  629. // // LLOGD("IP协议版本 %d, 协议 %d, 源端口 %d, 目的端口 %d", ipVersion, IP_HDR_GET_PROTO(ip), lwip_ntohs(ip->src.addr), udpPort);
  630. // // }
  631. // char buff[32] = {0};
  632. // ip4addr_ntoa_r(&ip->src, buff, 32);
  633. // LLOGD("IP协议版本 %d, 协议 %d, 源IP %s, 目的端口 %d", ipVersion, ip->_proto, buff, udpPort);
  634. // }
  635. return ERR_OK;
  636. }