ip_addr.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_ADDR_H__
  33. #define __LWIP_IP_ADDR_H__
  34. #include "lwip/opt.h"
  35. #include "lwip/def.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /* This is the aligned version of ip_addr_t,
  40. used as local variable, on the stack, etc. */
  41. struct ip_addr {
  42. u32_t addr;
  43. };
  44. /* This is the packed version of ip_addr_t,
  45. used in network headers that are itself packed */
  46. #ifdef PACK_STRUCT_USE_INCLUDES
  47. # include "arch/bpstruct.h"
  48. #endif
  49. PACK_STRUCT_BEGIN
  50. struct ip_addr_packed {
  51. PACK_STRUCT_FIELD(u32_t addr);
  52. } PACK_STRUCT_STRUCT;
  53. PACK_STRUCT_END
  54. #ifdef PACK_STRUCT_USE_INCLUDES
  55. # include "arch/epstruct.h"
  56. #endif
  57. /** ip_addr_t uses a struct for convenience only, so that the same defines can
  58. * operate both on ip_addr_t as well as on ip_addr_p_t. */
  59. typedef struct ip_addr ip_addr_t;
  60. typedef struct ip_addr_packed ip_addr_p_t;
  61. /*
  62. * struct ipaddr2 is used in the definition of the ARP packet format in
  63. * order to support compilers that don't have structure packing.
  64. */
  65. #ifdef PACK_STRUCT_USE_INCLUDES
  66. # include "arch/bpstruct.h"
  67. #endif
  68. PACK_STRUCT_BEGIN
  69. struct ip_addr2 {
  70. PACK_STRUCT_FIELD(u16_t addrw[2]);
  71. } PACK_STRUCT_STRUCT;
  72. PACK_STRUCT_END
  73. #ifdef PACK_STRUCT_USE_INCLUDES
  74. # include "arch/epstruct.h"
  75. #endif
  76. /* Forward declaration to not include netif.h */
  77. struct netif;
  78. extern const ip_addr_t ip_addr_any;
  79. extern const ip_addr_t ip_addr_broadcast;
  80. /** IP_ADDR_ can be used as a fixed IP address
  81. * for the wildcard and the broadcast address
  82. */
  83. #define IP_ADDR_ANY ((ip_addr_t *)&ip_addr_any)
  84. #define IP_ADDR_BROADCAST ((ip_addr_t *)&ip_addr_broadcast)
  85. /** 255.255.255.255 */
  86. #define IPADDR_NONE ((u32_t)0xffffffffUL)
  87. /** 127.0.0.1 */
  88. #define IPADDR_LOOPBACK ((u32_t)0x7f000001UL)
  89. /** 0.0.0.0 */
  90. #define IPADDR_ANY ((u32_t)0x00000000UL)
  91. /** 255.255.255.255 */
  92. #define IPADDR_BROADCAST ((u32_t)0xffffffffUL)
  93. /* Definitions of the bits in an Internet address integer.
  94. On subnets, host and network parts are found according to
  95. the subnet mask, not these masks. */
  96. #define IP_CLASSA(a) ((((u32_t)(a)) & 0x80000000UL) == 0)
  97. #define IP_CLASSA_NET 0xff000000
  98. #define IP_CLASSA_NSHIFT 24
  99. #define IP_CLASSA_HOST (0xffffffff & ~IP_CLASSA_NET)
  100. #define IP_CLASSA_MAX 128
  101. #define IP_CLASSB(a) ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
  102. #define IP_CLASSB_NET 0xffff0000
  103. #define IP_CLASSB_NSHIFT 16
  104. #define IP_CLASSB_HOST (0xffffffff & ~IP_CLASSB_NET)
  105. #define IP_CLASSB_MAX 65536
  106. #define IP_CLASSC(a) ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
  107. #define IP_CLASSC_NET 0xffffff00
  108. #define IP_CLASSC_NSHIFT 8
  109. #define IP_CLASSC_HOST (0xffffffff & ~IP_CLASSC_NET)
  110. #define IP_CLASSD(a) (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
  111. #define IP_CLASSD_NET 0xf0000000 /* These ones aren't really */
  112. #define IP_CLASSD_NSHIFT 28 /* net and host fields, but */
  113. #define IP_CLASSD_HOST 0x0fffffff /* routing needn't know. */
  114. #define IP_MULTICAST(a) IP_CLASSD(a)
  115. #define IP_EXPERIMENTAL(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
  116. #define IP_BADCLASS(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
  117. #define IP_LOOPBACKNET 127 /* official! */
  118. #if BYTE_ORDER == BIG_ENDIAN
  119. /** Set an IP address given by the four byte-parts */
  120. #define IP4_ADDR(ipaddr, a,b,c,d) \
  121. (ipaddr)->addr = ((u32_t)((a) & 0xff) << 24) | \
  122. ((u32_t)((b) & 0xff) << 16) | \
  123. ((u32_t)((c) & 0xff) << 8) | \
  124. (u32_t)((d) & 0xff)
  125. #else
  126. /** Set an IP address given by the four byte-parts.
  127. Little-endian version that prevents the use of htonl. */
  128. #define IP4_ADDR(ipaddr, a,b,c,d) \
  129. (ipaddr)->addr = ((u32_t)((d) & 0xff) << 24) | \
  130. ((u32_t)((c) & 0xff) << 16) | \
  131. ((u32_t)((b) & 0xff) << 8) | \
  132. (u32_t)((a) & 0xff)
  133. #endif
  134. /** MEMCPY-like copying of IP addresses where addresses are known to be
  135. * 16-bit-aligned if the port is correctly configured (so a port could define
  136. * this to copying 2 u16_t's) - no NULL-pointer-checking needed. */
  137. #ifndef IPADDR2_COPY
  138. #define IPADDR2_COPY(dest, src) SMEMCPY(dest, src, sizeof(ip_addr_t))
  139. #endif
  140. /** Copy IP address - faster than ip_addr_set: no NULL check */
  141. #define ip_addr_copy(dest, src) ((dest).addr = (src).addr)
  142. /** Safely copy one IP address to another (src may be NULL) */
  143. #define ip_addr_set(dest, src) ((dest)->addr = \
  144. ((src) == NULL ? 0 : \
  145. (src)->addr))
  146. /** Set complete address to zero */
  147. #define ip_addr_set_zero(ipaddr) ((ipaddr)->addr = 0)
  148. /** Set address to IPADDR_ANY (no need for htonl()) */
  149. #define ip_addr_set_any(ipaddr) ((ipaddr)->addr = IPADDR_ANY)
  150. /** Set address to loopback address */
  151. #define ip_addr_set_loopback(ipaddr) ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
  152. /** Safely copy one IP address to another and change byte order
  153. * from host- to network-order. */
  154. #define ip_addr_set_hton(dest, src) ((dest)->addr = \
  155. ((src) == NULL ? 0:\
  156. htonl((src)->addr)))
  157. /** IPv4 only: set the IP address given as an u32_t */
  158. #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
  159. /** IPv4 only: get the IP address as an u32_t */
  160. #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
  161. /** Get the network address by combining host address with netmask */
  162. #define ip_addr_get_network(target, host, netmask) ((target)->addr = ((host)->addr) & ((netmask)->addr))
  163. /**
  164. * Determine if two address are on the same network.
  165. *
  166. * @arg addr1 IP address 1
  167. * @arg addr2 IP address 2
  168. * @arg mask network identifier mask
  169. * @return !0 if the network identifiers of both address match
  170. */
  171. #define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
  172. (mask)->addr) == \
  173. ((addr2)->addr & \
  174. (mask)->addr))
  175. #define ip_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
  176. #define ip_addr_isany(addr1) ((addr1) == NULL || (addr1)->addr == IPADDR_ANY)
  177. #define ip_addr_isbroadcast(ipaddr, netif) ip4_addr_isbroadcast((ipaddr)->addr, (netif))
  178. u8_t ip4_addr_isbroadcast(u32_t addr, const struct netif *netif)ICACHE_FLASH_ATTR;
  179. #define ip_addr_netmask_valid(netmask) ip4_addr_netmask_valid((netmask)->addr)
  180. u8_t ip4_addr_netmask_valid(u32_t netmask)ICACHE_FLASH_ATTR;
  181. #define ip_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
  182. #define ip_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
  183. #define ip_addr_debug_print(debug, ipaddr) \
  184. LWIP_DEBUGF(debug, ("%"U16_F".%"U16_F".%"U16_F".%"U16_F, \
  185. ipaddr != NULL ? ip4_addr1_16(ipaddr) : 0, \
  186. ipaddr != NULL ? ip4_addr2_16(ipaddr) : 0, \
  187. ipaddr != NULL ? ip4_addr3_16(ipaddr) : 0, \
  188. ipaddr != NULL ? ip4_addr4_16(ipaddr) : 0))
  189. /* Get one byte from the 4-byte address */
  190. #define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0])
  191. #define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1])
  192. #define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2])
  193. #define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3])
  194. /* These are cast to u16_t, with the intent that they are often arguments
  195. * to printf using the U16_F format from cc.h. */
  196. #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
  197. #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
  198. #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
  199. #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
  200. /** For backwards compatibility */
  201. #define ip_ntoa(ipaddr) ipaddr_ntoa(ipaddr)
  202. u32_t ipaddr_addr(const char *cp)ICACHE_FLASH_ATTR;
  203. int ipaddr_aton(const char *cp, ip_addr_t *addr)ICACHE_FLASH_ATTR;
  204. /** returns ptr to static buffer; not reentrant! */
  205. char *ipaddr_ntoa(const ip_addr_t *addr)ICACHE_FLASH_ATTR;
  206. char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)ICACHE_FLASH_ATTR;
  207. #define IP2STR(ipaddr) ip4_addr1_16(ipaddr), \
  208. ip4_addr2_16(ipaddr), \
  209. ip4_addr3_16(ipaddr), \
  210. ip4_addr4_16(ipaddr)
  211. #define IPSTR "%d.%d.%d.%d"
  212. struct ip_info {
  213. struct ip_addr ip;
  214. struct ip_addr netmask;
  215. struct ip_addr gw;
  216. };
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif /* __LWIP_IP_ADDR_H__ */