luat_network_lwip_tcpip_cb.c 59 KB

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