luat_lib_ulwip.c 19 KB

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