]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/SocketAddress.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 #include <sys/socket.h>
16 #include <netdb.h>
17 #include <netinet/in.h>
18
19 #if defined(__FreeBSD__)
20 #include <sys/types.h>
21 #endif
22
23 // C++ Includes
24 // Other libraries and framework includes
25 // Project includes
26
27 namespace lldb_private {
28
29 class SocketAddress
30 {
31 public:
32     //------------------------------------------------------------------
33     // Constructors and Destructors
34     //------------------------------------------------------------------
35     SocketAddress ();
36     SocketAddress (const struct sockaddr &s);
37     SocketAddress (const struct sockaddr_in &s);
38     SocketAddress (const struct sockaddr_in6 &s);
39     SocketAddress (const struct sockaddr_storage &s);
40     SocketAddress (const SocketAddress& rhs);
41     ~SocketAddress ();
42
43     //------------------------------------------------------------------
44     // Operators
45     //------------------------------------------------------------------
46     const SocketAddress&
47     operator=(const SocketAddress& rhs);
48
49     const SocketAddress&
50     operator=(const struct addrinfo *addr_info);
51
52     const SocketAddress&
53     operator=(const struct sockaddr &s);
54
55     const SocketAddress&
56     operator=(const struct sockaddr_in &s);
57
58     const SocketAddress&
59     operator=(const struct sockaddr_in6 &s);
60
61     const SocketAddress&
62     operator=(const struct sockaddr_storage &s);
63     
64     //------------------------------------------------------------------
65     // Clear the contents of this socket address
66     //------------------------------------------------------------------
67     void
68     Clear ();
69
70     //------------------------------------------------------------------
71     // Get the length for the current socket address family
72     //------------------------------------------------------------------
73     socklen_t
74     GetLength () const;
75
76     //------------------------------------------------------------------
77     // Get the mex length for the the largest socket address supported.
78     //------------------------------------------------------------------
79     static socklen_t
80     GetMaxLength ();
81
82     //------------------------------------------------------------------
83     // Get the socket address family 
84     //------------------------------------------------------------------
85     sa_family_t
86     GetFamily () const;
87
88     //------------------------------------------------------------------
89     // Set the socket address family 
90     //------------------------------------------------------------------
91     void
92     SetFamily (sa_family_t family);
93
94     //------------------------------------------------------------------
95     // Get the port if the socket address for the family has a port
96     //------------------------------------------------------------------
97     in_port_t
98     GetPort () const;
99
100     //------------------------------------------------------------------
101     // Set the port if the socket address for the family has a port. 
102     // The family must be set correctly prior to calling this function.
103     //------------------------------------------------------------------
104     bool
105     SetPort (in_port_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     SetAddress (const struct addrinfo *hints_ptr,   // Optional hints where the family, protocol and other things can be specified.
116                 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")
117                 const char *service,                // Protocol name ("tcp", "http", etc) or a raw port number string ("81")
118                 struct addrinfo *addr_info_ptr);    // If non-NULL, this will get filled in with the match
119
120     //------------------------------------------------------------------
121     // Quick way to set the SocketAddress to localhost given the family.
122     // Returns true if successful, false if "family" doesn't support 
123     // localhost or if "family" is not supported by this class.
124     //------------------------------------------------------------------
125     bool
126     SetToLocalhost (sa_family_t family, 
127                     in_port_t port);
128
129     //------------------------------------------------------------------
130     // Returns true if there is a valid socket address in this object.
131     //------------------------------------------------------------------
132     bool
133     IsValid () const;
134
135     //------------------------------------------------------------------
136     // Direct access to all of the sockaddr structures
137     //------------------------------------------------------------------
138     struct sockaddr &
139     sockaddr ()
140     {
141         return m_socket_addr.sa;
142     }
143
144     const struct sockaddr &
145     sockaddr () const
146     {
147         return m_socket_addr.sa;
148     }
149     
150     struct sockaddr_in &
151     sockaddr_in ()
152     {
153         return m_socket_addr.sa_ipv4;
154     }
155     
156     const struct sockaddr_in &
157     sockaddr_in () const
158     {
159         return m_socket_addr.sa_ipv4;
160     }
161     
162     struct sockaddr_in6 &
163     sockaddr_in6 ()
164     {
165         return m_socket_addr.sa_ipv6;
166     }
167     
168     const struct sockaddr_in6 &
169     sockaddr_in6 () const
170     {
171         return m_socket_addr.sa_ipv6;
172     }
173     
174     struct sockaddr_storage &
175     sockaddr_storage ()
176     {
177         return m_socket_addr.sa_storage;
178     }
179
180     
181     const struct sockaddr_storage &
182     sockaddr_storage () const
183     {
184         return m_socket_addr.sa_storage;
185     }
186     
187     
188     //------------------------------------------------------------------
189     // Conversion operators to allow getting the contents of this class
190     // as a pointer to the appropriate structure. This allows an instance
191     // of this class to be used in calls that take one of the sockaddr
192     // structure variants without having to manally use the correct
193     // accessor function.
194     //------------------------------------------------------------------
195     
196     operator struct sockaddr * ()
197     {
198         return &m_socket_addr.sa;
199     }
200     
201     operator const struct sockaddr * () const
202     {
203         return &m_socket_addr.sa;
204     }
205
206     operator struct sockaddr_in * ()
207     {
208         return &m_socket_addr.sa_ipv4;
209     }
210     
211     operator const struct sockaddr_in * () const
212     {
213         return &m_socket_addr.sa_ipv4;
214     }
215
216     operator struct sockaddr_in6 * ()
217     {
218         return &m_socket_addr.sa_ipv6;
219     }
220     
221     operator const struct sockaddr_in6 * () const
222     {
223         return &m_socket_addr.sa_ipv6;
224     }
225
226     operator const struct sockaddr_storage * () const
227     {
228         return &m_socket_addr.sa_storage;
229     }
230
231     operator struct sockaddr_storage * ()
232     {
233         return &m_socket_addr.sa_storage;
234     }
235
236     
237 protected:
238     typedef union sockaddr_tag
239     {
240         struct sockaddr         sa;
241         struct sockaddr_in      sa_ipv4;
242         struct sockaddr_in6     sa_ipv6;
243         struct sockaddr_storage sa_storage;
244     } sockaddr_t;
245
246     //------------------------------------------------------------------
247     // Classes that inherit from SocketAddress can see and modify these
248     //------------------------------------------------------------------
249     sockaddr_t m_socket_addr;
250 };
251
252
253 } // namespace lldb_private
254
255
256 #endif  // liblldb_SocketAddress_h_