Sockaddr.h 6.5 KB

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