address.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #pragma once
  2. #include "ip.h"
  3. #include "socket.h"
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/string.h>
  6. #include <util/network/sock.h>
  7. namespace NAddr {
  8. class IRemoteAddr {
  9. public:
  10. virtual ~IRemoteAddr() = default;
  11. virtual const sockaddr* Addr() const = 0;
  12. virtual socklen_t Len() const = 0;
  13. };
  14. using IRemoteAddrPtr = THolder<IRemoteAddr>;
  15. using IRemoteAddrRef = TAtomicSharedPtr<NAddr::IRemoteAddr>;
  16. IRemoteAddrPtr GetSockAddr(SOCKET s);
  17. IRemoteAddrPtr GetPeerAddr(SOCKET s);
  18. void PrintHost(IOutputStream& out, const IRemoteAddr& addr);
  19. TString PrintHost(const IRemoteAddr& addr);
  20. TString PrintHostAndPort(const IRemoteAddr& addr);
  21. bool IsLoopback(const IRemoteAddr& addr);
  22. bool IsSame(const IRemoteAddr& lhs, const IRemoteAddr& rhs);
  23. socklen_t SockAddrLength(const sockaddr* addr);
  24. //for accept, recvfrom - see LenPtr()
  25. class TOpaqueAddr: public IRemoteAddr {
  26. public:
  27. inline TOpaqueAddr() noexcept
  28. : L_(sizeof(S_))
  29. {
  30. Zero(S_);
  31. }
  32. inline TOpaqueAddr(const IRemoteAddr* addr) noexcept {
  33. Assign(addr->Addr(), addr->Len());
  34. }
  35. inline TOpaqueAddr(const sockaddr* addr) {
  36. Assign(addr, SockAddrLength(addr));
  37. }
  38. const sockaddr* Addr() const override {
  39. return MutableAddr();
  40. }
  41. socklen_t Len() const override {
  42. return L_;
  43. }
  44. inline sockaddr* MutableAddr() const noexcept {
  45. return (sockaddr*)&S_;
  46. }
  47. inline socklen_t* LenPtr() noexcept {
  48. return &L_;
  49. }
  50. private:
  51. inline void Assign(const sockaddr* addr, socklen_t len) noexcept {
  52. L_ = len;
  53. memcpy(MutableAddr(), addr, L_);
  54. }
  55. private:
  56. sockaddr_storage S_;
  57. socklen_t L_;
  58. };
  59. //for TNetworkAddress
  60. class TAddrInfo: public IRemoteAddr {
  61. public:
  62. inline TAddrInfo(const addrinfo* ai) noexcept
  63. : AI_(ai)
  64. {
  65. }
  66. const sockaddr* Addr() const override {
  67. return AI_->ai_addr;
  68. }
  69. socklen_t Len() const override {
  70. return (socklen_t)AI_->ai_addrlen;
  71. }
  72. private:
  73. const addrinfo* const AI_;
  74. };
  75. //compat, for TIpAddress
  76. class TIPv4Addr: public IRemoteAddr {
  77. public:
  78. inline TIPv4Addr(const TIpAddress& addr) noexcept
  79. : A_(addr)
  80. {
  81. }
  82. const sockaddr* Addr() const override {
  83. return A_;
  84. }
  85. socklen_t Len() const override {
  86. return A_;
  87. }
  88. private:
  89. const TIpAddress A_;
  90. };
  91. //same, for ipv6 addresses
  92. class TIPv6Addr: public IRemoteAddr {
  93. public:
  94. inline TIPv6Addr(const sockaddr_in6& a) noexcept
  95. : A_(a)
  96. {
  97. }
  98. const sockaddr* Addr() const override {
  99. return (sockaddr*)&A_;
  100. }
  101. socklen_t Len() const override {
  102. return sizeof(A_);
  103. }
  104. private:
  105. const sockaddr_in6 A_;
  106. };
  107. class TUnixSocketAddr: public IRemoteAddr {
  108. public:
  109. explicit TUnixSocketAddr(TStringBuf path);
  110. const sockaddr* Addr() const override;
  111. socklen_t Len() const override;
  112. private:
  113. TSockAddrLocal SockAddr_;
  114. };
  115. }