pbuf.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef __LWIP_PBUF_H__
  33. #define __LWIP_PBUF_H__
  34. #include "lwip/opt.h"
  35. #include "lwip/err.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /** Currently, the pbuf_custom code is only needed for one specific configuration
  40. * of IP_FRAG */
  41. #define LWIP_SUPPORT_CUSTOM_PBUF (IP_FRAG && !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF)
  42. #define PBUF_TRANSPORT_HLEN 20
  43. #define PBUF_IP_HLEN 20
  44. typedef enum {
  45. PBUF_TRANSPORT,
  46. PBUF_IP,
  47. PBUF_LINK,
  48. PBUF_RAW
  49. } pbuf_layer;
  50. typedef enum {
  51. PBUF_RAM, /* pbuf data is stored in RAM */
  52. PBUF_ROM, /* pbuf data is stored in ROM */
  53. PBUF_REF, /* pbuf comes from the pbuf pool */
  54. PBUF_POOL, /* pbuf payload refers to RAM */
  55. #ifdef EBUF_LWIP
  56. PBUF_ESF_RX /* pbuf payload is from WLAN */
  57. #endif /* ESF_LWIP */
  58. } pbuf_type;
  59. /** indicates this packet's data should be immediately passed to the application */
  60. #define PBUF_FLAG_PUSH 0x01U
  61. /** indicates this is a custom pbuf: pbuf_free and pbuf_header handle such a
  62. a pbuf differently */
  63. #define PBUF_FLAG_IS_CUSTOM 0x02U
  64. /** indicates this pbuf is UDP multicast to be looped back */
  65. #define PBUF_FLAG_MCASTLOOP 0x04U
  66. struct pbuf {
  67. /** next pbuf in singly linked pbuf chain */
  68. struct pbuf *next;
  69. /** pointer to the actual data in the buffer */
  70. void *payload;
  71. /**
  72. * total length of this buffer and all next buffers in chain
  73. * belonging to the same packet.
  74. *
  75. * For non-queue packet chains this is the invariant:
  76. * p->tot_len == p->len + (p->next? p->next->tot_len: 0)
  77. */
  78. u16_t tot_len;
  79. /** length of this buffer */
  80. u16_t len;
  81. /** pbuf_type as u8_t instead of enum to save space */
  82. u8_t /*pbuf_type*/ type;
  83. /** misc flags */
  84. u8_t flags;
  85. /**
  86. * the reference count always equals the number of pointers
  87. * that refer to this pbuf. This can be pointers from an application,
  88. * the stack itself, or pbuf->next pointers from a chain.
  89. */
  90. u16_t ref;
  91. /* add a pointer for esf_buf */
  92. void * eb;
  93. };
  94. #if LWIP_SUPPORT_CUSTOM_PBUF
  95. /** Prototype for a function to free a custom pbuf */
  96. typedef void (*pbuf_free_custom_fn)(struct pbuf *p);
  97. /** A custom pbuf: like a pbuf, but following a function pointer to free it. */
  98. struct pbuf_custom {
  99. /** The actual pbuf */
  100. struct pbuf pbuf;
  101. /** This function is called when pbuf_free deallocates this pbuf(_custom) */
  102. pbuf_free_custom_fn custom_free_function;
  103. };
  104. #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
  105. /* Initializes the pbuf module. This call is empty for now, but may not be in future. */
  106. #define pbuf_init()
  107. struct pbuf *pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type)ICACHE_FLASH_ATTR;
  108. #if LWIP_SUPPORT_CUSTOM_PBUF
  109. struct pbuf *pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type,
  110. struct pbuf_custom *p, void *payload_mem,
  111. u16_t payload_mem_len)ICACHE_FLASH_ATTR;
  112. #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
  113. void pbuf_realloc(struct pbuf *p, u16_t size)ICACHE_FLASH_ATTR;
  114. u8_t pbuf_header(struct pbuf *p, s16_t header_size)ICACHE_FLASH_ATTR;
  115. void pbuf_ref(struct pbuf *p)ICACHE_FLASH_ATTR;
  116. u8_t pbuf_free(struct pbuf *p)ICACHE_FLASH_ATTR;
  117. u8_t pbuf_clen(struct pbuf *p)ICACHE_FLASH_ATTR;
  118. void pbuf_cat(struct pbuf *head, struct pbuf *tail)ICACHE_FLASH_ATTR;
  119. void pbuf_chain(struct pbuf *head, struct pbuf *tail)ICACHE_FLASH_ATTR;
  120. struct pbuf *pbuf_dechain(struct pbuf *p)ICACHE_FLASH_ATTR;
  121. err_t pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)ICACHE_FLASH_ATTR;
  122. u16_t pbuf_copy_partial(struct pbuf *p, void *dataptr, u16_t len, u16_t offset)ICACHE_FLASH_ATTR;
  123. err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)ICACHE_FLASH_ATTR;
  124. struct pbuf *pbuf_coalesce(struct pbuf *p, pbuf_layer layer)ICACHE_FLASH_ATTR;
  125. #if LWIP_CHECKSUM_ON_COPY
  126. err_t pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
  127. u16_t len, u16_t *chksum)ICACHE_FLASH_ATTR;
  128. #endif /* LWIP_CHECKSUM_ON_COPY */
  129. u8_t pbuf_get_at(struct pbuf* p, u16_t offset)ICACHE_FLASH_ATTR;
  130. u16_t pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n)ICACHE_FLASH_ATTR;
  131. u16_t pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)ICACHE_FLASH_ATTR;
  132. u16_t pbuf_strstr(struct pbuf* p, const char* substr)ICACHE_FLASH_ATTR;
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif /* __LWIP_PBUF_H__ */