The Pedigree Project  0.1
api.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
25 /*
26  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without modification,
30  * are permitted provided that the following conditions are met:
31  *
32  * 1. Redistributions of source code must retain the above copyright notice,
33  * this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright notice,
35  * this list of conditions and the following disclaimer in the documentation
36  * and/or other materials provided with the distribution.
37  * 3. The name of the author may not be used to endorse or promote products
38  * derived from this software without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
43  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
45  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
48  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
49  * OF SUCH DAMAGE.
50  *
51  * This file is part of the lwIP TCP/IP stack.
52  *
53  * Author: Adam Dunkels <adam@sics.se>
54  *
55  */
56 #ifndef LWIP_HDR_API_H
57 #define LWIP_HDR_API_H
58 
59 #include "lwip/opt.h"
60 
61 #if LWIP_NETCONN || LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
62 /* Note: Netconn API is always available when sockets are enabled -
63  * sockets are implemented on top of them */
64 
65 #include "lwip/arch.h"
66 #include "lwip/netbuf.h"
67 #include "lwip/sys.h"
68 #include "lwip/ip_addr.h"
69 #include "lwip/err.h"
70 
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74 
75 /* Throughout this file, IP addresses and port numbers are expected to be in
76  * the same byte order as in the corresponding pcb.
77  */
78 
79 /* Flags for netconn_write (u8_t) */
80 #define NETCONN_NOFLAG 0x00
81 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
82 #define NETCONN_COPY 0x01
83 #define NETCONN_MORE 0x02
84 #define NETCONN_DONTBLOCK 0x04
85 
86 /* Flags for struct netconn.flags (u8_t) */
88 #define NETCONN_FLAG_NON_BLOCKING 0x02
89 
90 #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
91 
93 #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
94 #if LWIP_IPV6
95 
98 #define NETCONN_FLAG_IPV6_V6ONLY 0x20
99 #endif /* LWIP_IPV6 */
100 
101 
102 /* Helpers to process several netconn_types by the same code */
103 #define NETCONNTYPE_GROUP(t) ((t)&0xF0)
104 #define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0)
105 #if LWIP_IPV6
106 #define NETCONN_TYPE_IPV6 0x08
107 #define NETCONNTYPE_ISIPV6(t) (((t)&NETCONN_TYPE_IPV6) != 0)
108 #define NETCONNTYPE_ISUDPLITE(t) (((t)&0xF3) == NETCONN_UDPLITE)
109 #define NETCONNTYPE_ISUDPNOCHKSUM(t) (((t)&0xF3) == NETCONN_UDPNOCHKSUM)
110 #else /* LWIP_IPV6 */
111 #define NETCONNTYPE_ISIPV6(t) (0)
112 #define NETCONNTYPE_ISUDPLITE(t) ((t) == NETCONN_UDPLITE)
113 #define NETCONNTYPE_ISUDPNOCHKSUM(t) ((t) == NETCONN_UDPNOCHKSUM)
114 #endif /* LWIP_IPV6 */
115 
119 enum netconn_type {
120  NETCONN_INVALID = 0,
122  NETCONN_TCP = 0x10,
123 #if LWIP_IPV6
124 
125  NETCONN_TCP_IPV6 = NETCONN_TCP | NETCONN_TYPE_IPV6 /* 0x18 */,
126 #endif /* LWIP_IPV6 */
127 
128  NETCONN_UDP = 0x20,
130  NETCONN_UDPLITE = 0x21,
132  NETCONN_UDPNOCHKSUM = 0x22,
133 
134 #if LWIP_IPV6
135 
136  NETCONN_UDP_IPV6 = NETCONN_UDP | NETCONN_TYPE_IPV6 /* 0x28 */,
138  NETCONN_UDPLITE_IPV6 = NETCONN_UDPLITE | NETCONN_TYPE_IPV6 /* 0x29 */,
140  NETCONN_UDPNOCHKSUM_IPV6 = NETCONN_UDPNOCHKSUM | NETCONN_TYPE_IPV6 /* 0x2a */,
141 #endif /* LWIP_IPV6 */
142 
144  NETCONN_RAW = 0x40
145 #if LWIP_IPV6
146 
147  , NETCONN_RAW_IPV6 = NETCONN_RAW | NETCONN_TYPE_IPV6 /* 0x48 */
148 #endif /* LWIP_IPV6 */
149 };
150 
153 enum netconn_state {
154  NETCONN_NONE,
155  NETCONN_WRITE,
156  NETCONN_LISTEN,
157  NETCONN_CONNECT,
158  NETCONN_CLOSE
159 };
160 
187 enum netconn_evt {
188  NETCONN_EVT_RCVPLUS,
189  NETCONN_EVT_RCVMINUS,
190  NETCONN_EVT_SENDPLUS,
191  NETCONN_EVT_SENDMINUS,
192  NETCONN_EVT_ERROR
193 };
194 
195 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
196 
197 enum netconn_igmp {
198  NETCONN_JOIN,
199  NETCONN_LEAVE
200 };
201 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
202 
203 #if LWIP_DNS
204 /* Used for netconn_gethostbyname_addrtype(), these should match the DNS_ADDRTYPE defines in dns.h */
205 #define NETCONN_DNS_DEFAULT NETCONN_DNS_IPV4_IPV6
206 #define NETCONN_DNS_IPV4 0
207 #define NETCONN_DNS_IPV6 1
208 #define NETCONN_DNS_IPV4_IPV6 2 /* try to resolve IPv4 first, try IPv6 if IPv4 fails only */
209 #define NETCONN_DNS_IPV6_IPV4 3 /* try to resolve IPv6 first, try IPv4 if IPv6 fails only */
210 #endif /* LWIP_DNS */
211 
212 /* forward-declare some structs to avoid to include their headers */
213 struct ip_pcb;
214 struct tcp_pcb;
215 struct udp_pcb;
216 struct raw_pcb;
217 struct netconn;
218 struct api_msg;
219 
221 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
222 
224 struct netconn {
226  enum netconn_type type;
228  enum netconn_state state;
230  union {
231  struct ip_pcb *ip;
232  struct tcp_pcb *tcp;
233  struct udp_pcb *udp;
234  struct raw_pcb *raw;
235  } pcb;
237  err_t last_err;
238 #if !LWIP_NETCONN_SEM_PER_THREAD
239 
240  sys_sem_t op_completed;
241 #endif
242 
244  sys_mbox_t recvmbox;
245 #if LWIP_TCP
246 
248  sys_mbox_t acceptmbox;
249 #endif /* LWIP_TCP */
250 
251 #if LWIP_SOCKET
252  int socket;
253 #endif /* LWIP_SOCKET */
254 #if LWIP_SO_SNDTIMEO
255 
257  s32_t send_timeout;
258 #endif /* LWIP_SO_RCVTIMEO */
259 #if LWIP_SO_RCVTIMEO
260 
262  int recv_timeout;
263 #endif /* LWIP_SO_RCVTIMEO */
264 #if LWIP_SO_RCVBUF
265 
267  int recv_bufsize;
271  int recv_avail;
272 #endif /* LWIP_SO_RCVBUF */
273 #if LWIP_SO_LINGER
274 
275  s16_t linger;
276 #endif /* LWIP_SO_LINGER */
277 
278  u8_t flags;
279 #if LWIP_TCP
280 
282  size_t write_offset;
286  struct api_msg *current_msg;
287 #endif /* LWIP_TCP */
288 
289  netconn_callback callback;
290 };
291 
293 #define API_EVENT(c,e,l) if (c->callback) { \
294  (*c->callback)(c, e, l); \
295  }
296 
298 #define NETCONN_SET_SAFE_ERR(conn, err) do { if ((conn) != NULL) { \
299  SYS_ARCH_DECL_PROTECT(netconn_set_safe_err_lev); \
300  SYS_ARCH_PROTECT(netconn_set_safe_err_lev); \
301  if (!ERR_IS_FATAL((conn)->last_err)) { \
302  (conn)->last_err = err; \
303  } \
304  SYS_ARCH_UNPROTECT(netconn_set_safe_err_lev); \
305 }} while(0);
306 
307 /* Network connection functions: */
308 
312 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
313 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
314 struct netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
315  netconn_callback callback);
316 err_t netconn_delete(struct netconn *conn);
318 #define netconn_type(conn) (conn->type)
319 
320 err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
321  u16_t *port, u8_t local);
323 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
324 
325 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
326 
327 err_t netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port);
328 err_t netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port);
329 err_t netconn_disconnect (struct netconn *conn);
330 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
332 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
333 err_t netconn_accept(struct netconn *conn, struct netconn **new_conn);
334 err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf);
335 err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
336 err_t netconn_sendto(struct netconn *conn, struct netbuf *buf,
337  const ip_addr_t *addr, u16_t port);
338 err_t netconn_send(struct netconn *conn, struct netbuf *buf);
339 err_t netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
340  u8_t apiflags, size_t *bytes_written);
342 #define netconn_write(conn, dataptr, size, apiflags) \
343  netconn_write_partly(conn, dataptr, size, apiflags, NULL)
344 err_t netconn_close(struct netconn *conn);
345 err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
346 
347 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
348 err_t netconn_join_leave_group(struct netconn *conn, const ip_addr_t *multiaddr,
349  const ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
350 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
351 #if LWIP_DNS
352 #if LWIP_IPV4 && LWIP_IPV6
353 err_t netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype);
354 #define netconn_gethostbyname(name, addr) netconn_gethostbyname_addrtype(name, addr, NETCONN_DNS_DEFAULT)
355 #else /* LWIP_IPV4 && LWIP_IPV6 */
356 err_t netconn_gethostbyname(const char *name, ip_addr_t *addr);
357 #define netconn_gethostbyname_addrtype(name, addr, dns_addrtype) netconn_gethostbyname(name, addr)
358 #endif /* LWIP_IPV4 && LWIP_IPV6 */
359 #endif /* LWIP_DNS */
360 
361 #define netconn_err(conn) ((conn)->last_err)
362 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
363 
365 #define netconn_set_nonblocking(conn, val) do { if(val) { \
366  (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
367 } else { \
368  (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
369 
370 #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
371 
372 #if LWIP_IPV6
373 
376 #define netconn_set_ipv6only(conn, val) do { if(val) { \
377  (conn)->flags |= NETCONN_FLAG_IPV6_V6ONLY; \
378 } else { \
379  (conn)->flags &= ~ NETCONN_FLAG_IPV6_V6ONLY; }} while(0)
380 
383 #define netconn_get_ipv6only(conn) (((conn)->flags & NETCONN_FLAG_IPV6_V6ONLY) != 0)
384 #endif /* LWIP_IPV6 */
385 
386 #if LWIP_SO_SNDTIMEO
387 
388 #define netconn_set_sendtimeout(conn, timeout) ((conn)->send_timeout = (timeout))
389 
390 #define netconn_get_sendtimeout(conn) ((conn)->send_timeout)
391 #endif /* LWIP_SO_SNDTIMEO */
392 #if LWIP_SO_RCVTIMEO
393 
394 #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout))
395 
396 #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout)
397 #endif /* LWIP_SO_RCVTIMEO */
398 #if LWIP_SO_RCVBUF
399 
400 #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize))
401 
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 */
Definition: pbuf.h:161
s8_t err_t
Definition: err.h:76
Definition: ip.h:108