net_lwip.c 67 KB

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