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