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
35 namespace lldb_private {
40 //------------------------------------------------------------------
41 // Constructors and Destructors
42 //------------------------------------------------------------------
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);
51 //------------------------------------------------------------------
53 //------------------------------------------------------------------
55 operator=(const SocketAddress& rhs);
58 operator=(const struct addrinfo *addr_info);
61 operator=(const struct sockaddr &s);
64 operator=(const struct sockaddr_in &s);
67 operator=(const struct sockaddr_in6 &s);
70 operator=(const struct sockaddr_storage &s);
72 //------------------------------------------------------------------
73 // Clear the contents of this socket address
74 //------------------------------------------------------------------
78 //------------------------------------------------------------------
79 // Get the length for the current socket address family
80 //------------------------------------------------------------------
84 //------------------------------------------------------------------
85 // Get the mex length for the the largest socket address supported.
86 //------------------------------------------------------------------
90 //------------------------------------------------------------------
91 // Get the socket address family
92 //------------------------------------------------------------------
96 //------------------------------------------------------------------
97 // Set the socket address family
98 //------------------------------------------------------------------
100 SetFamily (sa_family_t family);
102 //------------------------------------------------------------------
103 // Get the port if the socket address for the family has a port
104 //------------------------------------------------------------------
108 //------------------------------------------------------------------
109 // Set the port if the socket address for the family has a port.
110 // The family must be set correctly prior to calling this function.
111 //------------------------------------------------------------------
113 SetPort (uint16_t port);
115 //------------------------------------------------------------------
116 // Set the socket address according to the first match from a call
117 // to getaddrinfo() (or equivalent functions for systems that don't
118 // have getaddrinfo(). If "addr_info_ptr" is not NULL, it will get
119 // filled in with the match that was used to populate this socket
121 //------------------------------------------------------------------
123 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")
124 const char *service, // Protocol name ("tcp", "http", etc) or a raw port number string ("81")
125 int ai_family = PF_UNSPEC,
130 //------------------------------------------------------------------
131 // Quick way to set the SocketAddress to localhost given the family.
132 // Returns true if successful, false if "family" doesn't support
133 // localhost or if "family" is not supported by this class.
134 //------------------------------------------------------------------
136 SetToLocalhost (sa_family_t family,
140 SetToAnyAddress (sa_family_t family,
143 //------------------------------------------------------------------
144 // Returns true if there is a valid socket address in this object.
145 //------------------------------------------------------------------
149 //------------------------------------------------------------------
150 // Direct access to all of the sockaddr structures
151 //------------------------------------------------------------------
155 return m_socket_addr.sa;
158 const struct sockaddr &
161 return m_socket_addr.sa;
167 return m_socket_addr.sa_ipv4;
170 const struct sockaddr_in &
173 return m_socket_addr.sa_ipv4;
176 struct sockaddr_in6 &
179 return m_socket_addr.sa_ipv6;
182 const struct sockaddr_in6 &
183 sockaddr_in6 () const
185 return m_socket_addr.sa_ipv6;
188 struct sockaddr_storage &
191 return m_socket_addr.sa_storage;
195 const struct sockaddr_storage &
196 sockaddr_storage () const
198 return m_socket_addr.sa_storage;
202 //------------------------------------------------------------------
203 // Conversion operators to allow getting the contents of this class
204 // as a pointer to the appropriate structure. This allows an instance
205 // of this class to be used in calls that take one of the sockaddr
206 // structure variants without having to manally use the correct
207 // accessor function.
208 //------------------------------------------------------------------
210 operator struct sockaddr * ()
212 return &m_socket_addr.sa;
215 operator const struct sockaddr * () const
217 return &m_socket_addr.sa;
220 operator struct sockaddr_in * ()
222 return &m_socket_addr.sa_ipv4;
225 operator const struct sockaddr_in * () const
227 return &m_socket_addr.sa_ipv4;
230 operator struct sockaddr_in6 * ()
232 return &m_socket_addr.sa_ipv6;
235 operator const struct sockaddr_in6 * () const
237 return &m_socket_addr.sa_ipv6;
240 operator const struct sockaddr_storage * () const
242 return &m_socket_addr.sa_storage;
245 operator struct sockaddr_storage * ()
247 return &m_socket_addr.sa_storage;
252 typedef union sockaddr_tag
255 struct sockaddr_in sa_ipv4;
256 struct sockaddr_in6 sa_ipv6;
257 struct sockaddr_storage sa_storage;
260 //------------------------------------------------------------------
261 // Classes that inherit from SocketAddress can see and modify these
262 //------------------------------------------------------------------
263 sockaddr_t m_socket_addr;
267 } // namespace lldb_private
270 #endif // liblldb_SocketAddress_h_