]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/SocketAddress.h
Merge ^/head r317216 through r317280.
[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> GetAddressInfo(const char *hostname,
45                                                  const char *servname);
46
47   //------------------------------------------------------------------
48   // Constructors and Destructors
49   //------------------------------------------------------------------
50   SocketAddress();
51   SocketAddress(const struct addrinfo *addr_info);
52   SocketAddress(const struct sockaddr &s);
53   SocketAddress(const struct sockaddr_in &s);
54   SocketAddress(const struct sockaddr_in6 &s);
55   SocketAddress(const struct sockaddr_storage &s);
56   SocketAddress(const SocketAddress &rhs);
57   ~SocketAddress();
58
59   //------------------------------------------------------------------
60   // Operators
61   //------------------------------------------------------------------
62   const SocketAddress &operator=(const SocketAddress &rhs);
63
64   const SocketAddress &operator=(const struct addrinfo *addr_info);
65
66   const SocketAddress &operator=(const struct sockaddr &s);
67
68   const SocketAddress &operator=(const struct sockaddr_in &s);
69
70   const SocketAddress &operator=(const struct sockaddr_in6 &s);
71
72   const SocketAddress &operator=(const struct sockaddr_storage &s);
73
74   bool operator==(const SocketAddress &rhs) const;
75   bool operator!=(const SocketAddress &rhs) const;
76
77   //------------------------------------------------------------------
78   // Clear the contents of this socket address
79   //------------------------------------------------------------------
80   void Clear();
81
82   //------------------------------------------------------------------
83   // Get the length for the current socket address family
84   //------------------------------------------------------------------
85   socklen_t GetLength() const;
86
87   //------------------------------------------------------------------
88   // Get the max length for the largest socket address supported.
89   //------------------------------------------------------------------
90   static socklen_t GetMaxLength();
91
92   //------------------------------------------------------------------
93   // Get the socket address family
94   //------------------------------------------------------------------
95   sa_family_t GetFamily() const;
96
97   //------------------------------------------------------------------
98   // Set the socket address family
99   //------------------------------------------------------------------
100   void SetFamily(sa_family_t family);
101
102   //------------------------------------------------------------------
103   // Get the address
104   //------------------------------------------------------------------
105   std::string GetIPAddress() const;
106
107   //------------------------------------------------------------------
108   // Get the port if the socket address for the family has a port
109   //------------------------------------------------------------------
110   uint16_t GetPort() const;
111
112   //------------------------------------------------------------------
113   // Set the port if the socket address for the family has a port.
114   // The family must be set correctly prior to calling this function.
115   //------------------------------------------------------------------
116   bool SetPort(uint16_t port);
117
118   //------------------------------------------------------------------
119   // Set the socket address according to the first match from a call
120   // to getaddrinfo() (or equivalent functions for systems that don't
121   // have getaddrinfo(). If "addr_info_ptr" is not NULL, it will get
122   // filled in with the match that was used to populate this socket
123   // address.
124   //------------------------------------------------------------------
125   bool
126   getaddrinfo(const char *host,    // Hostname ("foo.bar.com" or "foo" or IP
127                                    // address string ("123.234.12.1" or
128                                    // "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
129               const char *service, // Protocol name ("tcp", "http", etc) or a
130                                    // raw port number string ("81")
131               int ai_family = PF_UNSPEC, int ai_socktype = 0,
132               int ai_protocol = 0, int ai_flags = 0);
133
134   //------------------------------------------------------------------
135   // Quick way to set the SocketAddress to localhost given the family.
136   // Returns true if successful, false if "family" doesn't support
137   // localhost or if "family" is not supported by this class.
138   //------------------------------------------------------------------
139   bool SetToLocalhost(sa_family_t family, uint16_t port);
140
141   bool SetToAnyAddress(sa_family_t family, uint16_t port);
142
143   //------------------------------------------------------------------
144   // Returns true if there is a valid socket address in this object.
145   //------------------------------------------------------------------
146   bool IsValid() const;
147
148   //------------------------------------------------------------------
149   // Returns true if the socket is INADDR_ANY
150   //------------------------------------------------------------------
151   bool IsAnyAddr() const;
152
153   //------------------------------------------------------------------
154   // Direct access to all of the sockaddr structures
155   //------------------------------------------------------------------
156   struct sockaddr &sockaddr() {
157     return m_socket_addr.sa;
158   }
159
160   const struct sockaddr &sockaddr() const { return m_socket_addr.sa; }
161
162   struct sockaddr_in &sockaddr_in() {
163     return m_socket_addr.sa_ipv4;
164   }
165
166   const struct sockaddr_in &sockaddr_in() const {
167     return m_socket_addr.sa_ipv4;
168   }
169
170   struct sockaddr_in6 &sockaddr_in6() {
171     return m_socket_addr.sa_ipv6;
172   }
173
174   const struct sockaddr_in6 &sockaddr_in6() const {
175     return m_socket_addr.sa_ipv6;
176   }
177
178   struct sockaddr_storage &sockaddr_storage() {
179     return m_socket_addr.sa_storage;
180   }
181
182   const struct sockaddr_storage &sockaddr_storage() const {
183     return m_socket_addr.sa_storage;
184   }
185
186   //------------------------------------------------------------------
187   // Conversion operators to allow getting the contents of this class
188   // as a pointer to the appropriate structure. This allows an instance
189   // of this class to be used in calls that take one of the sockaddr
190   // structure variants without having to manually use the correct
191   // accessor function.
192   //------------------------------------------------------------------
193
194   operator struct sockaddr *() { return &m_socket_addr.sa; }
195
196   operator const struct sockaddr *() const { return &m_socket_addr.sa; }
197
198   operator struct sockaddr_in *() { return &m_socket_addr.sa_ipv4; }
199
200   operator const struct sockaddr_in *() const { return &m_socket_addr.sa_ipv4; }
201
202   operator struct sockaddr_in6 *() { return &m_socket_addr.sa_ipv6; }
203
204   operator const struct sockaddr_in6 *() const {
205     return &m_socket_addr.sa_ipv6;
206   }
207
208   operator const struct sockaddr_storage *() const {
209     return &m_socket_addr.sa_storage;
210   }
211
212   operator struct sockaddr_storage *() { return &m_socket_addr.sa_storage; }
213
214 protected:
215   typedef union sockaddr_tag {
216     struct sockaddr sa;
217     struct sockaddr_in sa_ipv4;
218     struct sockaddr_in6 sa_ipv6;
219     struct sockaddr_storage sa_storage;
220   } sockaddr_t;
221
222   //------------------------------------------------------------------
223   // Classes that inherit from SocketAddress can see and modify these
224   //------------------------------------------------------------------
225   sockaddr_t m_socket_addr;
226 };
227
228 } // namespace lldb_private
229
230 #endif // liblldb_SocketAddress_h_