netbuf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. }
  124. /**
  125. * @ingroup netbuf
  126. * Let a netbuf reference existing (non-volatile) data.
  127. *
  128. * @param buf netbuf which should reference the data
  129. * @param dataptr pointer to the data to reference
  130. * @param size size of the data
  131. * @return ERR_OK if data is referenced
  132. * ERR_MEM if data couldn't be referenced due to lack of memory
  133. */
  134. err_t
  135. netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
  136. {
  137. LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
  138. if (buf->p != NULL) {
  139. pbuf_free(buf->p);
  140. }
  141. buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
  142. if (buf->p == NULL) {
  143. buf->ptr = NULL;
  144. return ERR_MEM;
  145. }
  146. ((struct pbuf_rom*)buf->p)->payload = dataptr;
  147. buf->p->len = buf->p->tot_len = size;
  148. buf->ptr = buf->p;
  149. return ERR_OK;
  150. }
  151. /**
  152. * @ingroup netbuf
  153. * Chain one netbuf to another (@see pbuf_chain)
  154. *
  155. * @param head the first netbuf
  156. * @param tail netbuf to chain after head, freed by this function, may not be reference after returning
  157. */
  158. void
  159. netbuf_chain(struct netbuf *head, struct netbuf *tail)
  160. {
  161. LWIP_ERROR("netbuf_chain: invalid head", (head != NULL), return;);
  162. LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
  163. pbuf_cat(head->p, tail->p);
  164. head->ptr = head->p;
  165. memp_free(MEMP_NETBUF, tail);
  166. }
  167. /**
  168. * @ingroup netbuf
  169. * Get the data pointer and length of the data inside a netbuf.
  170. *
  171. * @param buf netbuf to get the data from
  172. * @param dataptr pointer to a void pointer where to store the data pointer
  173. * @param len pointer to an u16_t where the length of the data is stored
  174. * @return ERR_OK if the information was retrieved,
  175. * ERR_BUF on error.
  176. */
  177. err_t
  178. netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
  179. {
  180. LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
  181. LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
  182. LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
  183. if (buf->ptr == NULL) {
  184. return ERR_BUF;
  185. }
  186. *dataptr = buf->ptr->payload;
  187. *len = buf->ptr->len;
  188. return ERR_OK;
  189. }
  190. /**
  191. * @ingroup netbuf
  192. * Move the current data pointer of a packet buffer contained in a netbuf
  193. * to the next part.
  194. * The packet buffer itself is not modified.
  195. *
  196. * @param buf the netbuf to modify
  197. * @return -1 if there is no next part
  198. * 1 if moved to the next part but now there is no next part
  199. * 0 if moved to the next part and there are still more parts
  200. */
  201. s8_t
  202. netbuf_next(struct netbuf *buf)
  203. {
  204. LWIP_ERROR("netbuf_next: invalid buf", (buf != NULL), return -1;);
  205. if (buf->ptr->next == NULL) {
  206. return -1;
  207. }
  208. buf->ptr = buf->ptr->next;
  209. if (buf->ptr->next == NULL) {
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. /**
  215. * @ingroup netbuf
  216. * Move the current data pointer of a packet buffer contained in a netbuf
  217. * to the beginning of the packet.
  218. * The packet buffer itself is not modified.
  219. *
  220. * @param buf the netbuf to modify
  221. */
  222. void
  223. netbuf_first(struct netbuf *buf)
  224. {
  225. LWIP_ERROR("netbuf_first: invalid buf", (buf != NULL), return;);
  226. buf->ptr = buf->p;
  227. }
  228. #endif /* LWIP_NETCONN */