1 //===-- SocketAddress.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_SocketAddress_h_
11 #define liblldb_SocketAddress_h_
17 #include "lldb/Host/windows/windows.h"
20 typedef ADDRESS_FAMILY sa_family_t;
22 #include <sys/socket.h>
24 #include <netinet/in.h>
27 #if defined(__FreeBSD__)
28 #include <sys/types.h>
32 // Other libraries and framework includes
36 namespace lldb_private {
41 //------------------------------------------------------------------
42 // Constructors and Destructors
43 //------------------------------------------------------------------
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);
52 //------------------------------------------------------------------
54 //------------------------------------------------------------------
56 operator=(const SocketAddress& rhs);
59 operator=(const struct addrinfo *addr_info);
62 operator=(const struct sockaddr &s);
65 operator=(const struct sockaddr_in &s);
68 operator=(const struct sockaddr_in6 &s);
71 operator=(const struct sockaddr_storage &s);
73 //------------------------------------------------------------------
74 // Clear the contents of this socket address
75 //------------------------------------------------------------------
79 //------------------------------------------------------------------
80 // Get the length for the current socket address family
81 //------------------------------------------------------------------
85 //------------------------------------------------------------------
86 // Get the max length for the largest socket address supported.
87 //------------------------------------------------------------------
91 //------------------------------------------------------------------
92 // Get the socket address family
93 //------------------------------------------------------------------
97 //------------------------------------------------------------------
98 // Set the socket address family
99 //------------------------------------------------------------------
101 SetFamily (sa_family_t family);
103 //------------------------------------------------------------------
105 //------------------------------------------------------------------
107 GetIPAddress () const;
109 //------------------------------------------------------------------
110 // Get the port if the socket address for the family has a port
111 //------------------------------------------------------------------
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 //------------------------------------------------------------------
120 SetPort (uint16_t port);
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
128 //------------------------------------------------------------------
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,
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 //------------------------------------------------------------------
143 SetToLocalhost (sa_family_t family,
147 SetToAnyAddress (sa_family_t family,
150 //------------------------------------------------------------------
151 // Returns true if there is a valid socket address in this object.
152 //------------------------------------------------------------------
156 //------------------------------------------------------------------
157 // Direct access to all of the sockaddr structures
158 //------------------------------------------------------------------
162 return m_socket_addr.sa;
165 const struct sockaddr &
168 return m_socket_addr.sa;
174 return m_socket_addr.sa_ipv4;
177 const struct sockaddr_in &
180 return m_socket_addr.sa_ipv4;
183 struct sockaddr_in6 &
186 return m_socket_addr.sa_ipv6;
189 const struct sockaddr_in6 &
190 sockaddr_in6 () const
192 return m_socket_addr.sa_ipv6;
195 struct sockaddr_storage &
198 return m_socket_addr.sa_storage;
202 const struct sockaddr_storage &
203 sockaddr_storage () const
205 return m_socket_addr.sa_storage;
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 //------------------------------------------------------------------
217 operator struct sockaddr * ()
219 return &m_socket_addr.sa;
222 operator const struct sockaddr * () const
224 return &m_socket_addr.sa;
227 operator struct sockaddr_in * ()
229 return &m_socket_addr.sa_ipv4;
232 operator const struct sockaddr_in * () const
234 return &m_socket_addr.sa_ipv4;
237 operator struct sockaddr_in6 * ()
239 return &m_socket_addr.sa_ipv6;
242 operator const struct sockaddr_in6 * () const
244 return &m_socket_addr.sa_ipv6;
247 operator const struct sockaddr_storage * () const
249 return &m_socket_addr.sa_storage;
252 operator struct sockaddr_storage * ()
254 return &m_socket_addr.sa_storage;
259 typedef union sockaddr_tag
262 struct sockaddr_in sa_ipv4;
263 struct sockaddr_in6 sa_ipv6;
264 struct sockaddr_storage sa_storage;
267 //------------------------------------------------------------------
268 // Classes that inherit from SocketAddress can see and modify these
269 //------------------------------------------------------------------
270 sockaddr_t m_socket_addr;
274 } // namespace lldb_private
277 #endif // liblldb_SocketAddress_h_