Baselib_NetworkAddress.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. // Baselib Network Address
  3. #include "Baselib_ErrorState.h"
  4. #include "Baselib_Alignment.h"
  5. #include "Internal/Baselib_EnumSizeCheck.h"
  6. #include <string.h>
  7. #ifdef __cplusplus
  8. BASELIB_C_INTERFACE
  9. {
  10. #endif
  11. // Address family.
  12. typedef enum Baselib_NetworkAddress_Family
  13. {
  14. Baselib_NetworkAddress_Family_Invalid = 0,
  15. Baselib_NetworkAddress_Family_IPv4 = 1,
  16. Baselib_NetworkAddress_Family_IPv6 = 2
  17. } Baselib_NetworkAddress_Family;
  18. BASELIB_ENUM_ENSURE_ABI_COMPATIBILITY(Baselib_NetworkAddress_Family);
  19. // Fixed size address structure, large enough to hold IPv4 and IPv6 addresses.
  20. typedef struct Baselib_NetworkAddress
  21. {
  22. union
  23. {
  24. uint8_t data[16];
  25. uint8_t ipv6[16]; // in network byte order
  26. uint8_t ipv4[4]; // in network byte order
  27. };
  28. BASELIB_ALIGN_AS(2) uint8_t port[2]; // in network byte order
  29. uint8_t family;
  30. uint8_t _padding; // Explicit padding to allow for deterministic bitwise compare.
  31. // Scope zone index for IPv6 (ignored for IPv4)
  32. // Defaults to zero if not specified.
  33. // Note that unlike the other fields in this struct, this is *not* in network byte order!
  34. uint32_t ipv6_scope_id;
  35. } Baselib_NetworkAddress;
  36. // Max length of any string representing an IP address
  37. static const uint32_t Baselib_NetworkAddress_IpMaxStringLength = 46;
  38. // Binary encode string representation of an address.
  39. //
  40. // Neither port not ipAddressBuffer scope id are parsed from the ip string.
  41. // dstAddress->ipv6_scope_id is set to zero and needs to be manually set if required.
  42. //
  43. // Possible error codes:
  44. // - Baselib_ErrorCode_InvalidArgument - One or more of the input parameters are invalid
  45. BASELIB_API void Baselib_NetworkAddress_Encode(
  46. Baselib_NetworkAddress* dstAddress,
  47. Baselib_NetworkAddress_Family family,
  48. const char ip[],
  49. uint16_t port,
  50. Baselib_ErrorState* errorState
  51. );
  52. // Decode binary representation of an address.
  53. //
  54. // family, ipAddressBuffer, and port are all optional arguments.
  55. // passing zero as ipAddressBufferLen is the same as passing an ipAddressBuffer nullptr.
  56. // Port and IPv6 scope id are not encodeded to ipAddressBuffer.
  57. //
  58. // Possible error codes:
  59. // - Baselib_ErrorCode_InvalidArgument - srcAddress is null or otherwise invalid.
  60. // - Baselib_ErrorCode_InvalidBufferSize - ipAddressBuffer is too small to hold decoded ip address.
  61. BASELIB_API void Baselib_NetworkAddress_Decode(
  62. const Baselib_NetworkAddress* srcAddress,
  63. Baselib_NetworkAddress_Family* family,
  64. char ipAddressBuffer[],
  65. uint32_t ipAddressBufferLen,
  66. uint16_t* port,
  67. Baselib_ErrorState* errorState
  68. );
  69. // Returns zero initialized network address struct
  70. static inline Baselib_NetworkAddress Baselib_NetworkAddress_Empty(void)
  71. {
  72. Baselib_NetworkAddress address;
  73. memset(&address, 0, sizeof(address));
  74. return address;
  75. }
  76. typedef enum Baselib_NetworkAddress_AddressReuse
  77. {
  78. Baselib_NetworkAddress_AddressReuse_DoNotAllow = 0,
  79. // Allow multiple sockets to be bound to the same address/port.
  80. // All sockets bound to the same address/port need to have this flag set.
  81. Baselib_NetworkAddress_AddressReuse_Allow = 1,
  82. } Baselib_NetworkAddress_AddressReuse;
  83. BASELIB_ENUM_ENSURE_ABI_COMPATIBILITY(Baselib_NetworkAddress_AddressReuse);
  84. #ifdef __cplusplus
  85. }
  86. #endif