LLDB  mainline
SocketAddress.h
Go to the documentation of this file.
1 //===-- SocketAddress.h -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef liblldb_SocketAddress_h_
10 #define liblldb_SocketAddress_h_
11 
12 #include <stdint.h>
13 
14 #ifdef _WIN32
16 #include <winsock2.h>
17 #include <ws2tcpip.h>
18 typedef ADDRESS_FAMILY sa_family_t;
19 #else
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23 #endif
24 
25 #if defined(__FreeBSD__)
26 #include <sys/types.h>
27 #endif
28 
29 #include <string>
30 #include <vector>
31 
32 namespace lldb_private {
33 
35 public:
36  // Static method to get all address information for a host and/or service
37  static std::vector<SocketAddress>
38  GetAddressInfo(const char *hostname, const char *servname, int ai_family,
39  int ai_socktype, int ai_protocol, int ai_flags = 0);
40 
41  // Constructors and Destructors
42  SocketAddress();
43  SocketAddress(const struct addrinfo *addr_info);
44  SocketAddress(const struct sockaddr &s);
45  SocketAddress(const struct sockaddr_in &s);
46  SocketAddress(const struct sockaddr_in6 &s);
47  SocketAddress(const struct sockaddr_storage &s);
48  SocketAddress(const SocketAddress &rhs);
50 
51  // Operators
52  const SocketAddress &operator=(const SocketAddress &rhs);
53 
54  const SocketAddress &operator=(const struct addrinfo *addr_info);
55 
56  const SocketAddress &operator=(const struct sockaddr &s);
57 
58  const SocketAddress &operator=(const struct sockaddr_in &s);
59 
60  const SocketAddress &operator=(const struct sockaddr_in6 &s);
61 
62  const SocketAddress &operator=(const struct sockaddr_storage &s);
63 
64  bool operator==(const SocketAddress &rhs) const;
65  bool operator!=(const SocketAddress &rhs) const;
66 
67  // Clear the contents of this socket address
68  void Clear();
69 
70  // Get the length for the current socket address family
71  socklen_t GetLength() const;
72 
73  // Get the max length for the largest socket address supported.
74  static socklen_t GetMaxLength();
75 
76  // Get the socket address family
77  sa_family_t GetFamily() const;
78 
79  // Set the socket address family
80  void SetFamily(sa_family_t family);
81 
82  // Get the address
83  std::string GetIPAddress() const;
84 
85  // Get the port if the socket address for the family has a port
86  uint16_t GetPort() const;
87 
88  // Set the port if the socket address for the family has a port. The family
89  // must be set correctly prior to calling this function.
90  bool SetPort(uint16_t port);
91 
92  // Set the socket address according to the first match from a call to
93  // getaddrinfo() (or equivalent functions for systems that don't have
94  // getaddrinfo(). If "addr_info_ptr" is not NULL, it will get filled in with
95  // the match that was used to populate this socket address.
96  bool
97  getaddrinfo(const char *host, // Hostname ("foo.bar.com" or "foo" or IP
98  // address string ("123.234.12.1" or
99  // "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
100  const char *service, // Protocol name ("tcp", "http", etc) or a
101  // raw port number string ("81")
102  int ai_family = PF_UNSPEC, int ai_socktype = 0,
103  int ai_protocol = 0, int ai_flags = 0);
104 
105  // Quick way to set the SocketAddress to localhost given the family. Returns
106  // true if successful, false if "family" doesn't support localhost or if
107  // "family" is not supported by this class.
108  bool SetToLocalhost(sa_family_t family, uint16_t port);
109 
110  bool SetToAnyAddress(sa_family_t family, uint16_t port);
111 
112  // Returns true if there is a valid socket address in this object.
113  bool IsValid() const;
114 
115  // Returns true if the socket is INADDR_ANY
116  bool IsAnyAddr() const;
117 
118  // Returns true if the socket is INADDR_LOOPBACK
119  bool IsLocalhost() const;
120 
121  // Direct access to all of the sockaddr structures
122  struct sockaddr &sockaddr() {
123  return m_socket_addr.sa;
124  }
125 
126  const struct sockaddr &sockaddr() const { return m_socket_addr.sa; }
127 
129  return m_socket_addr.sa_ipv4;
130  }
131 
132  const struct sockaddr_in &sockaddr_in() const {
133  return m_socket_addr.sa_ipv4;
134  }
135 
137  return m_socket_addr.sa_ipv6;
138  }
139 
140  const struct sockaddr_in6 &sockaddr_in6() const {
141  return m_socket_addr.sa_ipv6;
142  }
143 
145  return m_socket_addr.sa_storage;
146  }
147 
148  const struct sockaddr_storage &sockaddr_storage() const {
149  return m_socket_addr.sa_storage;
150  }
151 
152  // Conversion operators to allow getting the contents of this class as a
153  // pointer to the appropriate structure. This allows an instance of this
154  // class to be used in calls that take one of the sockaddr structure variants
155  // without having to manually use the correct accessor function.
156 
157  operator struct sockaddr *() { return &m_socket_addr.sa; }
158 
159  operator const struct sockaddr *() const { return &m_socket_addr.sa; }
160 
161  operator struct sockaddr_in *() { return &m_socket_addr.sa_ipv4; }
162 
163  operator const struct sockaddr_in *() const { return &m_socket_addr.sa_ipv4; }
164 
165  operator struct sockaddr_in6 *() { return &m_socket_addr.sa_ipv6; }
166 
167  operator const struct sockaddr_in6 *() const {
168  return &m_socket_addr.sa_ipv6;
169  }
170 
171  operator const struct sockaddr_storage *() const {
172  return &m_socket_addr.sa_storage;
173  }
174 
175  operator struct sockaddr_storage *() { return &m_socket_addr.sa_storage; }
176 
177 protected:
178  typedef union sockaddr_tag {
179  struct sockaddr sa;
183  } sockaddr_t;
184 
185  // Classes that inherit from SocketAddress can see and modify these
186  sockaddr_t m_socket_addr;
187 };
188 
189 } // namespace lldb_private
190 
191 #endif // liblldb_SocketAddress_h_
struct sockaddr_in & sockaddr_in()
struct sockaddr_in6 & sockaddr_in6()
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
bool getaddrinfo(const char *host, const char *service, int ai_family=PF_UNSPEC, int ai_socktype=0, int ai_protocol=0, int ai_flags=0)
struct sockaddr & sockaddr()
bool SetPort(uint16_t port)
union lldb_private::SocketAddress::sockaddr_tag sockaddr_t
static socklen_t GetMaxLength()
const struct sockaddr_in & sockaddr_in() const
sa_family_t GetFamily() const
void SetFamily(sa_family_t family)
static std::vector< SocketAddress > GetAddressInfo(const char *hostname, const char *servname, int ai_family, int ai_socktype, int ai_protocol, int ai_flags=0)
const struct sockaddr_storage & sockaddr_storage() const
const SocketAddress & operator=(const SocketAddress &rhs)
struct sockaddr_storage & sockaddr_storage()
bool operator==(const SocketAddress &rhs) const
bool SetToAnyAddress(sa_family_t family, uint16_t port)
bool operator!=(const SocketAddress &rhs) const
const struct sockaddr_in6 & sockaddr_in6() const
const struct sockaddr & sockaddr() const
std::string GetIPAddress() const
bool SetToLocalhost(sa_family_t family, uint16_t port)