1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * netconn API (to be used from non-TCPIP threads)
4195972f6Sopenharmony_ci */
5195972f6Sopenharmony_ci
6195972f6Sopenharmony_ci/*
7195972f6Sopenharmony_ci * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8195972f6Sopenharmony_ci * All rights reserved.
9195972f6Sopenharmony_ci *
10195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
11195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
12195972f6Sopenharmony_ci *
13195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
14195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
15195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
16195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
17195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
18195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
19195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
20195972f6Sopenharmony_ci *
21195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30195972f6Sopenharmony_ci * OF SUCH DAMAGE.
31195972f6Sopenharmony_ci *
32195972f6Sopenharmony_ci * This file is part of the lwIP TCP/IP stack.
33195972f6Sopenharmony_ci *
34195972f6Sopenharmony_ci * Author: Adam Dunkels <adam@sics.se>
35195972f6Sopenharmony_ci *
36195972f6Sopenharmony_ci */
37195972f6Sopenharmony_ci#ifndef LWIP_HDR_API_H
38195972f6Sopenharmony_ci#define LWIP_HDR_API_H
39195972f6Sopenharmony_ci
40195972f6Sopenharmony_ci#include "lwip/opt.h"
41195972f6Sopenharmony_ci
42195972f6Sopenharmony_ci#if LWIP_NETCONN || LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
43195972f6Sopenharmony_ci/* Note: Netconn API is always available when sockets are enabled -
44195972f6Sopenharmony_ci * sockets are implemented on top of them */
45195972f6Sopenharmony_ci
46195972f6Sopenharmony_ci#include "lwip/arch.h"
47195972f6Sopenharmony_ci#include "lwip/netbuf.h"
48195972f6Sopenharmony_ci#include "lwip/sys.h"
49195972f6Sopenharmony_ci#include "lwip/ip_addr.h"
50195972f6Sopenharmony_ci#include "lwip/err.h"
51195972f6Sopenharmony_ci
52195972f6Sopenharmony_ci#ifdef __cplusplus
53195972f6Sopenharmony_ciextern "C" {
54195972f6Sopenharmony_ci#endif
55195972f6Sopenharmony_ci
56195972f6Sopenharmony_ci/* Throughout this file, IP addresses and port numbers are expected to be in
57195972f6Sopenharmony_ci * the same byte order as in the corresponding pcb.
58195972f6Sopenharmony_ci */
59195972f6Sopenharmony_ci
60195972f6Sopenharmony_ci/* Flags for netconn_write (u8_t) */
61195972f6Sopenharmony_ci#define NETCONN_NOFLAG      0x00
62195972f6Sopenharmony_ci#define NETCONN_NOCOPY      0x00 /* Only for source code compatibility */
63195972f6Sopenharmony_ci#define NETCONN_COPY        0x01
64195972f6Sopenharmony_ci#define NETCONN_MORE        0x02
65195972f6Sopenharmony_ci#define NETCONN_DONTBLOCK   0x04
66195972f6Sopenharmony_ci#define NETCONN_NOAUTORCVD  0x08 /* prevent netconn_recv_data_tcp() from updating the tcp window - must be done manually via netconn_tcp_recvd() */
67195972f6Sopenharmony_ci#define NETCONN_NOFIN       0x10 /* upper layer already received data, leave FIN in queue until called again */
68195972f6Sopenharmony_ci
69195972f6Sopenharmony_ci/* Flags for struct netconn.flags (u8_t) */
70195972f6Sopenharmony_ci/** This netconn had an error, don't block on recvmbox/acceptmbox any more */
71195972f6Sopenharmony_ci#define NETCONN_FLAG_MBOXCLOSED               0x01
72195972f6Sopenharmony_ci/** Should this netconn avoid blocking? */
73195972f6Sopenharmony_ci#define NETCONN_FLAG_NON_BLOCKING             0x02
74195972f6Sopenharmony_ci/** Was the last connect action a non-blocking one? */
75195972f6Sopenharmony_ci#define NETCONN_FLAG_IN_NONBLOCKING_CONNECT   0x04
76195972f6Sopenharmony_ci#if LWIP_NETCONN_FULLDUPLEX
77195972f6Sopenharmony_ci  /** The mbox of this netconn is being deallocated, don't use it anymore */
78195972f6Sopenharmony_ci#define NETCONN_FLAG_MBOXINVALID              0x08
79195972f6Sopenharmony_ci#endif /* LWIP_NETCONN_FULLDUPLEX */
80195972f6Sopenharmony_ci/** If a nonblocking write has been rejected before, poll_tcp needs to
81195972f6Sopenharmony_ci    check if the netconn is writable again */
82195972f6Sopenharmony_ci#define NETCONN_FLAG_CHECK_WRITESPACE         0x10
83195972f6Sopenharmony_ci#if LWIP_IPV6
84195972f6Sopenharmony_ci/** If this flag is set then only IPv6 communication is allowed on the
85195972f6Sopenharmony_ci    netconn. As per RFC#3493 this features defaults to OFF allowing
86195972f6Sopenharmony_ci    dual-stack usage by default. */
87195972f6Sopenharmony_ci#define NETCONN_FLAG_IPV6_V6ONLY              0x20
88195972f6Sopenharmony_ci#endif /* LWIP_IPV6 */
89195972f6Sopenharmony_ci#if LWIP_NETBUF_RECVINFO
90195972f6Sopenharmony_ci/** Received packet info will be recorded for this netconn */
91195972f6Sopenharmony_ci#define NETCONN_FLAG_PKTINFO                  0x40
92195972f6Sopenharmony_ci#endif /* LWIP_NETBUF_RECVINFO */
93195972f6Sopenharmony_ci/** A FIN has been received but not passed to the application yet */
94195972f6Sopenharmony_ci#define NETCONN_FIN_RX_PENDING                0x80
95195972f6Sopenharmony_ci
96195972f6Sopenharmony_ci/* Helpers to process several netconn_types by the same code */
97195972f6Sopenharmony_ci#define NETCONNTYPE_GROUP(t)         ((t)&0xF0)
98195972f6Sopenharmony_ci#define NETCONNTYPE_DATAGRAM(t)      ((t)&0xE0)
99195972f6Sopenharmony_ci#if LWIP_IPV6
100195972f6Sopenharmony_ci#define NETCONN_TYPE_IPV6            0x08
101195972f6Sopenharmony_ci#define NETCONNTYPE_ISIPV6(t)        (((t)&NETCONN_TYPE_IPV6) != 0)
102195972f6Sopenharmony_ci#define NETCONNTYPE_ISUDPLITE(t)     (((t)&0xF3) == NETCONN_UDPLITE)
103195972f6Sopenharmony_ci#define NETCONNTYPE_ISUDPNOCHKSUM(t) (((t)&0xF3) == NETCONN_UDPNOCHKSUM)
104195972f6Sopenharmony_ci#else /* LWIP_IPV6 */
105195972f6Sopenharmony_ci#define NETCONNTYPE_ISIPV6(t)        (0)
106195972f6Sopenharmony_ci#define NETCONNTYPE_ISUDPLITE(t)     ((t) == NETCONN_UDPLITE)
107195972f6Sopenharmony_ci#define NETCONNTYPE_ISUDPNOCHKSUM(t) ((t) == NETCONN_UDPNOCHKSUM)
108195972f6Sopenharmony_ci#endif /* LWIP_IPV6 */
109195972f6Sopenharmony_ci
110195972f6Sopenharmony_ci/** @ingroup netconn_common
111195972f6Sopenharmony_ci * Protocol family and type of the netconn
112195972f6Sopenharmony_ci */
113195972f6Sopenharmony_cienum netconn_type {
114195972f6Sopenharmony_ci  NETCONN_INVALID     = 0,
115195972f6Sopenharmony_ci  /** TCP IPv4 */
116195972f6Sopenharmony_ci  NETCONN_TCP         = 0x10,
117195972f6Sopenharmony_ci#if LWIP_IPV6
118195972f6Sopenharmony_ci  /** TCP IPv6 */
119195972f6Sopenharmony_ci  NETCONN_TCP_IPV6    = NETCONN_TCP | NETCONN_TYPE_IPV6 /* 0x18 */,
120195972f6Sopenharmony_ci#endif /* LWIP_IPV6 */
121195972f6Sopenharmony_ci  /** UDP IPv4 */
122195972f6Sopenharmony_ci  NETCONN_UDP         = 0x20,
123195972f6Sopenharmony_ci  /** UDP IPv4 lite */
124195972f6Sopenharmony_ci  NETCONN_UDPLITE     = 0x21,
125195972f6Sopenharmony_ci  /** UDP IPv4 no checksum */
126195972f6Sopenharmony_ci  NETCONN_UDPNOCHKSUM = 0x22,
127195972f6Sopenharmony_ci
128195972f6Sopenharmony_ci#if LWIP_IPV6
129195972f6Sopenharmony_ci  /** UDP IPv6 (dual-stack by default, unless you call @ref netconn_set_ipv6only) */
130195972f6Sopenharmony_ci  NETCONN_UDP_IPV6         = NETCONN_UDP | NETCONN_TYPE_IPV6 /* 0x28 */,
131195972f6Sopenharmony_ci  /** UDP IPv6 lite (dual-stack by default, unless you call @ref netconn_set_ipv6only) */
132195972f6Sopenharmony_ci  NETCONN_UDPLITE_IPV6     = NETCONN_UDPLITE | NETCONN_TYPE_IPV6 /* 0x29 */,
133195972f6Sopenharmony_ci  /** UDP IPv6 no checksum (dual-stack by default, unless you call @ref netconn_set_ipv6only) */
134195972f6Sopenharmony_ci  NETCONN_UDPNOCHKSUM_IPV6 = NETCONN_UDPNOCHKSUM | NETCONN_TYPE_IPV6 /* 0x2a */,
135195972f6Sopenharmony_ci#endif /* LWIP_IPV6 */
136195972f6Sopenharmony_ci
137195972f6Sopenharmony_ci  /** Raw connection IPv4 */
138195972f6Sopenharmony_ci  NETCONN_RAW         = 0x40
139195972f6Sopenharmony_ci#if LWIP_IPV6
140195972f6Sopenharmony_ci  /** Raw connection IPv6 (dual-stack by default, unless you call @ref netconn_set_ipv6only) */
141195972f6Sopenharmony_ci  , NETCONN_RAW_IPV6    = NETCONN_RAW | NETCONN_TYPE_IPV6 /* 0x48 */
142195972f6Sopenharmony_ci#endif /* LWIP_IPV6 */
143195972f6Sopenharmony_ci};
144195972f6Sopenharmony_ci
145195972f6Sopenharmony_ci/** Current state of the netconn. Non-TCP netconns are always
146195972f6Sopenharmony_ci * in state NETCONN_NONE! */
147195972f6Sopenharmony_cienum netconn_state {
148195972f6Sopenharmony_ci  NETCONN_NONE,
149195972f6Sopenharmony_ci  NETCONN_WRITE,
150195972f6Sopenharmony_ci  NETCONN_LISTEN,
151195972f6Sopenharmony_ci  NETCONN_CONNECT,
152195972f6Sopenharmony_ci  NETCONN_CLOSE
153195972f6Sopenharmony_ci};
154195972f6Sopenharmony_ci
155195972f6Sopenharmony_ci/** Used to inform the callback function about changes
156195972f6Sopenharmony_ci *
157195972f6Sopenharmony_ci * Event explanation:
158195972f6Sopenharmony_ci *
159195972f6Sopenharmony_ci * In the netconn implementation, there are three ways to block a client:
160195972f6Sopenharmony_ci *
161195972f6Sopenharmony_ci * - accept mbox (sys_arch_mbox_fetch(&conn->acceptmbox, &accept_ptr, 0); in netconn_accept())
162195972f6Sopenharmony_ci * - receive mbox (sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0); in netconn_recv_data())
163195972f6Sopenharmony_ci * - send queue is full (sys_arch_sem_wait(LWIP_API_MSG_SEM(msg), 0); in lwip_netconn_do_write())
164195972f6Sopenharmony_ci *
165195972f6Sopenharmony_ci * The events have to be seen as events signaling the state of these mboxes/semaphores. For non-blocking
166195972f6Sopenharmony_ci * connections, you need to know in advance whether a call to a netconn function call would block or not,
167195972f6Sopenharmony_ci * and these events tell you about that.
168195972f6Sopenharmony_ci *
169195972f6Sopenharmony_ci * RCVPLUS events say: Safe to perform a potentially blocking call call once more.
170195972f6Sopenharmony_ci * They are counted in sockets - three RCVPLUS events for accept mbox means you are safe
171195972f6Sopenharmony_ci * to call netconn_accept 3 times without being blocked.
172195972f6Sopenharmony_ci * Same thing for receive mbox.
173195972f6Sopenharmony_ci *
174195972f6Sopenharmony_ci * RCVMINUS events say: Your call to to a possibly blocking function is "acknowledged".
175195972f6Sopenharmony_ci * Socket implementation decrements the counter.
176195972f6Sopenharmony_ci *
177195972f6Sopenharmony_ci * For TX, there is no need to count, its merely a flag. SENDPLUS means you may send something.
178195972f6Sopenharmony_ci * SENDPLUS occurs when enough data was delivered to peer so netconn_send() can be called again.
179195972f6Sopenharmony_ci * A SENDMINUS event occurs when the next call to a netconn_send() would be blocking.
180195972f6Sopenharmony_ci */
181195972f6Sopenharmony_cienum netconn_evt {
182195972f6Sopenharmony_ci  NETCONN_EVT_RCVPLUS,
183195972f6Sopenharmony_ci  NETCONN_EVT_RCVMINUS,
184195972f6Sopenharmony_ci  NETCONN_EVT_SENDPLUS,
185195972f6Sopenharmony_ci  NETCONN_EVT_SENDMINUS,
186195972f6Sopenharmony_ci  NETCONN_EVT_ERROR
187195972f6Sopenharmony_ci};
188195972f6Sopenharmony_ci
189195972f6Sopenharmony_ci#if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
190195972f6Sopenharmony_ci/** Used for netconn_join_leave_group() */
191195972f6Sopenharmony_cienum netconn_igmp {
192195972f6Sopenharmony_ci  NETCONN_JOIN,
193195972f6Sopenharmony_ci  NETCONN_LEAVE
194195972f6Sopenharmony_ci};
195195972f6Sopenharmony_ci#endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
196195972f6Sopenharmony_ci
197195972f6Sopenharmony_ci#if LWIP_DNS
198195972f6Sopenharmony_ci/* Used for netconn_gethostbyname_addrtype(), these should match the DNS_ADDRTYPE defines in dns.h */
199195972f6Sopenharmony_ci#define NETCONN_DNS_DEFAULT   NETCONN_DNS_IPV4_IPV6
200195972f6Sopenharmony_ci#define NETCONN_DNS_IPV4      0
201195972f6Sopenharmony_ci#define NETCONN_DNS_IPV6      1
202195972f6Sopenharmony_ci#define NETCONN_DNS_IPV4_IPV6 2 /* try to resolve IPv4 first, try IPv6 if IPv4 fails only */
203195972f6Sopenharmony_ci#define NETCONN_DNS_IPV6_IPV4 3 /* try to resolve IPv6 first, try IPv4 if IPv6 fails only */
204195972f6Sopenharmony_ci#endif /* LWIP_DNS */
205195972f6Sopenharmony_ci
206195972f6Sopenharmony_ci/* forward-declare some structs to avoid to include their headers */
207195972f6Sopenharmony_cistruct ip_pcb;
208195972f6Sopenharmony_cistruct tcp_pcb;
209195972f6Sopenharmony_cistruct udp_pcb;
210195972f6Sopenharmony_cistruct raw_pcb;
211195972f6Sopenharmony_cistruct netconn;
212195972f6Sopenharmony_cistruct api_msg;
213195972f6Sopenharmony_ci
214195972f6Sopenharmony_ci/** A callback prototype to inform about events for a netconn */
215195972f6Sopenharmony_citypedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
216195972f6Sopenharmony_ci
217195972f6Sopenharmony_ci/** A netconn descriptor */
218195972f6Sopenharmony_cistruct netconn {
219195972f6Sopenharmony_ci  /** type of the netconn (TCP, UDP or RAW) */
220195972f6Sopenharmony_ci  enum netconn_type type;
221195972f6Sopenharmony_ci  /** current state of the netconn */
222195972f6Sopenharmony_ci  enum netconn_state state;
223195972f6Sopenharmony_ci  /** the lwIP internal protocol control block */
224195972f6Sopenharmony_ci  union {
225195972f6Sopenharmony_ci    struct ip_pcb  *ip;
226195972f6Sopenharmony_ci    struct tcp_pcb *tcp;
227195972f6Sopenharmony_ci    struct udp_pcb *udp;
228195972f6Sopenharmony_ci    struct raw_pcb *raw;
229195972f6Sopenharmony_ci  } pcb;
230195972f6Sopenharmony_ci  /** the last asynchronous unreported error this netconn had */
231195972f6Sopenharmony_ci  err_t pending_err;
232195972f6Sopenharmony_ci#if !LWIP_NETCONN_SEM_PER_THREAD
233195972f6Sopenharmony_ci  /** sem that is used to synchronously execute functions in the core context */
234195972f6Sopenharmony_ci  sys_sem_t op_completed;
235195972f6Sopenharmony_ci#endif
236195972f6Sopenharmony_ci  /** mbox where received packets are stored until they are fetched
237195972f6Sopenharmony_ci      by the netconn application thread (can grow quite big) */
238195972f6Sopenharmony_ci  sys_mbox_t recvmbox;
239195972f6Sopenharmony_ci#if LWIP_TCP
240195972f6Sopenharmony_ci  /** mbox where new connections are stored until processed
241195972f6Sopenharmony_ci      by the application thread */
242195972f6Sopenharmony_ci  sys_mbox_t acceptmbox;
243195972f6Sopenharmony_ci#endif /* LWIP_TCP */
244195972f6Sopenharmony_ci#if LWIP_NETCONN_FULLDUPLEX
245195972f6Sopenharmony_ci  /** number of threads waiting on an mbox. This is required to unblock
246195972f6Sopenharmony_ci      all threads when closing while threads are waiting. */
247195972f6Sopenharmony_ci  int mbox_threads_waiting;
248195972f6Sopenharmony_ci#endif
249195972f6Sopenharmony_ci  /** only used for socket layer */
250195972f6Sopenharmony_ci#if LWIP_SOCKET
251195972f6Sopenharmony_ci  int socket;
252195972f6Sopenharmony_ci#endif /* LWIP_SOCKET */
253195972f6Sopenharmony_ci#if LWIP_SO_SNDTIMEO
254195972f6Sopenharmony_ci  /** timeout to wait for sending data (which means enqueueing data for sending
255195972f6Sopenharmony_ci      in internal buffers) in milliseconds */
256195972f6Sopenharmony_ci  s32_t send_timeout;
257195972f6Sopenharmony_ci#endif /* LWIP_SO_RCVTIMEO */
258195972f6Sopenharmony_ci#if LWIP_SO_RCVTIMEO
259195972f6Sopenharmony_ci  /** timeout in milliseconds to wait for new data to be received
260195972f6Sopenharmony_ci      (or connections to arrive for listening netconns) */
261195972f6Sopenharmony_ci  u32_t recv_timeout;
262195972f6Sopenharmony_ci#endif /* LWIP_SO_RCVTIMEO */
263195972f6Sopenharmony_ci#if LWIP_SO_RCVBUF
264195972f6Sopenharmony_ci  /** maximum amount of bytes queued in recvmbox
265195972f6Sopenharmony_ci      not used for TCP: adjust TCP_WND instead! */
266195972f6Sopenharmony_ci  int recv_bufsize;
267195972f6Sopenharmony_ci  /** number of bytes currently in recvmbox to be received,
268195972f6Sopenharmony_ci      tested against recv_bufsize to limit bytes on recvmbox
269195972f6Sopenharmony_ci      for UDP and RAW, used for FIONREAD */
270195972f6Sopenharmony_ci  int recv_avail;
271195972f6Sopenharmony_ci#endif /* LWIP_SO_RCVBUF */
272195972f6Sopenharmony_ci#if LWIP_SO_LINGER
273195972f6Sopenharmony_ci   /** values <0 mean linger is disabled, values > 0 are seconds to linger */
274195972f6Sopenharmony_ci  s16_t linger;
275195972f6Sopenharmony_ci#endif /* LWIP_SO_LINGER */
276195972f6Sopenharmony_ci  /** flags holding more netconn-internal state, see NETCONN_FLAG_* defines */
277195972f6Sopenharmony_ci  u8_t flags;
278195972f6Sopenharmony_ci#if LWIP_TCP
279195972f6Sopenharmony_ci  /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
280195972f6Sopenharmony_ci      this temporarily stores the message.
281195972f6Sopenharmony_ci      Also used during connect and close. */
282195972f6Sopenharmony_ci  struct api_msg *current_msg;
283195972f6Sopenharmony_ci#endif /* LWIP_TCP */
284195972f6Sopenharmony_ci  /** A callback function that is informed about events for this netconn */
285195972f6Sopenharmony_ci  netconn_callback callback;
286195972f6Sopenharmony_ci};
287195972f6Sopenharmony_ci
288195972f6Sopenharmony_ci/** This vector type is passed to @ref netconn_write_vectors_partly to send
289195972f6Sopenharmony_ci * multiple buffers at once.
290195972f6Sopenharmony_ci * ATTENTION: This type has to directly map struct iovec since one is casted
291195972f6Sopenharmony_ci *            into the other!
292195972f6Sopenharmony_ci */
293195972f6Sopenharmony_cistruct netvector {
294195972f6Sopenharmony_ci  /** pointer to the application buffer that contains the data to send */
295195972f6Sopenharmony_ci  const void *ptr;
296195972f6Sopenharmony_ci  /** size of the application data to send */
297195972f6Sopenharmony_ci  size_t len;
298195972f6Sopenharmony_ci};
299195972f6Sopenharmony_ci
300195972f6Sopenharmony_ci/** Register an Network connection event */
301195972f6Sopenharmony_ci#define API_EVENT(c,e,l) if (c->callback) {         \
302195972f6Sopenharmony_ci                           (*c->callback)(c, e, l); \
303195972f6Sopenharmony_ci                         }
304195972f6Sopenharmony_ci
305195972f6Sopenharmony_ci/* Network connection functions: */
306195972f6Sopenharmony_ci
307195972f6Sopenharmony_ci/** @ingroup netconn_common
308195972f6Sopenharmony_ci * Create new netconn connection
309195972f6Sopenharmony_ci * @param t @ref netconn_type */
310195972f6Sopenharmony_ci#define netconn_new(t)                  netconn_new_with_proto_and_callback(t, 0, NULL)
311195972f6Sopenharmony_ci#define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
312195972f6Sopenharmony_cistruct netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
313195972f6Sopenharmony_ci                                             netconn_callback callback);
314195972f6Sopenharmony_cierr_t   netconn_prepare_delete(struct netconn *conn);
315195972f6Sopenharmony_cierr_t   netconn_delete(struct netconn *conn);
316195972f6Sopenharmony_ci/** Get the type of a netconn (as enum netconn_type). */
317195972f6Sopenharmony_ci#define netconn_type(conn) (conn->type)
318195972f6Sopenharmony_ci
319195972f6Sopenharmony_cierr_t   netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
320195972f6Sopenharmony_ci                        u16_t *port, u8_t local);
321195972f6Sopenharmony_ci/** @ingroup netconn_common */
322195972f6Sopenharmony_ci#define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
323195972f6Sopenharmony_ci/** @ingroup netconn_common */
324195972f6Sopenharmony_ci#define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
325195972f6Sopenharmony_ci
326195972f6Sopenharmony_cierr_t   netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port);
327195972f6Sopenharmony_cierr_t   netconn_bind_if(struct netconn *conn, u8_t if_idx);
328195972f6Sopenharmony_cierr_t   netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port);
329195972f6Sopenharmony_cierr_t   netconn_disconnect (struct netconn *conn);
330195972f6Sopenharmony_cierr_t   netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
331195972f6Sopenharmony_ci/** @ingroup netconn_tcp */
332195972f6Sopenharmony_ci#define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
333195972f6Sopenharmony_cierr_t   netconn_accept(struct netconn *conn, struct netconn **new_conn);
334195972f6Sopenharmony_cierr_t   netconn_recv(struct netconn *conn, struct netbuf **new_buf);
335195972f6Sopenharmony_cierr_t   netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf);
336195972f6Sopenharmony_cierr_t   netconn_recv_udp_raw_netbuf_flags(struct netconn *conn, struct netbuf **new_buf, u8_t apiflags);
337195972f6Sopenharmony_cierr_t   netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
338195972f6Sopenharmony_cierr_t   netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags);
339195972f6Sopenharmony_cierr_t   netconn_tcp_recvd(struct netconn *conn, size_t len);
340195972f6Sopenharmony_cierr_t   netconn_sendto(struct netconn *conn, struct netbuf *buf,
341195972f6Sopenharmony_ci                             const ip_addr_t *addr, u16_t port);
342195972f6Sopenharmony_cierr_t   netconn_send(struct netconn *conn, struct netbuf *buf);
343195972f6Sopenharmony_cierr_t   netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
344195972f6Sopenharmony_ci                             u8_t apiflags, size_t *bytes_written);
345195972f6Sopenharmony_cierr_t   netconn_write_vectors_partly(struct netconn *conn, struct netvector *vectors, u16_t vectorcnt,
346195972f6Sopenharmony_ci                                     u8_t apiflags, size_t *bytes_written);
347195972f6Sopenharmony_ci/** @ingroup netconn_tcp */
348195972f6Sopenharmony_ci#define netconn_write(conn, dataptr, size, apiflags) \
349195972f6Sopenharmony_ci          netconn_write_partly(conn, dataptr, size, apiflags, NULL)
350195972f6Sopenharmony_cierr_t   netconn_close(struct netconn *conn);
351195972f6Sopenharmony_cierr_t   netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
352195972f6Sopenharmony_ci
353195972f6Sopenharmony_ci#if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
354195972f6Sopenharmony_cierr_t   netconn_join_leave_group(struct netconn *conn, const ip_addr_t *multiaddr,
355195972f6Sopenharmony_ci                             const ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
356195972f6Sopenharmony_cierr_t   netconn_join_leave_group_netif(struct netconn *conn, const ip_addr_t *multiaddr,
357195972f6Sopenharmony_ci                             u8_t if_idx, enum netconn_igmp join_or_leave);
358195972f6Sopenharmony_ci#endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
359195972f6Sopenharmony_ci#if LWIP_DNS
360195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6
361195972f6Sopenharmony_cierr_t   netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype);
362195972f6Sopenharmony_ci#define netconn_gethostbyname(name, addr) netconn_gethostbyname_addrtype(name, addr, NETCONN_DNS_DEFAULT)
363195972f6Sopenharmony_ci#else /* LWIP_IPV4 && LWIP_IPV6 */
364195972f6Sopenharmony_cierr_t   netconn_gethostbyname(const char *name, ip_addr_t *addr);
365195972f6Sopenharmony_ci#define netconn_gethostbyname_addrtype(name, addr, dns_addrtype) netconn_gethostbyname(name, addr)
366195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */
367195972f6Sopenharmony_ci#endif /* LWIP_DNS */
368195972f6Sopenharmony_ci
369195972f6Sopenharmony_cierr_t   netconn_err(struct netconn *conn);
370195972f6Sopenharmony_ci#define netconn_recv_bufsize(conn)      ((conn)->recv_bufsize)
371195972f6Sopenharmony_ci
372195972f6Sopenharmony_ci#define netconn_set_flags(conn, set_flags)     do { (conn)->flags = (u8_t)((conn)->flags |  (set_flags)); } while(0)
373195972f6Sopenharmony_ci#define netconn_clear_flags(conn, clr_flags)   do { (conn)->flags = (u8_t)((conn)->flags & (u8_t)(~(clr_flags) & 0xff)); } while(0)
374195972f6Sopenharmony_ci#define netconn_is_flag_set(conn, flag)        (((conn)->flags & (flag)) != 0)
375195972f6Sopenharmony_ci
376195972f6Sopenharmony_ci/** Set the blocking status of netconn calls (@todo: write/send is missing) */
377195972f6Sopenharmony_ci#define netconn_set_nonblocking(conn, val)  do { if(val) { \
378195972f6Sopenharmony_ci  netconn_set_flags(conn, NETCONN_FLAG_NON_BLOCKING); \
379195972f6Sopenharmony_ci} else { \
380195972f6Sopenharmony_ci  netconn_clear_flags(conn, NETCONN_FLAG_NON_BLOCKING); }} while(0)
381195972f6Sopenharmony_ci/** Get the blocking status of netconn calls (@todo: write/send is missing) */
382195972f6Sopenharmony_ci#define netconn_is_nonblocking(conn)        (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
383195972f6Sopenharmony_ci
384195972f6Sopenharmony_ci#if LWIP_IPV6
385195972f6Sopenharmony_ci/** @ingroup netconn_common
386195972f6Sopenharmony_ci * TCP: Set the IPv6 ONLY status of netconn calls (see NETCONN_FLAG_IPV6_V6ONLY)
387195972f6Sopenharmony_ci */
388195972f6Sopenharmony_ci#define netconn_set_ipv6only(conn, val)  do { if(val) { \
389195972f6Sopenharmony_ci  netconn_set_flags(conn, NETCONN_FLAG_IPV6_V6ONLY); \
390195972f6Sopenharmony_ci} else { \
391195972f6Sopenharmony_ci  netconn_clear_flags(conn, NETCONN_FLAG_IPV6_V6ONLY); }} while(0)
392195972f6Sopenharmony_ci/** @ingroup netconn_common
393195972f6Sopenharmony_ci * TCP: Get the IPv6 ONLY status of netconn calls (see NETCONN_FLAG_IPV6_V6ONLY)
394195972f6Sopenharmony_ci */
395195972f6Sopenharmony_ci#define netconn_get_ipv6only(conn)        (((conn)->flags & NETCONN_FLAG_IPV6_V6ONLY) != 0)
396195972f6Sopenharmony_ci#endif /* LWIP_IPV6 */
397195972f6Sopenharmony_ci
398195972f6Sopenharmony_ci#if LWIP_SO_SNDTIMEO
399195972f6Sopenharmony_ci/** Set the send timeout in milliseconds */
400195972f6Sopenharmony_ci#define netconn_set_sendtimeout(conn, timeout)      ((conn)->send_timeout = (timeout))
401195972f6Sopenharmony_ci/** Get the send timeout in milliseconds */
402195972f6Sopenharmony_ci#define netconn_get_sendtimeout(conn)               ((conn)->send_timeout)
403195972f6Sopenharmony_ci#endif /* LWIP_SO_SNDTIMEO */
404195972f6Sopenharmony_ci#if LWIP_SO_RCVTIMEO
405195972f6Sopenharmony_ci/** Set the receive timeout in milliseconds */
406195972f6Sopenharmony_ci#define netconn_set_recvtimeout(conn, timeout)      ((conn)->recv_timeout = (timeout))
407195972f6Sopenharmony_ci/** Get the receive timeout in milliseconds */
408195972f6Sopenharmony_ci#define netconn_get_recvtimeout(conn)               ((conn)->recv_timeout)
409195972f6Sopenharmony_ci#endif /* LWIP_SO_RCVTIMEO */
410195972f6Sopenharmony_ci#if LWIP_SO_RCVBUF
411195972f6Sopenharmony_ci/** Set the receive buffer in bytes */
412195972f6Sopenharmony_ci#define netconn_set_recvbufsize(conn, recvbufsize)  ((conn)->recv_bufsize = (recvbufsize))
413195972f6Sopenharmony_ci/** Get the receive buffer in bytes */
414195972f6Sopenharmony_ci#define netconn_get_recvbufsize(conn)               ((conn)->recv_bufsize)
415195972f6Sopenharmony_ci#endif /* LWIP_SO_RCVBUF*/
416195972f6Sopenharmony_ci
417195972f6Sopenharmony_ci#if LWIP_NETCONN_SEM_PER_THREAD
418195972f6Sopenharmony_civoid netconn_thread_init(void);
419195972f6Sopenharmony_civoid netconn_thread_cleanup(void);
420195972f6Sopenharmony_ci#else /* LWIP_NETCONN_SEM_PER_THREAD */
421195972f6Sopenharmony_ci#define netconn_thread_init()
422195972f6Sopenharmony_ci#define netconn_thread_cleanup()
423195972f6Sopenharmony_ci#endif /* LWIP_NETCONN_SEM_PER_THREAD */
424195972f6Sopenharmony_ci
425195972f6Sopenharmony_ci#ifdef __cplusplus
426195972f6Sopenharmony_ci}
427195972f6Sopenharmony_ci#endif
428195972f6Sopenharmony_ci
429195972f6Sopenharmony_ci#endif /* LWIP_NETCONN || LWIP_SOCKET */
430195972f6Sopenharmony_ci
431195972f6Sopenharmony_ci#endif /* LWIP_HDR_API_H */
432