luat_network_lwip_tcpip_cb.c 59 KB

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