ip_addr.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * ESPRESSIF MIT License
  3. *
  4. * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef __IP_ADDR_H__
  25. #define __IP_ADDR_H__
  26. #include "c_types.h"
  27. struct ip_addr {
  28. uint32 addr;
  29. };
  30. typedef struct ip_addr ip_addr_t;
  31. struct ip_info {
  32. struct ip_addr ip;
  33. struct ip_addr netmask;
  34. struct ip_addr gw;
  35. };
  36. /**
  37. * Determine if two address are on the same network.
  38. *
  39. * @arg addr1 IP address 1
  40. * @arg addr2 IP address 2
  41. * @arg mask network identifier mask
  42. * @return !0 if the network identifiers of both address match
  43. */
  44. #define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
  45. (mask)->addr) == \
  46. ((addr2)->addr & \
  47. (mask)->addr))
  48. /** Set an IP address given by the four byte-parts.
  49. Little-endian version that prevents the use of htonl. */
  50. #define IP4_ADDR(ipaddr, a,b,c,d) \
  51. (ipaddr)->addr = ((uint32)((d) & 0xff) << 24) | \
  52. ((uint32)((c) & 0xff) << 16) | \
  53. ((uint32)((b) & 0xff) << 8) | \
  54. (uint32)((a) & 0xff)
  55. #define ip4_addr1(ipaddr) (((uint8*)(ipaddr))[0])
  56. #define ip4_addr2(ipaddr) (((uint8*)(ipaddr))[1])
  57. #define ip4_addr3(ipaddr) (((uint8*)(ipaddr))[2])
  58. #define ip4_addr4(ipaddr) (((uint8*)(ipaddr))[3])
  59. #define ip4_addr1_16(ipaddr) ((uint16)ip4_addr1(ipaddr))
  60. #define ip4_addr2_16(ipaddr) ((uint16)ip4_addr2(ipaddr))
  61. #define ip4_addr3_16(ipaddr) ((uint16)ip4_addr3(ipaddr))
  62. #define ip4_addr4_16(ipaddr) ((uint16)ip4_addr4(ipaddr))
  63. /** 255.255.255.255 */
  64. #define IPADDR_NONE ((uint32)0xffffffffUL)
  65. /** 0.0.0.0 */
  66. #define IPADDR_ANY ((uint32)0x00000000UL)
  67. uint32 ipaddr_addr(const char *cp);
  68. #define IP2STR(ipaddr) ip4_addr1_16(ipaddr), \
  69. ip4_addr2_16(ipaddr), \
  70. ip4_addr3_16(ipaddr), \
  71. ip4_addr4_16(ipaddr)
  72. #define IPSTR "%d.%d.%d.%d"
  73. #endif /* __IP_ADDR_H__ */