Sockaddr.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Sockaddr_H
  16. #define Sockaddr_H
  17. #include "memory/Allocator.h"
  18. #include "util/Endian.h"
  19. #include "util/Linker.h"
  20. Linker_require("util/platform/Sockaddr.c");
  21. #include <stdint.h>
  22. struct Sockaddr
  23. {
  24. /** the length of this sockaddr, this field is included in the length. */
  25. uint16_t addrLen;
  26. #define Sockaddr_flags_BCAST 1
  27. #define Sockaddr_flags_PREFIX (1<<1)
  28. uint16_t flags;
  29. /** Only applies if flags & Sockaddr_flags_PREFIX is true. */
  30. uint8_t prefix;
  31. uint8_t pad1;
  32. uint16_t pad2;
  33. };
  34. /** The number of bytes of space taken for representing the addrLen at the beginning. */
  35. #define Sockaddr_OVERHEAD 8
  36. /** The maximum possible size for the native sockaddr (not including Sockaddr_OVERHEAD) */
  37. #define Sockaddr_MAXSIZE 128
  38. struct Sockaddr_storage
  39. {
  40. struct Sockaddr addr;
  41. uint64_t nativeAddr[Sockaddr_MAXSIZE / 8];
  42. };
  43. /** 127.0.0.1 and ::1 addresses for building from. */
  44. const struct Sockaddr* const Sockaddr_LOOPBACK_be;
  45. const struct Sockaddr* const Sockaddr_LOOPBACK_le;
  46. #define Sockaddr_LOOPBACK (Endian_isBigEndian() ? Sockaddr_LOOPBACK_be : Sockaddr_LOOPBACK_le)
  47. const struct Sockaddr* const Sockaddr_LOOPBACK6;
  48. int Sockaddr_getPrefix(struct Sockaddr* addr);
  49. /**
  50. * Parse a sockaddr from a string, may be IP4 or IP6.
  51. *
  52. * @param str a string representation of the sockaddr.
  53. * @param output a sockaddr_storage to populate, if null then the validity of the string will be
  54. * checked only.
  55. * @return 0 if all goes well, -1 if there is an error.
  56. */
  57. int Sockaddr_parse(const char* str, struct Sockaddr_storage* out);
  58. /**
  59. * Convert a sockaddr to a printable string.
  60. */
  61. char* Sockaddr_print(struct Sockaddr* addr, struct Allocator* alloc);
  62. /**
  63. * Get the port from a sockaddr if applicable.
  64. *
  65. * @param a sockaddr.
  66. * @return the port number or -1 if not applicable to this sockaddr.
  67. */
  68. int Sockaddr_getPort(struct Sockaddr* sa);
  69. /**
  70. * Set the port for a sockaddr if applicable.
  71. *
  72. * @param sa a sockaddr.
  73. * @param port the port number to set.
  74. * @return 0 if all goes well, -1 if not applicable to this sockaddr.
  75. */
  76. int Sockaddr_setPort(struct Sockaddr* sa, uint16_t port);
  77. /**
  78. * Get the address family for the address.
  79. *
  80. * @param a sockaddr.
  81. * @return the AF number for this sockaddr.
  82. */
  83. extern const int Sockaddr_AF_INET;
  84. extern const int Sockaddr_AF_INET6;
  85. int Sockaddr_getFamily(struct Sockaddr* sa);
  86. /**
  87. * Get the address stored in a sockaddr.
  88. *
  89. * @param sa a sockaddr.
  90. * @param addrPtr a pointer which will be set to the actual address component of the sockaddr.
  91. * If NULL, the length will be returned only.
  92. * @return the length of the address component in bytes, -1 if failed to parse.
  93. */
  94. int Sockaddr_getAddress(struct Sockaddr* sa, void* addrPtr);
  95. /**
  96. * Get a new sockaddr from the native form, IE: sockaddr_in or sockaddr_in6.
  97. */
  98. struct Sockaddr* Sockaddr_fromNative(const void* ss, int addrLen, struct Allocator* alloc);
  99. /**
  100. * Output the native form of a sockaddr.
  101. */
  102. static inline void* Sockaddr_asNative(struct Sockaddr* sa)
  103. {
  104. return (void*)(&sa[1]);
  105. }
  106. static inline const void* Sockaddr_asNativeConst(const struct Sockaddr* sa)
  107. {
  108. return (const void*)(&sa[1]);
  109. }
  110. /**
  111. * Contrast with Sockaddr_fromNative(), Sockaddr_fromBytes() takes
  112. * input as the bytes of the address eg: Sockaddr_fromBytes({127,0,0,1}, Sockaddr_AF_INET, alloc)
  113. */
  114. struct Sockaddr* Sockaddr_fromBytes(const uint8_t* bytes, int addrFamily, struct Allocator* alloc);
  115. /**
  116. * Clone the sockaddr, the clone will use only as much memory as the type of sockaddr requires.
  117. */
  118. struct Sockaddr* Sockaddr_clone(const struct Sockaddr* addr, struct Allocator* alloc);
  119. /**
  120. * Normalize inconsistent native sockaddr implementations
  121. */
  122. void Sockaddr_normalizeNative(void* nativeSockaddr);
  123. /**
  124. * Get a hash for hashtable lookup.
  125. */
  126. uint32_t Sockaddr_hash(const struct Sockaddr* addr);
  127. /**
  128. * Compare two sockaddrs for sorting, comparison does not put them in any specific order.
  129. */
  130. int Sockaddr_compare(const struct Sockaddr* a, const struct Sockaddr* b);
  131. #endif