udp_address.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #pragma once
  2. #include <util/generic/string.h>
  3. #include <util/generic/vector.h>
  4. #include <util/system/defaults.h>
  5. struct sockaddr_in6;
  6. namespace NNetliba {
  7. struct TUdpAddress {
  8. ui64 Network, Interface;
  9. int Scope, Port;
  10. TUdpAddress()
  11. : Network(0)
  12. , Interface(0)
  13. , Scope(0)
  14. , Port(0)
  15. {
  16. }
  17. bool IsIPv4() const {
  18. return (Network == 0 && (Interface & 0xffffffffll) == 0xffff0000ll);
  19. }
  20. ui32 GetIPv4() const {
  21. return Interface >> 32;
  22. }
  23. };
  24. inline bool operator==(const TUdpAddress& a, const TUdpAddress& b) {
  25. return a.Network == b.Network && a.Interface == b.Interface && a.Scope == b.Scope && a.Port == b.Port;
  26. }
  27. enum EUdpAddressType {
  28. UAT_ANY,
  29. UAT_IPV4,
  30. UAT_IPV6,
  31. };
  32. // accepts sockaddr_in & sockaddr_in6
  33. void GetUdpAddress(TUdpAddress* res, const sockaddr_in6& addr);
  34. // generates sockaddr_in6 for both ipv4 & ipv6
  35. void GetWinsockAddr(sockaddr_in6* res, const TUdpAddress& addr);
  36. // supports formats like hostname, hostname:124, 127.0.0.1, 127.0.0.1:80, fe34::12, [fe34::12]:80
  37. TUdpAddress CreateAddress(const TString& server, int defaultPort, EUdpAddressType type = UAT_ANY);
  38. TString GetAddressAsString(const TUdpAddress& addr);
  39. bool GetLocalAddresses(TVector<TUdpAddress>* addrs);
  40. }