1 //===-- SocketAddress.h -----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_SocketAddress_h_ 11 #define liblldb_SocketAddress_h_ 12 13 #include <stdint.h> 14 15 #ifdef _WIN32 16 #include "lldb/Host/windows/windows.h" 17 #include <winsock2.h> 18 #include <ws2tcpip.h> 19 typedef ADDRESS_FAMILY sa_family_t; 20 #else 21 #include <netdb.h> 22 #include <netinet/in.h> 23 #include <sys/socket.h> 24 #endif 25 26 #if defined(__FreeBSD__) 27 #include <sys/types.h> 28 #endif 29 30 #include <string> 31 #include <vector> 32 33 namespace lldb_private { 34 35 class SocketAddress { 36 public: 37 //---------------------------------------------------------------------------- 38 // Static method to get all address information for a host and/or service 39 //---------------------------------------------------------------------------- 40 static std::vector<SocketAddress> 41 GetAddressInfo(const char *hostname, const char *servname, int ai_family, 42 int ai_socktype, int ai_protocol, int ai_flags = 0); 43 44 //------------------------------------------------------------------ 45 // Constructors and Destructors 46 //------------------------------------------------------------------ 47 SocketAddress(); 48 SocketAddress(const struct addrinfo *addr_info); 49 SocketAddress(const struct sockaddr &s); 50 SocketAddress(const struct sockaddr_in &s); 51 SocketAddress(const struct sockaddr_in6 &s); 52 SocketAddress(const struct sockaddr_storage &s); 53 SocketAddress(const SocketAddress &rhs); 54 ~SocketAddress(); 55 56 //------------------------------------------------------------------ 57 // Operators 58 //------------------------------------------------------------------ 59 const SocketAddress &operator=(const SocketAddress &rhs); 60 61 const SocketAddress &operator=(const struct addrinfo *addr_info); 62 63 const SocketAddress &operator=(const struct sockaddr &s); 64 65 const SocketAddress &operator=(const struct sockaddr_in &s); 66 67 const SocketAddress &operator=(const struct sockaddr_in6 &s); 68 69 const SocketAddress &operator=(const struct sockaddr_storage &s); 70 71 bool operator==(const SocketAddress &rhs) const; 72 bool operator!=(const SocketAddress &rhs) const; 73 74 //------------------------------------------------------------------ 75 // Clear the contents of this socket address 76 //------------------------------------------------------------------ 77 void Clear(); 78 79 //------------------------------------------------------------------ 80 // Get the length for the current socket address family 81 //------------------------------------------------------------------ 82 socklen_t GetLength() const; 83 84 //------------------------------------------------------------------ 85 // Get the max length for the largest socket address supported. 86 //------------------------------------------------------------------ 87 static socklen_t GetMaxLength(); 88 89 //------------------------------------------------------------------ 90 // Get the socket address family 91 //------------------------------------------------------------------ 92 sa_family_t GetFamily() const; 93 94 //------------------------------------------------------------------ 95 // Set the socket address family 96 //------------------------------------------------------------------ 97 void SetFamily(sa_family_t family); 98 99 //------------------------------------------------------------------ 100 // Get the address 101 //------------------------------------------------------------------ 102 std::string GetIPAddress() const; 103 104 //------------------------------------------------------------------ 105 // Get the port if the socket address for the family has a port 106 //------------------------------------------------------------------ 107 uint16_t GetPort() const; 108 109 //------------------------------------------------------------------ 110 // Set the port if the socket address for the family has a port. The family 111 // must be set correctly prior to calling this function. 112 //------------------------------------------------------------------ 113 bool SetPort(uint16_t port); 114 115 //------------------------------------------------------------------ 116 // Set the socket address according to the first match from a call to 117 // getaddrinfo() (or equivalent functions for systems that don't have 118 // getaddrinfo(). If "addr_info_ptr" is not NULL, it will get filled in with 119 // the match that was used to populate this socket address. 120 //------------------------------------------------------------------ 121 bool 122 getaddrinfo(const char *host, // Hostname ("foo.bar.com" or "foo" or IP 123 // address string ("123.234.12.1" or 124 // "2001:0db8:85a3:0000:0000:8a2e:0370:7334") 125 const char *service, // Protocol name ("tcp", "http", etc) or a 126 // raw port number string ("81") 127 int ai_family = PF_UNSPEC, int ai_socktype = 0, 128 int ai_protocol = 0, int ai_flags = 0); 129 130 //------------------------------------------------------------------ 131 // Quick way to set the SocketAddress to localhost given the family. Returns 132 // true if successful, false if "family" doesn't support localhost or if 133 // "family" is not supported by this class. 134 //------------------------------------------------------------------ 135 bool SetToLocalhost(sa_family_t family, uint16_t port); 136 137 bool SetToAnyAddress(sa_family_t family, uint16_t port); 138 139 //------------------------------------------------------------------ 140 // Returns true if there is a valid socket address in this object. 141 //------------------------------------------------------------------ 142 bool IsValid() const; 143 144 //------------------------------------------------------------------ 145 // Returns true if the socket is INADDR_ANY 146 //------------------------------------------------------------------ 147 bool IsAnyAddr() const; 148 149 //------------------------------------------------------------------ 150 // Returns true if the socket is INADDR_LOOPBACK 151 //------------------------------------------------------------------ 152 bool IsLocalhost() const; 153 154 //------------------------------------------------------------------ 155 // Direct access to all of the sockaddr structures 156 //------------------------------------------------------------------ sockaddr()157 struct sockaddr &sockaddr() { 158 return m_socket_addr.sa; 159 } 160 sockaddr()161 const struct sockaddr &sockaddr() const { return m_socket_addr.sa; } 162 sockaddr_in()163 struct sockaddr_in &sockaddr_in() { 164 return m_socket_addr.sa_ipv4; 165 } 166 sockaddr_in()167 const struct sockaddr_in &sockaddr_in() const { 168 return m_socket_addr.sa_ipv4; 169 } 170 sockaddr_in6()171 struct sockaddr_in6 &sockaddr_in6() { 172 return m_socket_addr.sa_ipv6; 173 } 174 sockaddr_in6()175 const struct sockaddr_in6 &sockaddr_in6() const { 176 return m_socket_addr.sa_ipv6; 177 } 178 sockaddr_storage()179 struct sockaddr_storage &sockaddr_storage() { 180 return m_socket_addr.sa_storage; 181 } 182 sockaddr_storage()183 const struct sockaddr_storage &sockaddr_storage() const { 184 return m_socket_addr.sa_storage; 185 } 186 187 //------------------------------------------------------------------ 188 // Conversion operators to allow getting the contents of this class as a 189 // pointer to the appropriate structure. This allows an instance of this 190 // class to be used in calls that take one of the sockaddr structure variants 191 // without having to manually use the correct accessor function. 192 //------------------------------------------------------------------ 193 194 operator struct sockaddr *() { return &m_socket_addr.sa; } 195 196 operator const struct sockaddr *() const { return &m_socket_addr.sa; } 197 198 operator struct sockaddr_in *() { return &m_socket_addr.sa_ipv4; } 199 200 operator const struct sockaddr_in *() const { return &m_socket_addr.sa_ipv4; } 201 202 operator struct sockaddr_in6 *() { return &m_socket_addr.sa_ipv6; } 203 204 operator const struct sockaddr_in6 *() const { 205 return &m_socket_addr.sa_ipv6; 206 } 207 208 operator const struct sockaddr_storage *() const { 209 return &m_socket_addr.sa_storage; 210 } 211 212 operator struct sockaddr_storage *() { return &m_socket_addr.sa_storage; } 213 214 protected: 215 typedef union sockaddr_tag { 216 struct sockaddr sa; 217 struct sockaddr_in sa_ipv4; 218 struct sockaddr_in6 sa_ipv6; 219 struct sockaddr_storage sa_storage; 220 } sockaddr_t; 221 222 //------------------------------------------------------------------ 223 // Classes that inherit from SocketAddress can see and modify these 224 //------------------------------------------------------------------ 225 sockaddr_t m_socket_addr; 226 }; 227 228 } // namespace lldb_private 229 230 #endif // liblldb_SocketAddress_h_ 231