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, ai_protocol, ai_flags); 231 if (!addresses.empty()) 232 *this = addresses[0]; 233 return IsValid(); 234 } 235 236 std::vector<SocketAddress> 237 SocketAddress::GetAddressInfo(const char *hostname, const char *servname, 238 int ai_family, int ai_socktype, int ai_protocol, 239 int ai_flags) { 240 std::vector<SocketAddress> addr_list; 241 242 struct addrinfo hints; 243 memset(&hints, 0, sizeof(hints)); 244 hints.ai_family = ai_family; 245 hints.ai_socktype = ai_socktype; 246 hints.ai_protocol = ai_protocol; 247 hints.ai_flags = ai_flags; 248 249 struct addrinfo *service_info_list = NULL; 250 int err = ::getaddrinfo(hostname, servname, &hints, &service_info_list); 251 if (err == 0 && service_info_list) { 252 for (struct addrinfo *service_ptr = service_info_list; service_ptr != NULL; 253 service_ptr = service_ptr->ai_next) { 254 addr_list.emplace_back(SocketAddress(service_ptr)); 255 } 256 } 257 258 if (service_info_list) 259 ::freeaddrinfo(service_info_list); 260 return addr_list; 261 } 262 263 bool SocketAddress::SetToLocalhost(sa_family_t family, uint16_t port) { 264 switch (family) { 265 case AF_INET: 266 SetFamily(AF_INET); 267 if (SetPort(port)) { 268 m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 269 return true; 270 } 271 break; 272 273 case AF_INET6: 274 SetFamily(AF_INET6); 275 if (SetPort(port)) { 276 m_socket_addr.sa_ipv6.sin6_addr = in6addr_loopback; 277 return true; 278 } 279 break; 280 } 281 Clear(); 282 return false; 283 } 284 285 bool SocketAddress::SetToAnyAddress(sa_family_t family, uint16_t port) { 286 switch (family) { 287 case AF_INET: 288 SetFamily(AF_INET); 289 if (SetPort(port)) { 290 m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_ANY); 291 return true; 292 } 293 break; 294 295 case AF_INET6: 296 SetFamily(AF_INET6); 297 if (SetPort(port)) { 298 m_socket_addr.sa_ipv6.sin6_addr = in6addr_any; 299 return true; 300 } 301 break; 302 } 303 Clear(); 304 return false; 305 } 306 307 bool SocketAddress::IsAnyAddr() const { 308 return (GetFamily() == AF_INET) 309 ? m_socket_addr.sa_ipv4.sin_addr.s_addr == htonl(INADDR_ANY) 310 : 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, &in6addr_any, 16); 311 } 312 313 bool SocketAddress::operator==(const SocketAddress &rhs) const { 314 if (GetFamily() != rhs.GetFamily()) 315 return false; 316 if (GetLength() != rhs.GetLength()) 317 return false; 318 switch (GetFamily()) { 319 case AF_INET: 320 return m_socket_addr.sa_ipv4.sin_addr.s_addr == 321 rhs.m_socket_addr.sa_ipv4.sin_addr.s_addr; 322 case AF_INET6: 323 return 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, 324 &rhs.m_socket_addr.sa_ipv6.sin6_addr, 16); 325 } 326 return false; 327 } 328 329 bool SocketAddress::operator!=(const SocketAddress &rhs) const { 330 return !(*this == rhs); 331 } 332