ip_endpoint.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_BASE_IP_ENDPOINT_H_
  5. #define NET_BASE_IP_ENDPOINT_H_
  6. #include <stdint.h>
  7. #include <ostream>
  8. #include <string>
  9. #include "build/build_config.h"
  10. #include "net/base/address_family.h"
  11. #include "net/base/ip_address.h"
  12. #include "net/base/net_export.h"
  13. // Replicate these from Windows headers to avoid pulling net/sys_addrinfo.h.
  14. // Doing that transitively brings in windows.h. Including windows.h pollutes the
  15. // global namespace with thousands of macro definitions. This file is
  16. // transitively included in enough files that including windows.h potentially
  17. // impacts build performance.
  18. // Similarly, just pull in the minimal header necessary on non-Windows platforms
  19. // to help with build performance.
  20. struct sockaddr;
  21. #if BUILDFLAG(IS_WIN)
  22. typedef int socklen_t;
  23. #else
  24. #include <sys/socket.h>
  25. #endif
  26. namespace net {
  27. // An IPEndPoint represents the address of a transport endpoint:
  28. // * IP address (either v4 or v6)
  29. // * Port
  30. class NET_EXPORT IPEndPoint {
  31. public:
  32. IPEndPoint();
  33. ~IPEndPoint();
  34. IPEndPoint(const IPAddress& address, uint16_t port);
  35. IPEndPoint(const IPEndPoint& endpoint);
  36. const IPAddress& address() const { return address_; }
  37. // Returns the IPv4/IPv6 port if it has been set by the constructor or
  38. // `FromSockAddr`. This function will crash if the IPEndPoint is for a
  39. // Bluetooth socket.
  40. uint16_t port() const;
  41. // Returns AddressFamily of the address. Returns ADDRESS_FAMILY_UNSPECIFIED if
  42. // this is the IPEndPoint for a Bluetooth socket.
  43. AddressFamily GetFamily() const;
  44. // Returns the sockaddr family of the address, AF_INET or AF_INET6. Returns
  45. // AF_BTH if this is the IPEndPoint for a Bluetooth socket.
  46. int GetSockAddrFamily() const;
  47. // Convert to a provided sockaddr struct. This function will crash if the
  48. // IPEndPoint is for a Bluetooth socket.
  49. // |address| is the sockaddr to copy into. Should be at least
  50. // sizeof(struct sockaddr_storage) bytes.
  51. // |address_length| is an input/output parameter. On input, it is the
  52. // size of data in |address| available. On output, it is the size of
  53. // the address that was copied into |address|.
  54. // Returns true on success, false on failure.
  55. [[nodiscard]] bool ToSockAddr(struct sockaddr* address,
  56. socklen_t* address_length) const;
  57. // Convert from a sockaddr struct.
  58. // |address| is the address.
  59. // |address_length| is the length of |address|.
  60. // Returns true on success, false on failure.
  61. [[nodiscard]] bool FromSockAddr(const struct sockaddr* address,
  62. socklen_t address_length);
  63. // Returns value as a string (e.g. "127.0.0.1:80"). Returns the empty string
  64. // when |address_| is invalid (the port will be ignored). This function will
  65. // crash if the IPEndPoint is for a Bluetooth socket.
  66. std::string ToString() const;
  67. // As above, but without port. Returns the empty string when address_ is
  68. // invalid. The function will crash if the IPEndPoint is for a Bluetooth
  69. // socket.
  70. std::string ToStringWithoutPort() const;
  71. bool operator<(const IPEndPoint& that) const;
  72. bool operator==(const IPEndPoint& that) const;
  73. bool operator!=(const IPEndPoint& that) const;
  74. private:
  75. IPAddress address_;
  76. uint16_t port_ = 0;
  77. };
  78. NET_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
  79. const IPEndPoint& ip_endpoint);
  80. } // namespace net
  81. #endif // NET_BASE_IP_ENDPOINT_H_