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;
23 #include <netinet/in.h>
24 #include <sys/socket.h>
27 #if defined(__FreeBSD__)
28 #include <sys/types.h>
32 // Other libraries and framework includes
37 namespace lldb_private {
41 //----------------------------------------------------------------------------
42 // Static method to get all address information for a host and/or service
43 //----------------------------------------------------------------------------
44 static std::vector<SocketAddress>
45 GetAddressInfo(const char *hostname, const char *servname, int ai_family,
46 int ai_socktype, int ai_protocol, int ai_flags = 0);
48 //------------------------------------------------------------------
49 // Constructors and Destructors
50 //------------------------------------------------------------------
52 SocketAddress(const struct addrinfo *addr_info);
53 SocketAddress(const struct sockaddr &s);
54 SocketAddress(const struct sockaddr_in &s);
55 SocketAddress(const struct sockaddr_in6 &s);
56 SocketAddress(const struct sockaddr_storage &s);
57 SocketAddress(const SocketAddress &rhs);
60 //------------------------------------------------------------------
62 //------------------------------------------------------------------
63 const SocketAddress &operator=(const SocketAddress &rhs);
65 const SocketAddress &operator=(const struct addrinfo *addr_info);
67 const SocketAddress &operator=(const struct sockaddr &s);
69 const SocketAddress &operator=(const struct sockaddr_in &s);
71 const SocketAddress &operator=(const struct sockaddr_in6 &s);
73 const SocketAddress &operator=(const struct sockaddr_storage &s);
75 bool operator==(const SocketAddress &rhs) const;
76 bool operator!=(const SocketAddress &rhs) const;
78 //------------------------------------------------------------------
79 // Clear the contents of this socket address
80 //------------------------------------------------------------------
83 //------------------------------------------------------------------
84 // Get the length for the current socket address family
85 //------------------------------------------------------------------
86 socklen_t GetLength() const;
88 //------------------------------------------------------------------
89 // Get the max length for the largest socket address supported.
90 //------------------------------------------------------------------
91 static socklen_t GetMaxLength();
93 //------------------------------------------------------------------
94 // Get the socket address family
95 //------------------------------------------------------------------
96 sa_family_t GetFamily() const;
98 //------------------------------------------------------------------
99 // Set the socket address family
100 //------------------------------------------------------------------
101 void SetFamily(sa_family_t family);
103 //------------------------------------------------------------------
105 //------------------------------------------------------------------
106 std::string GetIPAddress() const;
108 //------------------------------------------------------------------
109 // Get the port if the socket address for the family has a port
110 //------------------------------------------------------------------
111 uint16_t GetPort() const;
113 //------------------------------------------------------------------
114 // Set the port if the socket address for the family has a port.
115 // The family must be set correctly prior to calling this function.
116 //------------------------------------------------------------------
117 bool SetPort(uint16_t port);
119 //------------------------------------------------------------------
120 // Set the socket address according to the first match from a call
121 // to getaddrinfo() (or equivalent functions for systems that don't
122 // have getaddrinfo(). If "addr_info_ptr" is not NULL, it will get
123 // filled in with the match that was used to populate this socket
125 //------------------------------------------------------------------
127 getaddrinfo(const char *host, // Hostname ("foo.bar.com" or "foo" or IP
128 // address string ("123.234.12.1" or
129 // "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
130 const char *service, // Protocol name ("tcp", "http", etc) or a
131 // raw port number string ("81")
132 int ai_family = PF_UNSPEC, int ai_socktype = 0,
133 int ai_protocol = 0, int ai_flags = 0);
135 //------------------------------------------------------------------
136 // Quick way to set the SocketAddress to localhost given the family.
137 // Returns true if successful, false if "family" doesn't support
138 // localhost or if "family" is not supported by this class.
139 //------------------------------------------------------------------
140 bool SetToLocalhost(sa_family_t family, uint16_t port);
142 bool SetToAnyAddress(sa_family_t family, uint16_t port);
144 //------------------------------------------------------------------
145 // Returns true if there is a valid socket address in this object.
146 //------------------------------------------------------------------
147 bool IsValid() const;
149 //------------------------------------------------------------------
150 // Returns true if the socket is INADDR_ANY
151 //------------------------------------------------------------------
152 bool IsAnyAddr() const;
154 //------------------------------------------------------------------
155 // Direct access to all of the sockaddr structures
156 //------------------------------------------------------------------
157 struct sockaddr &sockaddr() {
158 return m_socket_addr.sa;
161 const struct sockaddr &sockaddr() const { return m_socket_addr.sa; }
163 struct sockaddr_in &sockaddr_in() {
164 return m_socket_addr.sa_ipv4;
167 const struct sockaddr_in &sockaddr_in() const {
168 return m_socket_addr.sa_ipv4;
171 struct sockaddr_in6 &sockaddr_in6() {
172 return m_socket_addr.sa_ipv6;
175 const struct sockaddr_in6 &sockaddr_in6() const {
176 return m_socket_addr.sa_ipv6;
179 struct sockaddr_storage &sockaddr_storage() {
180 return m_socket_addr.sa_storage;
183 const struct sockaddr_storage &sockaddr_storage() const {
184 return m_socket_addr.sa_storage;
187 //------------------------------------------------------------------
188 // Conversion operators to allow getting the contents of this class
189 // as a pointer to the appropriate structure. This allows an instance
190 // of this class to be used in calls that take one of the sockaddr
191 // structure variants without having to manually use the correct
192 // accessor function.
193 //------------------------------------------------------------------
195 operator struct sockaddr *() { return &m_socket_addr.sa; }
197 operator const struct sockaddr *() const { return &m_socket_addr.sa; }
199 operator struct sockaddr_in *() { return &m_socket_addr.sa_ipv4; }
201 operator const struct sockaddr_in *() const { return &m_socket_addr.sa_ipv4; }
203 operator struct sockaddr_in6 *() { return &m_socket_addr.sa_ipv6; }
205 operator const struct sockaddr_in6 *() const {
206 return &m_socket_addr.sa_ipv6;
209 operator const struct sockaddr_storage *() const {
210 return &m_socket_addr.sa_storage;
213 operator struct sockaddr_storage *() { return &m_socket_addr.sa_storage; }
216 typedef union sockaddr_tag {
218 struct sockaddr_in sa_ipv4;
219 struct sockaddr_in6 sa_ipv6;
220 struct sockaddr_storage sa_storage;
223 //------------------------------------------------------------------
224 // Classes that inherit from SocketAddress can see and modify these
225 //------------------------------------------------------------------
226 sockaddr_t m_socket_addr;
229 } // namespace lldb_private
231 #endif // liblldb_SocketAddress_h_