netaddr.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #include <util/digest/numeric.h>
  3. #include <util/generic/hash.h>
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/strbuf.h>
  6. #include <util/generic/vector.h>
  7. #include <util/network/address.h>
  8. namespace NBus {
  9. using namespace NAddr;
  10. /// IP protocol version.
  11. enum EIpVersion {
  12. EIP_VERSION_4 = 1,
  13. EIP_VERSION_6 = 2,
  14. EIP_VERSION_ANY = EIP_VERSION_4 | EIP_VERSION_6,
  15. };
  16. inline bool IsFamilyAllowed(ui16 sa_family, EIpVersion ipVersion) {
  17. if (ipVersion == EIP_VERSION_4 && sa_family != AF_INET) {
  18. return false;
  19. }
  20. if (ipVersion == EIP_VERSION_6 && sa_family != AF_INET6) {
  21. return false;
  22. }
  23. return true;
  24. }
  25. const char* ToCString(EIpVersion);
  26. int ToAddrFamily(EIpVersion);
  27. /// Hold referenced pointer to address description structure (ex. sockaddr_storage)
  28. /// It's make possible to work with IPv4 / IPv6 addresses simultaneously
  29. class TNetAddr: public IRemoteAddr {
  30. public:
  31. class TError: public yexception {
  32. };
  33. TNetAddr();
  34. TNetAddr(TAutoPtr<IRemoteAddr> addr);
  35. TNetAddr(const char* hostPort, EIpVersion requireVersion = EIP_VERSION_ANY, EIpVersion preferVersion = EIP_VERSION_ANY);
  36. TNetAddr(TStringBuf host, int port, EIpVersion requireVersion = EIP_VERSION_ANY, EIpVersion preferVersion = EIP_VERSION_ANY);
  37. TNetAddr(const TNetworkAddress& na, EIpVersion requireVersion = EIP_VERSION_ANY, EIpVersion preferVersion = EIP_VERSION_ANY);
  38. TNetAddr(const TNetworkAddress& na, const TAddrInfo& ai);
  39. bool operator==(const TNetAddr&) const;
  40. bool operator!=(const TNetAddr& other) const {
  41. return !(*this == other);
  42. }
  43. inline explicit operator bool() const noexcept {
  44. return !!Ptr;
  45. }
  46. const sockaddr* Addr() const override;
  47. socklen_t Len() const override;
  48. bool IsIpv4() const;
  49. bool IsIpv6() const;
  50. int GetPort() const;
  51. private:
  52. TAtomicSharedPtr<IRemoteAddr> Ptr;
  53. };
  54. using TSockAddrInVector = TVector<TNetAddr>;
  55. struct TNetAddrHostPortHash {
  56. inline size_t operator()(const TNetAddr& a) const {
  57. const sockaddr* s = a.Addr();
  58. const sockaddr_in* const sa = reinterpret_cast<const sockaddr_in*>(s);
  59. const sockaddr_in6* const sa6 = reinterpret_cast<const sockaddr_in6*>(s);
  60. switch (s->sa_family) {
  61. case AF_INET:
  62. return CombineHashes<size_t>(ComputeHash(TStringBuf(reinterpret_cast<const char*>(&sa->sin_addr), sizeof(sa->sin_addr))), IntHashImpl(sa->sin_port));
  63. case AF_INET6:
  64. return CombineHashes<size_t>(ComputeHash(TStringBuf(reinterpret_cast<const char*>(&sa6->sin6_addr), sizeof(sa6->sin6_addr))), IntHashImpl(sa6->sin6_port));
  65. }
  66. return ComputeHash(TStringBuf(reinterpret_cast<const char*>(s), a.Len()));
  67. }
  68. };
  69. }