udp.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_UDP_H__
  33. #define __LWIP_UDP_H__
  34. #include "lwip/opt.h"
  35. #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
  36. #include "lwip/pbuf.h"
  37. #include "lwip/netif.h"
  38. #include "lwip/ip_addr.h"
  39. #include "lwip/ip.h"
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. #define UDP_HLEN 8
  44. /* Fields are (of course) in network byte order. */
  45. #ifdef PACK_STRUCT_USE_INCLUDES
  46. # include "arch/bpstruct.h"
  47. #endif
  48. PACK_STRUCT_BEGIN
  49. struct udp_hdr {
  50. PACK_STRUCT_FIELD(u16_t src);
  51. PACK_STRUCT_FIELD(u16_t dest); /* src/dest UDP ports */
  52. PACK_STRUCT_FIELD(u16_t len);
  53. PACK_STRUCT_FIELD(u16_t chksum);
  54. } PACK_STRUCT_STRUCT;
  55. PACK_STRUCT_END
  56. #ifdef PACK_STRUCT_USE_INCLUDES
  57. # include "arch/epstruct.h"
  58. #endif
  59. #define UDP_FLAGS_NOCHKSUM 0x01U
  60. #define UDP_FLAGS_UDPLITE 0x02U
  61. #define UDP_FLAGS_CONNECTED 0x04U
  62. #define UDP_FLAGS_MULTICAST_LOOP 0x08U
  63. struct udp_pcb;
  64. /** Function prototype for udp pcb receive callback functions
  65. * addr and port are in same byte order as in the pcb
  66. * The callback is responsible for freeing the pbuf
  67. * if it's not used any more.
  68. *
  69. * ATTENTION: Be aware that 'addr' points into the pbuf 'p' so freeing this pbuf
  70. * makes 'addr' invalid, too.
  71. *
  72. * @param arg user supplied argument (udp_pcb.recv_arg)
  73. * @param pcb the udp_pcb which received data
  74. * @param p the packet buffer that was received
  75. * @param addr the remote IP address from which the packet was received
  76. * @param port the remote port from which the packet was received
  77. */
  78. typedef void (*udp_recv_fn)(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  79. ip_addr_t *addr, u16_t port);
  80. struct udp_pcb {
  81. /* Common members of all PCB types */
  82. IP_PCB;
  83. /* Protocol specific PCB members */
  84. struct udp_pcb *next;
  85. u8_t flags;
  86. /** ports are in host byte order */
  87. u16_t local_port, remote_port;
  88. #if LWIP_IGMP
  89. /** outgoing network interface for multicast packets */
  90. ip_addr_t multicast_ip;
  91. #endif /* LWIP_IGMP */
  92. #if LWIP_UDPLITE
  93. /** used for UDP_LITE only */
  94. u16_t chksum_len_rx, chksum_len_tx;
  95. #endif /* LWIP_UDPLITE */
  96. /** receive callback function */
  97. udp_recv_fn recv;
  98. /** user-supplied argument for the recv callback */
  99. void *recv_arg;
  100. };
  101. /* udp_pcbs export for exernal reference (e.g. SNMP agent) */
  102. extern struct udp_pcb *udp_pcbs;
  103. /* The following functions is the application layer interface to the
  104. UDP code. */
  105. struct udp_pcb * udp_new (void)ICACHE_FLASH_ATTR;
  106. void udp_remove (struct udp_pcb *pcb)ICACHE_FLASH_ATTR;
  107. err_t udp_bind (struct udp_pcb *pcb, ip_addr_t *ipaddr,
  108. u16_t port)ICACHE_FLASH_ATTR;
  109. err_t udp_connect (struct udp_pcb *pcb, ip_addr_t *ipaddr,
  110. u16_t port)ICACHE_FLASH_ATTR;
  111. void udp_disconnect (struct udp_pcb *pcb)ICACHE_FLASH_ATTR;
  112. void udp_recv (struct udp_pcb *pcb, udp_recv_fn recv,
  113. void *recv_arg)ICACHE_FLASH_ATTR;
  114. err_t udp_sendto_if (struct udp_pcb *pcb, struct pbuf *p,
  115. ip_addr_t *dst_ip, u16_t dst_port,
  116. struct netif *netif)ICACHE_FLASH_ATTR;
  117. err_t udp_sendto (struct udp_pcb *pcb, struct pbuf *p,
  118. ip_addr_t *dst_ip, u16_t dst_port)ICACHE_FLASH_ATTR;
  119. err_t udp_send (struct udp_pcb *pcb, struct pbuf *p)ICACHE_FLASH_ATTR;
  120. #if LWIP_CHECKSUM_ON_COPY
  121. err_t udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p,
  122. ip_addr_t *dst_ip, u16_t dst_port,
  123. struct netif *netif, u8_t have_chksum,
  124. u16_t chksum)ICACHE_FLASH_ATTR;
  125. err_t udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p,
  126. ip_addr_t *dst_ip, u16_t dst_port,
  127. u8_t have_chksum, u16_t chksum)ICACHE_FLASH_ATTR;
  128. err_t udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
  129. u8_t have_chksum, u16_t chksum)ICACHE_FLASH_ATTR;
  130. #endif /* LWIP_CHECKSUM_ON_COPY */
  131. #define udp_flags(pcb) ((pcb)->flags)
  132. #define udp_setflags(pcb, f) ((pcb)->flags = (f))
  133. /* The following functions are the lower layer interface to UDP. */
  134. void udp_input (struct pbuf *p, struct netif *inp)ICACHE_FLASH_ATTR;
  135. #define udp_init() /* Compatibility define, not init needed. */
  136. #ifndef UDP_LOCAL_PORT_RANGE_START
  137. #define UDP_LOCAL_PORT_RANGE_START 4096
  138. #define UDP_LOCAL_PORT_RANGE_END 0x7fff
  139. #endif
  140. #if UDP_DEBUG
  141. void udp_debug_print(struct udp_hdr *udphdr)ICACHE_FLASH_ATTR;
  142. #else
  143. #define udp_debug_print(udphdr)
  144. #endif
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif /* LWIP_UDP */
  149. #endif /* __LWIP_UDP_H__ */