netaddr.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "netaddr.h"
  2. #include <util/network/address.h>
  3. #include <cstdlib>
  4. namespace NBus {
  5. const char* ToCString(EIpVersion ipVersion) {
  6. switch (ipVersion) {
  7. case EIP_VERSION_ANY:
  8. return "EIP_VERSION_ANY";
  9. case EIP_VERSION_4:
  10. return "EIP_VERSION_4";
  11. case EIP_VERSION_6:
  12. return "EIP_VERSION_6";
  13. }
  14. Y_ABORT();
  15. }
  16. int ToAddrFamily(EIpVersion ipVersion) {
  17. switch (ipVersion) {
  18. case EIP_VERSION_ANY:
  19. return AF_UNSPEC;
  20. case EIP_VERSION_4:
  21. return AF_INET;
  22. case EIP_VERSION_6:
  23. return AF_INET6;
  24. }
  25. Y_ABORT();
  26. }
  27. class TNetworkAddressRef: private TNetworkAddress, public TAddrInfo {
  28. public:
  29. TNetworkAddressRef(const TNetworkAddress& na, const TAddrInfo& ai)
  30. : TNetworkAddress(na)
  31. , TAddrInfo(ai)
  32. {
  33. }
  34. };
  35. static bool Compare(const IRemoteAddr& l, const IRemoteAddr& r) noexcept {
  36. if (l.Addr()->sa_family != r.Addr()->sa_family) {
  37. return false;
  38. }
  39. switch (l.Addr()->sa_family) {
  40. case AF_INET: {
  41. return memcmp(&(((const sockaddr_in*)l.Addr())->sin_addr), &(((const sockaddr_in*)r.Addr())->sin_addr), sizeof(in_addr)) == 0 &&
  42. ((const sockaddr_in*)l.Addr())->sin_port == ((const sockaddr_in*)r.Addr())->sin_port;
  43. }
  44. case AF_INET6: {
  45. return memcmp(&(((const sockaddr_in6*)l.Addr())->sin6_addr), &(((const sockaddr_in6*)r.Addr())->sin6_addr), sizeof(in6_addr)) == 0 &&
  46. ((const sockaddr_in6*)l.Addr())->sin6_port == ((const sockaddr_in6*)r.Addr())->sin6_port;
  47. }
  48. }
  49. return memcmp(l.Addr(), r.Addr(), Min<size_t>(l.Len(), r.Len())) == 0;
  50. }
  51. TNetAddr::TNetAddr()
  52. : Ptr(new TOpaqueAddr)
  53. {
  54. }
  55. TNetAddr::TNetAddr(TAutoPtr<IRemoteAddr> addr)
  56. : Ptr(addr)
  57. {
  58. Y_ABORT_UNLESS(!!Ptr);
  59. }
  60. namespace {
  61. using namespace NAddr;
  62. const char* Describe(EIpVersion version) {
  63. switch (version) {
  64. case EIP_VERSION_4:
  65. return "ipv4 address";
  66. case EIP_VERSION_6:
  67. return "ipv6 address";
  68. case EIP_VERSION_ANY:
  69. return "any address";
  70. default:
  71. Y_ABORT("unreachable");
  72. }
  73. }
  74. TAutoPtr<IRemoteAddr> MakeAddress(const TNetworkAddress& na, EIpVersion requireVersion, EIpVersion preferVersion) {
  75. TAutoPtr<IRemoteAddr> addr;
  76. for (TNetworkAddress::TIterator it = na.Begin(); it != na.End(); ++it) {
  77. if (IsFamilyAllowed(it->ai_family, requireVersion)) {
  78. if (IsFamilyAllowed(it->ai_family, preferVersion)) {
  79. return new TNetworkAddressRef(na, &*it);
  80. } else if (!addr) {
  81. addr.Reset(new TNetworkAddressRef(na, &*it));
  82. }
  83. }
  84. }
  85. return addr;
  86. }
  87. TAutoPtr<IRemoteAddr> MakeAddress(TStringBuf host, int port, EIpVersion requireVersion, EIpVersion preferVersion) {
  88. TString hostString(host);
  89. TNetworkAddress na(hostString, port);
  90. return MakeAddress(na, requireVersion, preferVersion);
  91. }
  92. TAutoPtr<IRemoteAddr> MakeAddress(const char* hostPort, EIpVersion requireVersion, EIpVersion preferVersion) {
  93. const char* portStr = strchr(hostPort, ':');
  94. if (!portStr) {
  95. ythrow TNetAddr::TError() << "port not specified in " << hostPort;
  96. }
  97. int port = atoi(portStr + 1);
  98. TNetworkAddress na(TString(hostPort, portStr), port);
  99. return MakeAddress(na, requireVersion, preferVersion);
  100. }
  101. }
  102. TNetAddr::TNetAddr(const char* hostPort, EIpVersion requireVersion /*= EIP_VERSION_ANY*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/)
  103. : Ptr(MakeAddress(hostPort, requireVersion, preferVersion))
  104. {
  105. if (!Ptr) {
  106. ythrow TNetAddr::TError() << "cannot resolve " << hostPort << " into " << Describe(requireVersion);
  107. }
  108. }
  109. TNetAddr::TNetAddr(TStringBuf host, int port, EIpVersion requireVersion /*= EIP_VERSION_ANY*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/)
  110. : Ptr(MakeAddress(host, port, requireVersion, preferVersion))
  111. {
  112. if (!Ptr) {
  113. ythrow TNetAddr::TError() << "cannot resolve " << host << ":" << port << " into " << Describe(requireVersion);
  114. }
  115. }
  116. TNetAddr::TNetAddr(const TNetworkAddress& na, EIpVersion requireVersion /*= EIP_VERSION_ANY*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/)
  117. : Ptr(MakeAddress(na, requireVersion, preferVersion))
  118. {
  119. if (!Ptr) {
  120. ythrow TNetAddr::TError() << "cannot resolve into " << Describe(requireVersion);
  121. }
  122. }
  123. TNetAddr::TNetAddr(const TNetworkAddress& na, const TAddrInfo& ai)
  124. : Ptr(new TNetworkAddressRef(na, ai))
  125. {
  126. }
  127. const sockaddr* TNetAddr::Addr() const {
  128. return Ptr->Addr();
  129. }
  130. socklen_t TNetAddr::Len() const {
  131. return Ptr->Len();
  132. }
  133. int TNetAddr::GetPort() const {
  134. switch (Ptr->Addr()->sa_family) {
  135. case AF_INET:
  136. return InetToHost(((sockaddr_in*)Ptr->Addr())->sin_port);
  137. case AF_INET6:
  138. return InetToHost(((sockaddr_in6*)Ptr->Addr())->sin6_port);
  139. default:
  140. Y_ABORT("unknown AF: %d", (int)Ptr->Addr()->sa_family);
  141. throw 1;
  142. }
  143. }
  144. bool TNetAddr::IsIpv4() const {
  145. return Ptr->Addr()->sa_family == AF_INET;
  146. }
  147. bool TNetAddr::IsIpv6() const {
  148. return Ptr->Addr()->sa_family == AF_INET6;
  149. }
  150. bool TNetAddr::operator==(const TNetAddr& rhs) const {
  151. return Ptr == rhs.Ptr || Compare(*Ptr, *rhs.Ptr);
  152. }
  153. }
  154. template <>
  155. void Out<NBus::TNetAddr>(IOutputStream& out, const NBus::TNetAddr& addr) {
  156. Out<NAddr::IRemoteAddr>(out, addr);
  157. }