1 /**
2  * @file
3  * netbuf API (for netconn API)
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_NETBUF_H
38 #define LWIP_HDR_NETBUF_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/pbuf.h"
47 #include "lwip/ip_addr.h"
48 #include "lwip/ip6_addr.h"
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /** This netbuf has dest-addr/port set */
55 #define NETBUF_FLAG_DESTADDR    0x01
56 /** This netbuf includes a checksum */
57 #define NETBUF_FLAG_CHKSUM      0x02
58 
59 /** "Network buffer" - contains data and addressing info */
60 struct netbuf {
61   struct pbuf *p, *ptr;
62   ip_addr_t addr;
63   u16_t port;
64 #if LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY
65 #if LWIP_CHECKSUM_ON_COPY
66   u8_t flags;
67 #endif /* LWIP_CHECKSUM_ON_COPY */
68   u16_t toport_chksum;
69 #if LWIP_NETBUF_RECVINFO
70   ip_addr_t toaddr;
71 #endif /* LWIP_NETBUF_RECVINFO */
72 #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */
73 };
74 
75 /* Network buffer functions: */
76 struct netbuf *   netbuf_new      (void);
77 void              netbuf_delete   (struct netbuf *buf);
78 void *            netbuf_alloc    (struct netbuf *buf, u16_t size);
79 void              netbuf_free     (struct netbuf *buf);
80 err_t             netbuf_ref      (struct netbuf *buf,
81                                    const void *dataptr, u16_t size);
82 void              netbuf_chain    (struct netbuf *head, struct netbuf *tail);
83 
84 err_t             netbuf_data     (struct netbuf *buf,
85                                    void **dataptr, u16_t *len);
86 s8_t              netbuf_next     (struct netbuf *buf);
87 void              netbuf_first    (struct netbuf *buf);
88 
89 
90 #define netbuf_copy_partial(buf, dataptr, len, offset) \
91   pbuf_copy_partial((buf)->p, (dataptr), (len), (offset))
92 #define netbuf_copy(buf,dataptr,len) netbuf_copy_partial(buf, dataptr, len, 0)
93 #define netbuf_take(buf, dataptr, len) pbuf_take((buf)->p, dataptr, len)
94 #define netbuf_len(buf)              ((buf)->p->tot_len)
95 #define netbuf_fromaddr(buf)         (&((buf)->addr))
96 #define netbuf_set_fromaddr(buf, fromaddr) ip_addr_set(&((buf)->addr), fromaddr)
97 #define netbuf_fromport(buf)         ((buf)->port)
98 #if LWIP_NETBUF_RECVINFO
99 #define netbuf_destaddr(buf)         (&((buf)->toaddr))
100 #define netbuf_set_destaddr(buf, destaddr) ip_addr_set(&((buf)->toaddr), destaddr)
101 #if LWIP_CHECKSUM_ON_COPY
102 #define netbuf_destport(buf)         (((buf)->flags & NETBUF_FLAG_DESTADDR) ? (buf)->toport_chksum : 0)
103 #else /* LWIP_CHECKSUM_ON_COPY */
104 #define netbuf_destport(buf)         ((buf)->toport_chksum)
105 #endif /* LWIP_CHECKSUM_ON_COPY */
106 #endif /* LWIP_NETBUF_RECVINFO */
107 #if LWIP_CHECKSUM_ON_COPY
108 #define netbuf_set_chksum(buf, chksum) do { (buf)->flags = NETBUF_FLAG_CHKSUM; \
109                                             (buf)->toport_chksum = chksum; } while(0)
110 #endif /* LWIP_CHECKSUM_ON_COPY */
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* LWIP_NETCONN || LWIP_SOCKET */
117 
118 #endif /* LWIP_HDR_NETBUF_H */
119