PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/sdk/external/lwip/src/include/lwip/ip.h

https://gitlab.com/VD-EDU/vd-iot-evalkit-sdk
C Header | 260 lines | 148 code | 27 blank | 85 comment | 5 complexity | 978387572a46ce7e9fe6e0602de1774a MD5 | raw file
  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_IP_H__
  33. #define __LWIP_IP_H__
  34. #include "lwip/opt.h"
  35. #include "lwip/def.h"
  36. #include "lwip/pbuf.h"
  37. #include "lwip/ip_addr.h"
  38. #include "lwip/err.h"
  39. #include "lwip/netif.h"
  40. #include "lwip/ip4.h"
  41. #include "lwip/ip6.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /* This is passed as the destination address to ip_output_if (not
  46. to ip_output), meaning that an IP header already is constructed
  47. in the pbuf. This is used when TCP retransmits. */
  48. #ifdef IP_HDRINCL
  49. #undef IP_HDRINCL
  50. #endif /* IP_HDRINCL */
  51. #define IP_HDRINCL NULL
  52. #if LWIP_NETIF_HWADDRHINT
  53. #define IP_PCB_ADDRHINT ;u8_t addr_hint
  54. #else
  55. #define IP_PCB_ADDRHINT
  56. #endif /* LWIP_NETIF_HWADDRHINT */
  57. #if LWIP_IPV6
  58. #define IP_PCB_ISIPV6_MEMBER u8_t isipv6;
  59. #define IP_PCB_IPVER_EQ(pcb1, pcb2) ((pcb1)->isipv6 == (pcb2)->isipv6)
  60. #define IP_PCB_IPVER_INPUT_MATCH(pcb) (ip_current_is_v6() ? \
  61. ((pcb)->isipv6 != 0) : \
  62. ((pcb)->isipv6 == 0))
  63. #define PCB_ISIPV6(pcb) ((pcb)->isipv6)
  64. #else
  65. #define IP_PCB_ISIPV6_MEMBER
  66. #define IP_PCB_IPVER_EQ(pcb1, pcb2) 1
  67. #define IP_PCB_IPVER_INPUT_MATCH(pcb) 1
  68. #define PCB_ISIPV6(pcb) 0
  69. #endif /* LWIP_IPV6 */
  70. /* This is the common part of all PCB types. It needs to be at the
  71. beginning of a PCB type definition. It is located here so that
  72. changes to this common part are made in one location instead of
  73. having to change all PCB structs. */
  74. #define IP_PCB \
  75. IP_PCB_ISIPV6_MEMBER \
  76. /* ip addresses in network byte order */ \
  77. ipX_addr_t local_ip; \
  78. ipX_addr_t remote_ip; \
  79. /* Socket options */ \
  80. u8_t so_options; \
  81. /* Type Of Service */ \
  82. u8_t tos; \
  83. /* Time To Live */ \
  84. u8_t ttl \
  85. /* link layer address resolution hint */ \
  86. IP_PCB_ADDRHINT
  87. struct ip_pcb {
  88. /* Common members of all PCB types */
  89. IP_PCB;
  90. };
  91. /*
  92. * Option flags per-socket. These are the same like SO_XXX.
  93. */
  94. /*#define SOF_DEBUG 0x01U Unimplemented: turn on debugging info recording */
  95. #define SOF_ACCEPTCONN 0x02U /* socket has had listen() */
  96. #define SOF_REUSEADDR 0x04U /* allow local address reuse */
  97. #define SOF_KEEPALIVE 0x08U /* keep connections alive */
  98. /*#define SOF_DONTROUTE 0x10U Unimplemented: just use interface addresses */
  99. #define SOF_BROADCAST 0x20U /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
  100. /*#define SOF_USELOOPBACK 0x40U Unimplemented: bypass hardware when possible */
  101. #define SOF_LINGER 0x80U /* linger on close if data present */
  102. /*#define SOF_OOBINLINE 0x0100U Unimplemented: leave received OOB data in line */
  103. /*#define SOF_REUSEPORT 0x0200U Unimplemented: allow local address & port reuse */
  104. /* These flags are inherited (e.g. from a listen-pcb to a connection-pcb): */
  105. #define SOF_INHERITED (SOF_REUSEADDR|SOF_KEEPALIVE|SOF_LINGER/*|SOF_DEBUG|SOF_DONTROUTE|SOF_OOBINLINE*/)
  106. /* Global variables of this module, kept in a struct for efficient access using base+index. */
  107. struct ip_globals
  108. {
  109. /** The interface that provided the packet for the current callback invocation. */
  110. struct netif *current_netif;
  111. /** Header of the input packet currently being processed. */
  112. const struct ip_hdr *current_ip4_header;
  113. #if LWIP_IPV6
  114. /** Header of the input IPv6 packet currently being processed. */
  115. const struct ip6_hdr *current_ip6_header;
  116. #endif /* LWIP_IPV6 */
  117. /** Total header length of current_ip4/6_header (i.e. after this, the UDP/TCP header starts) */
  118. u16_t current_ip_header_tot_len;
  119. /** Source IP address of current_header */
  120. ipX_addr_t current_iphdr_src;
  121. /** Destination IP address of current_header */
  122. ipX_addr_t current_iphdr_dest;
  123. };
  124. extern struct ip_globals ip_data;
  125. /** Get the interface that received the current packet.
  126. * This function must only be called from a receive callback (udp_recv,
  127. * raw_recv, tcp_accept). It will return NULL otherwise. */
  128. #define ip_current_netif() (ip_data.current_netif)
  129. /** Get the IP header of the current packet.
  130. * This function must only be called from a receive callback (udp_recv,
  131. * raw_recv, tcp_accept). It will return NULL otherwise. */
  132. #define ip_current_header() (ip_data.current_ip4_header)
  133. /** Total header length of ip(6)_current_header() (i.e. after this, the UDP/TCP header starts) */
  134. #define ip_current_header_tot_len() (ip_data.current_ip_header_tot_len)
  135. /** Source IP address of current_header */
  136. #define ipX_current_src_addr() (&ip_data.current_iphdr_src)
  137. /** Destination IP address of current_header */
  138. #define ipX_current_dest_addr() (&ip_data.current_iphdr_dest)
  139. #if LWIP_IPV6
  140. /** Get the IPv6 header of the current packet.
  141. * This function must only be called from a receive callback (udp_recv,
  142. * raw_recv, tcp_accept). It will return NULL otherwise. */
  143. #define ip6_current_header() (ip_data.current_ip6_header)
  144. /** Returns TRUE if the current IP input packet is IPv6, FALSE if it is IPv4 */
  145. #define ip_current_is_v6() (ip6_current_header() != NULL)
  146. /** Source IPv6 address of current_header */
  147. #define ip6_current_src_addr() (ipX_2_ip6(&ip_data.current_iphdr_src))
  148. /** Destination IPv6 address of current_header */
  149. #define ip6_current_dest_addr() (ipX_2_ip6(&ip_data.current_iphdr_dest))
  150. /** Get the transport layer protocol */
  151. #define ip_current_header_proto() (ip_current_is_v6() ? \
  152. IP6H_NEXTH(ip6_current_header()) :\
  153. IPH_PROTO(ip_current_header()))
  154. /** Get the transport layer header */
  155. #define ipX_next_header_ptr() ((void*)((ip_current_is_v6() ? \
  156. (u8_t*)ip6_current_header() : (u8_t*)ip_current_header()) + ip_current_header_tot_len()))
  157. /** Set an IP_PCB to IPv6 (IPv4 is the default) */
  158. #define ip_set_v6(pcb, val) do{if(pcb != NULL) { pcb->isipv6 = val; }}while(0)
  159. /** Source IP4 address of current_header */
  160. #define ip_current_src_addr() (ipX_2_ip(&ip_data.current_iphdr_src))
  161. /** Destination IP4 address of current_header */
  162. #define ip_current_dest_addr() (ipX_2_ip(&ip_data.current_iphdr_dest))
  163. #else /* LWIP_IPV6 */
  164. /** Always returns FALSE when only supporting IPv4 */
  165. #define ip_current_is_v6() 0
  166. /** Get the transport layer protocol */
  167. #define ip_current_header_proto() IPH_PROTO(ip_current_header())
  168. /** Get the transport layer header */
  169. #define ipX_next_header_ptr() ((void*)((u8_t*)ip_current_header() + ip_current_header_tot_len()))
  170. /** Source IP4 address of current_header */
  171. #define ip_current_src_addr() (&ip_data.current_iphdr_src)
  172. /** Destination IP4 address of current_header */
  173. #define ip_current_dest_addr() (&ip_data.current_iphdr_dest)
  174. #endif /* LWIP_IPV6 */
  175. /** Union source address of current_header */
  176. #define ipX_current_src_addr() (&ip_data.current_iphdr_src)
  177. /** Union destination address of current_header */
  178. #define ipX_current_dest_addr() (&ip_data.current_iphdr_dest)
  179. /** Gets an IP pcb option (SOF_* flags) */
  180. #define ip_get_option(pcb, opt) ((pcb)->so_options & (opt))
  181. /** Sets an IP pcb option (SOF_* flags) */
  182. #define ip_set_option(pcb, opt) ((pcb)->so_options |= (opt))
  183. /** Resets an IP pcb option (SOF_* flags) */
  184. #define ip_reset_option(pcb, opt) ((pcb)->so_options &= ~(opt))
  185. #if LWIP_IPV6
  186. #define ipX_output(isipv6, p, src, dest, ttl, tos, proto) \
  187. ((isipv6) ? \
  188. ip6_output(p, ipX_2_ip6(src), ipX_2_ip6(dest), ttl, tos, proto) : \
  189. ip_output(p, ipX_2_ip(src), ipX_2_ip(dest), ttl, tos, proto))
  190. #define ipX_output_if(isipv6, p, src, dest, ttl, tos, proto, netif) \
  191. ((isipv6) ? \
  192. ip6_output_if(p, ip_2_ip6(src), ip_2_ip6(dest), ttl, tos, proto, netif) : \
  193. ip_output_if(p, (src), (dest), ttl, tos, proto, netif))
  194. #define ipX_output_if_src(isipv6, p, src, dest, ttl, tos, proto, netif) \
  195. ((isipv6) ? \
  196. ip6_output_if_src(p, ip_2_ip6(src), ip_2_ip6(dest), ttl, tos, proto, netif) : \
  197. ip_output_if_src(p, (src), (dest), ttl, tos, proto, netif))
  198. #define ipX_output_hinted(isipv6, p, src, dest, ttl, tos, proto, addr_hint) \
  199. ((isipv6) ? \
  200. ip6_output_hinted(p, ipX_2_ip6(src), ipX_2_ip6(dest), ttl, tos, proto, addr_hint) : \
  201. ip_output_hinted(p, ipX_2_ip(src), ipX_2_ip(dest), ttl, tos, proto, addr_hint))
  202. #define ipX_route(isipv6, src, dest) \
  203. ((isipv6) ? \
  204. ip6_route(ipX_2_ip6(src), ipX_2_ip6(dest)) : \
  205. ip_route(ipX_2_ip(dest)))
  206. #define ipX_netif_get_local_ipX(isipv6, netif, dest) \
  207. ((isipv6) ? \
  208. ip6_netif_get_local_ipX(netif, ipX_2_ip6(dest)) : \
  209. ip_netif_get_local_ipX(netif))
  210. #define ipX_debug_print(is_ipv6, p) ((is_ipv6) ? ip6_debug_print(p) : ip_debug_print(p))
  211. #else /* LWIP_IPV6 */
  212. #define ipX_output(isipv6, p, src, dest, ttl, tos, proto) \
  213. ip_output(p, src, dest, ttl, tos, proto)
  214. #define ipX_output_if(isipv6, p, src, dest, ttl, tos, proto, netif) \
  215. ip_output_if(p, src, dest, ttl, tos, proto, netif)
  216. #define ipX_output_if_src(isipv6, p, src, dest, ttl, tos, proto, netif) \
  217. ip_output_if_src(p, src, dest, ttl, tos, proto, netif)
  218. #define ipX_output_hinted(isipv6, p, src, dest, ttl, tos, proto, addr_hint) \
  219. ip_output_hinted(p, src, dest, ttl, tos, proto, addr_hint)
  220. #define ipX_route(isipv6, src, dest) \
  221. ip_route(ipX_2_ip(dest))
  222. #define ipX_netif_get_local_ipX(isipv6, netif, dest) \
  223. ip_netif_get_local_ipX(netif)
  224. #define ipX_debug_print(is_ipv6, p) ip_debug_print(p)
  225. #endif /* LWIP_IPV6 */
  226. #define ipX_route_get_local_ipX(isipv6, src, dest, netif, ipXaddr) do { \
  227. (netif) = ipX_route(isipv6, src, dest); \
  228. (ipXaddr) = ipX_netif_get_local_ipX(isipv6, netif, dest); \
  229. }while(0)
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif /* __LWIP_IP_H__ */