sal_ipaddr.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * File : sal_ipaddr.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-18 ChenYong First version
  23. */
  24. #ifndef SAL_IPADDR_H__
  25. #define SAL_IPADDR_H__
  26. #include "sal_type.h"
  27. /** IPv4 only: set the IP address given as an u32_t */
  28. #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
  29. /** IPv4 only: get the IP address as an u32_t */
  30. #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
  31. #define IP4ADDR_STRLEN_MAX 16
  32. /* These macros should be calculated by the preprocessor and are used
  33. with compile-time constants only (so that there is no little-endian
  34. overhead at runtime). */
  35. #define PP_HTONS(x) ((((x) & 0x00ffUL) << 8) | (((x) & 0xff00UL) >> 8))
  36. #define PP_NTOHS(x) PP_HTONS(x)
  37. #define PP_HTONL(x) ((((x) & 0x000000ffUL) << 24) | \
  38. (((x) & 0x0000ff00UL) << 8) | \
  39. (((x) & 0x00ff0000UL) >> 8) | \
  40. (((x) & 0xff000000UL) >> 24))
  41. #define PP_NTOHL(x) PP_HTONL(x)
  42. #define htons(x) (u16_t)PP_HTONS(x)
  43. #define ntohs(x) (u16_t)PP_NTOHS(x)
  44. #define htonl(x) (u32_t)PP_HTONL(x)
  45. #define ntohl(x) (u32_t)PP_NTOHL(x)
  46. /* If your port already typedef's in_addr_t, define IN_ADDR_T_DEFINED
  47. to prevent this code from redefining it. */
  48. #if !defined(in_addr_t) && !defined(IN_ADDR_T_DEFINED)
  49. typedef u32_t in_addr_t;
  50. #endif
  51. struct in_addr
  52. {
  53. in_addr_t s_addr;
  54. };
  55. struct in6_addr
  56. {
  57. union
  58. {
  59. u32_t u32_addr[4];
  60. u8_t u8_addr[16];
  61. } un;
  62. #define s6_addr un.u8_addr
  63. };
  64. enum sal_ip_addr_type
  65. {
  66. /** IPv4 */
  67. IPADDR_TYPE_V4 = 0U,
  68. /** IPv6 */
  69. IPADDR_TYPE_V6 = 6U,
  70. /** IPv4+IPv6 ("dual-stack") */
  71. IPADDR_TYPE_ANY = 46U
  72. };
  73. typedef struct ip4_addr
  74. {
  75. u32_t addr;
  76. } ip4_addr_t;
  77. typedef struct ip6_addr
  78. {
  79. u32_t addr[4];
  80. } ip6_addr_t;
  81. typedef struct _ip_addr
  82. {
  83. union
  84. {
  85. ip6_addr_t ip6;
  86. ip4_addr_t ip4;
  87. } u_addr;
  88. /** @ref sal_ip_addr_type */
  89. u8_t type;
  90. } ip_addr_t;
  91. /** 255.255.255.255 */
  92. #define IPADDR_NONE ((u32_t)0xffffffffUL)
  93. /** 127.0.0.1 */
  94. #define IPADDR_LOOPBACK ((u32_t)0x7f000001UL)
  95. /** 0.0.0.0 */
  96. #define IPADDR_ANY ((u32_t)0x00000000UL)
  97. /** 255.255.255.255 */
  98. #define IPADDR_BROADCAST ((u32_t)0xffffffffUL)
  99. /** 255.255.255.255 */
  100. #define INADDR_NONE IPADDR_NONE
  101. /** 127.0.0.1 */
  102. #define INADDR_LOOPBACK IPADDR_LOOPBACK
  103. /** 0.0.0.0 */
  104. #define INADDR_ANY IPADDR_ANY
  105. /** 255.255.255.255 */
  106. #define INADDR_BROADCAST IPADDR_BROADCAST
  107. #define IPADDR_BROADCAST_STRING "255.255.255.255"
  108. in_addr_t sal_ipaddr_addr(const char *cp);
  109. int sal_ip4addr_aton(const char *cp, ip4_addr_t *addr);
  110. char *sal_ip4addr_ntoa(const ip4_addr_t *addr);
  111. char *sal_ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);
  112. #define inet_addr(cp) sal_ipaddr_addr(cp)
  113. #define inet_aton(cp,addr) sal_ip4addr_aton(cp,(ip4_addr_t*)addr)
  114. #define inet_ntoa(addr) sal_ip4addr_ntoa((const ip4_addr_t*)&(addr))
  115. #define inet_ntoa_r(addr, buf, buflen) sal_ip4addr_ntoa_r((const ip4_addr_t*)&(addr), buf, buflen)
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif /* SAL_IPADDR_H__ */