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