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
36 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 //------------------------------------------------------------------
54 const SocketAddress &operator=(const SocketAddress &rhs);
56 const SocketAddress &operator=(const struct addrinfo *addr_info);
58 const SocketAddress &operator=(const struct sockaddr &s);
60 const SocketAddress &operator=(const struct sockaddr_in &s);
62 const SocketAddress &operator=(const struct sockaddr_in6 &s);
64 const SocketAddress &operator=(const struct sockaddr_storage &s);
66 //------------------------------------------------------------------
67 // Clear the contents of this socket address
68 //------------------------------------------------------------------
71 //------------------------------------------------------------------
72 // Get the length for the current socket address family
73 //------------------------------------------------------------------
74 socklen_t GetLength() const;
76 //------------------------------------------------------------------
77 // Get the max length for the largest socket address supported.
78 //------------------------------------------------------------------
79 static socklen_t GetMaxLength();
81 //------------------------------------------------------------------
82 // Get the socket address family
83 //------------------------------------------------------------------
84 sa_family_t GetFamily() const;
86 //------------------------------------------------------------------
87 // Set the socket address family
88 //------------------------------------------------------------------
89 void SetFamily(sa_family_t family);
91 //------------------------------------------------------------------
93 //------------------------------------------------------------------
94 std::string GetIPAddress() const;
96 //------------------------------------------------------------------
97 // Get the port if the socket address for the family has a port
98 //------------------------------------------------------------------
99 uint16_t GetPort() const;
101 //------------------------------------------------------------------
102 // Set the port if the socket address for the family has a port.
103 // The family must be set correctly prior to calling this function.
104 //------------------------------------------------------------------
105 bool SetPort(uint16_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 getaddrinfo(const char *host, // Hostname ("foo.bar.com" or "foo" or IP
116 // address string ("123.234.12.1" or
117 // "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
118 const char *service, // Protocol name ("tcp", "http", etc) or a
119 // raw port number string ("81")
120 int ai_family = PF_UNSPEC, int ai_socktype = 0,
121 int ai_protocol = 0, int ai_flags = 0);
123 //------------------------------------------------------------------
124 // Quick way to set the SocketAddress to localhost given the family.
125 // Returns true if successful, false if "family" doesn't support
126 // localhost or if "family" is not supported by this class.
127 //------------------------------------------------------------------
128 bool SetToLocalhost(sa_family_t family, uint16_t port);
130 bool SetToAnyAddress(sa_family_t family, uint16_t port);
132 //------------------------------------------------------------------
133 // Returns true if there is a valid socket address in this object.
134 //------------------------------------------------------------------
135 bool IsValid() const;
137 //------------------------------------------------------------------
138 // Direct access to all of the sockaddr structures
139 //------------------------------------------------------------------
140 struct sockaddr &sockaddr() {
141 return m_socket_addr.sa;
144 const struct sockaddr &sockaddr() const { return m_socket_addr.sa; }
146 struct sockaddr_in &sockaddr_in() {
147 return m_socket_addr.sa_ipv4;
150 const struct sockaddr_in &sockaddr_in() const {
151 return m_socket_addr.sa_ipv4;
154 struct sockaddr_in6 &sockaddr_in6() {
155 return m_socket_addr.sa_ipv6;
158 const struct sockaddr_in6 &sockaddr_in6() const {
159 return m_socket_addr.sa_ipv6;
162 struct sockaddr_storage &sockaddr_storage() {
163 return m_socket_addr.sa_storage;
166 const struct sockaddr_storage &sockaddr_storage() const {
167 return m_socket_addr.sa_storage;
170 //------------------------------------------------------------------
171 // Conversion operators to allow getting the contents of this class
172 // as a pointer to the appropriate structure. This allows an instance
173 // of this class to be used in calls that take one of the sockaddr
174 // structure variants without having to manually use the correct
175 // accessor function.
176 //------------------------------------------------------------------
178 operator struct sockaddr *() { return &m_socket_addr.sa; }
180 operator const struct sockaddr *() const { return &m_socket_addr.sa; }
182 operator struct sockaddr_in *() { return &m_socket_addr.sa_ipv4; }
184 operator const struct sockaddr_in *() const { return &m_socket_addr.sa_ipv4; }
186 operator struct sockaddr_in6 *() { return &m_socket_addr.sa_ipv6; }
188 operator const struct sockaddr_in6 *() const {
189 return &m_socket_addr.sa_ipv6;
192 operator const struct sockaddr_storage *() const {
193 return &m_socket_addr.sa_storage;
196 operator struct sockaddr_storage *() { return &m_socket_addr.sa_storage; }
199 typedef union sockaddr_tag {
201 struct sockaddr_in sa_ipv4;
202 struct sockaddr_in6 sa_ipv6;
203 struct sockaddr_storage sa_storage;
206 //------------------------------------------------------------------
207 // Classes that inherit from SocketAddress can see and modify these
208 //------------------------------------------------------------------
209 sockaddr_t m_socket_addr;
212 } // namespace lldb_private
214 #endif // liblldb_SocketAddress_h_