netbuf.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * @file
  3. * Network buffer management
  4. *
  5. * @defgroup netbuf Network buffers
  6. * @ingroup netconn
  7. * Network buffer descriptor for @ref netconn. Based on @ref pbuf internally
  8. * to avoid copying data around.\n
  9. * Buffers must not be shared accross multiple threads, all functions except
  10. * netbuf_new() and netbuf_delete() are not thread-safe.
  11. */
  12. /*
  13. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. *
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. The name of the author may not be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  29. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  30. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  32. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  35. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  36. * OF SUCH DAMAGE.
  37. *
  38. * This file is part of the lwIP TCP/IP stack.
  39. *
  40. * Author: Adam Dunkels <adam@sics.se>
  41. *
  42. */
  43. #include "lwip/opt.h"
  44. #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
  45. #include "lwip/netbuf.h"
  46. #include "lwip/memp.h"
  47. #include <string.h>
  48. /**
  49. * @ingroup netbuf
  50. * Create (allocate) and initialize a new netbuf.
  51. * The netbuf doesn't yet contain a packet buffer!
  52. *
  53. * @return a pointer to a new netbuf
  54. * NULL on lack of memory
  55. */
  56. struct
  57. netbuf *netbuf_new(void)
  58. {
  59. struct netbuf *buf;
  60. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  61. if (buf != NULL) {
  62. memset(buf, 0, sizeof(struct netbuf));
  63. }
  64. return buf;
  65. }
  66. /**
  67. * @ingroup netbuf
  68. * Deallocate a netbuf allocated by netbuf_new().
  69. *
  70. * @param buf pointer to a netbuf allocated by netbuf_new()
  71. */
  72. void
  73. netbuf_delete(struct netbuf *buf)
  74. {
  75. if (buf != NULL) {
  76. if (buf->p != NULL) {
  77. pbuf_free(buf->p);
  78. buf->p = buf->ptr = NULL;
  79. }
  80. memp_free(MEMP_NETBUF, buf);
  81. }
  82. }
  83. /**
  84. * @ingroup netbuf
  85. * Allocate memory for a packet buffer for a given netbuf.
  86. *
  87. * @param buf the netbuf for which to allocate a packet buffer
  88. * @param size the size of the packet buffer to allocate
  89. * @return pointer to the allocated memory
  90. * NULL if no memory could be allocated
  91. */
  92. void *
  93. netbuf_alloc(struct netbuf *buf, u16_t size)
  94. {
  95. LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);
  96. /* Deallocate any previously allocated memory. */
  97. if (buf->p != NULL) {
  98. pbuf_free(buf->p);
  99. }
  100. buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
  101. if (buf->p == NULL) {
  102. return NULL;
  103. }
  104. LWIP_ASSERT("check that first pbuf can hold size",
  105. (buf->p->len >= size));
  106. buf->ptr = buf->p;
  107. return buf->p->payload;
  108. }
  109. /**
  110. * @ingroup netbuf
  111. * Free the packet buffer included in a netbuf
  112. *
  113. * @param buf pointer to the netbuf which contains the packet buffer to free
  114. */
  115. void
  116. netbuf_free(struct netbuf *buf)
  117. {
  118. LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
  119. if (buf->p != NULL) {
  120. pbuf_free(buf->p);
  121. }
  122. buf->p = buf->ptr = NULL;
  123. #if LWIP_CHECKSUM_ON_COPY
  124. buf->flags = 0;
  125. buf->toport_chksum = 0;
  126. #endif /* LWIP_CHECKSUM_ON_COPY */
  127. }
  128. /**
  129. * @ingroup netbuf
  130. * Let a netbuf reference existing (non-volatile) data.
  131. *
  132. * @param buf netbuf which should reference the data
  133. * @param dataptr pointer to the data to reference
  134. * @param size size of the data
  135. * @return ERR_OK if data is referenced
  136. * ERR_MEM if data couldn't be referenced due to lack of memory
  137. */
  138. err_t
  139. netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
  140. {
  141. LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
  142. if (buf->p != NULL) {
  143. pbuf_free(buf->p);
  144. }
  145. buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
  146. if (buf->p == NULL) {
  147. buf->ptr = NULL;
  148. return ERR_MEM;
  149. }
  150. ((struct pbuf_rom *)buf->p)->payload = dataptr;
  151. buf->p->len = buf->p->tot_len = size;
  152. buf->ptr = buf->p;
  153. return ERR_OK;
  154. }
  155. /**
  156. * @ingroup netbuf
  157. * Chain one netbuf to another (@see pbuf_chain)
  158. *
  159. * @param head the first netbuf
  160. * @param tail netbuf to chain after head, freed by this function, may not be reference after returning
  161. */
  162. void
  163. netbuf_chain(struct netbuf *head, struct netbuf *tail)
  164. {
  165. LWIP_ERROR("netbuf_chain: invalid head", (head != NULL), return;);
  166. LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
  167. pbuf_cat(head->p, tail->p);
  168. head->ptr = head->p;
  169. memp_free(MEMP_NETBUF, tail);
  170. }
  171. /**
  172. * @ingroup netbuf
  173. * Get the data pointer and length of the data inside a netbuf.
  174. *
  175. * @param buf netbuf to get the data from
  176. * @param dataptr pointer to a void pointer where to store the data pointer
  177. * @param len pointer to an u16_t where the length of the data is stored
  178. * @return ERR_OK if the information was retrieved,
  179. * ERR_BUF on error.
  180. */
  181. err_t
  182. netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
  183. {
  184. LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
  185. LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
  186. LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
  187. if (buf->ptr == NULL) {
  188. return ERR_BUF;
  189. }
  190. *dataptr = buf->ptr->payload;
  191. *len = buf->ptr->len;
  192. return ERR_OK;
  193. }
  194. /**
  195. * @ingroup netbuf
  196. * Move the current data pointer of a packet buffer contained in a netbuf
  197. * to the next part.
  198. * The packet buffer itself is not modified.
  199. *
  200. * @param buf the netbuf to modify
  201. * @return -1 if there is no next part
  202. * 1 if moved to the next part but now there is no next part
  203. * 0 if moved to the next part and there are still more parts
  204. */
  205. s8_t
  206. netbuf_next(struct netbuf *buf)
  207. {
  208. LWIP_ERROR("netbuf_next: invalid buf", (buf != NULL), return -1;);
  209. if (buf->ptr->next == NULL) {
  210. return -1;
  211. }
  212. buf->ptr = buf->ptr->next;
  213. if (buf->ptr->next == NULL) {
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. /**
  219. * @ingroup netbuf
  220. * Move the current data pointer of a packet buffer contained in a netbuf
  221. * to the beginning of the packet.
  222. * The packet buffer itself is not modified.
  223. *
  224. * @param buf the netbuf to modify
  225. */
  226. void
  227. netbuf_first(struct netbuf *buf)
  228. {
  229. LWIP_ERROR("netbuf_first: invalid buf", (buf != NULL), return;);
  230. buf->ptr = buf->p;
  231. }
  232. #endif /* LWIP_NETCONN */