net_lwip.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  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. {
  497. prvlwip.socket[socket_id].remote_close = 1;
  498. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_REMOTE_CLOSE, socket_id, 0, 0);
  499. }
  500. }
  501. else
  502. {
  503. net_lwip_tcp_error(adapter_index, socket_id);
  504. }
  505. return ERR_OK;
  506. }
  507. static err_t net_lwip_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb,
  508. u16_t len)
  509. {
  510. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  511. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  512. volatile uint16_t check_len = 0;
  513. volatile uint32_t rest_len;
  514. socket_data_t *p;
  515. SOCKET_LOCK(socket_id);
  516. while(check_len < len)
  517. {
  518. if (llist_empty(&prvlwip.socket[socket_id].wait_ack_head))
  519. {
  520. NET_DBG("!");
  521. goto SOCEKT_ERROR;
  522. }
  523. p = (socket_data_t *)prvlwip.socket[socket_id].wait_ack_head.next;
  524. rest_len = p->len - p->read_pos;
  525. if ((len - check_len) >= rest_len)
  526. {
  527. // NET_DBG("adapter %d socket %d, %ubytes ack", adapter_index, socket_id, p->len);
  528. llist_del(&p->node);
  529. free(p->data);
  530. free(p);
  531. check_len += rest_len;
  532. }
  533. else
  534. {
  535. p->read_pos += (len - check_len);
  536. check_len = len;
  537. // NET_DBG("adapter %d socket %d, all %ubytes ack %ubytes ", adapter_index, socket_id, p->len, p->read_pos);
  538. }
  539. }
  540. while (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  541. {
  542. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  543. if (p)
  544. {
  545. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0))
  546. {
  547. llist_del(&p->node);
  548. llist_add_tail(&p->node, &prvlwip.socket[socket_id].wait_ack_head);
  549. }
  550. else
  551. {
  552. NET_DBG("tcp buf is full, wait ack and send again");
  553. break;
  554. }
  555. }
  556. }
  557. SOCKET_UNLOCK(socket_id);
  558. tcp_output(tpcb);
  559. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_TX_OK, socket_id, len, 0);
  560. return ERR_OK;
  561. SOCEKT_ERROR:
  562. SOCKET_UNLOCK(socket_id);
  563. net_lwip_tcp_error(adapter_index, socket_id);
  564. return ERR_OK;
  565. }
  566. static err_t net_lwip_tcp_err_cb(void *arg, err_t err)
  567. {
  568. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  569. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  570. if (!prvlwip.socket[socket_id].state && !prvlwip.socket[socket_id].remote_close)
  571. {
  572. NET_DBG("adapter %d socket %d not closing, but error %d", adapter_index, socket_id, err);
  573. prvlwip.socket[socket_id].pcb.ip = NULL;
  574. net_lwip_tcp_error(adapter_index, socket_id);
  575. }
  576. return 0;
  577. }
  578. static err_t net_lwip_tcp_fast_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  579. {
  580. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  581. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  582. if (err || !newpcb)
  583. {
  584. net_lwip_tcp_error(adapter_index, socket_id);
  585. return 0;
  586. }
  587. prvlwip.socket[socket_id].pcb.tcp = newpcb;
  588. prvlwip.socket[socket_id].rx_wait_size = 0;
  589. prvlwip.socket[socket_id].tx_wait_size = 0;
  590. prvlwip.socket[socket_id].pcb.tcp->callback_arg = arg;
  591. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip_tcp_recv_cb;
  592. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip_tcp_sent_cb;
  593. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip_tcp_err_cb;
  594. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  595. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  596. return ERR_OK;
  597. }
  598. static err_t net_lwip_tcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  599. {
  600. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  601. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  602. return ERR_OK;
  603. }
  604. static err_t net_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  605. const ip_addr_t *addr, u16_t port)
  606. {
  607. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  608. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  609. uint16_t len;
  610. if (p)
  611. {
  612. len = p->tot_len;
  613. if (net_lwip_rx_data(socket_id, p, addr, port))
  614. {
  615. NET_DBG("no memory!");
  616. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  617. }
  618. else
  619. {
  620. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_RX_NEW, socket_id, len, 0);
  621. }
  622. pbuf_free(p);
  623. }
  624. return ERR_OK;
  625. }
  626. static int32_t net_lwip_dns_check_result(void *data, void *param)
  627. {
  628. luat_dns_require_t *require = (luat_dns_require_t *)data;
  629. if (require->result != 0)
  630. {
  631. free(require->uri.Data);
  632. require->uri.Data = NULL;
  633. if (require->result > 0)
  634. {
  635. luat_dns_ip_result *ip_result = zalloc(sizeof(luat_dns_ip_result) * require->result);
  636. int i;
  637. for(i = 0; i < require->result; i++)
  638. {
  639. ip_result[i] = require->ip_result[i];
  640. }
  641. net_lwip_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, require->result, ip_result, require->param);
  642. }
  643. else
  644. {
  645. net_lwip_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, 0, 0, require->param);
  646. }
  647. return LIST_DEL;
  648. }
  649. else
  650. {
  651. return LIST_PASS;
  652. }
  653. }
  654. static err_t net_lwip_dns_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  655. const ip_addr_t *addr, u16_t port)
  656. {
  657. Buffer_Struct msg_buf;
  658. Buffer_Struct tx_msg_buf = {0,0,0};
  659. struct pbuf *out_p;
  660. int i;
  661. if (p)
  662. {
  663. OS_InitBuffer(&msg_buf, p->tot_len);
  664. pbuf_copy_partial(p, msg_buf.Data, p->tot_len, 0);
  665. pbuf_free(p);
  666. dns_run(&prvlwip.dns_client, &msg_buf, NULL, &i);
  667. OS_DeInitBuffer(&msg_buf);
  668. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  669. {
  670. dns_run(&prvlwip.dns_client, NULL, &tx_msg_buf, &i);
  671. if (tx_msg_buf.Pos)
  672. {
  673. out_p = pbuf_alloc(PBUF_RAW, tx_msg_buf.Pos, PBUF_ROM);
  674. if (out_p)
  675. {
  676. out_p->payload = tx_msg_buf.Data;
  677. if (prvlwip.dns_client.dns_server[i].type == IPADDR_TYPE_V4)
  678. {
  679. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip_addr;
  680. }
  681. else
  682. {
  683. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  684. {
  685. if (prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip6_addr_state[i] & IP6_ADDR_VALID)
  686. {
  687. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip6_addr[i];
  688. break;
  689. }
  690. }
  691. }
  692. err_t err = udp_sendto(prvlwip.dns_udp, out_p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  693. pbuf_free(out_p);
  694. }
  695. OS_DeInitBuffer(&tx_msg_buf);
  696. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  697. }
  698. }
  699. }
  700. return ERR_OK;
  701. }
  702. static void net_lwip_dns_tx_next(Buffer_Struct *tx_msg_buf)
  703. {
  704. int i;
  705. err_t err;
  706. struct pbuf *p;
  707. dns_run(&prvlwip.dns_client, NULL, tx_msg_buf, &i);
  708. if (tx_msg_buf->Pos)
  709. {
  710. p = pbuf_alloc(PBUF_RAW, tx_msg_buf->Pos, PBUF_ROM);
  711. if (p)
  712. {
  713. p->payload = tx_msg_buf->Data;
  714. if (prvlwip.dns_client.dns_server[i].type == IPADDR_TYPE_V4)
  715. {
  716. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip_addr;
  717. }
  718. else
  719. {
  720. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  721. {
  722. if (prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip6_addr_state[i] & IP6_ADDR_VALID)
  723. {
  724. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip6_addr[i];
  725. break;
  726. }
  727. }
  728. }
  729. err = udp_sendto(prvlwip.dns_udp, p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  730. pbuf_free(p);
  731. }
  732. OS_DeInitBuffer(tx_msg_buf);
  733. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  734. }
  735. }
  736. uint32_t net_lwip_rand()
  737. {
  738. PV_Union uPV;
  739. luat_crypto_trng(uPV.u8, 4);
  740. return uPV.u32;
  741. }
  742. void net_lwip_init(void)
  743. {
  744. uint8_t i;
  745. for(i = 0; i < MAX_SOCK_NUM; i++)
  746. {
  747. INIT_LLIST_HEAD(&prvlwip.socket[i].wait_ack_head);
  748. INIT_LLIST_HEAD(&prvlwip.socket[i].tx_head);
  749. INIT_LLIST_HEAD(&prvlwip.socket[i].rx_head);
  750. prvlwip.socket[i].mutex = platform_create_mutex();
  751. }
  752. prvlwip.tcp_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_TCP_TIMER, 0);
  753. prvlwip.common_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_COMMON_TIMER, 0);
  754. prvlwip.fast_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_FAST_TIMER, 0);
  755. prvlwip.dhcp_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_DHCP_TIMER, 0);
  756. tcp_ticks = luat_mcu_tick64_ms() / TCP_SLOW_INTERVAL;
  757. prvlwip.last_sleep_ms = luat_mcu_tick64_ms();
  758. platform_create_task(&prvlwip.task_handle, 16 * 1024, 40, "lwip", net_lwip_task, NULL, 64);
  759. lwip_init();
  760. platform_start_timer(prvlwip.common_timer, 1000, 1);
  761. prvlwip.dns_udp = udp_new();
  762. prvlwip.dns_udp->recv = net_lwip_dns_recv_cb;
  763. udp_bind(prvlwip.dns_udp, NULL, 55);
  764. dns_init_client(&prvlwip.dns_client);
  765. }
  766. void tcp_timer_needed(void)
  767. {
  768. if (!prvlwip.tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
  769. prvlwip.tcpip_tcp_timer_active = 1;
  770. platform_start_timer(prvlwip.tcp_timer, TCP_TMR_INTERVAL, 1);
  771. NET_DBG("tcp timer start");
  772. }
  773. }
  774. u32_t sys_now(void)
  775. {
  776. return (u32_t)luat_mcu_tick64_ms();
  777. }
  778. static ip_addr_t *net_lwip_get_ip6(void)
  779. {
  780. int i;
  781. for(i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  782. {
  783. if (prvlwip.lwip_netif->ip6_addr_state[i] & IP6_ADDR_PREFERRED)
  784. {
  785. return &prvlwip.lwip_netif->ip6_addr[i];
  786. }
  787. }
  788. for(i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  789. {
  790. if (prvlwip.lwip_netif->ip6_addr_state[i] & IP6_ADDR_VALID)
  791. {
  792. return &prvlwip.lwip_netif->ip6_addr[i];
  793. }
  794. }
  795. return NULL;
  796. }
  797. static void net_lwip_close_tcp(int socket_id)
  798. {
  799. prvlwip.socket[socket_id].pcb.tcp->sent = NULL;
  800. prvlwip.socket[socket_id].pcb.tcp->errf = NULL;
  801. prvlwip.socket[socket_id].pcb.tcp->poll = NULL;
  802. prvlwip.socket[socket_id].pcb.tcp->recv = tcp_recv_null;
  803. prvlwip.socket[socket_id].pcb.tcp->callback_arg = 0;
  804. if (tcp_close(prvlwip.socket[socket_id].pcb.tcp))
  805. {
  806. tcp_abort(prvlwip.socket[socket_id].pcb.tcp);
  807. }
  808. }
  809. static void net_lwip_task(void *param)
  810. {
  811. luat_network_cb_param_t cb_param;
  812. OS_EVENT event;
  813. Buffer_Struct tx_msg_buf = {0,0,0};
  814. HANDLE cur_task = luat_get_current_task();
  815. struct tcp_pcb *pcb, dpcb;
  816. struct netif *netif;
  817. struct dhcp *dhcp;
  818. socket_data_t *p;
  819. ip_addr_t *p_ip, *local_ip;
  820. struct pbuf *out_p;
  821. int error, i;
  822. PV_Union uPV;
  823. uint8_t active_flag;
  824. uint8_t socket_id;
  825. uint8_t adapter_index;
  826. while(1)
  827. {
  828. if (luat_wait_event_from_task(cur_task, 0, &event, NULL, 0) != ERROR_NONE)
  829. {
  830. continue;
  831. }
  832. if (!prvlwip.tcpip_tcp_timer_active)
  833. {
  834. if ((luat_mcu_tick64_ms() - prvlwip.last_sleep_ms) >= TCP_SLOW_INTERVAL)
  835. {
  836. tcp_ticks += (luat_mcu_tick64_ms() - prvlwip.last_sleep_ms) / TCP_SLOW_INTERVAL;
  837. prvlwip.last_sleep_ms = luat_mcu_tick64_ms();
  838. // NET_DBG("tcp ticks add to %u", tcp_ticks);
  839. }
  840. }
  841. else
  842. {
  843. prvlwip.last_sleep_ms = luat_mcu_tick64_ms();
  844. }
  845. socket_id = event.Param1;
  846. adapter_index = event.Param3;
  847. switch(event.ID)
  848. {
  849. case EV_LWIP_SOCKET_TX:
  850. SOCKET_LOCK(socket_id);
  851. if (prvlwip.socket[socket_id].in_use && prvlwip.socket[socket_id].pcb.ip)
  852. {
  853. if (!prvlwip.socket[socket_id].pcb.tcp->unsent && !prvlwip.socket[socket_id].pcb.tcp->unacked)
  854. {
  855. active_flag = 0;
  856. }
  857. else
  858. {
  859. active_flag = 1;
  860. }
  861. if (prvlwip.socket[socket_id].is_tcp)
  862. {
  863. while (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  864. {
  865. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  866. if (p->len <= tcp_sndbuf(prvlwip.socket[socket_id].pcb.tcp))
  867. {
  868. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0))
  869. {
  870. llist_del(&p->node);
  871. llist_add_tail(&p->node, &prvlwip.socket[socket_id].wait_ack_head);
  872. }
  873. else
  874. {
  875. // NET_DBG("tcp buf is full, wait ack and send again");
  876. break;
  877. }
  878. }
  879. else
  880. {
  881. // NET_DBG("tcp buf is full, wait ack and send again");
  882. break;
  883. }
  884. }
  885. SOCKET_UNLOCK(socket_id);
  886. tcp_output(prvlwip.socket[socket_id].pcb.tcp);
  887. }
  888. else
  889. {
  890. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  891. if (p)
  892. {
  893. llist_del(&p->node);
  894. }
  895. SOCKET_UNLOCK(socket_id);
  896. if (p)
  897. {
  898. uint32_t len = p->len;
  899. out_p = pbuf_alloc(PBUF_RAW, p->len, PBUF_ROM);
  900. if (out_p)
  901. {
  902. out_p->payload = p->data;
  903. error = udp_sendto(prvlwip.socket[socket_id].pcb.udp, out_p, &p->ip, p->port);
  904. pbuf_free(out_p);
  905. }
  906. else
  907. {
  908. NET_DBG("mem err send fail");
  909. }
  910. free(p->data);
  911. free(p);
  912. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_TX_OK, socket_id, len, 0);
  913. }
  914. }
  915. }
  916. else
  917. {
  918. NET_DBG("adapter %d socket %d no in use! %x", adapter_index, socket_id, prvlwip.socket[socket_id].pcb.ip);
  919. SOCKET_UNLOCK(socket_id);
  920. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  921. }
  922. break;
  923. case EV_LWIP_NETIF_INPUT:
  924. netif = (struct netif *)event.Param3;
  925. error = netif->input((struct pbuf *)event.Param1, netif);
  926. if(error != ERR_OK)
  927. {
  928. NET_DBG("%d", error);
  929. pbuf_free((struct pbuf *)event.Param1);
  930. }
  931. break;
  932. case EV_LWIP_TCP_TIMER:
  933. tcp_tmr();
  934. if (tcp_active_pcbs || tcp_tw_pcbs)
  935. {
  936. dpcb = NULL;
  937. for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  938. {
  939. if (FIN_WAIT_1 == pcb->state)
  940. {
  941. uPV.p = pcb->callback_arg;
  942. uPV.u32++;
  943. pcb->callback_arg = uPV.p;
  944. if (uPV.u32 >= 20)
  945. {
  946. NET_DBG("tcp %x fin send too much time!", pcb);
  947. dpcb = pcb;
  948. }
  949. }
  950. }
  951. if (dpcb)
  952. {
  953. tcp_abor(dpcb);
  954. }
  955. }
  956. else
  957. {
  958. prvlwip.tcpip_tcp_timer_active = 0;
  959. platform_stop_timer(prvlwip.tcp_timer);
  960. NET_DBG("tcp timer stop");
  961. }
  962. break;
  963. case EV_LWIP_COMMON_TIMER:
  964. #ifdef LUAT_USE_DNS
  965. net_lwip_dns_tx_next(&tx_msg_buf);
  966. #endif
  967. #if IP_REASSEMBLY
  968. ip_reass_tmr();
  969. #endif
  970. #if LWIP_ARP
  971. etharp_tmr();
  972. #endif
  973. #if LWIP_DNS
  974. dns_tmr();
  975. #endif
  976. #if LWIP_IPV6
  977. nd6_tmr();
  978. #endif
  979. #if LWIP_IPV6_REASS
  980. ip6_reass_tmr();
  981. #endif
  982. #if LWIP_DHCP
  983. prvlwip.dhcp_check_cnt++;
  984. if (prvlwip.dhcp_check_cnt >= DHCP_COARSE_TIMER_SECS)
  985. {
  986. prvlwip.dhcp_check_cnt = 0;
  987. dhcp_coarse_tmr();
  988. if (!prvlwip.dhcp_timer_active)
  989. {
  990. prvlwip.dhcp_timer_active = 1;
  991. platform_start_timer(prvlwip.dhcp_timer, DHCP_FINE_TIMER_MSECS, 1);
  992. NET_DBG("dhcp timer start");
  993. }
  994. }
  995. #endif
  996. break;
  997. case EV_LWIP_SOCKET_RX_DONE:
  998. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip || !prvlwip.socket[socket_id].is_tcp)
  999. {
  1000. 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);
  1001. break;
  1002. }
  1003. // NET_DBG("socket %d rx ack %dbytes", socket_id, event.Param2);
  1004. tcp_recved(prvlwip.socket[socket_id].pcb.tcp, event.Param2);
  1005. break;
  1006. case EV_LWIP_SOCKET_CREATE:
  1007. net_lwip_create_socket_now(adapter_index, socket_id);
  1008. break;
  1009. case EV_LWIP_SOCKET_CONNECT:
  1010. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  1011. {
  1012. 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);
  1013. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  1014. break;
  1015. }
  1016. p_ip = (ip_addr_t *)event.Param2;
  1017. local_ip = NULL;
  1018. if (p_ip->type == IPADDR_TYPE_V4)
  1019. {
  1020. local_ip = &prvlwip.lwip_netif[adapter_index]->ip_addr;
  1021. }
  1022. else
  1023. {
  1024. local_ip = net_lwip_get_ip6();
  1025. }
  1026. if (!local_ip)
  1027. {
  1028. NET_DBG("netif no ip !!!!!!");
  1029. net_lwip_tcp_error(adapter_index, socket_id);
  1030. break;
  1031. }
  1032. if (prvlwip.socket[socket_id].is_tcp)
  1033. {
  1034. tcp_bind(prvlwip.socket[socket_id].pcb.tcp, local_ip, prvlwip.socket[socket_id].local_port);
  1035. error = tcp_connect(prvlwip.socket[socket_id].pcb.tcp, p_ip, prvlwip.socket[socket_id].remote_port, net_lwip_tcp_connected_cb);
  1036. if (error)
  1037. {
  1038. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  1039. net_lwip_tcp_error(adapter_index, socket_id);
  1040. }
  1041. }
  1042. else
  1043. {
  1044. udp_bind(prvlwip.socket[socket_id].pcb.udp, local_ip, prvlwip.socket[socket_id].local_port);
  1045. error = udp_connect(prvlwip.socket[socket_id].pcb.udp, p_ip, prvlwip.socket[socket_id].remote_port);
  1046. if (error)
  1047. {
  1048. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  1049. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  1050. }
  1051. else
  1052. {
  1053. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  1054. }
  1055. }
  1056. break;
  1057. case EV_LWIP_SOCKET_DNS:
  1058. case EV_LWIP_SOCKET_DNS_IPV6:
  1059. dns_require_ipv6(&prvlwip.dns_client, event.Param1, event.Param2, event.Param3, event.ID - EV_LWIP_SOCKET_DNS);
  1060. net_lwip_dns_tx_next(&tx_msg_buf);
  1061. break;
  1062. case EV_LWIP_SOCKET_LISTEN:
  1063. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  1064. {
  1065. 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);
  1066. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  1067. break;
  1068. }
  1069. tcp_bind(prvlwip.socket[socket_id].pcb.tcp, NULL, prvlwip.socket[socket_id].local_port);
  1070. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->local_ip, IPADDR_TYPE_ANY);
  1071. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->remote_ip, IPADDR_TYPE_ANY);
  1072. prvlwip.socket[socket_id].listen_tcp = tcp_listen_with_backlog(prvlwip.socket[socket_id].pcb.tcp, 1);
  1073. if (!prvlwip.socket[socket_id].listen_tcp) {
  1074. NET_DBG("socket %d listen failed");
  1075. net_lwip_tcp_error(adapter_index, socket_id);
  1076. } else {
  1077. PV_Union uPV;
  1078. uPV.u16[0] = socket_id;
  1079. uPV.u16[1] = adapter_index;
  1080. prvlwip.socket[socket_id].listen_tcp->callback_arg = uPV.u32;
  1081. prvlwip.socket[socket_id].listen_tcp->accept = net_lwip_tcp_fast_accept_cb;
  1082. prvlwip.socket[socket_id].pcb.tcp = NULL;
  1083. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_LISTEN, socket_id, 0, 0);
  1084. }
  1085. break;
  1086. // case EV_LWIP_SOCKET_ACCPET:
  1087. //
  1088. // break;
  1089. case EV_LWIP_SOCKET_CLOSE:
  1090. if (!prvlwip.socket[socket_id].in_use)
  1091. {
  1092. NET_DBG("socket %d no in use!,%x", socket_id);
  1093. break;
  1094. }
  1095. if (prvlwip.socket[socket_id].listen_tcp)
  1096. {
  1097. tcp_close(prvlwip.socket[socket_id].listen_tcp);
  1098. prvlwip.socket[socket_id].listen_tcp = NULL;
  1099. if (prvlwip.socket[socket_id].pcb.tcp)
  1100. {
  1101. net_lwip_close_tcp(socket_id);
  1102. }
  1103. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  1104. break;
  1105. }
  1106. if (prvlwip.socket[socket_id].pcb.ip)
  1107. {
  1108. if (prvlwip.socket[socket_id].is_tcp)
  1109. {
  1110. net_lwip_close_tcp(socket_id);
  1111. }
  1112. else
  1113. {
  1114. udp_remove(prvlwip.socket[socket_id].pcb.udp);
  1115. }
  1116. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  1117. break;
  1118. }
  1119. if (prvlwip.socket[socket_id].remote_close)
  1120. {
  1121. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  1122. break;
  1123. }
  1124. break;
  1125. case EV_LWIP_DHCP_TIMER:
  1126. #if LWIP_DHCP
  1127. dhcp_fine_tmr();
  1128. active_flag = 0;
  1129. NETIF_FOREACH(netif)
  1130. {
  1131. dhcp = netif_dhcp_data(netif);
  1132. /* only act on DHCP configured interfaces */
  1133. if (dhcp && dhcp->request_timeout && (dhcp->state != DHCP_STATE_BOUND))
  1134. {
  1135. active_flag = 1;
  1136. break;
  1137. }
  1138. }
  1139. if (!active_flag)
  1140. {
  1141. NET_DBG("stop dhcp timer!");
  1142. prvlwip.dhcp_timer_active = 0;
  1143. platform_stop_timer(prvlwip.dhcp_timer);
  1144. }
  1145. #endif
  1146. break;
  1147. case EV_LWIP_FAST_TIMER:
  1148. #if LWIP_AUTOIP
  1149. autoip_tmr();
  1150. #endif
  1151. #if LWIP_IGMP
  1152. igmp_tmr();
  1153. #endif
  1154. #if LWIP_IPV6_MLD
  1155. mld6_tmr();
  1156. #endif
  1157. active_flag = 0;
  1158. NETIF_FOREACH(netif)
  1159. {
  1160. if (
  1161. #if LWIP_IPV6_MLD
  1162. netif_mld6_data(netif)
  1163. #endif
  1164. #if LWIP_IGMP
  1165. || netif_igmp_data(netif)
  1166. #endif
  1167. #if LWIP_AUTOIP
  1168. || netif_autoip_data(netif)
  1169. #endif
  1170. )
  1171. {
  1172. active_flag = 1;
  1173. break;
  1174. }
  1175. }
  1176. if (!active_flag)
  1177. {
  1178. NET_DBG("stop fast timer!");
  1179. prvlwip.fast_timer_active = 0;
  1180. platform_stop_timer(prvlwip.fast_timer);
  1181. }
  1182. break;
  1183. case EV_LWIP_NETIF_SET_IP:
  1184. netif = prvlwip.lwip_netif[event.Param3];
  1185. p_ip = (ip_addr_t *)event.Param1;
  1186. if ((p_ip[0].type != IPADDR_TYPE_ANY) && p_ip[0].u_addr.ip4.addr)
  1187. {
  1188. dhcp_release_and_stop(netif);
  1189. }
  1190. netif_set_addr(netif, &p_ip[0].u_addr.ip4, &p_ip[1].u_addr.ip4, &p_ip[2].u_addr.ip4);
  1191. netif_ip6_addr_set(netif, 1, &p_ip[3]);
  1192. if (event.Param2)
  1193. {
  1194. netif_ip6_addr_set_state(netif, 1, IP6_ADDR_VALID);
  1195. }
  1196. else
  1197. {
  1198. netif_ip6_addr_set_state(netif, 1, IP6_ADDR_INVALID);
  1199. }
  1200. if (!prvlwip.fast_timer_active)
  1201. {
  1202. NET_DBG("start fast timer!");
  1203. prvlwip.fast_timer_active = 1;
  1204. platform_start_timer(prvlwip.fast_timer, 100, 1);
  1205. }
  1206. free(p_ip);
  1207. break;
  1208. case EV_LWIP_NETIF_LINK_STATE:
  1209. NET_DBG("%d,%d,%d", event.Param3, prvlwip.netif_network_ready[event.Param3], event.Param1);
  1210. if (prvlwip.netif_network_ready[event.Param3] != event.Param1)
  1211. {
  1212. if (event.Param1)
  1213. {
  1214. netif_set_link_up(prvlwip.lwip_netif[event.Param3]);
  1215. }
  1216. else
  1217. {
  1218. netif_set_link_down(prvlwip.lwip_netif[event.Param3]);
  1219. }
  1220. }
  1221. net_lwip_check_network_ready(event.Param3);
  1222. break;
  1223. case EV_LWIP_NETIF_IPV6_BY_MAC:
  1224. netif_create_ip6_linklocal_address(prvlwip.lwip_netif[event.Param3], 1);
  1225. break;
  1226. default:
  1227. NET_DBG("unknow event %x,%x", event.ID, event.Param1);
  1228. break;
  1229. }
  1230. }
  1231. }
  1232. static void net_lwip_check_network_ready(uint8_t adapter_index)
  1233. {
  1234. int i;
  1235. char ip_string[64];
  1236. struct netif *netif = prvlwip.lwip_netif[adapter_index];
  1237. uint8_t active_flag = netif_is_flag_set(netif, NETIF_FLAG_LINK_UP);
  1238. uint8_t ip_ready = 0;
  1239. if (!active_flag)
  1240. {
  1241. dhcp_release_and_stop(netif);
  1242. }
  1243. #if LWIP_IPV4
  1244. if (netif->ip_addr.u_addr.ip4.addr)
  1245. {
  1246. ip_ready = 1;
  1247. }
  1248. #endif
  1249. #if LWIP_IPV6
  1250. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  1251. {
  1252. if (ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)))
  1253. {
  1254. ip_ready = 1;
  1255. break;
  1256. }
  1257. }
  1258. #endif
  1259. if (!ip_ready)
  1260. {
  1261. #if LWIP_DHCP
  1262. if (netif->flags & NETIF_FLAG_ETHARP)
  1263. {
  1264. dhcp_start(netif);
  1265. if (!prvlwip.dhcp_timer_active)
  1266. {
  1267. prvlwip.dhcp_timer_active = 1;
  1268. platform_start_timer(prvlwip.dhcp_timer, 500, 1);
  1269. }
  1270. }
  1271. #endif
  1272. active_flag = 0;
  1273. }
  1274. if (prvlwip.netif_network_ready[adapter_index] != active_flag)
  1275. {
  1276. prvlwip.netif_network_ready[adapter_index] = active_flag;
  1277. if (!active_flag)
  1278. {
  1279. dns_clear(&prvlwip.dns_client);
  1280. prvlwip.dns_client.is_run = 0;
  1281. NET_DBG("network not ready");
  1282. net_lwip_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 0, adapter_index);
  1283. }
  1284. else
  1285. {
  1286. NET_DBG("network ready");
  1287. for(i = 0; i < MAX_DNS_SERVER; i++)
  1288. {
  1289. if (prvlwip.dns_client.dns_server[i].type != 0xff)
  1290. {
  1291. NET_DBG("DNS%d:%s",i, ipaddr_ntoa_r(&prvlwip.dns_client.dns_server[i], ip_string, sizeof(ip_string)));
  1292. }
  1293. }
  1294. net_lwip_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 1, adapter_index);
  1295. }
  1296. }
  1297. }
  1298. static int net_lwip_check_socket(void *user_data, int socket_id, uint64_t tag)
  1299. {
  1300. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1301. if (socket_id >= MAX_SOCK_NUM) return -1;
  1302. if (prvlwip.socket[socket_id].tag != tag) return -1;
  1303. if (!prvlwip.socket[socket_id].in_use || prvlwip.socket[socket_id].state) return -1;
  1304. return 0;
  1305. }
  1306. static int net_lwip_socket_check(int socket_id, uint64_t tag, void *user_data)
  1307. {
  1308. return net_lwip_check_socket(user_data, socket_id, tag);
  1309. }
  1310. static uint8_t net_lwip_check_ready(void *user_data)
  1311. {
  1312. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return 0;
  1313. return (prvlwip.netif_network_ready[(uint32_t)user_data]);
  1314. }
  1315. static void net_lwip_create_socket_now(uint8_t adapter_index, uint8_t socket_id)
  1316. {
  1317. PV_Union uPV;
  1318. uPV.u16[0] = socket_id;
  1319. uPV.u16[1] = adapter_index;
  1320. if (prvlwip.socket[socket_id].is_tcp)
  1321. {
  1322. prvlwip.socket[socket_id].pcb.tcp = tcp_new();
  1323. if (prvlwip.socket[socket_id].pcb.tcp)
  1324. {
  1325. prvlwip.socket[socket_id].pcb.tcp->netif_idx = netif_get_index(prvlwip.lwip_netif[adapter_index]);
  1326. prvlwip.socket[socket_id].rx_wait_size = 0;
  1327. prvlwip.socket[socket_id].tx_wait_size = 0;
  1328. prvlwip.socket[socket_id].pcb.tcp->callback_arg = uPV.p;
  1329. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip_tcp_recv_cb;
  1330. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip_tcp_sent_cb;
  1331. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip_tcp_err_cb;
  1332. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  1333. // tcp_set_flags(prvlwip.socket[socket_id].pcb.tcp, TCP_NODELAY);
  1334. if (adapter_index == NW_ADAPTER_INDEX_LWIP_GPRS)
  1335. {
  1336. prvlwip.socket[socket_id].pcb.tcp->more_delay = 6;
  1337. }
  1338. }
  1339. else
  1340. {
  1341. NET_DBG("tcp pcb full!");
  1342. net_lwip_tcp_error(adapter_index, socket_id);
  1343. }
  1344. }
  1345. else
  1346. {
  1347. prvlwip.socket[socket_id].pcb.udp = udp_new();
  1348. if (prvlwip.socket[socket_id].pcb.udp)
  1349. {
  1350. prvlwip.socket[socket_id].pcb.udp->netif_idx = netif_get_index(prvlwip.lwip_netif[adapter_index]);
  1351. prvlwip.socket[socket_id].pcb.udp->recv_arg = uPV.p;
  1352. prvlwip.socket[socket_id].pcb.udp->recv = net_lwip_udp_recv_cb;
  1353. prvlwip.socket[socket_id].pcb.udp->so_options |= SOF_BROADCAST|SOF_REUSEADDR;
  1354. }
  1355. else
  1356. {
  1357. NET_DBG("udp pcb full!");
  1358. net_lwip_tcp_error(adapter_index, socket_id);
  1359. }
  1360. }
  1361. }
  1362. static int net_lwip_create_socket(uint8_t is_tcp, uint64_t *tag, void *param, uint8_t is_ipv6, void *user_data)
  1363. {
  1364. uint8_t index = (uint32_t)user_data;
  1365. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1366. int i, socket_id;
  1367. socket_id = -1;
  1368. OS_LOCK;
  1369. if (!prvlwip.socket[prvlwip.next_socket_index].in_use)
  1370. {
  1371. socket_id = prvlwip.next_socket_index;
  1372. prvlwip.next_socket_index++;
  1373. }
  1374. else
  1375. {
  1376. for (i = 0; i < MAX_SOCK_NUM; i++)
  1377. {
  1378. if (!prvlwip.socket[i].in_use)
  1379. {
  1380. socket_id = i;
  1381. prvlwip.next_socket_index = i + 1;
  1382. break;
  1383. }
  1384. }
  1385. }
  1386. if (prvlwip.next_socket_index >= MAX_SOCK_NUM)
  1387. {
  1388. prvlwip.next_socket_index = 0;
  1389. }
  1390. if (socket_id >= 0)
  1391. {
  1392. LWIP_ASSERT("socket must free before create", !prvlwip.socket[socket_id].pcb.ip);
  1393. prvlwip.socket_tag++;
  1394. *tag = prvlwip.socket_tag;
  1395. prvlwip.socket[socket_id].in_use = 1;
  1396. prvlwip.socket[socket_id].tag = *tag;
  1397. prvlwip.socket[socket_id].param = param;
  1398. prvlwip.socket[socket_id].is_tcp = is_tcp;
  1399. llist_traversal(&prvlwip.socket[socket_id].wait_ack_head, net_lwip_del_data_cache, NULL);
  1400. llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_del_data_cache, NULL);
  1401. llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_del_data_cache, NULL);
  1402. OS_UNLOCK;
  1403. if (platform_get_current_task() == prvlwip.task_handle)
  1404. {
  1405. net_lwip_create_socket_now(index, socket_id);
  1406. return socket_id;
  1407. }
  1408. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CREATE, socket_id, 0, user_data);
  1409. }
  1410. else
  1411. {
  1412. OS_UNLOCK;
  1413. }
  1414. return socket_id;
  1415. }
  1416. //作为client绑定一个port,并连接remote_ip和remote_port对应的server
  1417. 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)
  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. prvlwip.socket[socket_id].remote_port = remote_port;
  1423. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CONNECT, socket_id, remote_ip, user_data);
  1424. return 0;
  1425. }
  1426. //作为server绑定一个port,开始监听
  1427. static int net_lwip_socket_listen(int socket_id, uint64_t tag, uint16_t local_port, void *user_data)
  1428. {
  1429. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1430. if (result) return result;
  1431. prvlwip.socket[socket_id].local_port = local_port;
  1432. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_LISTEN, socket_id, local_port, user_data);
  1433. return 0;
  1434. }
  1435. //作为server接受一个client
  1436. 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)
  1437. {
  1438. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1439. if (result) return result;
  1440. *remote_ip = prvlwip.socket[socket_id].pcb.tcp->remote_ip;
  1441. *remote_port = prvlwip.socket[socket_id].pcb.tcp->remote_port;
  1442. return 0;
  1443. }
  1444. //主动断开一个tcp连接,需要走完整个tcp流程,用户需要接收到close ok回调才能确认彻底断开
  1445. static int net_lwip_socket_disconnect(int socket_id, uint64_t tag, void *user_data)
  1446. {
  1447. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1448. if (result) return result;
  1449. prvlwip.socket[socket_id].state = 1;
  1450. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CLOSE, socket_id, 1, user_data);
  1451. return 0;
  1452. }
  1453. static int net_lwip_socket_force_close(int socket_id, void *user_data)
  1454. {
  1455. if (prvlwip.socket[socket_id].in_use && !prvlwip.socket[socket_id].state)
  1456. {
  1457. prvlwip.socket[socket_id].state = 1;
  1458. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CLOSE, socket_id, 0, user_data);
  1459. }
  1460. return 0;
  1461. }
  1462. static int net_lwip_socket_close(int socket_id, uint64_t tag, void *user_data)
  1463. {
  1464. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1465. if (socket_id >= MAX_SOCK_NUM) return -1;
  1466. if (prvlwip.socket[socket_id].tag != tag)
  1467. {
  1468. NET_DBG("socket %d used by other!", socket_id);
  1469. return -1;
  1470. }
  1471. if (!prvlwip.socket[socket_id].in_use) return 0;
  1472. net_lwip_socket_force_close(socket_id, user_data);
  1473. return 0;
  1474. }
  1475. 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)
  1476. {
  1477. uint32_t dummy_len;
  1478. dummy_len = ((p->len - p->read_pos) > (len - *read_len))?(len - *read_len):(p->len - p->read_pos);
  1479. memcpy(buf, p->data + p->read_pos, dummy_len);
  1480. p->read_pos += dummy_len;
  1481. if (p->read_pos >= p->len)
  1482. {
  1483. if (prvlwip.socket[socket_id].is_tcp)
  1484. {
  1485. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_RX_DONE, socket_id, p->len, 0);
  1486. }
  1487. llist_del(&p->node);
  1488. free(p->data);
  1489. free(p);
  1490. }
  1491. *read_len += dummy_len;
  1492. return dummy_len;
  1493. }
  1494. 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)
  1495. {
  1496. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1497. if (result) return result;
  1498. uint32_t read_len = 0;
  1499. if (buf)
  1500. {
  1501. SOCKET_LOCK(socket_id);
  1502. socket_data_t *p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  1503. if (prvlwip.socket[socket_id].is_tcp)
  1504. {
  1505. while((read_len < len) && p)
  1506. {
  1507. prvlwip.socket[socket_id].rx_wait_size -= net_lwip_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1508. p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  1509. }
  1510. }
  1511. else
  1512. {
  1513. if (p)
  1514. {
  1515. if (remote_ip)
  1516. {
  1517. *remote_ip = p->ip;
  1518. }
  1519. if (remote_port)
  1520. {
  1521. *remote_port = p->port;
  1522. }
  1523. prvlwip.socket[socket_id].rx_wait_size -= net_lwip_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1524. }
  1525. }
  1526. if (llist_empty(&prvlwip.socket[socket_id].rx_head))
  1527. {
  1528. prvlwip.socket[socket_id].rx_wait_size = 0;
  1529. }
  1530. SOCKET_UNLOCK(socket_id);
  1531. }
  1532. else
  1533. {
  1534. read_len = prvlwip.socket[socket_id].rx_wait_size;
  1535. }
  1536. return read_len;
  1537. }
  1538. 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)
  1539. {
  1540. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1541. if (result) return result;
  1542. SOCKET_LOCK(socket_id);
  1543. uint32_t save_len = 0;
  1544. uint32_t dummy_len = 0;
  1545. socket_data_t *p;
  1546. if (prvlwip.socket[socket_id].is_tcp)
  1547. {
  1548. while(save_len < len)
  1549. {
  1550. dummy_len = ((len - save_len) > SOCKET_BUF_LEN)?SOCKET_BUF_LEN:(len - save_len);
  1551. p = net_lwip_create_data_node(socket_id, &buf[save_len], dummy_len, remote_ip, remote_port);
  1552. if (p)
  1553. {
  1554. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1555. }
  1556. else
  1557. {
  1558. SOCKET_UNLOCK(socket_id);
  1559. return -1;
  1560. }
  1561. save_len += dummy_len;
  1562. }
  1563. }
  1564. else
  1565. {
  1566. p = net_lwip_create_data_node(socket_id, buf, len, remote_ip, remote_port);
  1567. if (p)
  1568. {
  1569. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1570. }
  1571. else
  1572. {
  1573. SOCKET_UNLOCK(socket_id);
  1574. return -1;
  1575. }
  1576. }
  1577. SOCKET_UNLOCK(socket_id);
  1578. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_TX, socket_id, 0, user_data);
  1579. result = len;
  1580. return result;
  1581. }
  1582. void net_lwip_socket_clean(int *vaild_socket_list, uint32_t num, void *user_data)
  1583. {
  1584. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  1585. int socket_list[MAX_SOCK_NUM];
  1586. memset(socket_list, 0, sizeof(socket_list));
  1587. uint32_t i;
  1588. for(i = 0; i < num; i++)
  1589. {
  1590. if ( (vaild_socket_list[i] > 0) && (vaild_socket_list[i] < MAX_SOCK_NUM) )
  1591. {
  1592. socket_list[vaild_socket_list[i]] = 1;
  1593. }
  1594. NET_DBG("%d,%d",i,vaild_socket_list[i]);
  1595. }
  1596. for(i = 0; i < MAX_SOCK_NUM; i++)
  1597. {
  1598. NET_DBG("%d,%d",i,socket_list[i]);
  1599. if ( !socket_list[i] )
  1600. {
  1601. net_lwip_socket_force_close(i, user_data);
  1602. }
  1603. }
  1604. }
  1605. static int net_lwip_getsockopt(int socket_id, uint64_t tag, int level, int optname, void *optval, uint32_t *optlen, void *user_data)
  1606. {
  1607. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1608. if (result) return result;
  1609. for(int i = 0; i < 10; i++)
  1610. {
  1611. if (!prvlwip.socket[socket_id].pcb.ip)
  1612. {
  1613. platform_task_sleep(1);
  1614. }
  1615. else
  1616. {
  1617. break;
  1618. }
  1619. }
  1620. if (!prvlwip.socket[socket_id].pcb.ip)
  1621. {
  1622. NET_DBG("socket %d not create try later! %d, %d, %x, %d", socket_id, level, optname, optval, optlen);
  1623. return -1;
  1624. }
  1625. switch (level)
  1626. {
  1627. /* Level: SOL_SOCKET */
  1628. case SOL_SOCKET:
  1629. switch (optname)
  1630. {
  1631. #if LWIP_TCP
  1632. case SO_ACCEPTCONN:
  1633. if (!prvlwip.socket[socket_id].is_tcp) {
  1634. return -1;
  1635. }
  1636. if ((prvlwip.socket[socket_id].pcb.tcp != NULL) && (prvlwip.socket[socket_id].pcb.tcp->state == LISTEN)) {
  1637. *(int *)optval = 1;
  1638. } else {
  1639. *(int *)optval = 0;
  1640. }
  1641. break;
  1642. #endif /* LWIP_TCP */
  1643. /* The option flags */
  1644. case SO_BROADCAST:
  1645. case SO_KEEPALIVE:
  1646. #if SO_REUSE
  1647. case SO_REUSEADDR:
  1648. #endif /* SO_REUSE */
  1649. if ((optname == SO_BROADCAST) && prvlwip.socket[socket_id].is_tcp) {
  1650. return -1;
  1651. }
  1652. optname = lwip_sockopt_to_ipopt(optname);
  1653. *(int *)optval = ip_get_option(prvlwip.socket[socket_id].pcb.ip, optname);
  1654. break;
  1655. case SO_TYPE:
  1656. if (prvlwip.socket[socket_id].is_tcp)
  1657. {
  1658. *(int *)optval = SOCK_STREAM;
  1659. }
  1660. else
  1661. {
  1662. *(int *)optval = SOCK_DGRAM;
  1663. }
  1664. break;
  1665. case SO_ERROR:
  1666. return -1;
  1667. break;
  1668. #if LWIP_UDP
  1669. case SO_NO_CHECK:
  1670. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1671. #if LWIP_UDPLITE
  1672. if (udp_is_flag_set(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_UDPLITE)) {
  1673. /* this flag is only available for UDP, not for UDP lite */
  1674. done_socket(sock);
  1675. return -1;
  1676. }
  1677. #endif /* LWIP_UDPLITE */
  1678. *(int *)optval = udp_is_flag_set(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_NOCHKSUM) ? 1 : 0;
  1679. break;
  1680. #endif /* LWIP_UDP*/
  1681. default:
  1682. return -1;
  1683. break;
  1684. } /* switch (optname) */
  1685. break;
  1686. /* Level: IPPROTO_IP */
  1687. case IPPROTO_IP:
  1688. switch (optname) {
  1689. case IP_TTL:
  1690. *(int *)optval = prvlwip.socket[socket_id].pcb.ip->ttl;
  1691. break;
  1692. case IP_TOS:
  1693. *(int *)optval = prvlwip.socket[socket_id].pcb.ip->tos;
  1694. break;
  1695. #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP
  1696. case IP_MULTICAST_TTL:
  1697. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1698. *(u8_t *)optval = udp_get_multicast_ttl(prvlwip.socket[socket_id].pcb.udp);
  1699. break;
  1700. case IP_MULTICAST_IF:
  1701. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1702. ((struct in_addr *)optval)->s_addr = prvlwip.socket[socket_id].pcb.udp->mcast_ip4.addr;
  1703. break;
  1704. case IP_MULTICAST_LOOP:
  1705. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1706. if ((prvlwip.socket[socket_id].pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) {
  1707. *(u8_t *)optval = 1;
  1708. } else {
  1709. *(u8_t *)optval = 0;
  1710. }
  1711. break;
  1712. #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */
  1713. default:
  1714. return -1;
  1715. } /* switch (optname) */
  1716. break;
  1717. #if LWIP_TCP
  1718. /* Level: IPPROTO_TCP */
  1719. case IPPROTO_TCP:
  1720. /* Special case: all IPPROTO_TCP option take an int */
  1721. if (!prvlwip.socket[socket_id].is_tcp) return -1;
  1722. if (prvlwip.socket[socket_id].pcb.tcp->state == LISTEN) {
  1723. return -1;
  1724. }
  1725. switch (optname) {
  1726. case TCP_NODELAY:
  1727. *(int *)optval = tcp_nagle_disabled(prvlwip.socket[socket_id].pcb.tcp);
  1728. break;
  1729. case TCP_KEEPALIVE:
  1730. *(int *)optval = (int)prvlwip.socket[socket_id].pcb.tcp->keep_idle;
  1731. break;
  1732. #if LWIP_TCP_KEEPALIVE
  1733. case TCP_KEEPIDLE:
  1734. *(int *)optval = (int)(prvlwip.socket[socket_id].pcb.tcp->keep_idle / 1000);
  1735. break;
  1736. case TCP_KEEPINTVL:
  1737. *(int *)optval = (int)(prvlwip.socket[socket_id].pcb.tcp->keep_intvl / 1000);
  1738. break;
  1739. case TCP_KEEPCNT:
  1740. *(int *)optval = (int)prvlwip.socket[socket_id].pcb.tcp->keep_cnt;
  1741. break;
  1742. #endif /* LWIP_TCP_KEEPALIVE */
  1743. default:
  1744. return -1;
  1745. break;
  1746. } /* switch (optname) */
  1747. break;
  1748. #endif /* LWIP_TCP */
  1749. #if LWIP_UDP && LWIP_UDPLITE
  1750. /* Level: IPPROTO_UDPLITE */
  1751. case IPPROTO_UDPLITE:
  1752. /* Special case: all IPPROTO_UDPLITE option take an int */
  1753. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
  1754. /* If this is no UDP lite socket, ignore any options. */
  1755. if (!NETCONNTYPE_ISUDPLITE(netconn_type(sock->conn))) {
  1756. done_socket(sock);
  1757. return ENOPROTOOPT;
  1758. }
  1759. switch (optname) {
  1760. case UDPLITE_SEND_CSCOV:
  1761. *(int *)optval = prvlwip.socket[socket_id].pcb.udp->chksum_len_tx;
  1762. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n",
  1763. s, (*(int *)optval)) );
  1764. break;
  1765. case UDPLITE_RECV_CSCOV:
  1766. *(int *)optval = prvlwip.socket[socket_id].pcb.udp->chksum_len_rx;
  1767. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n",
  1768. s, (*(int *)optval)) );
  1769. break;
  1770. default:
  1771. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  1772. s, optname));
  1773. err = ENOPROTOOPT;
  1774. break;
  1775. } /* switch (optname) */
  1776. break;
  1777. #endif /* LWIP_UDP */
  1778. /* Level: IPPROTO_RAW */
  1779. case IPPROTO_RAW:
  1780. return -1;
  1781. default:
  1782. return -1;
  1783. break;
  1784. } /* switch (level) */
  1785. return 0;
  1786. }
  1787. static int net_lwip_setsockopt(int socket_id, uint64_t tag, int level, int optname, const void *optval, uint32_t optlen, void *user_data)
  1788. {
  1789. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1790. if (result) return result;
  1791. for(int i = 0; i < 10; i++)
  1792. {
  1793. if (!prvlwip.socket[socket_id].pcb.ip)
  1794. {
  1795. platform_task_sleep(1);
  1796. }
  1797. else
  1798. {
  1799. break;
  1800. }
  1801. }
  1802. if (!prvlwip.socket[socket_id].pcb.ip)
  1803. {
  1804. NET_DBG("socket %d not create try later! %d, %d, %x, %d", socket_id, level, optname, optval, optlen);
  1805. return -1;
  1806. }
  1807. switch (level)
  1808. {
  1809. /* Level: SOL_SOCKET */
  1810. case SOL_SOCKET:
  1811. switch (optname)
  1812. {
  1813. /* SO_ACCEPTCONN is get-only */
  1814. /* The option flags */
  1815. case SO_BROADCAST:
  1816. case SO_KEEPALIVE:
  1817. #if SO_REUSE
  1818. case SO_REUSEADDR:
  1819. #endif /* SO_REUSE */
  1820. if ((optname == SO_BROADCAST) &&
  1821. (prvlwip.socket[socket_id].is_tcp)) {
  1822. return -1;
  1823. }
  1824. optname = lwip_sockopt_to_ipopt(optname);
  1825. if (*(const int *)optval) {
  1826. ip_set_option(prvlwip.socket[socket_id].pcb.ip, optname);
  1827. } else {
  1828. ip_reset_option(prvlwip.socket[socket_id].pcb.ip, optname);
  1829. }
  1830. break;
  1831. #if LWIP_UDP
  1832. case SO_NO_CHECK:
  1833. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1834. if (*(const int *)optval) {
  1835. udp_set_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_NOCHKSUM);
  1836. } else {
  1837. udp_clear_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_NOCHKSUM);
  1838. }
  1839. break;
  1840. #endif /* LWIP_UDP */
  1841. default:
  1842. return -1;
  1843. } /* switch (optname) */
  1844. break;
  1845. /* Level: IPPROTO_IP */
  1846. case IPPROTO_IP:
  1847. switch (optname)
  1848. {
  1849. case IP_TTL:
  1850. prvlwip.socket[socket_id].pcb.ip->ttl = (u8_t)(*(const int *)optval);
  1851. break;
  1852. case IP_TOS:
  1853. prvlwip.socket[socket_id].pcb.ip->tos = (u8_t)(*(const int *)optval);
  1854. break;
  1855. #if LWIP_NETBUF_RECVINFO
  1856. case IP_PKTINFO:
  1857. if (*(const int *)optval) {
  1858. prvlwip.socket[socket_id].flags |= NETCONN_FLAG_PKTINFO;
  1859. } else {
  1860. prvlwip.socket[socket_id].flags &= ~NETCONN_FLAG_PKTINFO;
  1861. }
  1862. break;
  1863. #endif /* LWIP_NETBUF_RECVINFO */
  1864. #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP
  1865. case IP_MULTICAST_TTL:
  1866. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1867. udp_set_multicast_ttl(prvlwip.socket[socket_id].pcb.udp, (u8_t)(*(const u8_t *)optval));
  1868. break;
  1869. case IP_MULTICAST_IF:
  1870. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1871. prvlwip.socket[socket_id].pcb.udp->mcast_ip4.addr = ((struct in_addr *)optval)->s_addr;
  1872. break;
  1873. case IP_MULTICAST_LOOP:
  1874. if (*(const u8_t *)optval) {
  1875. udp_set_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
  1876. } else {
  1877. udp_clear_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
  1878. }
  1879. break;
  1880. #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */
  1881. default:
  1882. return -1;
  1883. break;
  1884. } /* switch (optname) */
  1885. break;
  1886. #if LWIP_TCP
  1887. /* Level: IPPROTO_TCP */
  1888. case IPPROTO_TCP:
  1889. /* Special case: all IPPROTO_TCP option take an int */
  1890. if (!prvlwip.socket[socket_id].is_tcp) return -1;
  1891. if (prvlwip.socket[socket_id].pcb.tcp->state == LISTEN) {
  1892. return -1;
  1893. }
  1894. switch (optname)
  1895. {
  1896. case TCP_NODELAY:
  1897. if (*(const int *)optval) {
  1898. tcp_nagle_disable(prvlwip.socket[socket_id].pcb.tcp);
  1899. } else {
  1900. tcp_nagle_enable(prvlwip.socket[socket_id].pcb.tcp);
  1901. }
  1902. break;
  1903. case TCP_KEEPALIVE:
  1904. prvlwip.socket[socket_id].pcb.tcp->keep_idle = (u32_t)(*(const int *)optval);
  1905. break;
  1906. #if LWIP_TCP_KEEPALIVE
  1907. case TCP_KEEPIDLE:
  1908. prvlwip.socket[socket_id].pcb.tcp->keep_idle = 1000 * (u32_t)(*(const int *)optval);
  1909. break;
  1910. case TCP_KEEPINTVL:
  1911. prvlwip.socket[socket_id].pcb.tcp->keep_intvl = 1000 * (u32_t)(*(const int *)optval);
  1912. break;
  1913. case TCP_KEEPCNT:
  1914. prvlwip.socket[socket_id].pcb.tcp->keep_cnt = (u32_t)(*(const int *)optval);
  1915. break;
  1916. #endif /* LWIP_TCP_KEEPALIVE */
  1917. default:
  1918. return -1;
  1919. break;
  1920. } /* switch (optname) */
  1921. break;
  1922. #endif /* LWIP_TCP*/
  1923. #if LWIP_IPV6
  1924. /* Level: IPPROTO_IPV6 */
  1925. case IPPROTO_IPV6:
  1926. return -1;
  1927. break;
  1928. #endif /* LWIP_IPV6 */
  1929. /* Level: IPPROTO_RAW */
  1930. case IPPROTO_RAW:
  1931. return -1;
  1932. break;
  1933. default:
  1934. return -1;
  1935. } /* switch (level) */
  1936. return 0;
  1937. }
  1938. 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)
  1939. {
  1940. uint8_t index = (uint32_t)user_data;
  1941. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1942. if (!prvlwip.lwip_netif[index]) return -1;
  1943. *ip = prvlwip.lwip_netif[index]->ip_addr;
  1944. *submask = prvlwip.lwip_netif[index]->netmask;
  1945. *gateway = prvlwip.lwip_netif[index]->gw;
  1946. return 0;
  1947. }
  1948. 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)
  1949. {
  1950. uint8_t index = (uint32_t)user_data;
  1951. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1952. if (!prvlwip.lwip_netif) return -1;
  1953. *ip = prvlwip.lwip_netif->ip_addr;
  1954. *submask = prvlwip.lwip_netif->netmask;
  1955. *gateway = prvlwip.lwip_netif->gw;
  1956. luat_ip_addr_t *local_ip = net_lwip_get_ip6();
  1957. if (local_ip)
  1958. {
  1959. *ipv6 = *local_ip;
  1960. }
  1961. else
  1962. {
  1963. ipv6->type = 0xff;
  1964. }
  1965. return 0;
  1966. }
  1967. static int net_lwip_user_cmd(int socket_id, uint64_t tag, uint32_t cmd, uint32_t value, void *user_data)
  1968. {
  1969. return 0;
  1970. }
  1971. static int net_lwip_dns(const char *domain_name, uint32_t len, void *param, void *user_data)
  1972. {
  1973. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1974. char *prv_domain_name = (char *)zalloc(len + 1);
  1975. memcpy(prv_domain_name, domain_name, len);
  1976. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_DNS, prv_domain_name, param, user_data);
  1977. return 0;
  1978. }
  1979. static int net_lwip_dns_ipv6(const char *domain_name, uint32_t len, void *param, void *user_data)
  1980. {
  1981. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1982. char *prv_domain_name = (char *)zalloc(len + 1);
  1983. memcpy(prv_domain_name, domain_name, len);
  1984. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_DNS_IPV6, prv_domain_name, param, user_data);
  1985. return 0;
  1986. }
  1987. static int net_lwip_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data)
  1988. {
  1989. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1990. if (server_index >= MAX_DNS_SERVER) return -1;
  1991. prvlwip.dns_client.dns_server[server_index] = *ip;
  1992. prvlwip.dns_client.is_static_dns[server_index] = 1;
  1993. return 0;
  1994. }
  1995. static int net_lwip_set_mac(uint8_t *mac, void *user_data)
  1996. {
  1997. uint8_t index = (uint32_t)user_data;
  1998. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1999. if (!prvlwip.lwip_netif[index]) return -1;
  2000. memcpy(prvlwip.lwip_netif[index]->hwaddr, mac, 6);
  2001. return -1;
  2002. }
  2003. 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)
  2004. {
  2005. uint8_t index = (uint32_t)user_data;
  2006. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  2007. if (!prvlwip.lwip_netif[index]) return -1;
  2008. luat_ip_addr_t *p_ip = zalloc(sizeof(luat_ip_addr_t) * 5);
  2009. p_ip[0] = ip?(*ip):ip_addr_any_type;
  2010. p_ip[1] = submask?(*submask):ip_addr_any_type;
  2011. p_ip[2] = gateway?(*gateway):ip_addr_any_type;
  2012. p_ip[3] = ipv6?(*ipv6):ip_addr_any_type;
  2013. platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_SET_IP, p_ip, ipv6, user_data);
  2014. return 0;
  2015. }
  2016. static int32_t net_lwip_dummy_callback(void *pData, void *pParam)
  2017. {
  2018. return 0;
  2019. }
  2020. static void net_lwip_socket_set_callback(CBFuncEx_t cb_fun, void *param, void *user_data)
  2021. {
  2022. uint8_t index = (uint32_t)user_data;
  2023. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  2024. prvlwip.socket_cb[index] = cb_fun?cb_fun:net_lwip_dummy_callback;
  2025. prvlwip.user_data[index] = param;
  2026. }
  2027. static network_adapter_info prv_net_lwip_adapter =
  2028. {
  2029. .check_ready = net_lwip_check_ready,
  2030. .create_soceket = net_lwip_create_socket,
  2031. .socket_connect = net_lwip_socket_connect,
  2032. .socket_listen = net_lwip_socket_listen,
  2033. .socket_accept = net_lwip_socket_accept,
  2034. .socket_disconnect = net_lwip_socket_disconnect,
  2035. .socket_close = net_lwip_socket_close,
  2036. .socket_force_close = net_lwip_socket_force_close,
  2037. .socket_receive = net_lwip_socket_receive,
  2038. .socket_send = net_lwip_socket_send,
  2039. .socket_check = net_lwip_socket_check,
  2040. .socket_clean = net_lwip_socket_clean,
  2041. .getsockopt = net_lwip_getsockopt,
  2042. .setsockopt = net_lwip_setsockopt,
  2043. .user_cmd = net_lwip_user_cmd,
  2044. .dns = net_lwip_dns,
  2045. .dns_ipv6 = net_lwip_dns_ipv6,
  2046. .set_dns_server = net_lwip_set_dns_server,
  2047. .set_mac = net_lwip_set_mac,
  2048. .set_static_ip = net_lwip_set_static_ip,
  2049. .get_local_ip_info = net_lwip_get_local_ip_info,
  2050. .get_full_ip_info = net_lwip_get_full_ip_info,
  2051. .socket_set_callback = net_lwip_socket_set_callback,
  2052. .name = "lwip",
  2053. .max_socket_num = MAX_SOCK_NUM,
  2054. .no_accept = 1,
  2055. .is_posix = 1,
  2056. };
  2057. void net_lwip_register_adapter(uint8_t adapter_index)
  2058. {
  2059. network_register_adapter(adapter_index, &prv_net_lwip_adapter, adapter_index);
  2060. }
  2061. int net_lwip_check_all_ack(int socket_id)
  2062. {
  2063. if (!llist_empty(&prvlwip.socket[socket_id].wait_ack_head))
  2064. {
  2065. NET_DBG("socekt %d not all ack", socket_id);
  2066. return -1;
  2067. }
  2068. if (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  2069. {
  2070. NET_DBG("socekt %d not all send", socket_id);
  2071. return -1;
  2072. }
  2073. if (prvlwip.socket[socket_id].pcb.tcp->snd_buf != TCP_SND_BUF)
  2074. {
  2075. NET_DBG("socket %d send buf %ubytes, need %u",socket_id, prvlwip.socket[socket_id].pcb.tcp->snd_buf, TCP_SND_BUF);
  2076. }
  2077. return 0;
  2078. }
  2079. void net_lwip_set_netif(uint8_t adapter_index, struct netif *netif, void *init, uint8_t is_default)
  2080. {
  2081. if (!netif)
  2082. {
  2083. netif = zalloc(sizeof(struct netif));
  2084. }
  2085. prvlwip.lwip_netif[adapter_index] = netif;
  2086. netif_add(netif, NULL, NULL, NULL, NULL, init, netif_input);
  2087. netif->dhcp_done_callback = net_lwip_dhcp_done_cb;
  2088. netif->dhcp_done_arg = (void *)adapter_index;
  2089. // switch(adapter_index)
  2090. // {
  2091. // case NW_ADAPTER_INDEX_LWIP_WIFI_STA:
  2092. // case NW_ADAPTER_INDEX_LWIP_WIFI_AP:
  2093. // case NW_ADAPTER_INDEX_LWIP_ETH:
  2094. // prvlwip.lwip_netif[adapter_index]->flags |= (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET|NETIF_FLAG_BROADCAST);
  2095. // break;
  2096. // }
  2097. if (is_default)
  2098. {
  2099. prvlwip.dns_udp->netif_idx = netif_get_index(netif);
  2100. netif_set_default(netif);
  2101. prvlwip.dns_adapter_index = adapter_index;
  2102. }
  2103. }
  2104. struct netif * net_lwip_get_netif(uint8_t adapter_index)
  2105. {
  2106. return prvlwip.lwip_netif[adapter_index];
  2107. }
  2108. void net_lwip_input_packets(struct netif *netif, struct pbuf *p)
  2109. {
  2110. platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_INPUT, p, 0, netif);
  2111. }
  2112. void net_lwip_set_link_state(uint8_t adapter_index, uint8_t onoff)
  2113. {
  2114. platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_LINK_STATE, onoff, 0, adapter_index);
  2115. }
  2116. void net_lwip_set_ipv6_by_mac(uint8_t adapter_index)
  2117. {
  2118. platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_IPV6_BY_MAC, 0, 0, adapter_index);
  2119. }