xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/platform/api/quic_socket_address.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2016 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 
5 #ifndef QUICHE_QUIC_PLATFORM_API_QUIC_SOCKET_ADDRESS_H_
6 #define QUICHE_QUIC_PLATFORM_API_QUIC_SOCKET_ADDRESS_H_
7 
8 #include <string>
9 
10 #include "quiche/quic/platform/api/quic_export.h"
11 #include "quiche/quic/platform/api/quic_ip_address.h"
12 
13 namespace quic {
14 
15 // A class representing a socket endpoint address (i.e., IP address plus a
16 // port) in QUIC.
17 class QUIC_EXPORT_PRIVATE QuicSocketAddress {
18  public:
QuicSocketAddress()19   QuicSocketAddress() {}
20   QuicSocketAddress(QuicIpAddress address, uint16_t port);
21   explicit QuicSocketAddress(const struct sockaddr_storage& saddr);
22   explicit QuicSocketAddress(const sockaddr* saddr, socklen_t len);
23   QuicSocketAddress(const QuicSocketAddress& other) = default;
24   QuicSocketAddress& operator=(const QuicSocketAddress& other) = default;
25   QuicSocketAddress& operator=(QuicSocketAddress&& other) = default;
26   QUIC_EXPORT_PRIVATE friend bool operator==(const QuicSocketAddress& lhs,
27                                              const QuicSocketAddress& rhs);
28   QUIC_EXPORT_PRIVATE friend bool operator!=(const QuicSocketAddress& lhs,
29                                              const QuicSocketAddress& rhs);
30 
31   bool IsInitialized() const;
32   std::string ToString() const;
33 
34   // TODO(ericorth): Convert usage over to socket_api::GetSocketAddress() and
35   // remove.
36   int FromSocket(int fd);
37 
38   QuicSocketAddress Normalized() const;
39 
40   QuicIpAddress host() const;
41   uint16_t port() const;
42   sockaddr_storage generic_address() const;
43 
44   // Hashes this address to an uint32_t.
45   uint32_t Hash() const;
46 
47  private:
48   QuicIpAddress host_;
49   uint16_t port_ = 0;
50 };
51 
52 inline std::ostream& operator<<(std::ostream& os,
53                                 const QuicSocketAddress address) {
54   os << address.ToString();
55   return os;
56 }
57 
58 class QUIC_EXPORT_PRIVATE QuicSocketAddressHash {
59  public:
operator()60   size_t operator()(QuicSocketAddress const& address) const noexcept {
61     return address.Hash();
62   }
63 };
64 
65 }  // namespace quic
66 
67 #endif  // QUICHE_QUIC_PLATFORM_API_QUIC_SOCKET_ADDRESS_H_
68