netbuf.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * @file
  3. * Network buffer management
  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. #include "lwip/opt.h"
  38. #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
  39. #include "lwip/netbuf.h"
  40. #include "lwip/memp.h"
  41. #include <string.h>
  42. /**
  43. * Create (allocate) and initialize a new netbuf.
  44. * The netbuf doesn't yet contain a packet buffer!
  45. *
  46. * @return a pointer to a new netbuf
  47. * NULL on lack of memory
  48. */
  49. struct
  50. netbuf *netbuf_new(void)
  51. {
  52. struct netbuf *buf;
  53. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  54. if (buf != NULL) {
  55. buf->p = NULL;
  56. buf->ptr = NULL;
  57. ip_addr_set_zero(&buf->addr);
  58. buf->port = 0;
  59. #if LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY
  60. #if LWIP_CHECKSUM_ON_COPY
  61. buf->flags = 0;
  62. #endif /* LWIP_CHECKSUM_ON_COPY */
  63. buf->toport_chksum = 0;
  64. #if LWIP_NETBUF_RECVINFO
  65. ip_addr_set_zero(&buf->toaddr);
  66. #endif /* LWIP_NETBUF_RECVINFO */
  67. #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */
  68. return buf;
  69. } else {
  70. return NULL;
  71. }
  72. }
  73. /**
  74. * Deallocate a netbuf allocated by netbuf_new().
  75. *
  76. * @param buf pointer to a netbuf allocated by netbuf_new()
  77. */
  78. void
  79. netbuf_delete(struct netbuf *buf)
  80. {
  81. if (buf != NULL) {
  82. if (buf->p != NULL) {
  83. pbuf_free(buf->p);
  84. buf->p = buf->ptr = NULL;
  85. }
  86. memp_free(MEMP_NETBUF, buf);
  87. }
  88. }
  89. /**
  90. * Allocate memory for a packet buffer for a given netbuf.
  91. *
  92. * @param buf the netbuf for which to allocate a packet buffer
  93. * @param size the size of the packet buffer to allocate
  94. * @return pointer to the allocated memory
  95. * NULL if no memory could be allocated
  96. */
  97. void *
  98. netbuf_alloc(struct netbuf *buf, u16_t size)
  99. {
  100. LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);
  101. /* Deallocate any previously allocated memory. */
  102. if (buf->p != NULL) {
  103. pbuf_free(buf->p);
  104. }
  105. buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
  106. if (buf->p == NULL) {
  107. return NULL;
  108. }
  109. LWIP_ASSERT("check that first pbuf can hold size",
  110. (buf->p->len >= size));
  111. buf->ptr = buf->p;
  112. return buf->p->payload;
  113. }
  114. /**
  115. * Free the packet buffer included in a netbuf
  116. *
  117. * @param buf pointer to the netbuf which contains the packet buffer to free
  118. */
  119. void
  120. netbuf_free(struct netbuf *buf)
  121. {
  122. LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
  123. if (buf->p != NULL) {
  124. pbuf_free(buf->p);
  125. }
  126. buf->p = buf->ptr = NULL;
  127. }
  128. /**
  129. * Let a netbuf reference existing (non-volatile) data.
  130. *
  131. * @param buf netbuf which should reference the data
  132. * @param dataptr pointer to the data to reference
  133. * @param size size of the data
  134. * @return ERR_OK if data is referenced
  135. * ERR_MEM if data couldn't be referenced due to lack of memory
  136. */
  137. err_t
  138. netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
  139. {
  140. LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
  141. if (buf->p != NULL) {
  142. pbuf_free(buf->p);
  143. }
  144. buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
  145. if (buf->p == NULL) {
  146. buf->ptr = NULL;
  147. return ERR_MEM;
  148. }
  149. ((struct pbuf_rom*)buf->p)->payload = dataptr;
  150. buf->p->len = buf->p->tot_len = size;
  151. buf->ptr = buf->p;
  152. return ERR_OK;
  153. }
  154. /**
  155. * Chain one netbuf to another (@see pbuf_chain)
  156. *
  157. * @param head the first netbuf
  158. * @param tail netbuf to chain after head, freed by this function, may not be reference after returning
  159. */
  160. void
  161. netbuf_chain(struct netbuf *head, struct netbuf *tail)
  162. {
  163. LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;);
  164. LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
  165. pbuf_cat(head->p, tail->p);
  166. head->ptr = head->p;
  167. memp_free(MEMP_NETBUF, tail);
  168. }
  169. /**
  170. * Get the data pointer and length of the data inside a netbuf.
  171. *
  172. * @param buf netbuf to get the data from
  173. * @param dataptr pointer to a void pointer where to store the data pointer
  174. * @param len pointer to an u16_t where the length of the data is stored
  175. * @return ERR_OK if the information was retrieved,
  176. * ERR_BUF on error.
  177. */
  178. err_t
  179. netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
  180. {
  181. LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
  182. LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
  183. LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
  184. if (buf->ptr == NULL) {
  185. return ERR_BUF;
  186. }
  187. *dataptr = buf->ptr->payload;
  188. *len = buf->ptr->len;
  189. return ERR_OK;
  190. }
  191. /**
  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_free: 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. * Move the current data pointer of a packet buffer contained in a netbuf
  216. * to the beginning of the packet.
  217. * The packet buffer itself is not modified.
  218. *
  219. * @param buf the netbuf to modify
  220. */
  221. void
  222. netbuf_first(struct netbuf *buf)
  223. {
  224. LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
  225. buf->ptr = buf->p;
  226. }
  227. #endif /* LWIP_NETCONN */