• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined (__cplusplus) && __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 #if PF_PKT_SUPPORT
59 /*
60  * This netbuf includes the destination mac addr only
61  * this is in case of PF_PACKET RAW sockets
62  */
63 #define NETBUF_FLAG_IFINDEX     0x04
64 #endif
65 
66 /** "Network buffer" - contains data and addressing info */
67 struct netbuf {
68   struct pbuf *p, *ptr;
69   ip_addr_t addr;
70   u16_t port;
71 #if PF_PKT_SUPPORT
72   u16_t hatype;
73   u16_t reserve; /* padded */
74   /* Used to store destination mac addr in case of PF_PACKET RAW sockets */
75   u8_t netifindex;
76 #if !LWIP_CHECKSUM_ON_COPY
77   u8_t flags;
78 #endif
79 #endif
80 
81 #if LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY
82 #if LWIP_CHECKSUM_ON_COPY
83   u8_t flags;
84 #endif /* LWIP_CHECKSUM_ON_COPY */
85   u16_t toport_chksum;
86 #if LWIP_NETBUF_RECVINFO
87   ip_addr_t toaddr;
88 #endif /* LWIP_NETBUF_RECVINFO */
89 #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */
90 };
91 
92 /* Network buffer functions: */
93 struct netbuf *netbuf_new      (void);
94 void              netbuf_delete   (struct netbuf *buf);
95 void *netbuf_alloc    (struct netbuf *buf, u16_t size, u8_t netconn_type);
96 void              netbuf_free     (struct netbuf *buf);
97 err_t             netbuf_ref      (struct netbuf *buf,
98                                    const void *dataptr, u16_t size);
99 #if LWIP_API_RICH
100 void              netbuf_chain    (struct netbuf *head, struct netbuf *tail);
101 
102 err_t             netbuf_data     (struct netbuf *buf,
103                                    void **dataptr, u16_t *len);
104 s8_t              netbuf_next     (struct netbuf *buf);
105 void              netbuf_first    (struct netbuf *buf);
106 #endif /* LWIP_API_RICH */
107 
108 #define netbuf_copy_partial(buf, dataptr, len, offset) \
109   pbuf_copy_partial((buf)->p, (dataptr), (len), (offset))
110 #define netbuf_copy(buf, dataptr, len) netbuf_copy_partial(buf, dataptr, len, 0)
111 #define netbuf_take(buf, dataptr, len) pbuf_take((buf)->p, dataptr, len)
112 #define netbuf_len(buf)              ((buf)->p->tot_len)
113 #define netbuf_fromaddr(buf)         (&((buf)->addr))
114 #define netbuf_set_fromaddr(buf, fromaddr) ip_addr_set(&((buf)->addr), fromaddr)
115 #define netbuf_fromport(buf)         ((buf)->port)
116 #define netbuf_fromifindex(buf)     ((buf)->netifindex)
117 #define netbuf_fromhatype(buf)      ((buf)->hatype)
118 
119 #if LWIP_NETBUF_RECVINFO
120 #define netbuf_destaddr(buf)         (&((buf)->toaddr))
121 #define netbuf_set_destaddr(buf, destaddr) ip_addr_set(&((buf)->toaddr), destaddr)
122 #if LWIP_CHECKSUM_ON_COPY
123 #define netbuf_destport(buf)         (((buf)->flags & NETBUF_FLAG_DESTADDR) ? (buf)->toport_chksum : 0)
124 #else /* LWIP_CHECKSUM_ON_COPY */
125 #define netbuf_destport(buf)         ((buf)->toport_chksum)
126 #endif /* LWIP_CHECKSUM_ON_COPY */
127 #endif /* LWIP_NETBUF_RECVINFO */
128 #if LWIP_CHECKSUM_ON_COPY
129 #define netbuf_set_chksum(buf, chksum) do { (buf)->flags = NETBUF_FLAG_CHKSUM; \
130                                             (buf)->toport_chksum = chksum; } while (0)
131 #endif /* LWIP_CHECKSUM_ON_COPY */
132 
133 #if defined (__cplusplus) && __cplusplus
134 }
135 #endif
136 
137 #endif /* LWIP_NETCONN || LWIP_SOCKET */
138 
139 #endif /* LWIP_HDR_NETBUF_H */
140