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   u8_t flags;
66   u16_t toport_chksum;
67 #if LWIP_NETBUF_RECVINFO
68   ip_addr_t toaddr;
69 #endif /* LWIP_NETBUF_RECVINFO */
70 #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */
71 };
72 
73 /* Network buffer functions: */
74 struct netbuf *   netbuf_new      (void);
75 void              netbuf_delete   (struct netbuf *buf);
76 void *            netbuf_alloc    (struct netbuf *buf, u16_t size);
77 void              netbuf_free     (struct netbuf *buf);
78 err_t             netbuf_ref      (struct netbuf *buf,
79                                    const void *dataptr, u16_t size);
80 void              netbuf_chain    (struct netbuf *head, struct netbuf *tail);
81 
82 err_t             netbuf_data     (struct netbuf *buf,
83                                    void **dataptr, u16_t *len);
84 s8_t              netbuf_next     (struct netbuf *buf);
85 void              netbuf_first    (struct netbuf *buf);
86 
87 
88 #define netbuf_copy_partial(buf, dataptr, len, offset) \
89   pbuf_copy_partial((buf)->p, (dataptr), (len), (offset))
90 #define netbuf_copy(buf,dataptr,len) netbuf_copy_partial(buf, dataptr, len, 0)
91 #define netbuf_take(buf, dataptr, len) pbuf_take((buf)->p, dataptr, len)
92 #define netbuf_len(buf)              ((buf)->p->tot_len)
93 #define netbuf_fromaddr(buf)         (&((buf)->addr))
94 #define netbuf_set_fromaddr(buf, fromaddr) ip_addr_set(&((buf)->addr), fromaddr)
95 #define netbuf_fromport(buf)         ((buf)->port)
96 #if LWIP_NETBUF_RECVINFO
97 #define netbuf_destaddr(buf)         (&((buf)->toaddr))
98 #define netbuf_set_destaddr(buf, destaddr) ip_addr_set(&((buf)->toaddr), destaddr)
99 #if LWIP_CHECKSUM_ON_COPY
100 #define netbuf_destport(buf)         (((buf)->flags & NETBUF_FLAG_DESTADDR) ? (buf)->toport_chksum : 0)
101 #else /* LWIP_CHECKSUM_ON_COPY */
102 #define netbuf_destport(buf)         ((buf)->toport_chksum)
103 #endif /* LWIP_CHECKSUM_ON_COPY */
104 #endif /* LWIP_NETBUF_RECVINFO */
105 #if LWIP_CHECKSUM_ON_COPY
106 #define netbuf_set_chksum(buf, chksum) do { (buf)->flags = NETBUF_FLAG_CHKSUM; \
107                                             (buf)->toport_chksum = chksum; } while(0)
108 #endif /* LWIP_CHECKSUM_ON_COPY */
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 #endif /* LWIP_NETCONN || LWIP_SOCKET */
115 
116 #endif /* LWIP_HDR_NETBUF_H */
117