luat_lib_ulwip.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. #define LUAT_LOG_TAG "ulwip"
  24. #include "luat_log.h"
  25. static ulwip_ctx_t nets[USERLWIP_NET_COUNT];
  26. static void dhcp_client_cb(void *arg);
  27. static err_t ulwip_etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr);
  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. // 回调函数, 用于lwip的netif输出数据
  52. static int netif_output_cb(lua_State *L, void* ptr) {
  53. // LLOGD("netif_output_cb");
  54. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  55. int idx = find_index(msg->arg2);
  56. if (idx < 0 || nets[idx].netif == NULL) {
  57. LLOGE("非法的适配器索引号 %d", msg->arg2);
  58. return 0;
  59. }
  60. lua_geti(L, LUA_REGISTRYINDEX, nets[idx].output_lua_ref);
  61. if (lua_isfunction(L, -1)) {
  62. lua_pushinteger(L, msg->arg2);
  63. if (nets[idx].use_zbuff_out) {
  64. luat_zbuff_t* buff = lua_newuserdata(L, sizeof(luat_zbuff_t));
  65. if (buff == NULL)
  66. {
  67. LLOGE("malloc failed for netif_output_cb");
  68. return 0;
  69. }
  70. memset(buff, 0, sizeof(luat_zbuff_t));
  71. buff->addr = ptr;
  72. buff->len = msg->arg1;
  73. buff->used = msg->arg1;
  74. luaL_setmetatable(L, LUAT_ZBUFF_TYPE);
  75. }
  76. else {
  77. lua_pushlstring(L, (const char*)ptr, msg->arg1);
  78. luat_heap_free(ptr);
  79. }
  80. lua_call(L, 2, 0);
  81. }
  82. else {
  83. // LLOGD("不是回调函数 %d", nets[msg->arg2].output_lua_ref);
  84. luat_heap_free(ptr);
  85. }
  86. return 0;
  87. }
  88. static err_t netif_output(struct netif *netif, struct pbuf *p) {
  89. // LLOGD("lwip待发送数据 %p %d", p, p->tot_len);
  90. rtos_msg_t msg = {0};
  91. msg.handler = netif_output_cb;
  92. msg.arg1 = p->tot_len;
  93. msg.arg2 = -1;
  94. for (size_t i = 0; i < USERLWIP_NET_COUNT; i++)
  95. {
  96. if (nets[i].netif == netif)
  97. {
  98. msg.arg2 = nets[i].adapter_index;
  99. break;
  100. }
  101. }
  102. if (msg.arg2 < 0) {
  103. LLOGE("netif_output %p not found", netif);
  104. return ERR_IF;
  105. }
  106. msg.ptr = luat_heap_malloc(p->tot_len);
  107. if (msg.ptr == NULL)
  108. {
  109. LLOGE("malloc %d failed for netif_output", p->tot_len);
  110. return ERR_MEM;
  111. }
  112. size_t offset = 0;
  113. do {
  114. memcpy((char*)msg.ptr + offset, p->payload, p->len);
  115. offset += p->len;
  116. p = p->next;
  117. } while (p);
  118. luat_msgbus_put(&msg, 0);
  119. return 0;
  120. }
  121. #ifdef TLS_CONFIG_CPU_XT804
  122. static void netif_status_callback(struct netif *netif, unsigned char stat) {
  123. (void)stat;
  124. #else
  125. static void netif_status_callback(struct netif *netif) {
  126. #endif
  127. LLOGD("netif status changed %s", ip4addr_ntoa(netif_ip4_addr(netif)));
  128. for (size_t i = 0; i < USERLWIP_NET_COUNT; i++)
  129. {
  130. if (nets[i].netif == netif)
  131. {
  132. if (!ip_addr_isany(&netif->ip_addr)) {
  133. LLOGD("设置网络状态为UP %d", nets[i].adapter_index);
  134. net_lwip2_set_link_state(nets[i].adapter_index, 1);
  135. }
  136. else {
  137. LLOGD("设置网络状态为DOWN %d", nets[i].adapter_index);
  138. net_lwip2_set_link_state(nets[i].adapter_index, 0);
  139. }
  140. break;
  141. }
  142. }
  143. }
  144. static err_t luat_netif_init(struct netif *netif) {
  145. for (size_t i = 0; i < USERLWIP_NET_COUNT; i++)
  146. {
  147. if (nets[i].netif == netif)
  148. {
  149. LLOGD("netif init %d %p", nets[i].adapter_index, netif);
  150. netif->linkoutput = netif_output;
  151. netif->output = ulwip_etharp_output;
  152. #if LWIP_IPV6
  153. netif->output_ip6 = ethip6_output;
  154. #endif
  155. netif->mtu = nets[i].mtu;
  156. netif->flags = nets[i].flags;
  157. memcpy(netif->hwaddr, nets[i].hwaddr, ETH_HWADDR_LEN);
  158. netif->hwaddr_len = ETH_HWADDR_LEN;
  159. return 0;
  160. }
  161. }
  162. return ERR_IF;
  163. }
  164. /*
  165. 初始化lwip netif
  166. @api ulwip.setup(adapter_index, mac, output_lua_ref)
  167. @int adapter_index 适配器编号
  168. @string mac 网卡mac地址
  169. @function output_lua_ref 回调函数, 参数为(adapter_index, data)
  170. @table 额外参数, 例如 {mtu=1500, flags=(ulwip.FLAG_BROADCAST | ulwip.FLAG_ETHARP |)}
  171. @return boolean 成功与否
  172. @usage
  173. -- 初始化一个适配器, 并设置回调函数
  174. ulwip.setup(socket.LWIP_STA, string.fromHex("18fe34a27b69"), function(adapter_index, data)
  175. log.info("ulwip", "output_lua_ref", adapter_index, data:toHex())
  176. end)
  177. -- 注意, setup之后, netif的状态是down, 调用ulwip.updown(adapter_index, true)后, 才能正常收发数据
  178. -- 额外参数配置table可选值
  179. -- mtu, 默认1460
  180. -- flags, 默认 ulwip.FLAG_BROADCAST | ulwip.FLAG_ETHARP | ulwip.FLAG_ETHERNET | ulwip.FLAG_IGMP | ulwip.FLAG_MLD6
  181. -- 即如下格式 {mtu=1460, flags=(ulwip.FLAG_BROADCAST | ulwip.FLAG_ETHARP | ulwip.FLAG_ETHERNET | ulwip.FLAG_IGMP | ulwip.FLAG_MLD6)}
  182. */
  183. static int l_ulwip_setup(lua_State *L) {
  184. // 必须有适配器编号
  185. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  186. // 设置MAC地址,必须的
  187. const char* mac = luaL_checkstring(L, 2);
  188. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY)
  189. {
  190. LLOGE("非法的adapter_index %d", adapter_index);
  191. return 0;
  192. }
  193. if (!lua_isfunction(L, 3)) {
  194. LLOGE("output_lua_ref must be a function");
  195. return 0;
  196. }
  197. uint16_t mtu = 1460;
  198. uint8_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
  199. uint16_t zbuff_out = 0;
  200. if (lua_istable(L, 4)) {
  201. lua_getfield(L, 4, "mtu");
  202. if (lua_isinteger(L, -1)) {
  203. mtu = (uint16_t)luaL_checkinteger(L, -1);
  204. }
  205. lua_pop(L, 1);
  206. lua_getfield(L, 4, "flags");
  207. if (lua_isinteger(L, -1)) {
  208. flags = (uint8_t)luaL_checkinteger(L, -1);
  209. }
  210. lua_pop(L, 1);
  211. lua_getfield(L, 4, "zbuff_out");
  212. if (lua_isboolean(L, -1)) {
  213. zbuff_out = lua_toboolean(L, -1);
  214. if (zbuff_out) {
  215. LLOGD("使用zbuff作为netif out的回调函数");
  216. }
  217. }
  218. lua_pop(L, 1);
  219. }
  220. struct netif *netif = NULL;
  221. struct netif *tmp = NULL;
  222. for (size_t i = 0; i < USERLWIP_NET_COUNT; i++)
  223. {
  224. if (nets[i].netif == NULL)
  225. {
  226. netif = luat_heap_malloc(sizeof(struct netif));
  227. if (netif) {
  228. memset(netif, 0, sizeof(struct netif));
  229. nets[i].adapter_index = adapter_index;
  230. nets[i].netif = netif;
  231. nets[i].mtu = mtu;
  232. nets[i].flags = flags;
  233. nets[i].use_zbuff_out = zbuff_out;
  234. lua_pushvalue(L, 3);
  235. memcpy(nets[i].hwaddr, mac, ETH_HWADDR_LEN);
  236. nets[i].output_lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  237. break;
  238. }
  239. }
  240. }
  241. if (netif == NULL)
  242. {
  243. LLOGE("没有空余的netif了");
  244. return 0;
  245. }
  246. // 已经分配netif, 继续初始化
  247. // #if defined(TYPE_EC718P)
  248. // net_lwip2_set_netif(adapter_index, netif, luat_netif_init, 0);
  249. // #else
  250. tmp = netif_add(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4, NULL, luat_netif_init, netif_input);
  251. netif->name[0] = 'u';
  252. netif->name[1] = 's';
  253. LLOGD("netif_add 返回值 %p", tmp);
  254. #if LWIP_IPV6
  255. netif_create_ip6_linklocal_address(netif, 1);
  256. netif->ip6_autoconfig_enabled = 1;
  257. #endif
  258. // #endif
  259. #if LWIP_NETIF_STATUS_CALLBACK
  260. //LLOGD("支持netif_status_callback");
  261. netif_set_status_callback(netif, netif_status_callback);
  262. #else
  263. LLOGD("不支持netif_status_callback");
  264. #endif
  265. // #if defined(TYPE_EC718P)
  266. // nothing
  267. // #else
  268. net_lwip2_set_netif(adapter_index, netif);
  269. // #endif
  270. lua_pushboolean(L, 1);
  271. return 1;
  272. }
  273. /*
  274. 设置netif的状态
  275. @api ulwip.updown(adapter_index, up)
  276. @int adapter_index 适配器编号
  277. @boolean up true为up, false为down
  278. @return boolean 成功与否
  279. @usage
  280. -- 参考ulwip.setup
  281. */
  282. static int l_ulwip_updown(lua_State *L) {
  283. // 必须有适配器编号
  284. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  285. struct netif* netif = find_netif(adapter_index);
  286. if (netif == NULL) {
  287. LLOGE("没有找到netif");
  288. return 0;
  289. }
  290. if (lua_isboolean(L, 2)) {
  291. if (lua_toboolean(L, 2)) {
  292. netif_set_up(netif);
  293. }
  294. else {
  295. netif_set_down(netif);
  296. }
  297. }
  298. lua_pushboolean(L, netif_is_up(netif));
  299. return 1;
  300. }
  301. /*
  302. 设置netif的物理链路状态
  303. @api ulwip.link(adapter_index, up)
  304. @int adapter_index 适配器编号
  305. @boolean up true为up, false为down
  306. @return boolean 当前状态
  307. @usage
  308. -- 参考ulwip.setup
  309. */
  310. static int l_ulwip_link(lua_State *L) {
  311. // 必须有适配器编号
  312. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  313. struct netif* netif = find_netif(adapter_index);
  314. if (netif == NULL) {
  315. LLOGE("没有找到netif");
  316. return 0;
  317. }
  318. if (lua_isboolean(L, 2))
  319. {
  320. if (lua_toboolean(L, 2))
  321. {
  322. netif_set_link_up(netif);
  323. }
  324. else {
  325. netif_set_link_down(netif);
  326. }
  327. #if LWIP_NETIF_STATUS_CALLBACK == 0
  328. netif_status_callback(netif);
  329. #endif
  330. }
  331. lua_pushboolean(L, netif_is_link_up(netif));
  332. return 1;
  333. }
  334. static void netif_input_cb(void *ptr) {
  335. netif_cb_ctx_t* ctx = (netif_cb_ctx_t*)ptr;
  336. ctx->netif->input(ctx->p, ctx->netif);
  337. luat_heap_free(ctx);
  338. }
  339. /*
  340. 往netif输入数据
  341. @api ulwip.input(adapter_index, data)
  342. @int adapter_index 适配器编号
  343. @string data 输入的数据
  344. @return boolean 成功与否
  345. @usage
  346. -- 参考ulwip.setup
  347. */
  348. static int l_ulwip_input(lua_State *L) {
  349. // 必须有适配器编号
  350. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  351. int ret = 0;
  352. struct pbuf *q = NULL;
  353. const char* data = NULL;
  354. size_t len = 0;
  355. size_t offset = 0;
  356. struct netif* netif = find_netif(adapter_index);
  357. if (netif == NULL) {
  358. LLOGE("没有找到netif %d", adapter_index);
  359. return 0;
  360. }
  361. if (lua_type(L, 2) == LUA_TSTRING)
  362. {
  363. data = luaL_checklstring(L, 2, &len);
  364. }
  365. else if (lua_type(L, 2) == LUA_TUSERDATA) {
  366. luat_zbuff_t* zb = (luat_zbuff_t*)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE);
  367. data = (const char*)zb->addr;
  368. if (lua_isinteger(L, 3)) {
  369. len = luaL_checkinteger(L, 3);
  370. offset = luaL_checkinteger(L, 4);
  371. data += offset;
  372. }
  373. else {
  374. len = zb->used;
  375. }
  376. }
  377. else {
  378. LLOGE("未知的数据格式, 当前仅支持zbuff和string");
  379. return 0;
  380. }
  381. // LLOGD("输入的mac帧 %d %02X:%02X:%02X:%02X:%02X:%02X", len, data[0], data[1], data[2], data[3], data[4], data[5]);
  382. struct pbuf *p = pbuf_alloc(PBUF_RAW, (uint16_t)len, PBUF_RAM);
  383. if (p == NULL) {
  384. LLOGE("pbuf_alloc failed");
  385. return 0;
  386. }
  387. for (q = p; q != NULL; q = q->next) {
  388. memcpy(q->payload, data, q->len);
  389. data += q->len;
  390. }
  391. #if NO_SYS
  392. ret = netif->input(p, netif);
  393. #else
  394. netif_cb_ctx_t* ctx = (netif_cb_ctx_t*)luat_heap_malloc(sizeof(netif_cb_ctx_t));
  395. if (ctx == NULL) {
  396. LLOGE("netif->input ret %d", ret);
  397. LWIP_DEBUGF(NETIF_DEBUG, ("l_ulwip_input: IP input error\n"));
  398. pbuf_free(p);
  399. return 0;
  400. }
  401. memset(ctx, 0, sizeof(netif_cb_ctx_t));
  402. ctx->netif = netif;
  403. ctx->p = p;
  404. ret = tcpip_callback(netif_input_cb, ctx);
  405. #endif
  406. if(ret != ERR_OK) {
  407. LLOGE("netif->input ret %d", ret);
  408. LWIP_DEBUGF(NETIF_DEBUG, ("l_ulwip_input: IP input error\n"));
  409. pbuf_free(p);
  410. return 0;
  411. }
  412. lua_pushboolean(L, 1);
  413. return 1;
  414. }
  415. static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
  416. /*
  417. 启动或关闭dhcp
  418. @api ulwip.dhcp(adapter_index, up)
  419. @int adapter_index 适配器编号
  420. @boolean up true为启动, false为关闭
  421. @return boolean 当前状态
  422. @usage
  423. -- 参考ulwip.setup
  424. */
  425. static int l_ulwip_dhcp(lua_State *L) {
  426. // 必须有适配器编号
  427. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  428. struct netif* netif = find_netif(adapter_index);
  429. if (netif == NULL) {
  430. LLOGE("没有找到netif");
  431. return 0;
  432. }
  433. int dhcp_enable = lua_toboolean(L, 2);
  434. #if LWIP_DHCP
  435. if (dhcp_enable)
  436. {
  437. dhcp_start(netif);
  438. }
  439. else {
  440. dhcp_stop(netif);
  441. }
  442. lua_pushboolean(L, 1);
  443. return 1;
  444. #else
  445. int i = find_index(adapter_index);
  446. if (i < 0)
  447. {
  448. LLOGE("没有找到adapter_index %d", adapter_index);
  449. return 0;
  450. }
  451. if (nets[i].dhcp_client == NULL) {
  452. nets[i].dhcp_client = luat_heap_malloc(sizeof(dhcp_client_info_t));
  453. memset(nets[i].dhcp_client, 0, sizeof(dhcp_client_info_t));
  454. memcpy(nets[i].dhcp_client->mac, netif->hwaddr, 6);
  455. sprintf_(nets[i].dhcp_client->name, "airm2m-%02x%02x%02x%02x%02x%02x",
  456. nets[i].dhcp_client->mac[0],nets[i].dhcp_client->mac[1], nets[i].dhcp_client->mac[2],
  457. nets[i].dhcp_client->mac[3],nets[i].dhcp_client->mac[4], nets[i].dhcp_client->mac[5]);
  458. luat_rtos_timer_create(&nets[i].dhcp_timer);
  459. nets[i].dhcp_pcb = udp_new();
  460. ip_set_option(nets[i].dhcp_pcb, SOF_BROADCAST);
  461. udp_bind(nets[i].dhcp_pcb, IP4_ADDR_ANY, 68);
  462. udp_connect(nets[i].dhcp_pcb, IP4_ADDR_ANY, 67);
  463. udp_recv(nets[i].dhcp_pcb, dhcp_recv, (void*)i);
  464. }
  465. if (nets[i].dhcp_timer != NULL)
  466. {
  467. if (dhcp_enable)
  468. {
  469. if (!luat_rtos_timer_is_active(nets[i].dhcp_timer))
  470. {
  471. nets[i].dhcp_client->state = DHCP_STATE_DISCOVER;
  472. luat_rtos_timer_start(nets[i].dhcp_timer, 1000, 1, dhcp_client_cb, (void*)i);
  473. dhcp_client_cb((void*)i);
  474. }
  475. }
  476. else {
  477. if (luat_rtos_timer_is_active(nets[i].dhcp_timer)) {
  478. luat_rtos_timer_stop(nets[i].dhcp_timer);
  479. }
  480. }
  481. lua_pushboolean(L, 1);
  482. return 1;
  483. }
  484. return 0;
  485. #endif
  486. }
  487. /*
  488. 设置或获取ip信息
  489. @api ulwip.ip(adapter_index, ip, netmask, gw)
  490. @int adapter_index 适配器编号
  491. @string ip IP地址, 仅获取时可以不填
  492. @string netmask 子网掩码, 仅获取时可以不填
  493. @string gw 网关地址, 仅获取时可以不填
  494. @return string ip地址, 子网掩码, 网关地址
  495. @usage
  496. -- 获取现有值
  497. local ip, netmask, gw = ulwip.ip(socket.LWIP_STA)
  498. -- 设置新值
  499. ulwip.ip(socket.LWIP_STA, "192.168.0.1", "255.255.255.0", "192.168.0.1")
  500. */
  501. static int l_ulwip_ip(lua_State *L) {
  502. const char* tmp = NULL;
  503. // 必须有适配器编号
  504. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  505. struct netif* netif = find_netif(adapter_index);
  506. if (netif == NULL) {
  507. LLOGE("没有找到netif %d", adapter_index);
  508. return 0;
  509. }
  510. if (lua_type(L, 2) == LUA_TSTRING)
  511. {
  512. tmp = luaL_checkstring(L, 2);
  513. ipaddr_aton(tmp, &netif->ip_addr);
  514. tmp = luaL_checkstring(L, 3);
  515. ipaddr_aton(tmp, &netif->netmask);
  516. tmp = luaL_checkstring(L, 4);
  517. ipaddr_aton(tmp, &netif->gw);
  518. #if LWIP_NETIF_STATUS_CALLBACK == 0
  519. netif_status_callback(netif);
  520. #endif
  521. }
  522. // 反馈IP信息
  523. tmp = ip_ntoa(&netif->ip_addr);
  524. lua_pushstring(L, tmp);
  525. tmp = ip_ntoa(&netif->netmask);
  526. lua_pushstring(L, tmp);
  527. tmp = ip_ntoa(&netif->gw);
  528. lua_pushstring(L, tmp);
  529. return 3;
  530. }
  531. /*
  532. 将netif注册到luatos socket中
  533. @api ulwip.reg(adapter_index)
  534. @int adapter_index 适配器编号
  535. @return boolean 成功与否
  536. @usage
  537. -- 参考ulwip.setup
  538. */
  539. static int l_ulwip_reg(lua_State *L) {
  540. // 必须有适配器编号
  541. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  542. struct netif* netif = find_netif(adapter_index);
  543. if (netif == NULL) {
  544. LLOGE("没有找到netif %d", adapter_index);
  545. return 0;
  546. }
  547. net_lwip2_register_adapter(adapter_index);
  548. lua_pushboolean(L, 1);
  549. return 1;
  550. }
  551. /*
  552. 设置默认netif网卡
  553. @api ulwip.dft(adapter_index)
  554. @int/boolean adapter_index 适配器编号或还原默认网卡
  555. @return boolean 成功与否
  556. @usage
  557. -- 将默认网卡设置为socket.LWIP_ETH
  558. ulwip.dft(socket.LWIP_ETH)
  559. -- 还原默认网卡
  560. ulwip.dft(true)
  561. */
  562. static struct netif* prev_netif;
  563. extern struct netif *netif_default;
  564. static int l_ulwip_dft(lua_State *L) {
  565. // 必须有适配器编号
  566. if (lua_type(L, 1) == LUA_TNUMBER)
  567. {
  568. uint8_t adapter_index = (uint8_t)luaL_checkinteger(L, 1);
  569. struct netif* netif = find_netif(adapter_index);
  570. if (netif == NULL) {
  571. LLOGE("没有找到netif %d", adapter_index);
  572. return 0;
  573. }
  574. if (prev_netif == NULL && netif_default != NULL) {
  575. LLOGD("保存系统默认网卡 %.2s %p", netif_default->name, netif_default);
  576. prev_netif = netif_default;
  577. }
  578. LLOGD("设置默认网卡 %.2s %p", netif->name, netif);
  579. netif_set_default(netif);
  580. }
  581. else if (lua_type(L, 1) == LUA_TBOOLEAN) {
  582. if (lua_toboolean(L, 1) && prev_netif != NULL) {
  583. LLOGD("还原系统默认网卡 %.2s %p", prev_netif->name, prev_netif);
  584. netif_set_default(prev_netif);
  585. }
  586. else {
  587. LLOGE("没有找到系统默认网卡");
  588. }
  589. }
  590. lua_pushboolean(L, 1);
  591. return 1;
  592. }
  593. #include "rotable2.h"
  594. static const rotable_Reg_t reg_ulwip[] =
  595. {
  596. { "input" , ROREG_FUNC(l_ulwip_input)},
  597. { "setup" , ROREG_FUNC(l_ulwip_setup)},
  598. { "updown" , ROREG_FUNC(l_ulwip_updown)},
  599. { "link" , ROREG_FUNC(l_ulwip_link)},
  600. { "dhcp" , ROREG_FUNC(l_ulwip_dhcp)},
  601. { "ip" , ROREG_FUNC(l_ulwip_ip)},
  602. { "reg" , ROREG_FUNC(l_ulwip_reg)},
  603. { "dft" , ROREG_FUNC(l_ulwip_dft)},
  604. // 网卡FLAGS,默认
  605. // NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6
  606. // @const FLAG_BROADCAST number 支持广播
  607. { "FLAG_BROADCAST", ROREG_INT(NETIF_FLAG_BROADCAST)},
  608. // @const FLAG_ETHARP number 支持ARP
  609. { "FLAG_ETHARP", ROREG_INT(NETIF_FLAG_ETHARP)},
  610. // @const FLAG_ETHERNET number 以太网模式
  611. { "FLAG_ETHERNET", ROREG_INT(NETIF_FLAG_ETHERNET)},
  612. // @const FLAG_IGMP number 支持IGMP
  613. { "FLAG_IGMP", ROREG_INT(NETIF_FLAG_IGMP)},
  614. // @const FLAG_MLD6 number 支持_MLD6
  615. { "FLAG_MLD6", ROREG_INT(NETIF_FLAG_MLD6)},
  616. { NULL, ROREG_INT(0)}
  617. };
  618. LUAMOD_API int luaopen_ulwip( lua_State *L ) {
  619. luat_newlib2(L, reg_ulwip);
  620. return 1;
  621. }
  622. // -------------------------------------
  623. // DHCP 相关的逻辑
  624. // -------------------------------------
  625. static int l_dhcp_client_cb(lua_State *L, void* ptr);
  626. // timer回调, 或者是直接被调用, arg是nets的索引号
  627. static void dhcp_client_cb(void *arg) {
  628. int i = (int)arg;
  629. // 简单防御一下
  630. if (i < 0 || i >= USERLWIP_NET_COUNT) {
  631. return;
  632. }
  633. if (nets[i].netif == NULL || nets[i].dhcp_client == NULL) {
  634. return;
  635. }
  636. // 压入lua线程进行处理
  637. rtos_msg_t msg = {
  638. .handler = l_dhcp_client_cb,
  639. .arg1 = i
  640. };
  641. luat_msgbus_put(&msg, 0);
  642. }
  643. static int dhcp_task_run(int idx, char* rxbuff, size_t len) {
  644. PV_Union uIP;
  645. // 检查dhcp的状态
  646. dhcp_client_info_t* dhcp = nets[idx].dhcp_client;
  647. struct netif* netif = nets[idx].netif;
  648. Buffer_Struct rx_msg_buf = {0,0,0};
  649. Buffer_Struct tx_msg_buf = {0,0,0};
  650. uint32_t remote_ip = 0;
  651. int result = 0;
  652. if (rxbuff) {
  653. rx_msg_buf.Data = (uint8_t*)rxbuff;
  654. rx_msg_buf.Pos = len;
  655. rx_msg_buf.MaxLen = len;
  656. }
  657. // 看看是不是获取成功了
  658. if (DHCP_STATE_CHECK == dhcp->state) {
  659. uIP.u32 = dhcp->ip;
  660. LLOGD("动态IP:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  661. uIP.u32 = dhcp->submask;
  662. LLOGD("子网掩码:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  663. uIP.u32 = dhcp->gateway;
  664. LLOGD("网关:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  665. LLOGD("租约时间:%u秒", dhcp->lease_time);
  666. // 设置到netif
  667. net_lwip2_set_link_state(nets[idx].adapter_index, 0);
  668. ip_addr_set_ip4_u32(&netif->ip_addr, dhcp->ip);
  669. ip_addr_set_ip4_u32(&netif->netmask, dhcp->submask);
  670. ip_addr_set_ip4_u32(&netif->gw, dhcp->gateway);
  671. net_lwip2_set_link_state(nets[idx].adapter_index, 1);
  672. dhcp->state = DHCP_STATE_WAIT_LEASE_P1;
  673. if (rxbuff) {
  674. luat_heap_free(rxbuff);
  675. }
  676. return 0;
  677. }
  678. result = ip4_dhcp_run(dhcp, rxbuff == NULL ? NULL : &rx_msg_buf, &tx_msg_buf, &remote_ip);
  679. if (rxbuff) {
  680. luat_heap_free(rxbuff);
  681. }
  682. if (result) {
  683. LLOGE("ip4_dhcp_run error %d", result);
  684. return 0;
  685. }
  686. if (!tx_msg_buf.Pos) {
  687. return 0; // 没有数据需要发送
  688. }
  689. // 通过UDP发出来
  690. struct pbuf *p;
  691. struct pbuf *q;
  692. // LLOGD("待发送DHCP包长度 %d 前4个字节分别是 %02X%02X%02X%02X", tx_msg_buf.Pos,
  693. // tx_msg_buf.Data[0], tx_msg_buf.Data[1], tx_msg_buf.Data[2], tx_msg_buf.Data[3]);
  694. p = pbuf_alloc(PBUF_RAW, tx_msg_buf.Pos, PBUF_RAM);
  695. char* data = (char*)tx_msg_buf.Data;
  696. for (q = p; q != NULL; q = q->next) {
  697. memcpy(q->payload, data, q->len);
  698. data += q->len;
  699. }
  700. data = p->payload;
  701. // LLOGI("dhcp payload len %d %02X%02X%02X%02X", p->tot_len, data[0], data[1], data[2], data[3]);
  702. udp_sendto_if(nets[idx].dhcp_pcb, p, IP_ADDR_BROADCAST, 67, netif);
  703. return 0;
  704. }
  705. static int l_dhcp_client_cb(lua_State *L, void* ptr) {
  706. (void)ptr;
  707. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  708. int idx = msg->arg1;
  709. dhcp_task_run(idx, ptr, msg->arg2);
  710. return 0;
  711. }
  712. static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) {
  713. LLOGD("收到DHCP数据包(len=%d)", p->tot_len);
  714. int idx = (int)arg;
  715. char* ptr = luat_heap_malloc(p->tot_len);
  716. if (!ptr) {
  717. return;
  718. }
  719. rtos_msg_t msg = {
  720. .ptr = ptr,
  721. .handler = l_dhcp_client_cb,
  722. };
  723. msg.arg1 = idx;
  724. msg.arg2 = p->tot_len;
  725. size_t offset = 0;
  726. do {
  727. memcpy((char*)msg.ptr + offset, p->payload, p->len);
  728. offset += p->len;
  729. p = p->next;
  730. } while (p);
  731. luat_msgbus_put(&msg, 0);
  732. LLOGD("传递DHCP数据包");
  733. return;
  734. }
  735. // ARP 修正
  736. static err_t ulwip_etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) {
  737. const struct eth_addr *dest;
  738. struct eth_addr mcastaddr;
  739. const ip4_addr_t *dst_addr = ipaddr;
  740. /* Determine on destination hardware address. Broadcasts and multicasts
  741. * are special, other IP addresses are looked up in the ARP table. */
  742. /* broadcast destination IP address? */
  743. if (ip4_addr_isbroadcast(ipaddr, netif)) {
  744. /* broadcast on Ethernet also */
  745. dest = (const struct eth_addr *)&ethbroadcast;
  746. /* multicast destination IP address? */
  747. } else if (ip4_addr_ismulticast(ipaddr)) {
  748. /* Hash IP multicast address to MAC address.*/
  749. mcastaddr.addr[0] = LL_IP4_MULTICAST_ADDR_0;
  750. mcastaddr.addr[1] = LL_IP4_MULTICAST_ADDR_1;
  751. mcastaddr.addr[2] = LL_IP4_MULTICAST_ADDR_2;
  752. mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
  753. mcastaddr.addr[4] = ip4_addr3(ipaddr);
  754. mcastaddr.addr[5] = ip4_addr4(ipaddr);
  755. /* destination Ethernet address is multicast */
  756. dest = &mcastaddr;
  757. /* unicast destination IP address? */
  758. } else {
  759. // s8_t i;
  760. #if 1
  761. /* outside local network? if so, this can neither be a global broadcast nor
  762. a subnet broadcast. */
  763. if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) &&
  764. !ip4_addr_islinklocal(ipaddr)) {
  765. #if LWIP_AUTOIP
  766. struct ip_hdr *iphdr = LWIP_ALIGNMENT_CAST(struct ip_hdr*, q->payload);
  767. /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with
  768. a link-local source address must always be "directly to its destination
  769. on the same physical link. The host MUST NOT send the packet to any
  770. router for forwarding". */
  771. if (!ip4_addr_islinklocal(&iphdr->src))
  772. #endif /* LWIP_AUTOIP */
  773. {
  774. #ifdef LWIP_HOOK_ETHARP_GET_GW
  775. /* For advanced routing, a single default gateway might not be enough, so get
  776. the IP address of the gateway to handle the current destination address. */
  777. dst_addr = LWIP_HOOK_ETHARP_GET_GW(netif, ipaddr);
  778. if (dst_addr == NULL)
  779. #endif /* LWIP_HOOK_ETHARP_GET_GW */
  780. {
  781. /* interface has default gateway? */
  782. if (!ip4_addr_isany_val(*netif_ip4_gw(netif))) {
  783. /* send to hardware address of default gateway IP address */
  784. dst_addr = netif_ip4_gw(netif);
  785. /* no default gateway available */
  786. } else {
  787. /* no route to destination error (default gateway missing) */
  788. return ERR_RTE;
  789. }
  790. }
  791. }
  792. }
  793. #endif
  794. /* no stable entry found, use the (slower) query function:
  795. queue on destination Ethernet address belonging to ipaddr */
  796. return etharp_query(netif, dst_addr, q);
  797. }
  798. /* continuation for multicast/broadcast destinations */
  799. /* obtain source Ethernet address of the given interface */
  800. /* send packet directly on the link */
  801. return ethernet_output(netif, q, (struct eth_addr*)(netif->hwaddr), dest, ETHTYPE_IP);
  802. }