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