net_lwip.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  1. #include "platform_def.h"
  2. #include "luat_base.h"
  3. #include "luat_mem.h"
  4. #include "luat_mcu.h"
  5. #include "luat_rtos.h"
  6. #include "dns_def.h"
  7. #include "luat_network_adapter.h"
  8. #include "net_lwip.h"
  9. #include "sockets.h"
  10. #include "lwip/tcp.h"
  11. #include "lwip/udp.h"
  12. #include "lwip/err.h"
  13. #ifdef __USE_SDK_LWIP__
  14. #ifndef SOCKET_BUF_LEN
  15. #define SOCKET_BUF_LEN (3 * TCP_MSS)
  16. #endif
  17. #if LWIP_SOCKET
  18. #else
  19. /* Socket protocol types (TCP/UDP/RAW) */
  20. #define SOCK_STREAM 1
  21. #define SOCK_DGRAM 2
  22. #define SOCK_RAW 3
  23. /*
  24. * Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c)
  25. */
  26. #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
  27. #define SO_KEEPALIVE 0x0008 /* keep connections alive */
  28. #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
  29. /*
  30. * Additional options, not kept in so_options.
  31. */
  32. #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
  33. #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
  34. #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
  35. #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
  36. #define SO_LINGER 0x0080 /* linger on close if data present */
  37. #define SO_DONTLINGER ((int)(~SO_LINGER))
  38. #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
  39. #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
  40. #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
  41. #define SO_RCVBUF 0x1002 /* receive buffer size */
  42. #define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */
  43. #define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */
  44. #define SO_SNDTIMEO 0x1005 /* send timeout */
  45. #define SO_RCVTIMEO 0x1006 /* receive timeout */
  46. #define SO_ERROR 0x1007 /* get error status and clear */
  47. #define SO_TYPE 0x1008 /* get socket type */
  48. #define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */
  49. #define SO_NO_CHECK 0x100a /* don't create UDP checksum */
  50. #define SO_BINDTODEVICE 0x100b /* bind to device */
  51. /*
  52. * Structure used for manipulating linger option.
  53. */
  54. /*
  55. * Level number for (get/set)sockopt() to apply to socket itself.
  56. */
  57. #define SOL_SOCKET 0xfff /* options for socket level */
  58. #define AF_UNSPEC 0
  59. #define AF_INET 2
  60. #if LWIP_IPV6
  61. #define AF_INET6 10
  62. #else /* LWIP_IPV6 */
  63. #define AF_INET6 AF_UNSPEC
  64. #endif /* LWIP_IPV6 */
  65. #define PF_INET AF_INET
  66. #define PF_INET6 AF_INET6
  67. #define PF_UNSPEC AF_UNSPEC
  68. #define IPPROTO_IP 0
  69. #define IPPROTO_ICMP 1
  70. #define IPPROTO_TCP 6
  71. #define IPPROTO_UDP 17
  72. #if LWIP_IPV6
  73. #define IPPROTO_IPV6 41
  74. #define IPPROTO_ICMPV6 58
  75. #endif /* LWIP_IPV6 */
  76. #define IPPROTO_UDPLITE 136
  77. #define IPPROTO_RAW 255
  78. /* Flags we can use with send and recv. */
  79. #define MSG_PEEK 0x01 /* Peeks at an incoming message */
  80. #define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
  81. #define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
  82. #define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */
  83. #define MSG_MORE 0x10 /* Sender will send more */
  84. #define MSG_NOSIGNAL 0x20 /* Uninmplemented: Requests not to send the SIGPIPE signal if an attempt to send is made on a stream-oriented socket that is no longer connected. */
  85. /*
  86. * Options for level IPPROTO_IP
  87. */
  88. #define IP_TOS 1
  89. #define IP_TTL 2
  90. #define IP_PKTINFO 8
  91. #if LWIP_TCP
  92. /*
  93. * Options for level IPPROTO_TCP
  94. */
  95. #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
  96. #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
  97. #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
  98. #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
  99. #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
  100. #endif /* LWIP_TCP */
  101. #if LWIP_IPV6
  102. /*
  103. * Options for level IPPROTO_IPV6
  104. */
  105. #define IPV6_CHECKSUM 7 /* RFC3542: calculate and insert the ICMPv6 checksum for raw sockets. */
  106. #define IPV6_V6ONLY 27 /* RFC3493: boolean control to restrict AF_INET6 sockets to IPv6 communications only. */
  107. #endif /* LWIP_IPV6 */
  108. #if LWIP_UDP && LWIP_UDPLITE
  109. /*
  110. * Options for level IPPROTO_UDPLITE
  111. */
  112. #define UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */
  113. #define UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */
  114. #endif /* LWIP_UDP && LWIP_UDPLITE*/
  115. #if LWIP_MULTICAST_TX_OPTIONS
  116. /*
  117. * Options and types for UDP multicast traffic handling
  118. */
  119. #define IP_MULTICAST_TTL 5
  120. #define IP_MULTICAST_IF 6
  121. #define IP_MULTICAST_LOOP 7
  122. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  123. #if LWIP_IGMP
  124. /*
  125. * Options and types related to multicast membership
  126. */
  127. #define IP_ADD_MEMBERSHIP 3
  128. #define IP_DROP_MEMBERSHIP 4
  129. typedef struct ip_mreq {
  130. struct in_addr imr_multiaddr; /* IP multicast address of group */
  131. struct in_addr imr_interface; /* local IP address of interface */
  132. } ip_mreq;
  133. #endif /* LWIP_IGMP */
  134. #if LWIP_IPV6_MLD
  135. /*
  136. * Options and types related to IPv6 multicast membership
  137. */
  138. #define IPV6_JOIN_GROUP 12
  139. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  140. #define IPV6_LEAVE_GROUP 13
  141. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  142. #endif /* LWIP_IPV6_MLD */
  143. /*
  144. * The Type of Service provides an indication of the abstract
  145. * parameters of the quality of service desired. These parameters are
  146. * to be used to guide the selection of the actual service parameters
  147. * when transmitting a datagram through a particular network. Several
  148. * networks offer service precedence, which somehow treats high
  149. * precedence traffic as more important than other traffic (generally
  150. * by accepting only traffic above a certain precedence at time of high
  151. * load). The major choice is a three way tradeoff between low-delay,
  152. * high-reliability, and high-throughput.
  153. * The use of the Delay, Throughput, and Reliability indications may
  154. * increase the cost (in some sense) of the service. In many networks
  155. * better performance for one of these parameters is coupled with worse
  156. * performance on another. Except for very unusual cases at most two
  157. * of these three indications should be set.
  158. */
  159. #define IPTOS_TOS_MASK 0x1E
  160. #define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK)
  161. #define IPTOS_LOWDELAY 0x10
  162. #define IPTOS_THROUGHPUT 0x08
  163. #define IPTOS_RELIABILITY 0x04
  164. #define IPTOS_LOWCOST 0x02
  165. #define IPTOS_MINCOST IPTOS_LOWCOST
  166. /*
  167. * The Network Control precedence designation is intended to be used
  168. * within a network only. The actual use and control of that
  169. * designation is up to each network. The Internetwork Control
  170. * designation is intended for use by gateway control originators only.
  171. * If the actual use of these precedence designations is of concern to
  172. * a particular network, it is the responsibility of that network to
  173. * control the access to, and use of, those precedence designations.
  174. */
  175. #define IPTOS_PREC_MASK 0xe0
  176. #define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK)
  177. #define IPTOS_PREC_NETCONTROL 0xe0
  178. #define IPTOS_PREC_INTERNETCONTROL 0xc0
  179. #define IPTOS_PREC_CRITIC_ECP 0xa0
  180. #define IPTOS_PREC_FLASHOVERRIDE 0x80
  181. #define IPTOS_PREC_FLASH 0x60
  182. #define IPTOS_PREC_IMMEDIATE 0x40
  183. #define IPTOS_PREC_PRIORITY 0x20
  184. #define IPTOS_PREC_ROUTINE 0x00
  185. /*
  186. * Commands for ioctlsocket(), taken from the BSD file fcntl.h.
  187. * lwip_ioctl only supports FIONREAD and FIONBIO, for now
  188. *
  189. * Ioctl's have the command encoded in the lower word,
  190. * and the size of any in or out parameters in the upper
  191. * word. The high 2 bits of the upper word are used
  192. * to encode the in/out status of the parameter; for now
  193. * we restrict parameters to at most 128 bytes.
  194. */
  195. #if !defined(FIONREAD) || !defined(FIONBIO)
  196. #define IOCPARM_MASK 0x7fU /* parameters must be < 128 bytes */
  197. #define IOC_VOID 0x20000000UL /* no parameters */
  198. #define IOC_OUT 0x40000000UL /* copy out parameters */
  199. #define IOC_IN 0x80000000UL /* copy in parameters */
  200. #define IOC_INOUT (IOC_IN|IOC_OUT)
  201. /* 0x20000000 distinguishes new &
  202. old ioctl's */
  203. #define _IO(x,y) ((long)(IOC_VOID|((x)<<8)|(y)))
  204. #define _IOR(x,y,t) ((long)(IOC_OUT|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y)))
  205. #define _IOW(x,y,t) ((long)(IOC_IN|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y)))
  206. #endif /* !defined(FIONREAD) || !defined(FIONBIO) */
  207. #ifndef FIONREAD
  208. #define FIONREAD _IOR('f', 127, unsigned long) /* get # bytes to read */
  209. #endif
  210. #ifndef FIONBIO
  211. #define FIONBIO _IOW('f', 126, unsigned long) /* set/clear non-blocking i/o */
  212. #endif
  213. /* Socket I/O Controls: unimplemented */
  214. #ifndef SIOCSHIWAT
  215. #define SIOCSHIWAT _IOW('s', 0, unsigned long) /* set high watermark */
  216. #define SIOCGHIWAT _IOR('s', 1, unsigned long) /* get high watermark */
  217. #define SIOCSLOWAT _IOW('s', 2, unsigned long) /* set low watermark */
  218. #define SIOCGLOWAT _IOR('s', 3, unsigned long) /* get low watermark */
  219. #define SIOCATMARK _IOR('s', 7, unsigned long) /* at oob mark? */
  220. #endif
  221. /* commands for fnctl */
  222. #ifndef F_GETFL
  223. #define F_GETFL 3
  224. #endif
  225. #ifndef F_SETFL
  226. #define F_SETFL 4
  227. #endif
  228. /* File status flags and file access modes for fnctl,
  229. these are bits in an int. */
  230. #ifndef O_NONBLOCK
  231. #define O_NONBLOCK 1 /* nonblocking I/O */
  232. #endif
  233. #ifndef O_NDELAY
  234. #define O_NDELAY O_NONBLOCK /* same as O_NONBLOCK, for compatibility */
  235. #endif
  236. #ifndef O_RDONLY
  237. #define O_RDONLY 2
  238. #endif
  239. #ifndef O_WRONLY
  240. #define O_WRONLY 4
  241. #endif
  242. #ifndef O_RDWR
  243. #define O_RDWR (O_RDONLY|O_WRONLY)
  244. #endif
  245. #endif //LWIP_SOCKET
  246. static int
  247. lwip_sockopt_to_ipopt(int optname)
  248. {
  249. /* Map SO_* values to our internal SOF_* values
  250. * We should not rely on #defines in socket.h
  251. * being in sync with ip.h.
  252. */
  253. switch (optname) {
  254. case SO_BROADCAST:
  255. return SOF_BROADCAST;
  256. case SO_KEEPALIVE:
  257. return SOF_KEEPALIVE;
  258. case SO_REUSEADDR:
  259. return SOF_REUSEADDR;
  260. default:
  261. LWIP_ASSERT("Unknown socket option", 0);
  262. return 0;
  263. }
  264. }
  265. #if LWIP_NUM_SOCKETS > 32
  266. #define MAX_SOCK_NUM 32
  267. #else
  268. #define MAX_SOCK_NUM LWIP_NUM_SOCKETS
  269. #endif
  270. extern LUAT_WEAK void DBG_Printf(const char* format, ...);
  271. #ifdef LUAT_LOG_NO_NEWLINE
  272. #define NET_DBG(x,y...) DBG_Printf("%s %d:"x, __FUNCTION__,__LINE__,##y)
  273. #else
  274. #define NET_DBG(x,y...) DBG_Printf("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##y)
  275. #endif
  276. #define NET_ERR NET_DBG
  277. #define SOCKET_LOCK(ID) platform_lock_mutex(prvlwip.socket[ID].mutex)
  278. #define SOCKET_UNLOCK(ID) platform_unlock_mutex(prvlwip.socket[ID].mutex)
  279. typedef struct
  280. {
  281. llist_head node;
  282. uint64_t tag; //考虑到socket复用的问题,必须有tag来做比对
  283. luat_ip_addr_t ip;
  284. uint8_t *data;
  285. uint32_t read_pos;
  286. uint16_t len;
  287. uint16_t port;
  288. uint8_t is_sending;
  289. uint8_t is_need_ack;
  290. }socket_data_t;
  291. typedef struct
  292. {
  293. uint64_t socket_tag;
  294. dns_client_t dns_client;
  295. socket_ctrl_t socket[MAX_SOCK_NUM];
  296. struct netif *lwip_netif[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
  297. CBFuncEx_t socket_cb[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
  298. void *user_data[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
  299. struct udp_pcb *dns_udp;
  300. uint32_t socket_busy;
  301. uint32_t socket_connect;
  302. HANDLE dns_timer;//dhcp_fine_tmr,dhcp6_tmr
  303. uint8_t netif_network_ready[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
  304. uint8_t dns_adapter_index;
  305. uint8_t dhcp_check_cnt;
  306. uint8_t next_socket_index;
  307. uint8_t fast_rx_ack;
  308. uint8_t only_ipv6;
  309. uint8_t check_enable;
  310. }net_lwip_ctrl_struct;
  311. static net_lwip_ctrl_struct prvlwip;
  312. static void net_lwip_check_network_ready(uint8_t adapter_index);
  313. static void net_lwip_task(void *param);
  314. static void net_lwip_create_socket_now(uint8_t adapter_index, uint8_t socket_id);
  315. static ip_addr_t *net_lwip_get_ip6(uint8_t adapter_index);
  316. static int net_lwip_del_data_cache(void *p, void *u)
  317. {
  318. socket_data_t *pdata = (socket_data_t *)p;
  319. free(pdata->data);
  320. return LIST_DEL;
  321. }
  322. static int net_lwip_next_data_cache(void *p, void *u)
  323. {
  324. socket_ctrl_t *socket = (socket_ctrl_t *)u;
  325. socket_data_t *pdata = (socket_data_t *)p;
  326. if (socket->tag != pdata->tag)
  327. {
  328. NET_DBG("tag error");
  329. free(pdata->data);
  330. return LIST_DEL;
  331. }
  332. return LIST_FIND;
  333. }
  334. static socket_data_t * net_lwip_create_data_node(uint8_t socket_id, uint8_t *data, uint32_t len, luat_ip_addr_t *remote_ip, uint16_t remote_port)
  335. {
  336. socket_data_t *p = (socket_data_t *)malloc(sizeof(socket_data_t));
  337. if (p)
  338. {
  339. memset(p, 0, sizeof(socket_data_t));
  340. p->len = len;
  341. p->port = remote_port;
  342. if (remote_ip)
  343. {
  344. p->ip = *remote_ip;
  345. }
  346. else
  347. {
  348. p->ip.type = 0xff;
  349. }
  350. p->tag = prvlwip.socket[socket_id].tag;
  351. if (data && len)
  352. {
  353. p->data = malloc(len);
  354. if (p->data)
  355. {
  356. memcpy(p->data, data, len);
  357. }
  358. else
  359. {
  360. free(p);
  361. return NULL;
  362. }
  363. }
  364. }
  365. return p;
  366. }
  367. static LUAT_RT_RET_TYPE net_lwip_timer_cb(LUAT_RT_CB_PARAM)
  368. {
  369. net_lwip_sdk_send_event((uint32_t)param, 0, 0, 0);
  370. return LUAT_RT_RET;
  371. }
  372. static void net_lwip_callback_to_nw_task(uint8_t adapter_index, uint32_t event_id, uint32_t param1, uint32_t param2, uint32_t param3)
  373. {
  374. luat_network_cb_param_t param = {.tag = 0, .param = prvlwip.user_data[adapter_index]};
  375. OS_EVENT event = { .ID = event_id, .Param1 = param1, .Param2 = param2, .Param3 = param3};
  376. if ((event_id > EV_NW_DNS_RESULT))
  377. {
  378. if (event_id != EV_NW_SOCKET_CLOSE_OK)
  379. {
  380. event.Param3 = prvlwip.socket[param1].param;
  381. param.tag = prvlwip.socket[param1].tag;
  382. }
  383. else
  384. {
  385. event.Param3 = ((luat_network_cb_param_t *)param3)->param;
  386. param.tag = ((luat_network_cb_param_t *)param3)->tag;
  387. }
  388. }
  389. switch(event_id)
  390. {
  391. case EV_NW_SOCKET_CLOSE_OK:
  392. case EV_NW_SOCKET_CONNECT_OK:
  393. case EV_NW_SOCKET_ERROR:
  394. prvlwip.socket_busy &= ~(1 << param1);
  395. break;
  396. }
  397. prvlwip.socket_cb[adapter_index](&event, &param);
  398. }
  399. static err_t net_lwip_tcp_connected_cb(void *arg, struct tcp_pcb *tpcb, err_t err)
  400. {
  401. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  402. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  403. // prvlwip.socket_connect &= ~(1 << socket_id);
  404. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  405. return ERR_OK;
  406. }
  407. static void net_lwip_tcp_error(uint8_t adapter_index, int socket_id)
  408. {
  409. prvlwip.socket[socket_id].remote_close = 1;
  410. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  411. }
  412. static int net_lwip_rx_data(int socket_id, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  413. {
  414. int is_mem_err = 0;
  415. SOCKET_LOCK(socket_id);
  416. socket_data_t *data_p = net_lwip_create_data_node(socket_id, NULL, 0, addr, port);
  417. if (data_p)
  418. {
  419. data_p->data = malloc(p->tot_len);
  420. if (data_p->data)
  421. {
  422. data_p->len = pbuf_copy_partial(p, data_p->data, p->tot_len, 0);
  423. // NET_DBG("new data %ubyte", p->tot_len);
  424. llist_add_tail(&data_p->node, &prvlwip.socket[socket_id].rx_head);
  425. prvlwip.socket[socket_id].rx_wait_size += p->tot_len;
  426. }
  427. else
  428. {
  429. free(data_p);
  430. is_mem_err = 1;
  431. }
  432. }
  433. else
  434. {
  435. is_mem_err = 1;
  436. }
  437. SOCKET_UNLOCK(socket_id);
  438. return is_mem_err;
  439. }
  440. static void net_lwip_tcp_close_done(uint8_t adapter_index, int socket_id, uint8_t notify)
  441. {
  442. luat_network_cb_param_t cb_param;
  443. SOCKET_LOCK(socket_id);
  444. OS_LOCK;
  445. cb_param.param = prvlwip.socket[socket_id].param;
  446. cb_param.tag = prvlwip.socket[socket_id].tag;
  447. prvlwip.socket[socket_id].pcb.ip = NULL;
  448. prvlwip.socket[socket_id].listen_tcp = NULL;
  449. prvlwip.socket[socket_id].remote_close = 0;
  450. prvlwip.socket[socket_id].state = 0;
  451. prvlwip.socket[socket_id].in_use = 0;
  452. prvlwip.socket[socket_id].param = NULL;
  453. prvlwip.socket[socket_id].rx_wait_size = 0;
  454. prvlwip.socket[socket_id].tx_wait_size = 0;
  455. llist_traversal(&prvlwip.socket[socket_id].wait_ack_head, net_lwip_del_data_cache, NULL);
  456. llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_del_data_cache, NULL);
  457. llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_del_data_cache, NULL);
  458. prvlwip.socket_busy &= ~(1 << socket_id);
  459. prvlwip.socket_connect &= ~(1 << socket_id);
  460. OS_UNLOCK;
  461. SOCKET_UNLOCK(socket_id);
  462. if (notify)
  463. {
  464. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CLOSE_OK, socket_id, 0, &cb_param);
  465. }
  466. }
  467. static err_t net_lwip_tcp_recv_cb(void *arg, struct tcp_pcb *tpcb,
  468. struct pbuf *p, err_t err)
  469. {
  470. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  471. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  472. uint16_t len;
  473. if (p)
  474. {
  475. if (prvlwip.socket[socket_id].fast_rx_ack)
  476. {
  477. tcp_recved(tpcb, p->tot_len);
  478. }
  479. len = p->tot_len;
  480. if (net_lwip_rx_data(socket_id, p, NULL, 0))
  481. {
  482. NET_DBG("no memory!");
  483. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  484. }
  485. else
  486. {
  487. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_RX_NEW, socket_id, len, 0);
  488. }
  489. pbuf_free(p);
  490. }
  491. else if (err == ERR_OK)
  492. {
  493. {
  494. prvlwip.socket[socket_id].remote_close = 1;
  495. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_REMOTE_CLOSE, socket_id, 0, 0);
  496. }
  497. }
  498. else
  499. {
  500. net_lwip_tcp_error(adapter_index, socket_id);
  501. }
  502. return ERR_OK;
  503. }
  504. static err_t net_lwip_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb,
  505. u16_t len)
  506. {
  507. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  508. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  509. volatile uint16_t check_len = 0;
  510. volatile uint32_t rest_len;
  511. socket_data_t *p;
  512. SOCKET_LOCK(socket_id);
  513. while(check_len < len)
  514. {
  515. if (llist_empty(&prvlwip.socket[socket_id].wait_ack_head))
  516. {
  517. NET_DBG("!");
  518. goto SOCEKT_ERROR;
  519. }
  520. p = (socket_data_t *)prvlwip.socket[socket_id].wait_ack_head.next;
  521. rest_len = p->len - p->read_pos;
  522. if ((len - check_len) >= rest_len)
  523. {
  524. // NET_DBG("adapter %d socket %d, %ubytes ack", adapter_index, socket_id, p->len);
  525. llist_del(&p->node);
  526. free(p->data);
  527. free(p);
  528. check_len += rest_len;
  529. }
  530. else
  531. {
  532. p->read_pos += (len - check_len);
  533. check_len = len;
  534. // NET_DBG("adapter %d socket %d, all %ubytes ack %ubytes ", adapter_index, socket_id, p->len, p->read_pos);
  535. }
  536. }
  537. while (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  538. {
  539. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  540. if (p)
  541. {
  542. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0))
  543. {
  544. llist_del(&p->node);
  545. llist_add_tail(&p->node, &prvlwip.socket[socket_id].wait_ack_head);
  546. }
  547. else
  548. {
  549. // NET_DBG("tcp buf is full, wait ack and send again");
  550. break;
  551. }
  552. }
  553. }
  554. SOCKET_UNLOCK(socket_id);
  555. tcp_output(tpcb);
  556. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_TX_OK, socket_id, len, 0);
  557. return ERR_OK;
  558. SOCEKT_ERROR:
  559. SOCKET_UNLOCK(socket_id);
  560. net_lwip_tcp_error(adapter_index, socket_id);
  561. return ERR_OK;
  562. }
  563. static err_t net_lwip_tcp_err_cb(void *arg, err_t err)
  564. {
  565. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  566. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  567. if (prvlwip.socket[socket_id].is_tcp)
  568. {
  569. if (prvlwip.socket[socket_id].pcb.tcp)
  570. {
  571. prvlwip.socket[socket_id].pcb.tcp = NULL;
  572. }
  573. }
  574. if (!prvlwip.socket[socket_id].state && !prvlwip.socket[socket_id].remote_close)
  575. {
  576. NET_DBG("adapter %d socket %d not closing, but error %d", adapter_index, socket_id, err);
  577. net_lwip_tcp_error(adapter_index, socket_id);
  578. }
  579. return 0;
  580. }
  581. static err_t net_lwip_tcp_fast_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  582. {
  583. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  584. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  585. if (err || !newpcb)
  586. {
  587. net_lwip_tcp_error(adapter_index, socket_id);
  588. return 0;
  589. }
  590. prvlwip.socket[socket_id].pcb.tcp = newpcb;
  591. prvlwip.socket[socket_id].rx_wait_size = 0;
  592. prvlwip.socket[socket_id].tx_wait_size = 0;
  593. prvlwip.socket[socket_id].pcb.tcp->callback_arg = arg;
  594. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip_tcp_recv_cb;
  595. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip_tcp_sent_cb;
  596. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip_tcp_err_cb;
  597. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  598. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  599. return ERR_OK;
  600. }
  601. static err_t net_lwip_tcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  602. {
  603. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  604. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  605. return ERR_OK;
  606. }
  607. static err_t net_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  608. const ip_addr_t *addr, u16_t port)
  609. {
  610. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  611. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  612. uint16_t len;
  613. if (p)
  614. {
  615. len = p->tot_len;
  616. if (net_lwip_rx_data(socket_id, p, addr, port))
  617. {
  618. NET_DBG("no memory!");
  619. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  620. }
  621. else
  622. {
  623. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_RX_NEW, socket_id, len, 0);
  624. }
  625. pbuf_free(p);
  626. }
  627. return ERR_OK;
  628. }
  629. static int32_t net_lwip_dns_check_result(void *data, void *param)
  630. {
  631. luat_dns_require_t *require = (luat_dns_require_t *)data;
  632. if (require->result != 0)
  633. {
  634. free(require->uri.Data);
  635. require->uri.Data = NULL;
  636. if (require->result > 0)
  637. {
  638. luat_dns_ip_result *ip_result = zalloc(sizeof(luat_dns_ip_result) * require->result);
  639. int i;
  640. for(i = 0; i < require->result; i++)
  641. {
  642. ip_result[i] = require->ip_result[i];
  643. }
  644. net_lwip_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, require->result, ip_result, require->param);
  645. }
  646. else
  647. {
  648. net_lwip_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, 0, 0, require->param);
  649. }
  650. return LIST_DEL;
  651. }
  652. else
  653. {
  654. return LIST_PASS;
  655. }
  656. }
  657. static err_t net_lwip_dns_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  658. const ip_addr_t *addr, u16_t port)
  659. {
  660. Buffer_Struct msg_buf;
  661. Buffer_Struct tx_msg_buf = {0,0,0};
  662. struct pbuf *out_p;
  663. ip_addr_t *t_ip;
  664. int i;
  665. if (p)
  666. {
  667. OS_InitBuffer(&msg_buf, p->tot_len);
  668. pbuf_copy_partial(p, msg_buf.Data, p->tot_len, 0);
  669. pbuf_free(p);
  670. dns_run(&prvlwip.dns_client, &msg_buf, NULL, &i);
  671. OS_DeInitBuffer(&msg_buf);
  672. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  673. {
  674. dns_run(&prvlwip.dns_client, NULL, &tx_msg_buf, &i);
  675. if (tx_msg_buf.Pos)
  676. {
  677. out_p = pbuf_alloc(PBUF_RAW, tx_msg_buf.Pos, PBUF_ROM);
  678. if (out_p)
  679. {
  680. out_p->payload = tx_msg_buf.Data;
  681. if (prvlwip.dns_client.dns_server[i].type == IPADDR_TYPE_V4)
  682. {
  683. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip_addr;
  684. }
  685. else
  686. {
  687. t_ip = net_lwip_get_ip6(prvlwip.dns_adapter_index);
  688. prvlwip.dns_udp->local_ip = *t_ip;
  689. }
  690. err_t err = udp_sendto(prvlwip.dns_udp, out_p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  691. pbuf_free(out_p);
  692. }
  693. OS_DeInitBuffer(&tx_msg_buf);
  694. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  695. }
  696. }
  697. }
  698. if (!prvlwip.dns_client.is_run)
  699. {
  700. platform_stop_timer(prvlwip.dns_timer);
  701. }
  702. return ERR_OK;
  703. }
  704. static void net_lwip_dns_tx_next(Buffer_Struct *tx_msg_buf)
  705. {
  706. int i;
  707. err_t err;
  708. struct pbuf *p;
  709. ip_addr_t *t_ip;
  710. dns_run(&prvlwip.dns_client, NULL, tx_msg_buf, &i);
  711. if (tx_msg_buf->Pos)
  712. {
  713. p = pbuf_alloc(PBUF_RAW, tx_msg_buf->Pos, PBUF_ROM);
  714. if (p)
  715. {
  716. p->payload = tx_msg_buf->Data;
  717. if (prvlwip.dns_client.dns_server[i].type == IPADDR_TYPE_V4)
  718. {
  719. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip_addr;
  720. }
  721. else
  722. {
  723. t_ip = net_lwip_get_ip6(prvlwip.dns_adapter_index);
  724. prvlwip.dns_udp->local_ip = *t_ip;
  725. }
  726. err = udp_sendto(prvlwip.dns_udp, p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  727. pbuf_free(p);
  728. }
  729. OS_DeInitBuffer(tx_msg_buf);
  730. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  731. prvlwip.dns_client.new_result = 0;
  732. }
  733. }
  734. void net_lwip_init(void)
  735. {
  736. uint8_t i;
  737. for(i = 0; i < MAX_SOCK_NUM; i++)
  738. {
  739. INIT_LLIST_HEAD(&prvlwip.socket[i].wait_ack_head);
  740. INIT_LLIST_HEAD(&prvlwip.socket[i].tx_head);
  741. INIT_LLIST_HEAD(&prvlwip.socket[i].rx_head);
  742. prvlwip.socket[i].mutex = platform_create_mutex();
  743. }
  744. prvlwip.dns_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_COMMON_TIMER, 0);
  745. dns_init_client(&prvlwip.dns_client);
  746. }
  747. static ip_addr_t *net_lwip_get_ip6(uint8_t adapter_index)
  748. {
  749. int i;
  750. for(i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  751. {
  752. if (IP6_ADDR_PREFERRED == (prvlwip.lwip_netif[adapter_index]->ip6_addr_state[i] & IP6_ADDR_PREFERRED))
  753. {
  754. return &prvlwip.lwip_netif[adapter_index]->ip6_addr[i];
  755. }
  756. }
  757. for(i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  758. {
  759. if (prvlwip.lwip_netif[adapter_index]->ip6_addr_state[i] & IP6_ADDR_VALID)
  760. {
  761. return &prvlwip.lwip_netif[adapter_index]->ip6_addr[i];
  762. }
  763. }
  764. return NULL;
  765. }
  766. static void net_lwip_close_tcp(int socket_id)
  767. {
  768. prvlwip.socket[socket_id].pcb.tcp->sent = NULL;
  769. prvlwip.socket[socket_id].pcb.tcp->errf = NULL;
  770. prvlwip.socket[socket_id].pcb.tcp->poll = NULL;
  771. prvlwip.socket[socket_id].pcb.tcp->recv = tcp_recv_null;
  772. prvlwip.socket[socket_id].pcb.tcp->callback_arg = (uint32_t)(luat_mcu_tick64_ms()/1000);
  773. if (tcp_close(prvlwip.socket[socket_id].pcb.tcp))
  774. {
  775. tcp_abort(prvlwip.socket[socket_id].pcb.tcp);
  776. }
  777. prvlwip.socket[socket_id].pcb.tcp = NULL;
  778. }
  779. void net_lwip_do_event(OS_EVENT event)
  780. {
  781. // luat_network_cb_param_t cb_param;
  782. Buffer_Struct tx_msg_buf = {0,0,0};
  783. // HANDLE cur_task = luat_get_current_task();
  784. struct tcp_pcb *pcb, dpcb;
  785. struct netif *netif;
  786. // struct dhcp *dhcp;
  787. socket_data_t *p;
  788. ip_addr_t *p_ip;
  789. ip_addr_t *local_ip;
  790. struct pbuf *out_p;
  791. int error;
  792. uint32_t clat_temp;
  793. uint8_t need_clat;
  794. // PV_Union uPV;
  795. uint8_t socket_id;
  796. uint8_t adapter_index;
  797. socket_id = event.Param1;
  798. adapter_index = event.Param3;
  799. switch(event.ID)
  800. {
  801. case EV_LWIP_SOCKET_TX:
  802. SOCKET_LOCK(socket_id);
  803. if (prvlwip.socket[socket_id].in_use && prvlwip.socket[socket_id].pcb.ip)
  804. {
  805. if (prvlwip.socket[socket_id].is_tcp)
  806. {
  807. while (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  808. {
  809. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  810. if (p->len <= tcp_sndbuf(prvlwip.socket[socket_id].pcb.tcp))
  811. {
  812. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0))
  813. {
  814. llist_del(&p->node);
  815. llist_add_tail(&p->node, &prvlwip.socket[socket_id].wait_ack_head);
  816. }
  817. else
  818. {
  819. // NET_DBG("tcp buf is full, wait ack and send again");
  820. break;
  821. }
  822. }
  823. else
  824. {
  825. // NET_DBG("tcp buf is full, wait ack and send again");
  826. break;
  827. }
  828. }
  829. SOCKET_UNLOCK(socket_id);
  830. tcp_output(prvlwip.socket[socket_id].pcb.tcp);
  831. prvlwip.socket_busy |= (1 << socket_id);
  832. }
  833. else
  834. {
  835. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  836. if (p)
  837. {
  838. llist_del(&p->node);
  839. }
  840. SOCKET_UNLOCK(socket_id);
  841. if (p)
  842. {
  843. uint32_t len = p->len;
  844. out_p = pbuf_alloc(PBUF_RAW, p->len, PBUF_ROM);
  845. if (out_p)
  846. {
  847. out_p->payload = p->data;
  848. error = udp_sendto(prvlwip.socket[socket_id].pcb.udp, out_p, &p->ip, p->port);
  849. pbuf_free(out_p);
  850. }
  851. else
  852. {
  853. NET_DBG("mem err send fail");
  854. }
  855. free(p->data);
  856. free(p);
  857. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_TX_OK, socket_id, len, 0);
  858. }
  859. }
  860. }
  861. else
  862. {
  863. NET_DBG("adapter %d socket %d no in use! %x", adapter_index, socket_id, prvlwip.socket[socket_id].pcb.ip);
  864. SOCKET_UNLOCK(socket_id);
  865. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  866. }
  867. break;
  868. // case EV_LWIP_NETIF_INPUT: //大部分情况下需要SDK的lwip自行处理
  869. // netif = (struct netif *)event.Param3;
  870. // error = netif->input((struct pbuf *)event.Param1, netif);
  871. // if(error != ERR_OK)
  872. // {
  873. // NET_DBG("%d", error);
  874. // pbuf_free((struct pbuf *)event.Param1);
  875. // }
  876. // break;
  877. case EV_LWIP_COMMON_TIMER:
  878. #ifdef LUAT_USE_DNS
  879. net_lwip_dns_tx_next(&tx_msg_buf);
  880. #endif
  881. if (!prvlwip.dns_client.is_run)
  882. {
  883. platform_stop_timer(prvlwip.dns_timer);
  884. }
  885. break;
  886. case EV_LWIP_SOCKET_RX_DONE:
  887. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip || !prvlwip.socket[socket_id].is_tcp)
  888. {
  889. NET_DBG("error socket %d state %d,%x,%d", socket_id, prvlwip.socket[socket_id].in_use, prvlwip.socket[socket_id].pcb.ip, prvlwip.socket[socket_id].is_tcp);
  890. break;
  891. }
  892. if (prvlwip.socket[socket_id].fast_rx_ack)
  893. {
  894. NET_DBG("socket %d rx ack %dbytes, but in fast ack mode", socket_id, event.Param2);
  895. }
  896. tcp_recved(prvlwip.socket[socket_id].pcb.tcp, event.Param2);
  897. break;
  898. case EV_LWIP_SOCKET_CREATE:
  899. net_lwip_create_socket_now(adapter_index, socket_id);
  900. break;
  901. case EV_LWIP_SOCKET_CONNECT:
  902. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  903. {
  904. NET_DBG("adapter %d socket %d cannot use! %d,%x", adapter_index, socket_id, prvlwip.socket[socket_id].in_use, prvlwip.socket[socket_id].pcb.ip);
  905. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  906. break;
  907. }
  908. p_ip = (ip_addr_t *)event.Param2;
  909. local_ip = NULL;
  910. need_clat = 0;
  911. if (p_ip->type == IPADDR_TYPE_V4 && prvlwip.only_ipv6)
  912. {
  913. need_clat = 1;
  914. }
  915. else if (p_ip->type == IPADDR_TYPE_V6)
  916. {
  917. if (0x9bff6400 == p_ip->u_addr.ip6.addr[0])
  918. {
  919. need_clat = 2;
  920. }
  921. }
  922. if (need_clat)
  923. {
  924. //NET_DBG("adapter src is ipv6, dest is ipv4 use clat!!!");
  925. if (1 == need_clat)
  926. {
  927. clat_temp = p_ip->u_addr.ip4.addr;
  928. p_ip->type = IPADDR_TYPE_V6;
  929. p_ip->u_addr.ip6.addr[0] = 0x9bff6400;
  930. p_ip->u_addr.ip6.addr[1] = 0;
  931. p_ip->u_addr.ip6.addr[2] = 0;
  932. p_ip->u_addr.ip6.addr[3] = clat_temp;
  933. }
  934. local_ip = net_lwip_get_ip6(NW_ADAPTER_INDEX_LWIP_GPRS);
  935. NET_DBG("clat src %s", ipaddr_ntoa(local_ip));
  936. NET_DBG("clat dest %s", ipaddr_ntoa(p_ip));
  937. }
  938. else if (p_ip->type == IPADDR_TYPE_V4)
  939. {
  940. local_ip = &prvlwip.lwip_netif[adapter_index]->ip_addr;
  941. }
  942. else
  943. {
  944. local_ip = net_lwip_get_ip6(adapter_index);
  945. }
  946. if (!local_ip)
  947. {
  948. NET_DBG("netif no ip !!!!!!");
  949. net_lwip_tcp_error(adapter_index, socket_id);
  950. break;
  951. }
  952. if (prvlwip.socket[socket_id].is_tcp)
  953. {
  954. tcp_bind(prvlwip.socket[socket_id].pcb.tcp, local_ip, prvlwip.socket[socket_id].local_port);
  955. error = tcp_connect(prvlwip.socket[socket_id].pcb.tcp, p_ip, prvlwip.socket[socket_id].remote_port, net_lwip_tcp_connected_cb);
  956. if (error)
  957. {
  958. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  959. net_lwip_tcp_error(adapter_index, socket_id);
  960. }
  961. // else
  962. // {
  963. // prvlwip.socket_connect |= (1 << socket_id);
  964. // }
  965. }
  966. else
  967. {
  968. udp_bind(prvlwip.socket[socket_id].pcb.udp, local_ip, prvlwip.socket[socket_id].local_port);
  969. error = udp_connect(prvlwip.socket[socket_id].pcb.udp, p_ip, prvlwip.socket[socket_id].remote_port);
  970. if (error)
  971. {
  972. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  973. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  974. }
  975. else
  976. {
  977. if (!prvlwip.socket[socket_id].remote_port)
  978. {
  979. prvlwip.socket[socket_id].pcb.udp->flags &= ~UDP_FLAGS_CONNECTED;
  980. }
  981. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  982. }
  983. }
  984. break;
  985. case EV_LWIP_SOCKET_DNS:
  986. case EV_LWIP_SOCKET_DNS_IPV6:
  987. if (!prvlwip.dns_client.is_run)
  988. {
  989. platform_start_timer(prvlwip.dns_timer, 1000, 1);
  990. }
  991. dns_require_ipv6(&prvlwip.dns_client, event.Param1, event.Param2, event.Param3, event.ID - EV_LWIP_SOCKET_DNS);
  992. net_lwip_dns_tx_next(&tx_msg_buf);
  993. break;
  994. case EV_LWIP_SOCKET_LISTEN:
  995. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  996. {
  997. NET_DBG("adapter %d socket %d cannot use! %d,%x", adapter_index, socket_id, prvlwip.socket[socket_id].in_use, prvlwip.socket[socket_id].pcb.ip);
  998. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  999. break;
  1000. }
  1001. tcp_bind(prvlwip.socket[socket_id].pcb.tcp, NULL, prvlwip.socket[socket_id].local_port);
  1002. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->local_ip, IPADDR_TYPE_ANY);
  1003. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->remote_ip, IPADDR_TYPE_ANY);
  1004. prvlwip.socket[socket_id].listen_tcp = tcp_listen_with_backlog(prvlwip.socket[socket_id].pcb.tcp, 1);
  1005. if (!prvlwip.socket[socket_id].listen_tcp) {
  1006. NET_DBG("socket %d listen failed");
  1007. net_lwip_tcp_error(adapter_index, socket_id);
  1008. } else {
  1009. PV_Union uPV;
  1010. uPV.u16[0] = socket_id;
  1011. uPV.u16[1] = adapter_index;
  1012. prvlwip.socket[socket_id].listen_tcp->callback_arg = uPV.u32;
  1013. prvlwip.socket[socket_id].listen_tcp->accept = net_lwip_tcp_fast_accept_cb;
  1014. prvlwip.socket[socket_id].pcb.tcp = NULL;
  1015. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_LISTEN, socket_id, 0, 0);
  1016. }
  1017. break;
  1018. case EV_LWIP_SOCKET_ACCPET:
  1019. NET_DBG("no need accpet");
  1020. break;
  1021. case EV_LWIP_SOCKET_CLOSE:
  1022. if (!prvlwip.socket[socket_id].in_use)
  1023. {
  1024. NET_DBG("socket %d no in use!,%x", socket_id);
  1025. break;
  1026. }
  1027. if (prvlwip.socket[socket_id].listen_tcp)
  1028. {
  1029. tcp_close(prvlwip.socket[socket_id].listen_tcp);
  1030. prvlwip.socket[socket_id].listen_tcp = NULL;
  1031. if (prvlwip.socket[socket_id].pcb.tcp)
  1032. {
  1033. net_lwip_close_tcp(socket_id);
  1034. }
  1035. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  1036. break;
  1037. }
  1038. if (prvlwip.socket[socket_id].pcb.ip)
  1039. {
  1040. if (prvlwip.socket[socket_id].is_tcp)
  1041. {
  1042. net_lwip_close_tcp(socket_id);
  1043. }
  1044. else
  1045. {
  1046. udp_remove(prvlwip.socket[socket_id].pcb.udp);
  1047. }
  1048. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  1049. break;
  1050. }
  1051. if (prvlwip.socket[socket_id].remote_close)
  1052. {
  1053. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  1054. break;
  1055. }
  1056. break;
  1057. case EV_LWIP_NETIF_SET_IP: //一般由SDK的LWIP内部处理
  1058. netif = prvlwip.lwip_netif[event.Param3];
  1059. p_ip = (ip_addr_t *)event.Param1;
  1060. // if ((p_ip[0].type != IPADDR_TYPE_ANY) && p_ip[0].u_addr.ip4.addr)
  1061. // {
  1062. // dhcp_release_and_stop(netif);
  1063. // }
  1064. // netif_set_addr(netif, &p_ip[0].u_addr.ip4, &p_ip[1].u_addr.ip4, &p_ip[2].u_addr.ip4);
  1065. // netif_ip6_addr_set(netif, 1, &p_ip[3]);
  1066. // if (event.Param2)
  1067. // {
  1068. // netif_ip6_addr_set_state(netif, 1, IP6_ADDR_VALID);
  1069. // }
  1070. // else
  1071. // {
  1072. // netif_ip6_addr_set_state(netif, 1, IP6_ADDR_INVALID);
  1073. // }
  1074. free(p_ip);
  1075. break;
  1076. case EV_LWIP_NETIF_LINK_STATE:
  1077. // NET_DBG("%d,%d,%d", event.Param3, prvlwip.netif_network_ready[event.Param3], event.Param1);
  1078. // if (prvlwip.netif_network_ready[event.Param3] != event.Param1) //link up和down 一般由SDK的LWIP内部处理
  1079. // {
  1080. // if (event.Param1)
  1081. // {
  1082. // netif_set_link_up(prvlwip.lwip_netif[event.Param3]);
  1083. // }
  1084. // else
  1085. // {
  1086. // netif_set_link_down(prvlwip.lwip_netif[event.Param3]);
  1087. // }
  1088. // }
  1089. net_lwip_check_network_ready(event.Param3);
  1090. break;
  1091. // case EV_LWIP_NETIF_IPV6_BY_MAC: //一般由SDK的LWIP内部处理
  1092. // netif_create_ip6_linklocal_address(prvlwip.lwip_netif[event.Param3], 1);
  1093. // break;
  1094. default:
  1095. NET_DBG("unknow event %x,%x", event.ID, event.Param1);
  1096. break;
  1097. }
  1098. }
  1099. static void net_lwip_check_network_ready(uint8_t adapter_index)
  1100. {
  1101. int i;
  1102. char ip_string[64];
  1103. struct netif *netif = prvlwip.lwip_netif[adapter_index];
  1104. uint8_t active_flag = netif_is_flag_set(netif, NETIF_FLAG_LINK_UP);
  1105. if (prvlwip.netif_network_ready[adapter_index] != active_flag)
  1106. {
  1107. prvlwip.netif_network_ready[adapter_index] = active_flag;
  1108. if (!active_flag)
  1109. {
  1110. dns_clear(&prvlwip.dns_client);
  1111. prvlwip.dns_client.is_run = 0;
  1112. // NET_DBG("network not ready");
  1113. net_lwip_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 0, adapter_index);
  1114. }
  1115. else
  1116. {
  1117. // NET_DBG("network ready");
  1118. // for(i = 0; i < MAX_DNS_SERVER; i++)
  1119. // {
  1120. // if (prvlwip.dns_client.dns_server[i].type != 0xff)
  1121. // {
  1122. // NET_DBG("DNS%d:%s",i, ipaddr_ntoa_r(&prvlwip.dns_client.dns_server[i], ip_string, sizeof(ip_string)));
  1123. // }
  1124. // }
  1125. net_lwip_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 1, adapter_index);
  1126. }
  1127. }
  1128. }
  1129. static int net_lwip_check_socket(void *user_data, int socket_id, uint64_t tag)
  1130. {
  1131. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1132. if (socket_id >= MAX_SOCK_NUM) return -1;
  1133. if (prvlwip.socket[socket_id].tag != tag) return -1;
  1134. if (!prvlwip.socket[socket_id].in_use || prvlwip.socket[socket_id].state) return -1;
  1135. return 0;
  1136. }
  1137. static int net_lwip_socket_check(int socket_id, uint64_t tag, void *user_data)
  1138. {
  1139. return net_lwip_check_socket(user_data, socket_id, tag);
  1140. }
  1141. static uint8_t net_lwip_check_ready(void *user_data)
  1142. {
  1143. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return 0;
  1144. return (prvlwip.netif_network_ready[(uint32_t)user_data]);
  1145. }
  1146. static void net_lwip_create_socket_now(uint8_t adapter_index, uint8_t socket_id)
  1147. {
  1148. PV_Union uPV;
  1149. uPV.u16[0] = socket_id;
  1150. uPV.u16[1] = adapter_index;
  1151. if (prvlwip.socket[socket_id].is_tcp)
  1152. {
  1153. prvlwip.socket[socket_id].pcb.tcp = tcp_new();
  1154. if (!prvlwip.socket[socket_id].pcb.tcp)
  1155. {
  1156. NET_DBG("try to abort fin wait 1 tcp");
  1157. struct tcp_pcb *pcb, *dpcb;
  1158. uint32_t low_time = (uint32_t)(luat_mcu_tick64_ms() / 1000);
  1159. dpcb = NULL;
  1160. for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  1161. {
  1162. if (FIN_WAIT_1 == pcb->state)
  1163. {
  1164. if (((uint32_t)pcb->callback_arg) < low_time)
  1165. {
  1166. dpcb = pcb;
  1167. low_time = (uint32_t)pcb->callback_arg;
  1168. }
  1169. }
  1170. }
  1171. if (dpcb)
  1172. {
  1173. tcp_abort(dpcb);
  1174. }
  1175. prvlwip.socket[socket_id].pcb.tcp = tcp_new();
  1176. }
  1177. if (prvlwip.socket[socket_id].pcb.tcp)
  1178. {
  1179. tcp_bind_netif(prvlwip.socket[socket_id].pcb.tcp, prvlwip.lwip_netif[adapter_index]);
  1180. prvlwip.socket[socket_id].rx_wait_size = 0;
  1181. prvlwip.socket[socket_id].tx_wait_size = 0;
  1182. prvlwip.socket[socket_id].pcb.tcp->callback_arg = uPV.p;
  1183. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip_tcp_recv_cb;
  1184. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip_tcp_sent_cb;
  1185. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip_tcp_err_cb;
  1186. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  1187. prvlwip.socket[socket_id].fast_rx_ack = prvlwip.fast_rx_ack;
  1188. // tcp_set_flags(prvlwip.socket[socket_id].pcb.tcp, TCP_NODELAY);
  1189. }
  1190. else
  1191. {
  1192. NET_DBG("tcp pcb full!");
  1193. net_lwip_tcp_error(adapter_index, socket_id);
  1194. }
  1195. }
  1196. else
  1197. {
  1198. prvlwip.socket[socket_id].pcb.udp = udp_new();
  1199. if (prvlwip.socket[socket_id].pcb.udp)
  1200. {
  1201. udp_bind_netif(prvlwip.socket[socket_id].pcb.udp, prvlwip.lwip_netif[adapter_index]);
  1202. prvlwip.socket[socket_id].pcb.udp->recv_arg = uPV.p;
  1203. prvlwip.socket[socket_id].pcb.udp->recv = net_lwip_udp_recv_cb;
  1204. prvlwip.socket[socket_id].pcb.udp->so_options |= SOF_BROADCAST|SOF_REUSEADDR;
  1205. }
  1206. else
  1207. {
  1208. NET_DBG("udp pcb full!");
  1209. net_lwip_tcp_error(adapter_index, socket_id);
  1210. }
  1211. }
  1212. }
  1213. static int net_lwip_create_socket(uint8_t is_tcp, uint64_t *tag, void *param, uint8_t is_ipv6, void *user_data)
  1214. {
  1215. uint8_t index = (uint32_t)user_data;
  1216. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1217. int i, socket_id;
  1218. socket_id = -1;
  1219. OS_LOCK;
  1220. if (!prvlwip.socket[prvlwip.next_socket_index].in_use)
  1221. {
  1222. socket_id = prvlwip.next_socket_index;
  1223. prvlwip.next_socket_index++;
  1224. }
  1225. else
  1226. {
  1227. for (i = (prvlwip.next_socket_index + 1); i < MAX_SOCK_NUM; i++)
  1228. {
  1229. if (!prvlwip.socket[i].in_use)
  1230. {
  1231. socket_id = i;
  1232. prvlwip.next_socket_index = i + 1;
  1233. break;
  1234. }
  1235. }
  1236. if (socket_id < 0)
  1237. {
  1238. for (i = 0; i < MAX_SOCK_NUM; i++)
  1239. {
  1240. if (!prvlwip.socket[i].in_use)
  1241. {
  1242. socket_id = i;
  1243. prvlwip.next_socket_index = i + 1;
  1244. break;
  1245. }
  1246. }
  1247. }
  1248. }
  1249. if (prvlwip.next_socket_index >= MAX_SOCK_NUM)
  1250. {
  1251. prvlwip.next_socket_index = 0;
  1252. }
  1253. if (socket_id >= 0)
  1254. {
  1255. LWIP_ASSERT("socket must free before create", !prvlwip.socket[socket_id].pcb.ip);
  1256. prvlwip.socket_busy &= ~(1 << socket_id);
  1257. prvlwip.socket_connect &= ~(1 << socket_id);
  1258. prvlwip.socket_tag++;
  1259. *tag = prvlwip.socket_tag;
  1260. prvlwip.socket[socket_id].in_use = 1;
  1261. prvlwip.socket[socket_id].tag = *tag;
  1262. prvlwip.socket[socket_id].param = param;
  1263. prvlwip.socket[socket_id].is_tcp = is_tcp;
  1264. llist_traversal(&prvlwip.socket[socket_id].wait_ack_head, net_lwip_del_data_cache, NULL);
  1265. llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_del_data_cache, NULL);
  1266. llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_del_data_cache, NULL);
  1267. OS_UNLOCK;
  1268. if (net_lwip_check_in_sdk_task())
  1269. {
  1270. net_lwip_create_socket_now(index, socket_id);
  1271. return socket_id;
  1272. }
  1273. net_lwip_sdk_send_event(EV_LWIP_SOCKET_CREATE, socket_id, 0, user_data);
  1274. }
  1275. else
  1276. {
  1277. OS_UNLOCK;
  1278. }
  1279. return socket_id;
  1280. }
  1281. //作为client绑定一个port,并连接remote_ip和remote_port对应的server
  1282. static int net_lwip_socket_connect(int socket_id, uint64_t tag, uint16_t local_port, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
  1283. {
  1284. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1285. if (result) return result;
  1286. prvlwip.socket[socket_id].local_port = local_port;
  1287. prvlwip.socket[socket_id].remote_port = remote_port;
  1288. net_lwip_sdk_send_event(EV_LWIP_SOCKET_CONNECT, socket_id, remote_ip, user_data);
  1289. return 0;
  1290. }
  1291. //作为server绑定一个port,开始监听
  1292. static int net_lwip_socket_listen(int socket_id, uint64_t tag, uint16_t local_port, void *user_data)
  1293. {
  1294. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1295. if (result) return result;
  1296. prvlwip.socket[socket_id].local_port = local_port;
  1297. net_lwip_sdk_send_event(EV_LWIP_SOCKET_LISTEN, socket_id, local_port, user_data);
  1298. return 0;
  1299. }
  1300. //作为server接受一个client
  1301. static int net_lwip_socket_accept(int socket_id, uint64_t tag, luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
  1302. {
  1303. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1304. if (result) return result;
  1305. *remote_ip = prvlwip.socket[socket_id].pcb.tcp->remote_ip;
  1306. *remote_port = prvlwip.socket[socket_id].pcb.tcp->remote_port;
  1307. return 0;
  1308. }
  1309. //主动断开一个tcp连接,需要走完整个tcp流程,用户需要接收到close ok回调才能确认彻底断开
  1310. static int net_lwip_socket_disconnect(int socket_id, uint64_t tag, void *user_data)
  1311. {
  1312. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1313. if (result) return result;
  1314. prvlwip.socket[socket_id].state = 1;
  1315. net_lwip_sdk_send_event(EV_LWIP_SOCKET_CLOSE, socket_id, 1, user_data);
  1316. return 0;
  1317. }
  1318. static int net_lwip_socket_force_close(int socket_id, void *user_data)
  1319. {
  1320. if (prvlwip.socket[socket_id].in_use && !prvlwip.socket[socket_id].state)
  1321. {
  1322. prvlwip.socket[socket_id].state = 1;
  1323. net_lwip_sdk_send_event(EV_LWIP_SOCKET_CLOSE, socket_id, 0, user_data);
  1324. }
  1325. return 0;
  1326. }
  1327. static int net_lwip_socket_close(int socket_id, uint64_t tag, void *user_data)
  1328. {
  1329. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1330. if (socket_id >= MAX_SOCK_NUM) return -1;
  1331. if (prvlwip.socket[socket_id].tag != tag)
  1332. {
  1333. NET_DBG("socket %d used by other!", socket_id);
  1334. return -1;
  1335. }
  1336. if (!prvlwip.socket[socket_id].in_use) return 0;
  1337. net_lwip_socket_force_close(socket_id, user_data);
  1338. return 0;
  1339. }
  1340. static uint32_t net_lwip_socket_read_data(int socket_id, uint8_t *buf, uint32_t *read_len, uint32_t len, socket_data_t *p)
  1341. {
  1342. uint32_t dummy_len;
  1343. dummy_len = ((p->len - p->read_pos) > (len - *read_len))?(len - *read_len):(p->len - p->read_pos);
  1344. memcpy(buf, p->data + p->read_pos, dummy_len);
  1345. p->read_pos += dummy_len;
  1346. if (p->read_pos >= p->len)
  1347. {
  1348. if (prvlwip.socket[socket_id].is_tcp && !prvlwip.socket[socket_id].fast_rx_ack)
  1349. {
  1350. net_lwip_sdk_send_event(EV_LWIP_SOCKET_RX_DONE, socket_id, p->len, 0);
  1351. }
  1352. llist_del(&p->node);
  1353. free(p->data);
  1354. free(p);
  1355. }
  1356. *read_len += dummy_len;
  1357. return dummy_len;
  1358. }
  1359. static int net_lwip_socket_receive(int socket_id, uint64_t tag, uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
  1360. {
  1361. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1362. if (result) return result;
  1363. uint32_t read_len = 0;
  1364. if (buf)
  1365. {
  1366. SOCKET_LOCK(socket_id);
  1367. socket_data_t *p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  1368. if (prvlwip.socket[socket_id].is_tcp)
  1369. {
  1370. while((read_len < len) && p)
  1371. {
  1372. prvlwip.socket[socket_id].rx_wait_size -= net_lwip_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1373. p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  1374. }
  1375. }
  1376. else
  1377. {
  1378. if (p)
  1379. {
  1380. if (remote_ip)
  1381. {
  1382. *remote_ip = p->ip;
  1383. }
  1384. if (remote_port)
  1385. {
  1386. *remote_port = p->port;
  1387. }
  1388. prvlwip.socket[socket_id].rx_wait_size -= net_lwip_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1389. }
  1390. }
  1391. if (llist_empty(&prvlwip.socket[socket_id].rx_head))
  1392. {
  1393. prvlwip.socket[socket_id].rx_wait_size = 0;
  1394. }
  1395. SOCKET_UNLOCK(socket_id);
  1396. }
  1397. else
  1398. {
  1399. read_len = prvlwip.socket[socket_id].rx_wait_size;
  1400. }
  1401. return read_len;
  1402. }
  1403. static int net_lwip_socket_send(int socket_id, uint64_t tag, const uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
  1404. {
  1405. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1406. if (result) return result;
  1407. SOCKET_LOCK(socket_id);
  1408. uint32_t save_len = 0;
  1409. uint32_t dummy_len = 0;
  1410. socket_data_t *p;
  1411. if (prvlwip.socket[socket_id].is_tcp)
  1412. {
  1413. while(save_len < len)
  1414. {
  1415. dummy_len = ((len - save_len) > SOCKET_BUF_LEN)?SOCKET_BUF_LEN:(len - save_len);
  1416. p = net_lwip_create_data_node(socket_id, &buf[save_len], dummy_len, remote_ip, remote_port);
  1417. if (p)
  1418. {
  1419. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1420. }
  1421. else
  1422. {
  1423. SOCKET_UNLOCK(socket_id);
  1424. return -1;
  1425. }
  1426. save_len += dummy_len;
  1427. }
  1428. }
  1429. else
  1430. {
  1431. p = net_lwip_create_data_node(socket_id, buf, len, remote_ip, remote_port);
  1432. if (p)
  1433. {
  1434. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1435. }
  1436. else
  1437. {
  1438. SOCKET_UNLOCK(socket_id);
  1439. return -1;
  1440. }
  1441. }
  1442. SOCKET_UNLOCK(socket_id);
  1443. net_lwip_sdk_send_event(EV_LWIP_SOCKET_TX, socket_id, 0, user_data);
  1444. result = len;
  1445. return result;
  1446. }
  1447. void net_lwip_socket_clean(int *vaild_socket_list, uint32_t num, void *user_data)
  1448. {
  1449. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  1450. int socket_list[MAX_SOCK_NUM];
  1451. memset(socket_list, 0, sizeof(socket_list));
  1452. uint32_t i;
  1453. for(i = 0; i < num; i++)
  1454. {
  1455. if ( (vaild_socket_list[i] >= 0) && (vaild_socket_list[i] < MAX_SOCK_NUM) )
  1456. {
  1457. socket_list[vaild_socket_list[i]] = 1;
  1458. }
  1459. NET_DBG("%d,%d",i,vaild_socket_list[i]);
  1460. }
  1461. for(i = 0; i < MAX_SOCK_NUM; i++)
  1462. {
  1463. NET_DBG("%d,%d,%d,%d",i,socket_list[i],prvlwip.socket[i].in_use, !prvlwip.socket[i].state);
  1464. if ( !socket_list[i] )
  1465. {
  1466. net_lwip_socket_force_close(i, user_data);
  1467. }
  1468. }
  1469. }
  1470. static int net_lwip_getsockopt(int socket_id, uint64_t tag, int level, int optname, void *optval, uint32_t *optlen, void *user_data)
  1471. {
  1472. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1473. if (result) return result;
  1474. for(int i = 0; i < 10; i++)
  1475. {
  1476. if (!prvlwip.socket[socket_id].pcb.ip)
  1477. {
  1478. platform_task_sleep(1);
  1479. }
  1480. else
  1481. {
  1482. break;
  1483. }
  1484. }
  1485. if (!prvlwip.socket[socket_id].pcb.ip)
  1486. {
  1487. NET_DBG("socket %d not create try later! %d, %d, %x, %d", socket_id, level, optname, optval, optlen);
  1488. return -1;
  1489. }
  1490. switch (level)
  1491. {
  1492. /* Level: SOL_SOCKET */
  1493. case SOL_SOCKET:
  1494. switch (optname)
  1495. {
  1496. #if LWIP_TCP
  1497. case SO_ACCEPTCONN:
  1498. if (!prvlwip.socket[socket_id].is_tcp) {
  1499. return -1;
  1500. }
  1501. if ((prvlwip.socket[socket_id].pcb.tcp != NULL) && (prvlwip.socket[socket_id].pcb.tcp->state == LISTEN)) {
  1502. *(int *)optval = 1;
  1503. } else {
  1504. *(int *)optval = 0;
  1505. }
  1506. break;
  1507. #endif /* LWIP_TCP */
  1508. /* The option flags */
  1509. case SO_BROADCAST:
  1510. case SO_KEEPALIVE:
  1511. #if SO_REUSE
  1512. case SO_REUSEADDR:
  1513. #endif /* SO_REUSE */
  1514. if ((optname == SO_BROADCAST) && prvlwip.socket[socket_id].is_tcp) {
  1515. return -1;
  1516. }
  1517. optname = lwip_sockopt_to_ipopt(optname);
  1518. *(int *)optval = ip_get_option(prvlwip.socket[socket_id].pcb.ip, optname);
  1519. break;
  1520. case SO_TYPE:
  1521. if (prvlwip.socket[socket_id].is_tcp)
  1522. {
  1523. *(int *)optval = SOCK_STREAM;
  1524. }
  1525. else
  1526. {
  1527. *(int *)optval = SOCK_DGRAM;
  1528. }
  1529. break;
  1530. case SO_ERROR:
  1531. return -1;
  1532. break;
  1533. #if LWIP_UDP
  1534. case SO_NO_CHECK:
  1535. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1536. #if LWIP_UDPLITE
  1537. if (udp_is_flag_set(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_UDPLITE)) {
  1538. /* this flag is only available for UDP, not for UDP lite */
  1539. done_socket(sock);
  1540. return -1;
  1541. }
  1542. #endif /* LWIP_UDPLITE */
  1543. *(int *)optval = udp_is_flag_set(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_NOCHKSUM) ? 1 : 0;
  1544. break;
  1545. #endif /* LWIP_UDP*/
  1546. default:
  1547. return -1;
  1548. break;
  1549. } /* switch (optname) */
  1550. break;
  1551. /* Level: IPPROTO_IP */
  1552. case IPPROTO_IP:
  1553. switch (optname) {
  1554. case IP_TTL:
  1555. *(int *)optval = prvlwip.socket[socket_id].pcb.ip->ttl;
  1556. break;
  1557. case IP_TOS:
  1558. *(int *)optval = prvlwip.socket[socket_id].pcb.ip->tos;
  1559. break;
  1560. #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP
  1561. case IP_MULTICAST_TTL:
  1562. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1563. *(u8_t *)optval = udp_get_multicast_ttl(prvlwip.socket[socket_id].pcb.udp);
  1564. break;
  1565. case IP_MULTICAST_IF:
  1566. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1567. ((struct in_addr *)optval)->s_addr = prvlwip.socket[socket_id].pcb.udp->mcast_ip4.addr;
  1568. break;
  1569. case IP_MULTICAST_LOOP:
  1570. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1571. if ((prvlwip.socket[socket_id].pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) {
  1572. *(u8_t *)optval = 1;
  1573. } else {
  1574. *(u8_t *)optval = 0;
  1575. }
  1576. break;
  1577. #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */
  1578. default:
  1579. return -1;
  1580. } /* switch (optname) */
  1581. break;
  1582. #if LWIP_TCP
  1583. /* Level: IPPROTO_TCP */
  1584. case IPPROTO_TCP:
  1585. /* Special case: all IPPROTO_TCP option take an int */
  1586. if (!prvlwip.socket[socket_id].is_tcp) return -1;
  1587. if (prvlwip.socket[socket_id].pcb.tcp->state == LISTEN) {
  1588. return -1;
  1589. }
  1590. switch (optname) {
  1591. case TCP_NODELAY:
  1592. *(int *)optval = tcp_nagle_disabled(prvlwip.socket[socket_id].pcb.tcp);
  1593. break;
  1594. case TCP_KEEPALIVE:
  1595. *(int *)optval = (int)prvlwip.socket[socket_id].pcb.tcp->keep_idle;
  1596. break;
  1597. #if LWIP_TCP_KEEPALIVE
  1598. case TCP_KEEPIDLE:
  1599. *(int *)optval = (int)(prvlwip.socket[socket_id].pcb.tcp->keep_idle / 1000);
  1600. break;
  1601. case TCP_KEEPINTVL:
  1602. *(int *)optval = (int)(prvlwip.socket[socket_id].pcb.tcp->keep_intvl / 1000);
  1603. break;
  1604. case TCP_KEEPCNT:
  1605. *(int *)optval = (int)prvlwip.socket[socket_id].pcb.tcp->keep_cnt;
  1606. break;
  1607. #endif /* LWIP_TCP_KEEPALIVE */
  1608. default:
  1609. return -1;
  1610. break;
  1611. } /* switch (optname) */
  1612. break;
  1613. #endif /* LWIP_TCP */
  1614. #if LWIP_UDP && LWIP_UDPLITE
  1615. /* Level: IPPROTO_UDPLITE */
  1616. case IPPROTO_UDPLITE:
  1617. /* Special case: all IPPROTO_UDPLITE option take an int */
  1618. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
  1619. /* If this is no UDP lite socket, ignore any options. */
  1620. if (!NETCONNTYPE_ISUDPLITE(netconn_type(sock->conn))) {
  1621. done_socket(sock);
  1622. return ENOPROTOOPT;
  1623. }
  1624. switch (optname) {
  1625. case UDPLITE_SEND_CSCOV:
  1626. *(int *)optval = prvlwip.socket[socket_id].pcb.udp->chksum_len_tx;
  1627. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n",
  1628. s, (*(int *)optval)) );
  1629. break;
  1630. case UDPLITE_RECV_CSCOV:
  1631. *(int *)optval = prvlwip.socket[socket_id].pcb.udp->chksum_len_rx;
  1632. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n",
  1633. s, (*(int *)optval)) );
  1634. break;
  1635. default:
  1636. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  1637. s, optname));
  1638. err = ENOPROTOOPT;
  1639. break;
  1640. } /* switch (optname) */
  1641. break;
  1642. #endif /* LWIP_UDP */
  1643. /* Level: IPPROTO_RAW */
  1644. case IPPROTO_RAW:
  1645. return -1;
  1646. default:
  1647. return -1;
  1648. break;
  1649. } /* switch (level) */
  1650. return 0;
  1651. }
  1652. static int net_lwip_setsockopt(int socket_id, uint64_t tag, int level, int optname, const void *optval, uint32_t optlen, void *user_data)
  1653. {
  1654. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1655. if (result) return result;
  1656. for(int i = 0; i < 10; i++)
  1657. {
  1658. if (!prvlwip.socket[socket_id].pcb.ip)
  1659. {
  1660. platform_task_sleep(1);
  1661. }
  1662. else
  1663. {
  1664. break;
  1665. }
  1666. }
  1667. if (!prvlwip.socket[socket_id].pcb.ip)
  1668. {
  1669. NET_DBG("socket %d not create try later! %d, %d, %x, %d", socket_id, level, optname, optval, optlen);
  1670. return -1;
  1671. }
  1672. switch (level)
  1673. {
  1674. /* Level: SOL_SOCKET */
  1675. case SOL_SOCKET:
  1676. switch (optname)
  1677. {
  1678. /* SO_ACCEPTCONN is get-only */
  1679. /* The option flags */
  1680. case SO_BROADCAST:
  1681. case SO_KEEPALIVE:
  1682. #if SO_REUSE
  1683. case SO_REUSEADDR:
  1684. #endif /* SO_REUSE */
  1685. if ((optname == SO_BROADCAST) &&
  1686. (prvlwip.socket[socket_id].is_tcp)) {
  1687. return -1;
  1688. }
  1689. optname = lwip_sockopt_to_ipopt(optname);
  1690. if (*(const int *)optval) {
  1691. ip_set_option(prvlwip.socket[socket_id].pcb.ip, optname);
  1692. } else {
  1693. ip_reset_option(prvlwip.socket[socket_id].pcb.ip, optname);
  1694. }
  1695. break;
  1696. #if LWIP_UDP
  1697. case SO_NO_CHECK:
  1698. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1699. if (*(const int *)optval) {
  1700. udp_set_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_NOCHKSUM);
  1701. } else {
  1702. udp_clear_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_NOCHKSUM);
  1703. }
  1704. break;
  1705. #endif /* LWIP_UDP */
  1706. default:
  1707. return -1;
  1708. } /* switch (optname) */
  1709. break;
  1710. /* Level: IPPROTO_IP */
  1711. case IPPROTO_IP:
  1712. switch (optname)
  1713. {
  1714. case IP_TTL:
  1715. prvlwip.socket[socket_id].pcb.ip->ttl = (u8_t)(*(const int *)optval);
  1716. break;
  1717. case IP_TOS:
  1718. prvlwip.socket[socket_id].pcb.ip->tos = (u8_t)(*(const int *)optval);
  1719. break;
  1720. #if LWIP_NETBUF_RECVINFO
  1721. case IP_PKTINFO:
  1722. if (*(const int *)optval) {
  1723. prvlwip.socket[socket_id].flags |= NETCONN_FLAG_PKTINFO;
  1724. } else {
  1725. prvlwip.socket[socket_id].flags &= ~NETCONN_FLAG_PKTINFO;
  1726. }
  1727. break;
  1728. #endif /* LWIP_NETBUF_RECVINFO */
  1729. #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP
  1730. case IP_MULTICAST_TTL:
  1731. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1732. udp_set_multicast_ttl(prvlwip.socket[socket_id].pcb.udp, (u8_t)(*(const u8_t *)optval));
  1733. break;
  1734. case IP_MULTICAST_IF:
  1735. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1736. prvlwip.socket[socket_id].pcb.udp->mcast_ip4.addr = ((struct in_addr *)optval)->s_addr;
  1737. break;
  1738. case IP_MULTICAST_LOOP:
  1739. if (*(const u8_t *)optval) {
  1740. udp_set_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
  1741. } else {
  1742. udp_clear_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
  1743. }
  1744. break;
  1745. #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */
  1746. default:
  1747. return -1;
  1748. break;
  1749. } /* switch (optname) */
  1750. break;
  1751. #if LWIP_TCP
  1752. /* Level: IPPROTO_TCP */
  1753. case IPPROTO_TCP:
  1754. /* Special case: all IPPROTO_TCP option take an int */
  1755. if (!prvlwip.socket[socket_id].is_tcp) return -1;
  1756. if (prvlwip.socket[socket_id].pcb.tcp->state == LISTEN) {
  1757. return -1;
  1758. }
  1759. switch (optname)
  1760. {
  1761. case TCP_NODELAY:
  1762. if (*(const int *)optval) {
  1763. tcp_nagle_disable(prvlwip.socket[socket_id].pcb.tcp);
  1764. } else {
  1765. tcp_nagle_enable(prvlwip.socket[socket_id].pcb.tcp);
  1766. }
  1767. break;
  1768. case TCP_KEEPALIVE:
  1769. prvlwip.socket[socket_id].pcb.tcp->keep_idle = (u32_t)(*(const int *)optval);
  1770. break;
  1771. #if LWIP_TCP_KEEPALIVE
  1772. case TCP_KEEPIDLE:
  1773. prvlwip.socket[socket_id].pcb.tcp->keep_idle = 1000 * (u32_t)(*(const int *)optval);
  1774. break;
  1775. case TCP_KEEPINTVL:
  1776. prvlwip.socket[socket_id].pcb.tcp->keep_intvl = 1000 * (u32_t)(*(const int *)optval);
  1777. break;
  1778. case TCP_KEEPCNT:
  1779. prvlwip.socket[socket_id].pcb.tcp->keep_cnt = (u32_t)(*(const int *)optval);
  1780. break;
  1781. #endif /* LWIP_TCP_KEEPALIVE */
  1782. default:
  1783. return -1;
  1784. break;
  1785. } /* switch (optname) */
  1786. break;
  1787. #endif /* LWIP_TCP*/
  1788. #if LWIP_IPV6
  1789. /* Level: IPPROTO_IPV6 */
  1790. case IPPROTO_IPV6:
  1791. return -1;
  1792. break;
  1793. #endif /* LWIP_IPV6 */
  1794. /* Level: IPPROTO_RAW */
  1795. case IPPROTO_RAW:
  1796. return -1;
  1797. break;
  1798. default:
  1799. return -1;
  1800. } /* switch (level) */
  1801. return 0;
  1802. }
  1803. static int net_lwip_get_local_ip_info(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, void *user_data)
  1804. {
  1805. uint8_t index = (uint32_t)user_data;
  1806. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1807. if (!prvlwip.lwip_netif[index]) return -1;
  1808. *ip = prvlwip.lwip_netif[index]->ip_addr;
  1809. *submask = prvlwip.lwip_netif[index]->netmask;
  1810. *gateway = prvlwip.lwip_netif[index]->gw;
  1811. return 0;
  1812. }
  1813. static int net_lwip_get_full_ip_info(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, luat_ip_addr_t *ipv6, void *user_data)
  1814. {
  1815. uint8_t index = (uint32_t)user_data;
  1816. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1817. if (!prvlwip.lwip_netif[index]) return -1;
  1818. *ip = prvlwip.lwip_netif[index]->ip_addr;
  1819. *submask = prvlwip.lwip_netif[index]->netmask;
  1820. *gateway = prvlwip.lwip_netif[index]->gw;
  1821. luat_ip_addr_t *local_ip = net_lwip_get_ip6(index);
  1822. if (local_ip)
  1823. {
  1824. *ipv6 = *local_ip;
  1825. }
  1826. else
  1827. {
  1828. ipv6->type = 0xff;
  1829. }
  1830. return 0;
  1831. }
  1832. static int net_lwip_user_cmd(int socket_id, uint64_t tag, uint32_t cmd, uint32_t value, void *user_data)
  1833. {
  1834. return 0;
  1835. }
  1836. static int net_lwip_dns(const char *domain_name, uint32_t len, void *param, void *user_data)
  1837. {
  1838. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1839. char *prv_domain_name = (char *)zalloc(len + 1);
  1840. memcpy(prv_domain_name, domain_name, len);
  1841. net_lwip_sdk_send_event(EV_LWIP_SOCKET_DNS, prv_domain_name, param, user_data);
  1842. return 0;
  1843. }
  1844. static int net_lwip_dns_ipv6(const char *domain_name, uint32_t len, void *param, void *user_data)
  1845. {
  1846. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1847. uint8_t index = (uint32_t)user_data;
  1848. char *prv_domain_name = (char *)zalloc(len + 1);
  1849. memcpy(prv_domain_name, domain_name, len);
  1850. net_lwip_sdk_send_event(net_lwip_get_ip6(index)?EV_LWIP_SOCKET_DNS_IPV6:EV_LWIP_SOCKET_DNS, prv_domain_name, param, user_data);
  1851. return 0;
  1852. }
  1853. static int net_lwip_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data)
  1854. {
  1855. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1856. if (server_index >= MAX_DNS_SERVER) return -1;
  1857. prvlwip.dns_client.dns_server[server_index] = *ip;
  1858. prvlwip.dns_client.is_static_dns[server_index] = 1;
  1859. return 0;
  1860. }
  1861. static int net_lwip_set_mac(uint8_t *mac, void *user_data)
  1862. {
  1863. uint8_t index = (uint32_t)user_data;
  1864. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1865. if (!prvlwip.lwip_netif[index]) return -1;
  1866. memcpy(prvlwip.lwip_netif[index]->hwaddr, mac, 6);
  1867. return -1;
  1868. }
  1869. int net_lwip_set_static_ip(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, luat_ip_addr_t *ipv6, void *user_data)
  1870. {
  1871. uint8_t index = (uint32_t)user_data;
  1872. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1873. if (!prvlwip.lwip_netif[index]) return -1;
  1874. luat_ip_addr_t *p_ip = zalloc(sizeof(luat_ip_addr_t) * 5);
  1875. p_ip[0] = ip?(*ip):ip_addr_any_type;
  1876. p_ip[1] = submask?(*submask):ip_addr_any_type;
  1877. p_ip[2] = gateway?(*gateway):ip_addr_any_type;
  1878. p_ip[3] = ipv6?(*ipv6):ip_addr_any_type;
  1879. net_lwip_sdk_send_event(EV_LWIP_NETIF_SET_IP, p_ip, ipv6, user_data);
  1880. return 0;
  1881. }
  1882. static int32_t net_lwip_dummy_callback(void *pData, void *pParam)
  1883. {
  1884. return 0;
  1885. }
  1886. static void net_lwip_socket_set_callback(CBFuncEx_t cb_fun, void *param, void *user_data)
  1887. {
  1888. uint8_t index = (uint32_t)user_data;
  1889. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  1890. prvlwip.socket_cb[index] = cb_fun?cb_fun:net_lwip_dummy_callback;
  1891. prvlwip.user_data[index] = param;
  1892. }
  1893. static network_adapter_info prv_net_lwip_adapter =
  1894. {
  1895. .check_ready = net_lwip_check_ready,
  1896. .create_soceket = net_lwip_create_socket,
  1897. .socket_connect = net_lwip_socket_connect,
  1898. .socket_listen = net_lwip_socket_listen,
  1899. .socket_accept = net_lwip_socket_accept,
  1900. .socket_disconnect = net_lwip_socket_disconnect,
  1901. .socket_close = net_lwip_socket_close,
  1902. .socket_force_close = net_lwip_socket_force_close,
  1903. .socket_receive = net_lwip_socket_receive,
  1904. .socket_send = net_lwip_socket_send,
  1905. .socket_check = net_lwip_socket_check,
  1906. .socket_clean = net_lwip_socket_clean,
  1907. .getsockopt = net_lwip_getsockopt,
  1908. .setsockopt = net_lwip_setsockopt,
  1909. .user_cmd = net_lwip_user_cmd,
  1910. .dns = net_lwip_dns,
  1911. .dns_ipv6 = net_lwip_dns_ipv6,
  1912. .set_dns_server = net_lwip_set_dns_server,
  1913. .set_mac = net_lwip_set_mac,
  1914. .set_static_ip = net_lwip_set_static_ip,
  1915. .get_local_ip_info = net_lwip_get_local_ip_info,
  1916. .get_full_ip_info = net_lwip_get_full_ip_info,
  1917. .socket_set_callback = net_lwip_socket_set_callback,
  1918. .name = "lwip",
  1919. .max_socket_num = MAX_SOCK_NUM,
  1920. .no_accept = 1,
  1921. .is_posix = 1,
  1922. };
  1923. void net_lwip_register_adapter(uint8_t adapter_index)
  1924. {
  1925. network_register_adapter(adapter_index, &prv_net_lwip_adapter, adapter_index);
  1926. }
  1927. int net_lwip_check_all_ack(int socket_id)
  1928. {
  1929. if (!llist_empty(&prvlwip.socket[socket_id].wait_ack_head))
  1930. {
  1931. NET_DBG("socekt %d not all ack", socket_id);
  1932. prvlwip.socket_busy |= (1 << socket_id);
  1933. return -1;
  1934. }
  1935. if (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  1936. {
  1937. NET_DBG("socekt %d not all send", socket_id);
  1938. prvlwip.socket_busy |= (1 << socket_id);
  1939. return -1;
  1940. }
  1941. if (prvlwip.socket[socket_id].pcb.tcp->snd_buf != TCP_SND_BUF)
  1942. {
  1943. NET_DBG("socket %d send buf %ubytes, need %u",socket_id, prvlwip.socket[socket_id].pcb.tcp->snd_buf, TCP_SND_BUF);
  1944. prvlwip.socket_busy |= (1 << socket_id);
  1945. }
  1946. else
  1947. {
  1948. prvlwip.socket_busy &= ~(1 << socket_id);
  1949. }
  1950. return 0;
  1951. }
  1952. void net_lwip_check_switch(uint8_t onoff)
  1953. {
  1954. prvlwip.check_enable = onoff;
  1955. }
  1956. uint8_t soc_network_check_free_hook(void)
  1957. {
  1958. if (prvlwip.check_enable)
  1959. {
  1960. return !(prvlwip.dns_client.is_run || prvlwip.socket_busy || prvlwip.socket_connect);
  1961. }
  1962. else
  1963. {
  1964. return 1;
  1965. }
  1966. }
  1967. void net_lwip_set_netif(uint8_t adapter_index, struct netif *netif)
  1968. {
  1969. prvlwip.lwip_netif[adapter_index] = netif;
  1970. // switch(adapter_index)
  1971. // {
  1972. // case NW_ADAPTER_INDEX_LWIP_WIFI_STA:
  1973. // case NW_ADAPTER_INDEX_LWIP_WIFI_AP:
  1974. // case NW_ADAPTER_INDEX_LWIP_ETH:
  1975. // prvlwip.lwip_netif[adapter_index]->flags |= (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET|NETIF_FLAG_BROADCAST);
  1976. // break;
  1977. // }
  1978. }
  1979. void net_lwip_set_dns_adapter(uint8_t adapter_index)
  1980. {
  1981. if (!prvlwip.dns_udp)
  1982. {
  1983. prvlwip.dns_udp = udp_new();
  1984. prvlwip.dns_udp->recv = net_lwip_dns_recv_cb;
  1985. udp_bind(prvlwip.dns_udp, NULL, 55);
  1986. }
  1987. prvlwip.dns_adapter_index = adapter_index;
  1988. udp_bind_netif(prvlwip.dns_udp, prvlwip.lwip_netif[adapter_index]);
  1989. }
  1990. struct netif * net_lwip_get_netif(uint8_t adapter_index)
  1991. {
  1992. return prvlwip.lwip_netif[adapter_index];
  1993. }
  1994. void net_lwip_input_packets(struct netif *netif, struct pbuf *p)
  1995. {
  1996. net_lwip_sdk_send_event(EV_LWIP_NETIF_INPUT, p, 0, netif);
  1997. }
  1998. void net_lwip_set_link_state(uint8_t adapter_index, uint8_t onoff)
  1999. {
  2000. net_lwip_sdk_send_event(EV_LWIP_NETIF_LINK_STATE, onoff, 0, adapter_index);
  2001. }
  2002. void net_lwip_set_ipv6_by_mac(uint8_t adapter_index)
  2003. {
  2004. net_lwip_sdk_send_event(EV_LWIP_NETIF_IPV6_BY_MAC, 0, 0, adapter_index);
  2005. }
  2006. void net_lwip_set_rx_fast_ack(uint8_t adapter_index, uint8_t onoff)
  2007. {
  2008. prvlwip.fast_rx_ack = onoff;
  2009. }
  2010. void net_lwip_ping_response(struct netif *inp, struct pbuf *p, uint8_t type) {;}
  2011. #endif