luat_lwip_air101.c 69 KB

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