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> GetAddressInfo(const char *hostname,
45 const char *servname);
47 //------------------------------------------------------------------
48 // Constructors and Destructors
49 //------------------------------------------------------------------
51 SocketAddress(const struct addrinfo *addr_info);
52 SocketAddress(const struct sockaddr &s);
53 SocketAddress(const struct sockaddr_in &s);
54 SocketAddress(const struct sockaddr_in6 &s);
55 SocketAddress(const struct sockaddr_storage &s);
56 SocketAddress(const SocketAddress &rhs);
59 //------------------------------------------------------------------
61 //------------------------------------------------------------------
62 const SocketAddress &operator=(const SocketAddress &rhs);
64 const SocketAddress &operator=(const struct addrinfo *addr_info);
66 const SocketAddress &operator=(const struct sockaddr &s);
68 const SocketAddress &operator=(const struct sockaddr_in &s);
70 const SocketAddress &operator=(const struct sockaddr_in6 &s);
72 const SocketAddress &operator=(const struct sockaddr_storage &s);
74 bool operator==(const SocketAddress &rhs) const;
75 bool operator!=(const SocketAddress &rhs) const;
77 //------------------------------------------------------------------
78 // Clear the contents of this socket address
79 //------------------------------------------------------------------
82 //------------------------------------------------------------------
83 // Get the length for the current socket address family
84 //------------------------------------------------------------------
85 socklen_t GetLength() const;
87 //------------------------------------------------------------------
88 // Get the max length for the largest socket address supported.
89 //------------------------------------------------------------------
90 static socklen_t GetMaxLength();
92 //------------------------------------------------------------------
93 // Get the socket address family
94 //------------------------------------------------------------------
95 sa_family_t GetFamily() const;
97 //------------------------------------------------------------------
98 // Set the socket address family
99 //------------------------------------------------------------------
100 void SetFamily(sa_family_t family);
102 //------------------------------------------------------------------
104 //------------------------------------------------------------------
105 std::string GetIPAddress() const;
107 //------------------------------------------------------------------
108 // Get the port if the socket address for the family has a port
109 //------------------------------------------------------------------
110 uint16_t GetPort() const;
112 //------------------------------------------------------------------
113 // Set the port if the socket address for the family has a port.
114 // The family must be set correctly prior to calling this function.
115 //------------------------------------------------------------------
116 bool SetPort(uint16_t port);
118 //------------------------------------------------------------------
119 // Set the socket address according to the first match from a call
120 // to getaddrinfo() (or equivalent functions for systems that don't
121 // have getaddrinfo(). If "addr_info_ptr" is not NULL, it will get
122 // filled in with the match that was used to populate this socket
124 //------------------------------------------------------------------
126 getaddrinfo(const char *host, // Hostname ("foo.bar.com" or "foo" or IP
127 // address string ("123.234.12.1" or
128 // "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
129 const char *service, // Protocol name ("tcp", "http", etc) or a
130 // raw port number string ("81")
131 int ai_family = PF_UNSPEC, int ai_socktype = 0,
132 int ai_protocol = 0, int ai_flags = 0);
134 //------------------------------------------------------------------
135 // Quick way to set the SocketAddress to localhost given the family.
136 // Returns true if successful, false if "family" doesn't support
137 // localhost or if "family" is not supported by this class.
138 //------------------------------------------------------------------
139 bool SetToLocalhost(sa_family_t family, uint16_t port);
141 bool SetToAnyAddress(sa_family_t family, uint16_t port);
143 //------------------------------------------------------------------
144 // Returns true if there is a valid socket address in this object.
145 //------------------------------------------------------------------
146 bool IsValid() const;
148 //------------------------------------------------------------------
149 // Returns true if the socket is INADDR_ANY
150 //------------------------------------------------------------------
151 bool IsAnyAddr() const;
153 //------------------------------------------------------------------
154 // Direct access to all of the sockaddr structures
155 //------------------------------------------------------------------
156 struct sockaddr &sockaddr() {
157 return m_socket_addr.sa;
160 const struct sockaddr &sockaddr() const { return m_socket_addr.sa; }
162 struct sockaddr_in &sockaddr_in() {
163 return m_socket_addr.sa_ipv4;
166 const struct sockaddr_in &sockaddr_in() const {
167 return m_socket_addr.sa_ipv4;
170 struct sockaddr_in6 &sockaddr_in6() {
171 return m_socket_addr.sa_ipv6;
174 const struct sockaddr_in6 &sockaddr_in6() const {
175 return m_socket_addr.sa_ipv6;
178 struct sockaddr_storage &sockaddr_storage() {
179 return m_socket_addr.sa_storage;
182 const struct sockaddr_storage &sockaddr_storage() const {
183 return m_socket_addr.sa_storage;
186 //------------------------------------------------------------------
187 // Conversion operators to allow getting the contents of this class
188 // as a pointer to the appropriate structure. This allows an instance
189 // of this class to be used in calls that take one of the sockaddr
190 // structure variants without having to manually use the correct
191 // accessor function.
192 //------------------------------------------------------------------
194 operator struct sockaddr *() { return &m_socket_addr.sa; }
196 operator const struct sockaddr *() const { return &m_socket_addr.sa; }
198 operator struct sockaddr_in *() { return &m_socket_addr.sa_ipv4; }
200 operator const struct sockaddr_in *() const { return &m_socket_addr.sa_ipv4; }
202 operator struct sockaddr_in6 *() { return &m_socket_addr.sa_ipv6; }
204 operator const struct sockaddr_in6 *() const {
205 return &m_socket_addr.sa_ipv6;
208 operator const struct sockaddr_storage *() const {
209 return &m_socket_addr.sa_storage;
212 operator struct sockaddr_storage *() { return &m_socket_addr.sa_storage; }
215 typedef union sockaddr_tag {
217 struct sockaddr_in sa_ipv4;
218 struct sockaddr_in6 sa_ipv6;
219 struct sockaddr_storage sa_storage;
222 //------------------------------------------------------------------
223 // Classes that inherit from SocketAddress can see and modify these
224 //------------------------------------------------------------------
225 sockaddr_t m_socket_addr;
228 } // namespace lldb_private
230 #endif // liblldb_SocketAddress_h_