BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file
3 * netconn API (to be used from non-TCPIP threads)
4 */
5  
6 /*
7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 *
32 * This file is part of the lwIP TCP/IP stack.
33 *
34 * Author: Adam Dunkels <adam@sics.se>
35 *
36 */
37 #ifndef LWIP_HDR_API_H
38 #define LWIP_HDR_API_H
39  
40 #include "lwip/opt.h"
41  
42 #if LWIP_NETCONN || LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
43 /* Note: Netconn API is always available when sockets are enabled -
44 * sockets are implemented on top of them */
45  
46 #include "lwip/arch.h"
47 #include "lwip/netbuf.h"
48 #include "lwip/sys.h"
49 #include "lwip/ip_addr.h"
50 #include "lwip/err.h"
51  
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55  
56 /* Throughout this file, IP addresses and port numbers are expected to be in
57 * the same byte order as in the corresponding pcb.
58 */
59  
60 /* Flags for netconn_write (u8_t) */
61 #define NETCONN_NOFLAG 0x00
62 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
63 #define NETCONN_COPY 0x01
64 #define NETCONN_MORE 0x02
65 #define NETCONN_DONTBLOCK 0x04
66 #define NETCONN_NOAUTORCVD 0x08 /* prevent netconn_recv_data_tcp() from updating the tcp window - must be done manually via netconn_tcp_recvd() */
67  
68 /* Flags for struct netconn.flags (u8_t) */
69 /** This netconn had an error, don't block on recvmbox/acceptmbox any more */
70 #define NETCONN_FLAG_MBOXCLOSED 0x01
71 /** Should this netconn avoid blocking? */
72 #define NETCONN_FLAG_NON_BLOCKING 0x02
73 /** Was the last connect action a non-blocking one? */
74 #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
75 /** If a nonblocking write has been rejected before, poll_tcp needs to
76 check if the netconn is writable again */
77 #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
78 #if LWIP_IPV6
79 /** If this flag is set then only IPv6 communication is allowed on the
80 netconn. As per RFC#3493 this features defaults to OFF allowing
81 dual-stack usage by default. */
82 #define NETCONN_FLAG_IPV6_V6ONLY 0x20
83 #endif /* LWIP_IPV6 */
84 #if LWIP_NETBUF_RECVINFO
85 /** Received packet info will be recorded for this netconn */
86 #define NETCONN_FLAG_PKTINFO 0x40
87 #endif /* LWIP_NETBUF_RECVINFO */
88  
89  
90 /* Helpers to process several netconn_types by the same code */
91 #define NETCONNTYPE_GROUP(t) ((t)&0xF0)
92 #define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0)
93 #if LWIP_IPV6
94 #define NETCONN_TYPE_IPV6 0x08
95 #define NETCONNTYPE_ISIPV6(t) (((t)&NETCONN_TYPE_IPV6) != 0)
96 #define NETCONNTYPE_ISUDPLITE(t) (((t)&0xF3) == NETCONN_UDPLITE)
97 #define NETCONNTYPE_ISUDPNOCHKSUM(t) (((t)&0xF3) == NETCONN_UDPNOCHKSUM)
98 #else /* LWIP_IPV6 */
99 #define NETCONNTYPE_ISIPV6(t) (0)
100 #define NETCONNTYPE_ISUDPLITE(t) ((t) == NETCONN_UDPLITE)
101 #define NETCONNTYPE_ISUDPNOCHKSUM(t) ((t) == NETCONN_UDPNOCHKSUM)
102 #endif /* LWIP_IPV6 */
103  
104 /** @ingroup netconn_common
105 * Protocol family and type of the netconn
106 */
107 enum netconn_type {
108 NETCONN_INVALID = 0,
109 /** TCP IPv4 */
110 NETCONN_TCP = 0x10,
111 #if LWIP_IPV6
112 /** TCP IPv6 */
113 NETCONN_TCP_IPV6 = NETCONN_TCP | NETCONN_TYPE_IPV6 /* 0x18 */,
114 #endif /* LWIP_IPV6 */
115 /** UDP IPv4 */
116 NETCONN_UDP = 0x20,
117 /** UDP IPv4 lite */
118 NETCONN_UDPLITE = 0x21,
119 /** UDP IPv4 no checksum */
120 NETCONN_UDPNOCHKSUM = 0x22,
121  
122 #if LWIP_IPV6
123 /** UDP IPv6 (dual-stack by default, unless you call @ref netconn_set_ipv6only) */
124 NETCONN_UDP_IPV6 = NETCONN_UDP | NETCONN_TYPE_IPV6 /* 0x28 */,
125 /** UDP IPv6 lite (dual-stack by default, unless you call @ref netconn_set_ipv6only) */
126 NETCONN_UDPLITE_IPV6 = NETCONN_UDPLITE | NETCONN_TYPE_IPV6 /* 0x29 */,
127 /** UDP IPv6 no checksum (dual-stack by default, unless you call @ref netconn_set_ipv6only) */
128 NETCONN_UDPNOCHKSUM_IPV6 = NETCONN_UDPNOCHKSUM | NETCONN_TYPE_IPV6 /* 0x2a */,
129 #endif /* LWIP_IPV6 */
130  
131 /** Raw connection IPv4 */
132 NETCONN_RAW = 0x40
133 #if LWIP_IPV6
134 /** Raw connection IPv6 (dual-stack by default, unless you call @ref netconn_set_ipv6only) */
135 , NETCONN_RAW_IPV6 = NETCONN_RAW | NETCONN_TYPE_IPV6 /* 0x48 */
136 #endif /* LWIP_IPV6 */
137 };
138  
139 /** Current state of the netconn. Non-TCP netconns are always
140 * in state NETCONN_NONE! */
141 enum netconn_state {
142 NETCONN_NONE,
143 NETCONN_WRITE,
144 NETCONN_LISTEN,
145 NETCONN_CONNECT,
146 NETCONN_CLOSE
147 };
148  
149 /** Used to inform the callback function about changes
150 *
151 * Event explanation:
152 *
153 * In the netconn implementation, there are three ways to block a client:
154 *
155 * - accept mbox (sys_arch_mbox_fetch(&conn->acceptmbox, &accept_ptr, 0); in netconn_accept())
156 * - receive mbox (sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0); in netconn_recv_data())
157 * - send queue is full (sys_arch_sem_wait(LWIP_API_MSG_SEM(msg), 0); in lwip_netconn_do_write())
158 *
159 * The events have to be seen as events signaling the state of these mboxes/semaphores. For non-blocking
160 * connections, you need to know in advance whether a call to a netconn function call would block or not,
161 * and these events tell you about that.
162 *
163 * RCVPLUS events say: Safe to perform a potentially blocking call call once more.
164 * They are counted in sockets - three RCVPLUS events for accept mbox means you are safe
165 * to call netconn_accept 3 times without being blocked.
166 * Same thing for receive mbox.
167 *
168 * RCVMINUS events say: Your call to to a possibly blocking function is "acknowledged".
169 * Socket implementation decrements the counter.
170 *
171 * For TX, there is no need to count, its merely a flag. SENDPLUS means you may send something.
172 * SENDPLUS occurs when enough data was delivered to peer so netconn_send() can be called again.
173 * A SENDMINUS event occurs when the next call to a netconn_send() would be blocking.
174 */
175 enum netconn_evt {
176 NETCONN_EVT_RCVPLUS,
177 NETCONN_EVT_RCVMINUS,
178 NETCONN_EVT_SENDPLUS,
179 NETCONN_EVT_SENDMINUS,
180 NETCONN_EVT_ERROR
181 };
182  
183 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
184 /** Used for netconn_join_leave_group() */
185 enum netconn_igmp {
186 NETCONN_JOIN,
187 NETCONN_LEAVE
188 };
189 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
190  
191 #if LWIP_DNS
192 /* Used for netconn_gethostbyname_addrtype(), these should match the DNS_ADDRTYPE defines in dns.h */
193 #define NETCONN_DNS_DEFAULT NETCONN_DNS_IPV4_IPV6
194 #define NETCONN_DNS_IPV4 0
195 #define NETCONN_DNS_IPV6 1
196 #define NETCONN_DNS_IPV4_IPV6 2 /* try to resolve IPv4 first, try IPv6 if IPv4 fails only */
197 #define NETCONN_DNS_IPV6_IPV4 3 /* try to resolve IPv6 first, try IPv4 if IPv6 fails only */
198 #endif /* LWIP_DNS */
199  
200 /* forward-declare some structs to avoid to include their headers */
201 struct ip_pcb;
202 struct tcp_pcb;
203 struct udp_pcb;
204 struct raw_pcb;
205 struct netconn;
206 struct api_msg;
207  
208 /** A callback prototype to inform about events for a netconn */
209 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
210  
211 /** A netconn descriptor */
212 struct netconn {
213 /** type of the netconn (TCP, UDP or RAW) */
214 enum netconn_type type;
215 /** current state of the netconn */
216 enum netconn_state state;
217 /** the lwIP internal protocol control block */
218 union {
219 struct ip_pcb *ip;
220 struct tcp_pcb *tcp;
221 struct udp_pcb *udp;
222 struct raw_pcb *raw;
223 } pcb;
224 /** the last asynchronous unreported error this netconn had */
225 err_t pending_err;
226 #if !LWIP_NETCONN_SEM_PER_THREAD
227 /** sem that is used to synchronously execute functions in the core context */
228 sys_sem_t op_completed;
229 #endif
230 /** mbox where received packets are stored until they are fetched
231 by the netconn application thread (can grow quite big) */
232 sys_mbox_t recvmbox;
233 #if LWIP_TCP
234 /** mbox where new connections are stored until processed
235 by the application thread */
236 sys_mbox_t acceptmbox;
237 #endif /* LWIP_TCP */
238 /** only used for socket layer */
239 #if LWIP_SOCKET
240 int socket;
241 #endif /* LWIP_SOCKET */
242 #if LWIP_SO_SNDTIMEO
243 /** timeout to wait for sending data (which means enqueueing data for sending
244 in internal buffers) in milliseconds */
245 s32_t send_timeout;
246 #endif /* LWIP_SO_RCVTIMEO */
247 #if LWIP_SO_RCVTIMEO
248 /** timeout in milliseconds to wait for new data to be received
249 (or connections to arrive for listening netconns) */
250 u32_t recv_timeout;
251 #endif /* LWIP_SO_RCVTIMEO */
252 #if LWIP_SO_RCVBUF
253 /** maximum amount of bytes queued in recvmbox
254 not used for TCP: adjust TCP_WND instead! */
255 int recv_bufsize;
256 /** number of bytes currently in recvmbox to be received,
257 tested against recv_bufsize to limit bytes on recvmbox
258 for UDP and RAW, used for FIONREAD */
259 int recv_avail;
260 #endif /* LWIP_SO_RCVBUF */
261 #if LWIP_SO_LINGER
262 /** values <0 mean linger is disabled, values > 0 are seconds to linger */
263 s16_t linger;
264 #endif /* LWIP_SO_LINGER */
265 /** flags holding more netconn-internal state, see NETCONN_FLAG_* defines */
266 u8_t flags;
267 #if LWIP_TCP
268 /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
269 this temporarily stores the message.
270 Also used during connect and close. */
271 struct api_msg *current_msg;
272 #endif /* LWIP_TCP */
273 /** A callback function that is informed about events for this netconn */
274 netconn_callback callback;
275 };
276  
277 /** This vector type is passed to @ref netconn_write_vectors_partly to send
278 * multiple buffers at once.
279 * ATTENTION: This type has to directly map struct iovec since one is casted
280 * into the other!
281 */
282 struct netvector {
283 /** pointer to the application buffer that contains the data to send */
284 const void *ptr;
285 /** size of the application data to send */
286 size_t len;
287 };
288  
289 /** Register an Network connection event */
290 #define API_EVENT(c,e,l) if (c->callback) { \
291 (*c->callback)(c, e, l); \
292 }
293  
294 /* Network connection functions: */
295  
296 /** @ingroup netconn_common
297 * Create new netconn connection
298 * @param t @ref netconn_type */
299 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
300 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
301 struct netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
302 netconn_callback callback);
303 err_t netconn_delete(struct netconn *conn);
304 /** Get the type of a netconn (as enum netconn_type). */
305 #define netconn_type(conn) (conn->type)
306  
307 err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
308 u16_t *port, u8_t local);
309 /** @ingroup netconn_common */
310 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
311 /** @ingroup netconn_common */
312 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
313  
314 err_t netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port);
315 err_t netconn_bind_if(struct netconn *conn, u8_t if_idx);
316 err_t netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port);
317 err_t netconn_disconnect (struct netconn *conn);
318 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
319 /** @ingroup netconn_tcp */
320 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
321 err_t netconn_accept(struct netconn *conn, struct netconn **new_conn);
322 err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf);
323 err_t netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf);
324 err_t netconn_recv_udp_raw_netbuf_flags(struct netconn *conn, struct netbuf **new_buf, u8_t apiflags);
325 err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
326 err_t netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags);
327 err_t netconn_tcp_recvd(struct netconn *conn, size_t len);
328 err_t netconn_sendto(struct netconn *conn, struct netbuf *buf,
329 const ip_addr_t *addr, u16_t port);
330 err_t netconn_send(struct netconn *conn, struct netbuf *buf);
331 err_t netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
332 u8_t apiflags, size_t *bytes_written);
333 err_t netconn_write_vectors_partly(struct netconn *conn, struct netvector *vectors, u16_t vectorcnt,
334 u8_t apiflags, size_t *bytes_written);
335 /** @ingroup netconn_tcp */
336 #define netconn_write(conn, dataptr, size, apiflags) \
337 netconn_write_partly(conn, dataptr, size, apiflags, NULL)
338 err_t netconn_close(struct netconn *conn);
339 err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
340  
341 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
342 err_t netconn_join_leave_group(struct netconn *conn, const ip_addr_t *multiaddr,
343 const ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
344 err_t netconn_join_leave_group_netif(struct netconn *conn, const ip_addr_t *multiaddr,
345 u8_t if_idx, enum netconn_igmp join_or_leave);
346 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
347 #if LWIP_DNS
348 #if LWIP_IPV4 && LWIP_IPV6
349 err_t netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype);
350 #define netconn_gethostbyname(name, addr) netconn_gethostbyname_addrtype(name, addr, NETCONN_DNS_DEFAULT)
351 #else /* LWIP_IPV4 && LWIP_IPV6 */
352 err_t netconn_gethostbyname(const char *name, ip_addr_t *addr);
353 #define netconn_gethostbyname_addrtype(name, addr, dns_addrtype) netconn_gethostbyname(name, addr)
354 #endif /* LWIP_IPV4 && LWIP_IPV6 */
355 #endif /* LWIP_DNS */
356  
357 err_t netconn_err(struct netconn *conn);
358 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
359  
360 #define netconn_set_flags(conn, set_flags) do { (conn)->flags = (u8_t)((conn)->flags | (set_flags)); } while(0)
361 #define netconn_clear_flags(conn, clr_flags) do { (conn)->flags = (u8_t)((conn)->flags & ~(clr_flags)); } while(0)
362 #define netconn_is_flag_set(conn, flag) (((conn)->flags & (flag)) != 0)
363  
364 /** Set the blocking status of netconn calls (@todo: write/send is missing) */
365 #define netconn_set_nonblocking(conn, val) do { if(val) { \
366 netconn_set_flags(conn, NETCONN_FLAG_NON_BLOCKING); \
367 } else { \
368 netconn_clear_flags(conn, NETCONN_FLAG_NON_BLOCKING); }} while(0)
369 /** Get the blocking status of netconn calls (@todo: write/send is missing) */
370 #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
371  
372 #if LWIP_IPV6
373 /** @ingroup netconn_common
374 * TCP: Set the IPv6 ONLY status of netconn calls (see NETCONN_FLAG_IPV6_V6ONLY)
375 */
376 #define netconn_set_ipv6only(conn, val) do { if(val) { \
377 netconn_set_flags(conn, NETCONN_FLAG_IPV6_V6ONLY); \
378 } else { \
379 netconn_clear_flags(conn, NETCONN_FLAG_IPV6_V6ONLY); }} while(0)
380 /** @ingroup netconn_common
381 * TCP: Get the IPv6 ONLY status of netconn calls (see NETCONN_FLAG_IPV6_V6ONLY)
382 */
383 #define netconn_get_ipv6only(conn) (((conn)->flags & NETCONN_FLAG_IPV6_V6ONLY) != 0)
384 #endif /* LWIP_IPV6 */
385  
386 #if LWIP_SO_SNDTIMEO
387 /** Set the send timeout in milliseconds */
388 #define netconn_set_sendtimeout(conn, timeout) ((conn)->send_timeout = (timeout))
389 /** Get the send timeout in milliseconds */
390 #define netconn_get_sendtimeout(conn) ((conn)->send_timeout)
391 #endif /* LWIP_SO_SNDTIMEO */
392 #if LWIP_SO_RCVTIMEO
393 /** Set the receive timeout in milliseconds */
394 #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout))
395 /** Get the receive timeout in milliseconds */
396 #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout)
397 #endif /* LWIP_SO_RCVTIMEO */
398 #if LWIP_SO_RCVBUF
399 /** Set the receive buffer in bytes */
400 #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize))
401 /** Get the receive buffer in bytes */
402 #define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize)
403 #endif /* LWIP_SO_RCVBUF*/
404  
405 #if LWIP_NETCONN_SEM_PER_THREAD
406 void netconn_thread_init(void);
407 void netconn_thread_cleanup(void);
408 #else /* LWIP_NETCONN_SEM_PER_THREAD */
409 #define netconn_thread_init()
410 #define netconn_thread_cleanup()
411 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
412  
413 #ifdef __cplusplus
414 }
415 #endif
416  
417 #endif /* LWIP_NETCONN || LWIP_SOCKET */
418  
419 #endif /* LWIP_HDR_API_H */