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