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_
15 #include <sys/socket.h>
17 #include <netinet/in.h>
19 #if defined(__FreeBSD__)
20 #include <sys/types.h>
24 // Other libraries and framework includes
27 namespace lldb_private {
32 //------------------------------------------------------------------
33 // Constructors and Destructors
34 //------------------------------------------------------------------
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);
43 //------------------------------------------------------------------
45 //------------------------------------------------------------------
47 operator=(const SocketAddress& rhs);
50 operator=(const struct addrinfo *addr_info);
53 operator=(const struct sockaddr &s);
56 operator=(const struct sockaddr_in &s);
59 operator=(const struct sockaddr_in6 &s);
62 operator=(const struct sockaddr_storage &s);
64 //------------------------------------------------------------------
65 // Clear the contents of this socket address
66 //------------------------------------------------------------------
70 //------------------------------------------------------------------
71 // Get the length for the current socket address family
72 //------------------------------------------------------------------
76 //------------------------------------------------------------------
77 // Get the mex length for the the largest socket address supported.
78 //------------------------------------------------------------------
82 //------------------------------------------------------------------
83 // Get the socket address family
84 //------------------------------------------------------------------
88 //------------------------------------------------------------------
89 // Set the socket address family
90 //------------------------------------------------------------------
92 SetFamily (sa_family_t family);
94 //------------------------------------------------------------------
95 // Get the port if the socket address for the family has a port
96 //------------------------------------------------------------------
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 //------------------------------------------------------------------
105 SetPort (in_port_t port);
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
113 //------------------------------------------------------------------
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
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 //------------------------------------------------------------------
126 SetToLocalhost (sa_family_t family,
129 //------------------------------------------------------------------
130 // Returns true if there is a valid socket address in this object.
131 //------------------------------------------------------------------
135 //------------------------------------------------------------------
136 // Direct access to all of the sockaddr structures
137 //------------------------------------------------------------------
141 return m_socket_addr.sa;
144 const struct sockaddr &
147 return m_socket_addr.sa;
153 return m_socket_addr.sa_ipv4;
156 const struct sockaddr_in &
159 return m_socket_addr.sa_ipv4;
162 struct sockaddr_in6 &
165 return m_socket_addr.sa_ipv6;
168 const struct sockaddr_in6 &
169 sockaddr_in6 () const
171 return m_socket_addr.sa_ipv6;
174 struct sockaddr_storage &
177 return m_socket_addr.sa_storage;
181 const struct sockaddr_storage &
182 sockaddr_storage () const
184 return m_socket_addr.sa_storage;
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 //------------------------------------------------------------------
196 operator struct sockaddr * ()
198 return &m_socket_addr.sa;
201 operator const struct sockaddr * () const
203 return &m_socket_addr.sa;
206 operator struct sockaddr_in * ()
208 return &m_socket_addr.sa_ipv4;
211 operator const struct sockaddr_in * () const
213 return &m_socket_addr.sa_ipv4;
216 operator struct sockaddr_in6 * ()
218 return &m_socket_addr.sa_ipv6;
221 operator const struct sockaddr_in6 * () const
223 return &m_socket_addr.sa_ipv6;
226 operator const struct sockaddr_storage * () const
228 return &m_socket_addr.sa_storage;
231 operator struct sockaddr_storage * ()
233 return &m_socket_addr.sa_storage;
238 typedef union sockaddr_tag
241 struct sockaddr_in sa_ipv4;
242 struct sockaddr_in6 sa_ipv6;
243 struct sockaddr_storage sa_storage;
246 //------------------------------------------------------------------
247 // Classes that inherit from SocketAddress can see and modify these
248 //------------------------------------------------------------------
249 sockaddr_t m_socket_addr;
253 } // namespace lldb_private
256 #endif // liblldb_SocketAddress_h_