ip.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /** Currently, the function ip_output_if_opt() is only used with IGMP */
  44. #define IP_OPTIONS_SEND LWIP_IGMP
  45. #define IP_HLEN 20
  46. #define IP_PROTO_ICMP 1
  47. #define IP_PROTO_IGMP 2
  48. #define IP_PROTO_UDP 17
  49. #define IP_PROTO_UDPLITE 136
  50. #define IP_PROTO_TCP 6
  51. /* This is passed as the destination address to ip_output_if (not
  52. to ip_output), meaning that an IP header already is constructed
  53. in the pbuf. This is used when TCP retransmits. */
  54. #ifdef IP_HDRINCL
  55. #undef IP_HDRINCL
  56. #endif /* IP_HDRINCL */
  57. #define IP_HDRINCL NULL
  58. #if LWIP_NETIF_HWADDRHINT
  59. #define IP_PCB_ADDRHINT ;u8_t addr_hint
  60. #else
  61. #define IP_PCB_ADDRHINT
  62. #endif /* LWIP_NETIF_HWADDRHINT */
  63. /* This is the common part of all PCB types. It needs to be at the
  64. beginning of a PCB type definition. It is located here so that
  65. changes to this common part are made in one location instead of
  66. having to change all PCB structs. */
  67. #define IP_PCB \
  68. /* ip addresses in network byte order */ \
  69. ip_addr_t local_ip; \
  70. ip_addr_t remote_ip; \
  71. /* Socket options */ \
  72. u8_t so_options; \
  73. /* Type Of Service */ \
  74. u8_t tos; \
  75. /* Time To Live */ \
  76. u8_t ttl \
  77. /* link layer address resolution hint */ \
  78. IP_PCB_ADDRHINT
  79. struct ip_pcb {
  80. /* Common members of all PCB types */
  81. IP_PCB;
  82. };
  83. /*
  84. * Option flags per-socket. These are the same like SO_XXX.
  85. */
  86. /*#define SOF_DEBUG (u8_t)0x01U Unimplemented: turn on debugging info recording */
  87. #define SOF_ACCEPTCONN (u8_t)0x02U /* socket has had listen() */
  88. #define SOF_REUSEADDR (u8_t)0x04U /* allow local address reuse */
  89. #define SOF_KEEPALIVE (u8_t)0x08U /* keep connections alive */
  90. /*#define SOF_DONTROUTE (u8_t)0x10U Unimplemented: just use interface addresses */
  91. #define SOF_BROADCAST (u8_t)0x20U /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
  92. /*#define SOF_USELOOPBACK (u8_t)0x40U Unimplemented: bypass hardware when possible */
  93. #define SOF_LINGER (u8_t)0x80U /* linger on close if data present */
  94. /*#define SOF_OOBINLINE (u16_t)0x0100U Unimplemented: leave received OOB data in line */
  95. /*#define SOF_REUSEPORT (u16_t)0x0200U Unimplemented: allow local address & port reuse */
  96. /* These flags are inherited (e.g. from a listen-pcb to a connection-pcb): */
  97. #define SOF_INHERITED (SOF_REUSEADDR|SOF_KEEPALIVE|SOF_LINGER/*|SOF_DEBUG|SOF_DONTROUTE|SOF_OOBINLINE*/)
  98. #ifdef PACK_STRUCT_USE_INCLUDES
  99. # include "arch/bpstruct.h"
  100. #endif
  101. PACK_STRUCT_BEGIN
  102. struct ip_hdr {
  103. /* version / header length / type of service */
  104. PACK_STRUCT_FIELD(u16_t _v_hl_tos);
  105. /* total length */
  106. PACK_STRUCT_FIELD(u16_t _len);
  107. /* identification */
  108. PACK_STRUCT_FIELD(u16_t _id);
  109. /* fragment offset field */
  110. PACK_STRUCT_FIELD(u16_t _offset);
  111. #define IP_RF 0x8000 /* reserved fragment flag */
  112. #define IP_DF 0x4000 /* dont fragment flag */
  113. #define IP_MF 0x2000 /* more fragments flag */
  114. #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
  115. /* time to live */
  116. PACK_STRUCT_FIELD(u8_t _ttl);
  117. /* protocol*/
  118. PACK_STRUCT_FIELD(u8_t _proto);
  119. /* checksum */
  120. PACK_STRUCT_FIELD(u16_t _chksum);
  121. /* source and destination IP addresses */
  122. PACK_STRUCT_FIELD(ip_addr_p_t src);
  123. PACK_STRUCT_FIELD(ip_addr_p_t dest);
  124. } PACK_STRUCT_STRUCT;
  125. PACK_STRUCT_END
  126. #ifdef PACK_STRUCT_USE_INCLUDES
  127. # include "arch/epstruct.h"
  128. #endif
  129. #define IPH_V(hdr) (ntohs((hdr)->_v_hl_tos) >> 12)
  130. #define IPH_HL(hdr) ((ntohs((hdr)->_v_hl_tos) >> 8) & 0x0f)
  131. #define IPH_TOS(hdr) (ntohs((hdr)->_v_hl_tos) & 0xff)
  132. #define IPH_LEN(hdr) ((hdr)->_len)
  133. #define IPH_ID(hdr) ((hdr)->_id)
  134. #define IPH_OFFSET(hdr) ((hdr)->_offset)
  135. #define IPH_TTL(hdr) ((hdr)->_ttl)
  136. #define IPH_PROTO(hdr) ((hdr)->_proto)
  137. #define IPH_CHKSUM(hdr) ((hdr)->_chksum)
  138. #define IPH_VHLTOS_SET(hdr, v, hl, tos) (hdr)->_v_hl_tos = (htons(((v) << 12) | ((hl) << 8) | (tos)))
  139. #define IPH_LEN_SET(hdr, len) (hdr)->_len = (len)
  140. #define IPH_ID_SET(hdr, id) (hdr)->_id = (id)
  141. #define IPH_OFFSET_SET(hdr, off) (hdr)->_offset = (off)
  142. #define IPH_TTL_SET(hdr, ttl) (hdr)->_ttl = (u8_t)(ttl)
  143. #define IPH_PROTO_SET(hdr, proto) (hdr)->_proto = (u8_t)(proto)
  144. #define IPH_CHKSUM_SET(hdr, chksum) (hdr)->_chksum = (chksum)
  145. /** The interface that provided the packet for the current callback invocation. */
  146. extern struct netif *current_netif;
  147. /** Header of the input packet currently being processed. */
  148. extern const struct ip_hdr *current_header;
  149. /** Source IP address of current_header */
  150. extern ip_addr_t current_iphdr_src;
  151. /** Destination IP address of current_header */
  152. extern ip_addr_t current_iphdr_dest;
  153. #define ip_init() /* Compatibility define, not init needed. */
  154. struct netif *ip_route(ip_addr_t *dest)ICACHE_FLASH_ATTR;
  155. struct netif *ip_router(ip_addr_t *dest, ip_addr_t *source);
  156. err_t ip_input(struct pbuf *p, struct netif *inp)ICACHE_FLASH_ATTR;
  157. err_t ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
  158. u8_t ttl, u8_t tos, u8_t proto)ICACHE_FLASH_ATTR;
  159. err_t ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
  160. u8_t ttl, u8_t tos, u8_t proto,
  161. struct netif *netif)ICACHE_FLASH_ATTR;
  162. #if LWIP_NETIF_HWADDRHINT
  163. err_t ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
  164. u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)ICACHE_FLASH_ATTR;
  165. #endif /* LWIP_NETIF_HWADDRHINT */
  166. #if IP_OPTIONS_SEND
  167. err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
  168. u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
  169. u16_t optlen)ICACHE_FLASH_ATTR;
  170. #endif /* IP_OPTIONS_SEND */
  171. /** Get the interface that received the current packet.
  172. * This function must only be called from a receive callback (udp_recv,
  173. * raw_recv, tcp_accept). It will return NULL otherwise. */
  174. #define ip_current_netif() (current_netif)
  175. /** Get the IP header of the current packet.
  176. * This function must only be called from a receive callback (udp_recv,
  177. * raw_recv, tcp_accept). It will return NULL otherwise. */
  178. #define ip_current_header() (current_header)
  179. /** Source IP address of current_header */
  180. #define ip_current_src_addr() (&current_iphdr_src)
  181. /** Destination IP address of current_header */
  182. #define ip_current_dest_addr() (&current_iphdr_dest)
  183. #if IP_DEBUG
  184. void ip_debug_print(struct pbuf *p)ICACHE_FLASH_ATTR;
  185. #else
  186. #define ip_debug_print(p)
  187. #endif /* IP_DEBUG */
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif /* __LWIP_IP_H__ */