]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/SocketAddress.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[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   // Direct access to all of the sockaddr structures
156   //------------------------------------------------------------------
157   struct sockaddr &sockaddr() {
158     return m_socket_addr.sa;
159   }
160
161   const struct sockaddr &sockaddr() const { return m_socket_addr.sa; }
162
163   struct sockaddr_in &sockaddr_in() {
164     return m_socket_addr.sa_ipv4;
165   }
166
167   const struct sockaddr_in &sockaddr_in() const {
168     return m_socket_addr.sa_ipv4;
169   }
170
171   struct sockaddr_in6 &sockaddr_in6() {
172     return m_socket_addr.sa_ipv6;
173   }
174
175   const struct sockaddr_in6 &sockaddr_in6() const {
176     return m_socket_addr.sa_ipv6;
177   }
178
179   struct sockaddr_storage &sockaddr_storage() {
180     return m_socket_addr.sa_storage;
181   }
182
183   const struct sockaddr_storage &sockaddr_storage() const {
184     return m_socket_addr.sa_storage;
185   }
186
187   //------------------------------------------------------------------
188   // Conversion operators to allow getting the contents of this class
189   // as a pointer to the appropriate structure. This allows an instance
190   // of this class to be used in calls that take one of the sockaddr
191   // structure variants without having to manually use the correct
192   // accessor function.
193   //------------------------------------------------------------------
194
195   operator struct sockaddr *() { return &m_socket_addr.sa; }
196
197   operator const struct sockaddr *() const { return &m_socket_addr.sa; }
198
199   operator struct sockaddr_in *() { return &m_socket_addr.sa_ipv4; }
200
201   operator const struct sockaddr_in *() const { return &m_socket_addr.sa_ipv4; }
202
203   operator struct sockaddr_in6 *() { return &m_socket_addr.sa_ipv6; }
204
205   operator const struct sockaddr_in6 *() const {
206     return &m_socket_addr.sa_ipv6;
207   }
208
209   operator const struct sockaddr_storage *() const {
210     return &m_socket_addr.sa_storage;
211   }
212
213   operator struct sockaddr_storage *() { return &m_socket_addr.sa_storage; }
214
215 protected:
216   typedef union sockaddr_tag {
217     struct sockaddr sa;
218     struct sockaddr_in sa_ipv4;
219     struct sockaddr_in6 sa_ipv6;
220     struct sockaddr_storage sa_storage;
221   } sockaddr_t;
222
223   //------------------------------------------------------------------
224   // Classes that inherit from SocketAddress can see and modify these
225   //------------------------------------------------------------------
226   sockaddr_t m_socket_addr;
227 };
228
229 } // namespace lldb_private
230
231 #endif // liblldb_SocketAddress_h_