netbuf.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file
  3. * netbuf API (for netconn API)
  4. */
  5. /*
  6. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Adam Dunkels <adam@sics.se>
  34. *
  35. */
  36. #ifndef LWIP_HDR_NETBUF_H
  37. #define LWIP_HDR_NETBUF_H
  38. #include "lwip/opt.h"
  39. #if LWIP_NETCONN || LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
  40. /* Note: Netconn API is always available when sockets are enabled -
  41. * sockets are implemented on top of them */
  42. #include "lwip/pbuf.h"
  43. #include "lwip/ip_addr.h"
  44. #include "lwip/ip6_addr.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /** This netbuf has dest-addr/port set */
  49. #define NETBUF_FLAG_DESTADDR 0x01
  50. /** This netbuf includes a checksum */
  51. #define NETBUF_FLAG_CHKSUM 0x02
  52. /** "Network buffer" - contains data and addressing info */
  53. struct netbuf {
  54. struct pbuf *p, *ptr;
  55. ip_addr_t addr;
  56. u16_t port;
  57. #if LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY
  58. #if LWIP_CHECKSUM_ON_COPY
  59. u8_t flags;
  60. #endif /* LWIP_CHECKSUM_ON_COPY */
  61. u16_t toport_chksum;
  62. #if LWIP_NETBUF_RECVINFO
  63. ip_addr_t toaddr;
  64. #endif /* LWIP_NETBUF_RECVINFO */
  65. #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */
  66. };
  67. /* Network buffer functions: */
  68. struct netbuf * netbuf_new (void);
  69. void netbuf_delete (struct netbuf *buf);
  70. void * netbuf_alloc (struct netbuf *buf, u16_t size);
  71. void netbuf_free (struct netbuf *buf);
  72. err_t netbuf_ref (struct netbuf *buf,
  73. const void *dataptr, u16_t size);
  74. void netbuf_chain (struct netbuf *head, struct netbuf *tail);
  75. err_t netbuf_data (struct netbuf *buf,
  76. void **dataptr, u16_t *len);
  77. s8_t netbuf_next (struct netbuf *buf);
  78. void netbuf_first (struct netbuf *buf);
  79. #define netbuf_copy_partial(buf, dataptr, len, offset) \
  80. pbuf_copy_partial((buf)->p, (dataptr), (len), (offset))
  81. #define netbuf_copy(buf,dataptr,len) netbuf_copy_partial(buf, dataptr, len, 0)
  82. #define netbuf_take(buf, dataptr, len) pbuf_take((buf)->p, dataptr, len)
  83. #define netbuf_len(buf) ((buf)->p->tot_len)
  84. #define netbuf_fromaddr(buf) (&((buf)->addr))
  85. #define netbuf_set_fromaddr(buf, fromaddr) ip_addr_set(&((buf)->addr), fromaddr)
  86. #define netbuf_fromport(buf) ((buf)->port)
  87. #if LWIP_NETBUF_RECVINFO
  88. #define netbuf_destaddr(buf) (&((buf)->toaddr))
  89. #define netbuf_set_destaddr(buf, destaddr) ip_addr_set(&((buf)->toaddr), destaddr)
  90. #if LWIP_CHECKSUM_ON_COPY
  91. #define netbuf_destport(buf) (((buf)->flags & NETBUF_FLAG_DESTADDR) ? (buf)->toport_chksum : 0)
  92. #else /* LWIP_CHECKSUM_ON_COPY */
  93. #define netbuf_destport(buf) ((buf)->toport_chksum)
  94. #endif /* LWIP_CHECKSUM_ON_COPY */
  95. #endif /* LWIP_NETBUF_RECVINFO */
  96. #if LWIP_CHECKSUM_ON_COPY
  97. #define netbuf_set_chksum(buf, chksum) do { (buf)->flags = NETBUF_FLAG_CHKSUM; \
  98. (buf)->toport_chksum = chksum; } while(0)
  99. #endif /* LWIP_CHECKSUM_ON_COPY */
  100. #ifdef __cplusplus
  101. }
  102. #endif
  103. #endif /* LWIP_NETCONN || LWIP_SOCKET */
  104. #endif /* LWIP_HDR_NETBUF_H */