]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/lib/contrib/src/include/lwip/pbuf.h
update
[l4.git] / l4 / pkg / ankh / lib / lwip / lib / contrib / src / include / lwip / pbuf.h
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved. 
4  * 
5  * Redistribution and use in source and binary forms, with or without modification, 
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission. 
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  * 
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32
33 #ifndef __LWIP_PBUF_H__
34 #define __LWIP_PBUF_H__
35
36 #include "lwip/opt.h"
37 #include "lwip/err.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /** Currently, the pbuf_custom code is only needed for one specific configuration
44  * of IP_FRAG */
45 #define LWIP_SUPPORT_CUSTOM_PBUF (IP_FRAG && !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF)
46
47 /* @todo: We need a mechanism to prevent wasting memory in every pbuf
48    (TCP vs. UDP, IPv4 vs. IPv6: UDP/IPv4 packets may waste up to 28 bytes) */
49
50 #define PBUF_TRANSPORT_HLEN 20
51 #if LWIP_IPV6
52 #define PBUF_IP_HLEN        40
53 #else
54 #define PBUF_IP_HLEN        20
55 #endif
56
57 typedef enum {
58   PBUF_TRANSPORT,
59   PBUF_IP,
60   PBUF_LINK,
61   PBUF_RAW
62 } pbuf_layer;
63
64 typedef enum {
65   PBUF_RAM, /* pbuf data is stored in RAM */
66   PBUF_ROM, /* pbuf data is stored in ROM */
67   PBUF_REF, /* pbuf comes from the pbuf pool */
68   PBUF_POOL /* pbuf payload refers to RAM */
69 } pbuf_type;
70
71
72 /** indicates this packet's data should be immediately passed to the application */
73 #define PBUF_FLAG_PUSH      0x01U
74 /** indicates this is a custom pbuf: pbuf_free and pbuf_header handle such a
75     a pbuf differently */
76 #define PBUF_FLAG_IS_CUSTOM 0x02U
77 /** indicates this pbuf is UDP multicast to be looped back */
78 #define PBUF_FLAG_MCASTLOOP 0x04U
79 /** indicates this pbuf was received as link-level broadcast */
80 #define PBUF_FLAG_LLBCAST   0x08U
81 /** indicates this pbuf was received as link-level multicast */
82 #define PBUF_FLAG_LLMCAST   0x10U
83
84 struct pbuf {
85   /** next pbuf in singly linked pbuf chain */
86   struct pbuf *next;
87
88   /** pointer to the actual data in the buffer */
89   void *payload;
90
91   /**
92    * total length of this buffer and all next buffers in chain
93    * belonging to the same packet.
94    *
95    * For non-queue packet chains this is the invariant:
96    * p->tot_len == p->len + (p->next? p->next->tot_len: 0)
97    */
98   u16_t tot_len;
99
100   /** length of this buffer */
101   u16_t len;
102
103   /** pbuf_type as u8_t instead of enum to save space */
104   u8_t /*pbuf_type*/ type;
105
106   /** misc flags */
107   u8_t flags;
108
109   /**
110    * the reference count always equals the number of pointers
111    * that refer to this pbuf. This can be pointers from an application,
112    * the stack itself, or pbuf->next pointers from a chain.
113    */
114   u16_t ref;
115 };
116
117 #if LWIP_SUPPORT_CUSTOM_PBUF
118 /** Prototype for a function to free a custom pbuf */
119 typedef void (*pbuf_free_custom_fn)(struct pbuf *p);
120
121 /** A custom pbuf: like a pbuf, but following a function pointer to free it. */
122 struct pbuf_custom {
123   /** The actual pbuf */
124   struct pbuf pbuf;
125   /** This function is called when pbuf_free deallocates this pbuf(_custom) */
126   pbuf_free_custom_fn custom_free_function;
127 };
128 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
129
130 #if LWIP_TCP && TCP_QUEUE_OOSEQ
131 /** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */
132 #ifndef PBUF_POOL_FREE_OOSEQ
133 #define PBUF_POOL_FREE_OOSEQ 1
134 #endif /* PBUF_POOL_FREE_OOSEQ */
135 #if NO_SYS && PBUF_POOL_FREE_OOSEQ
136 extern volatile u8_t pbuf_free_ooseq_pending;
137 void pbuf_free_ooseq();
138 /** When not using sys_check_timeouts(), call PBUF_CHECK_FREE_OOSEQ()
139     at regular intervals from main level to check if ooseq pbufs need to be
140     freed! */
141 #define PBUF_CHECK_FREE_OOSEQ() do { if(pbuf_free_ooseq_pending) { \
142   /* pbuf_alloc() reported PBUF_POOL to be empty -> try to free some \
143      ooseq queued pbufs now */ \
144   pbuf_free_ooseq(); }}while(0)
145 #endif /* NO_SYS && PBUF_POOL_FREE_OOSEQ*/
146 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ */
147
148 /* Initializes the pbuf module. This call is empty for now, but may not be in future. */
149 #define pbuf_init()
150
151 struct pbuf *pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type);
152 #if LWIP_SUPPORT_CUSTOM_PBUF
153 struct pbuf *pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type,
154                                  struct pbuf_custom *p, void *payload_mem,
155                                  u16_t payload_mem_len);
156 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
157 void pbuf_realloc(struct pbuf *p, u16_t size); 
158 u8_t pbuf_header(struct pbuf *p, s16_t header_size);
159 void pbuf_ref(struct pbuf *p);
160 u8_t pbuf_free(struct pbuf *p);
161 u8_t pbuf_clen(struct pbuf *p);  
162 void pbuf_cat(struct pbuf *head, struct pbuf *tail);
163 void pbuf_chain(struct pbuf *head, struct pbuf *tail);
164 struct pbuf *pbuf_dechain(struct pbuf *p);
165 err_t pbuf_copy(struct pbuf *p_to, struct pbuf *p_from);
166 u16_t pbuf_copy_partial(struct pbuf *p, void *dataptr, u16_t len, u16_t offset);
167 err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len);
168 struct pbuf *pbuf_coalesce(struct pbuf *p, pbuf_layer layer);
169 #if LWIP_CHECKSUM_ON_COPY
170 err_t pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
171                        u16_t len, u16_t *chksum);
172 #endif /* LWIP_CHECKSUM_ON_COPY */
173
174 u8_t pbuf_get_at(struct pbuf* p, u16_t offset);
175 u16_t pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n);
176 u16_t pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset);
177 u16_t pbuf_strstr(struct pbuf* p, const char* substr);
178
179 #ifdef __cplusplus
180 }
181 #endif
182
183 #endif /* __LWIP_PBUF_H__ */