sal_ipaddr.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-18 ChenYong First version
  9. */
  10. #ifndef SAL_IPADDR_H__
  11. #define SAL_IPADDR_H__
  12. #include "sal_type.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /** IPv4 only: set the IP address given as an u32_t */
  17. #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
  18. /** IPv4 only: get the IP address as an u32_t */
  19. #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
  20. #define IP4ADDR_STRLEN_MAX 16
  21. /* These macros should be calculated by the preprocessor and are used
  22. with compile-time constants only (so that there is no little-endian
  23. overhead at runtime). */
  24. #define PP_HTONS(x) ((((x) & 0x00ffUL) << 8) | (((x) & 0xff00UL) >> 8))
  25. #define PP_NTOHS(x) PP_HTONS(x)
  26. #define PP_HTONL(x) ((((x) & 0x000000ffUL) << 24) | \
  27. (((x) & 0x0000ff00UL) << 8) | \
  28. (((x) & 0x00ff0000UL) >> 8) | \
  29. (((x) & 0xff000000UL) >> 24))
  30. #define PP_NTOHL(x) PP_HTONL(x)
  31. #define htons(x) (u16_t)PP_HTONS(x)
  32. #define ntohs(x) (u16_t)PP_NTOHS(x)
  33. #define htonl(x) (u32_t)PP_HTONL(x)
  34. #define ntohl(x) (u32_t)PP_NTOHL(x)
  35. /* If your port already typedef's in_addr_t, define IN_ADDR_T_DEFINED
  36. to prevent this code from redefining it. */
  37. #if !defined(in_addr_t) && !defined(IN_ADDR_T_DEFINED)
  38. typedef u32_t in_addr_t;
  39. #endif
  40. struct in_addr
  41. {
  42. in_addr_t s_addr;
  43. };
  44. struct in6_addr
  45. {
  46. union
  47. {
  48. u32_t u32_addr[4];
  49. u8_t u8_addr[16];
  50. } un;
  51. #define s6_addr un.u8_addr
  52. };
  53. enum sal_ip_addr_type
  54. {
  55. /** IPv4 */
  56. IPADDR_TYPE_V4 = 0U,
  57. /** IPv6 */
  58. IPADDR_TYPE_V6 = 6U,
  59. /** IPv4+IPv6 ("dual-stack") */
  60. IPADDR_TYPE_ANY = 46U
  61. };
  62. typedef struct ip4_addr
  63. {
  64. u32_t addr;
  65. } ip4_addr_t;
  66. typedef struct ip6_addr
  67. {
  68. u32_t addr[4];
  69. } ip6_addr_t;
  70. typedef struct _ip_addr
  71. {
  72. union
  73. {
  74. ip6_addr_t ip6;
  75. ip4_addr_t ip4;
  76. } u_addr;
  77. /** @ref sal_ip_addr_type */
  78. u8_t type;
  79. } ip_addr_t;
  80. /** 255.255.255.255 */
  81. #define IPADDR_NONE ((u32_t)0xffffffffUL)
  82. /** 127.0.0.1 */
  83. #define IPADDR_LOOPBACK ((u32_t)0x7f000001UL)
  84. /** 0.0.0.0 */
  85. #define IPADDR_ANY ((u32_t)0x00000000UL)
  86. /** 255.255.255.255 */
  87. #define IPADDR_BROADCAST ((u32_t)0xffffffffUL)
  88. /** 255.255.255.255 */
  89. #define INADDR_NONE IPADDR_NONE
  90. /** 127.0.0.1 */
  91. #define INADDR_LOOPBACK IPADDR_LOOPBACK
  92. /** 0.0.0.0 */
  93. #define INADDR_ANY IPADDR_ANY
  94. /** 255.255.255.255 */
  95. #define INADDR_BROADCAST IPADDR_BROADCAST
  96. #define IPADDR_BROADCAST_STRING "255.255.255.255"
  97. in_addr_t sal_ipaddr_addr(const char *cp);
  98. int sal_ip4addr_aton(const char *cp, ip4_addr_t *addr);
  99. char *sal_ip4addr_ntoa(const ip4_addr_t *addr);
  100. char *sal_ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);
  101. #define inet_addr(cp) sal_ipaddr_addr(cp)
  102. #define inet_aton(cp,addr) sal_ip4addr_aton(cp,(ip4_addr_t*)addr)
  103. #define inet_ntoa(addr) sal_ip4addr_ntoa((const ip4_addr_t*)&(addr))
  104. #define inet_ntoa_r(addr, buf, buflen) sal_ip4addr_ntoa_r((const ip4_addr_t*)&(addr), buf, buflen)
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /* SAL_IPADDR_H__ */