1 //===-- SocketAddress.cpp ---------------------------------------*- 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 #if defined(_MSC_VER) 11 #define _WINSOCK_DEPRECATED_NO_WARNINGS 12 #endif 13 14 #include "lldb/Host/SocketAddress.h" 15 #include <stddef.h> 16 #include <stdio.h> 17 18 // C Includes 19 #if !defined(_WIN32) 20 #include <arpa/inet.h> 21 #endif 22 23 #include <assert.h> 24 #include <string.h> 25 26 // C++ Includes 27 // Other libraries and framework includes 28 // Project includes 29 #include "lldb/Host/PosixApi.h" 30 31 // WindowsXP needs an inet_ntop implementation 32 #ifdef _WIN32 33 34 #ifndef INET6_ADDRSTRLEN // might not be defined in older Windows SDKs 35 #define INET6_ADDRSTRLEN 46 36 #endif 37 38 // TODO: implement shortened form "::" for runs of zeros 39 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) { 40 if (size == 0) { 41 return nullptr; 42 } 43 44 switch (af) { 45 case AF_INET: { 46 { 47 const char *formatted = inet_ntoa(*static_cast<const in_addr *>(src)); 48 if (formatted && strlen(formatted) < static_cast<size_t>(size)) { 49 return ::strcpy(dst, formatted); 50 } 51 } 52 return nullptr; 53 case AF_INET6: { 54 char tmp[INET6_ADDRSTRLEN] = {0}; 55 const uint16_t *src16 = static_cast<const uint16_t *>(src); 56 int full_size = ::snprintf( 57 tmp, sizeof(tmp), "%x:%x:%x:%x:%x:%x:%x:%x", ntohs(src16[0]), 58 ntohs(src16[1]), ntohs(src16[2]), ntohs(src16[3]), ntohs(src16[4]), 59 ntohs(src16[5]), ntohs(src16[6]), ntohs(src16[7])); 60 if (full_size < static_cast<int>(size)) { 61 return ::strcpy(dst, tmp); 62 } 63 return nullptr; 64 } 65 } 66 } 67 return nullptr; 68 } 69 #endif 70 71 using namespace lldb_private; 72 73 //---------------------------------------------------------------------- 74 // SocketAddress constructor 75 //---------------------------------------------------------------------- 76 SocketAddress::SocketAddress() { Clear(); } 77 78 SocketAddress::SocketAddress(const struct sockaddr &s) { m_socket_addr.sa = s; } 79 80 SocketAddress::SocketAddress(const struct sockaddr_in &s) { 81 m_socket_addr.sa_ipv4 = s; 82 } 83 84 SocketAddress::SocketAddress(const struct sockaddr_in6 &s) { 85 m_socket_addr.sa_ipv6 = s; 86 } 87 88 SocketAddress::SocketAddress(const struct sockaddr_storage &s) { 89 m_socket_addr.sa_storage = s; 90 } 91 92 SocketAddress::SocketAddress(const struct addrinfo *addr_info) { 93 *this = addr_info; 94 } 95 96 //---------------------------------------------------------------------- 97 // SocketAddress copy constructor 98 //---------------------------------------------------------------------- 99 SocketAddress::SocketAddress(const SocketAddress &rhs) 100 : m_socket_addr(rhs.m_socket_addr) {} 101 102 //---------------------------------------------------------------------- 103 // Destructor 104 //---------------------------------------------------------------------- 105 SocketAddress::~SocketAddress() {} 106 107 void SocketAddress::Clear() { 108 memset(&m_socket_addr, 0, sizeof(m_socket_addr)); 109 } 110 111 bool SocketAddress::IsValid() const { return GetLength() != 0; } 112 113 static socklen_t GetFamilyLength(sa_family_t family) { 114 switch (family) { 115 case AF_INET: 116 return sizeof(struct sockaddr_in); 117 case AF_INET6: 118 return sizeof(struct sockaddr_in6); 119 } 120 assert(0 && "Unsupported address family"); 121 return 0; 122 } 123 124 socklen_t SocketAddress::GetLength() const { 125 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) 126 return m_socket_addr.sa.sa_len; 127 #else 128 return GetFamilyLength(GetFamily()); 129 #endif 130 } 131 132 socklen_t SocketAddress::GetMaxLength() { return sizeof(sockaddr_t); } 133 134 sa_family_t SocketAddress::GetFamily() const { 135 return m_socket_addr.sa.sa_family; 136 } 137 138 void SocketAddress::SetFamily(sa_family_t family) { 139 m_socket_addr.sa.sa_family = family; 140 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) 141 m_socket_addr.sa.sa_len = GetFamilyLength(family); 142 #endif 143 } 144 145 std::string SocketAddress::GetIPAddress() const { 146 char str[INET6_ADDRSTRLEN] = {0}; 147 switch (GetFamily()) { 148 case AF_INET: 149 if (inet_ntop(GetFamily(), &m_socket_addr.sa_ipv4.sin_addr, str, 150 sizeof(str))) 151 return str; 152 break; 153 case AF_INET6: 154 if (inet_ntop(GetFamily(), &m_socket_addr.sa_ipv6.sin6_addr, str, 155 sizeof(str))) 156 return str; 157 break; 158 } 159 return ""; 160 } 161 162 uint16_t SocketAddress::GetPort() const { 163 switch (GetFamily()) { 164 case AF_INET: 165 return ntohs(m_socket_addr.sa_ipv4.sin_port); 166 case AF_INET6: 167 return ntohs(m_socket_addr.sa_ipv6.sin6_port); 168 } 169 return 0; 170 } 171 172 bool SocketAddress::SetPort(uint16_t port) { 173 switch (GetFamily()) { 174 case AF_INET: 175 m_socket_addr.sa_ipv4.sin_port = htons(port); 176 return true; 177 178 case AF_INET6: 179 m_socket_addr.sa_ipv6.sin6_port = htons(port); 180 return true; 181 } 182 return false; 183 } 184 185 //---------------------------------------------------------------------- 186 // SocketAddress assignment operator 187 //---------------------------------------------------------------------- 188 const SocketAddress &SocketAddress::operator=(const SocketAddress &rhs) { 189 if (this != &rhs) 190 m_socket_addr = rhs.m_socket_addr; 191 return *this; 192 } 193 194 const SocketAddress &SocketAddress:: 195 operator=(const struct addrinfo *addr_info) { 196 Clear(); 197 if (addr_info && addr_info->ai_addr && addr_info->ai_addrlen > 0 && 198 addr_info->ai_addrlen <= sizeof m_socket_addr) { 199 ::memcpy(&m_socket_addr, addr_info->ai_addr, addr_info->ai_addrlen); 200 } 201 return *this; 202 } 203 204 const SocketAddress &SocketAddress::operator=(const struct sockaddr &s) { 205 m_socket_addr.sa = s; 206 return *this; 207 } 208 209 const SocketAddress &SocketAddress::operator=(const struct sockaddr_in &s) { 210 m_socket_addr.sa_ipv4 = s; 211 return *this; 212 } 213 214 const SocketAddress &SocketAddress::operator=(const struct sockaddr_in6 &s) { 215 m_socket_addr.sa_ipv6 = s; 216 return *this; 217 } 218 219 const SocketAddress &SocketAddress:: 220 operator=(const struct sockaddr_storage &s) { 221 m_socket_addr.sa_storage = s; 222 return *this; 223 } 224 225 bool SocketAddress::getaddrinfo(const char *host, const char *service, 226 int ai_family, int ai_socktype, int ai_protocol, 227 int ai_flags) { 228 Clear(); 229 230 auto addresses = GetAddressInfo(host, service, ai_family, ai_socktype, 231 ai_protocol, ai_flags); 232 if (!addresses.empty()) 233 *this = addresses[0]; 234 return IsValid(); 235 } 236 237 std::vector<SocketAddress> 238 SocketAddress::GetAddressInfo(const char *hostname, const char *servname, 239 int ai_family, int ai_socktype, int ai_protocol, 240 int ai_flags) { 241 std::vector<SocketAddress> addr_list; 242 243 struct addrinfo hints; 244 memset(&hints, 0, sizeof(hints)); 245 hints.ai_family = ai_family; 246 hints.ai_socktype = ai_socktype; 247 hints.ai_protocol = ai_protocol; 248 hints.ai_flags = ai_flags; 249 250 struct addrinfo *service_info_list = NULL; 251 int err = ::getaddrinfo(hostname, servname, &hints, &service_info_list); 252 if (err == 0 && service_info_list) { 253 for (struct addrinfo *service_ptr = service_info_list; service_ptr != NULL; 254 service_ptr = service_ptr->ai_next) { 255 addr_list.emplace_back(SocketAddress(service_ptr)); 256 } 257 } 258 259 if (service_info_list) 260 ::freeaddrinfo(service_info_list); 261 return addr_list; 262 } 263 264 bool SocketAddress::SetToLocalhost(sa_family_t family, uint16_t port) { 265 switch (family) { 266 case AF_INET: 267 SetFamily(AF_INET); 268 if (SetPort(port)) { 269 m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 270 return true; 271 } 272 break; 273 274 case AF_INET6: 275 SetFamily(AF_INET6); 276 if (SetPort(port)) { 277 m_socket_addr.sa_ipv6.sin6_addr = in6addr_loopback; 278 return true; 279 } 280 break; 281 } 282 Clear(); 283 return false; 284 } 285 286 bool SocketAddress::SetToAnyAddress(sa_family_t family, uint16_t port) { 287 switch (family) { 288 case AF_INET: 289 SetFamily(AF_INET); 290 if (SetPort(port)) { 291 m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_ANY); 292 return true; 293 } 294 break; 295 296 case AF_INET6: 297 SetFamily(AF_INET6); 298 if (SetPort(port)) { 299 m_socket_addr.sa_ipv6.sin6_addr = in6addr_any; 300 return true; 301 } 302 break; 303 } 304 Clear(); 305 return false; 306 } 307 308 bool SocketAddress::IsAnyAddr() const { 309 return (GetFamily() == AF_INET) 310 ? m_socket_addr.sa_ipv4.sin_addr.s_addr == htonl(INADDR_ANY) 311 : 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, &in6addr_any, 16); 312 } 313 314 bool SocketAddress::operator==(const SocketAddress &rhs) const { 315 if (GetFamily() != rhs.GetFamily()) 316 return false; 317 if (GetLength() != rhs.GetLength()) 318 return false; 319 switch (GetFamily()) { 320 case AF_INET: 321 return m_socket_addr.sa_ipv4.sin_addr.s_addr == 322 rhs.m_socket_addr.sa_ipv4.sin_addr.s_addr; 323 case AF_INET6: 324 return 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, 325 &rhs.m_socket_addr.sa_ipv6.sin6_addr, 16); 326 } 327 return false; 328 } 329 330 bool SocketAddress::operator!=(const SocketAddress &rhs) const { 331 return !(*this == rhs); 332 } 333