Sockaddr.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. typedef 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. uint8_t flags;
  29. #define Sockaddr_PLATFORM 0
  30. #define Sockaddr_HANDLE 1
  31. uint8_t type;
  32. /** Only applies if flags & Sockaddr_flags_PREFIX is true. */
  33. uint8_t prefix;
  34. uint8_t pad1;
  35. uint16_t pad2;
  36. } Sockaddr_t;
  37. /** The number of bytes of space taken for representing the addrLen at the beginning. */
  38. #define Sockaddr_OVERHEAD 8
  39. /** The maximum possible size for the native sockaddr (not including Sockaddr_OVERHEAD) */
  40. #define Sockaddr_MAXSIZE 128
  41. struct Sockaddr_storage
  42. {
  43. Sockaddr_t addr;
  44. uint64_t nativeAddr[Sockaddr_MAXSIZE / 8];
  45. };
  46. /** 127.0.0.1 and ::1 addresses for building from. */
  47. extern const Sockaddr_t* const Sockaddr_LOOPBACK_be;
  48. extern const Sockaddr_t* const Sockaddr_LOOPBACK_le;
  49. #define Sockaddr_LOOPBACK (Endian_isBigEndian() ? Sockaddr_LOOPBACK_be : Sockaddr_LOOPBACK_le)
  50. extern const Sockaddr_t* const Sockaddr_LOOPBACK6;
  51. /**
  52. * Parse an internal form of Sockaddr which is used to represent a uint32_t handle.
  53. * If the length of the address is not equal to sizeof(Sockaddr_t) or the type is not
  54. * Sockaddr_HANDLE then this returns Sockaddr_addrHandle_INVALID.
  55. */
  56. #define Sockaddr_addrHandle_INVALID 0xffffffffu
  57. uint32_t Sockaddr_addrHandle(const Sockaddr_t* addr);
  58. /**
  59. * Create a handle sockaddr from a numeric handle, if handle is equal to
  60. * Sockaddr_addrHandle_INVALID then this will trigger an assertion.
  61. */
  62. void Sockaddr_addrFromHandle(Sockaddr_t* addr, uint32_t handle);
  63. int Sockaddr_getPrefix(Sockaddr_t* addr);
  64. /**
  65. * Parse a sockaddr from a string, may be IP4 or IP6.
  66. *
  67. * @param str a string representation of the sockaddr.
  68. * @param output a sockaddr_storage to populate, if null then the validity of the string will be
  69. * checked only.
  70. * @return 0 if all goes well, -1 if there is an error.
  71. */
  72. int Sockaddr_parse(const char* str, struct Sockaddr_storage* out);
  73. /**
  74. * Convert a sockaddr to a printable string.
  75. */
  76. char* Sockaddr_print(Sockaddr_t* addr, struct Allocator* alloc);
  77. /**
  78. * Get the port from a sockaddr if applicable.
  79. *
  80. * @param a sockaddr.
  81. * @return the port number or -1 if not applicable to this sockaddr.
  82. */
  83. int Sockaddr_getPort(const Sockaddr_t* sa);
  84. /**
  85. * Set the port for a sockaddr if applicable.
  86. *
  87. * @param sa a sockaddr.
  88. * @param port the port number to set.
  89. * @return 0 if all goes well, -1 if not applicable to this sockaddr.
  90. */
  91. int Sockaddr_setPort(Sockaddr_t* sa, uint16_t port);
  92. /**
  93. * Get the address family for the address.
  94. *
  95. * @param a sockaddr.
  96. * @return the AF number for this sockaddr.
  97. */
  98. extern const int Sockaddr_AF_INET;
  99. extern const int Sockaddr_AF_INET6;
  100. int Sockaddr_getFamily(const Sockaddr_t* sa);
  101. /**
  102. * Get the address stored in a sockaddr.
  103. *
  104. * @param sa a sockaddr.
  105. * @param addrPtr a pointer which will be set to the actual address component of the sockaddr.
  106. * If NULL, the length will be returned only.
  107. * @return the length of the address component in bytes, -1 if failed to parse.
  108. */
  109. int Sockaddr_getAddress(Sockaddr_t* sa, void* addrPtr);
  110. /**
  111. * Whatever the address is, attempt to represent it as an IPv6 address.
  112. * IPv6 addresses are easy.
  113. * IPv4 are represented as ::ffff::aabb::ccdd
  114. * Other addresses smaller or equal than 14 bytes are represented as fffe::<address bytes>
  115. * Other addresses larger than 14 bytes are represented as ffff::<14 bytes of sha256 hash of address>
  116. */
  117. void Sockaddr_asIp6(uint8_t addrOut[static 16], const Sockaddr_t* sockaddr);
  118. /**
  119. * Output the native form of a sockaddr.
  120. */
  121. static inline void* Sockaddr_asNative(Sockaddr_t* sa)
  122. {
  123. return (void*)(&sa[1]);
  124. }
  125. /**
  126. * @brief Initialize a Sockaddr in place from bytes.
  127. *
  128. * @param out The location in memory to place the sockaddr data
  129. * @param bytes The bytes of the address, e.g. {127,0,0,1} (port not included)
  130. * @param addrFamily The address family (Sockaddr_AF_INET / Sockaddr_AF_INET6)
  131. * @return Sockaddr_t* which points to the memory of `out`
  132. */
  133. Sockaddr_t* Sockaddr_initFromBytes(struct Sockaddr_storage* out, const uint8_t* bytes, int addrFamily);
  134. /**
  135. * Sockaddr_fromBytes() takes
  136. * input as the bytes of the address eg: Sockaddr_fromBytes({127,0,0,1}, Sockaddr_AF_INET, alloc)
  137. */
  138. Sockaddr_t* Sockaddr_fromBytes(const uint8_t* bytes, int addrFamily, struct Allocator* alloc);
  139. /**
  140. * Clone the sockaddr, the clone will use only as much memory as the type of sockaddr requires.
  141. */
  142. Sockaddr_t* Sockaddr_clone(const Sockaddr_t* addr, struct Allocator* alloc);
  143. /**
  144. * Normalize inconsistent native sockaddr implementations
  145. */
  146. void Sockaddr_normalizeNative(void* nativeSockaddr);
  147. /**
  148. * Get a hash for hashtable lookup.
  149. */
  150. uint32_t Sockaddr_hash(const Sockaddr_t* addr);
  151. /**
  152. * Compare two sockaddrs for sorting, comparison does not put them in any specific order.
  153. */
  154. int Sockaddr_compare(const Sockaddr_t* a, const Sockaddr_t* b);
  155. #endif