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 // Destructor
96 SocketAddress::~SocketAddress() {}
97 
98 void SocketAddress::Clear() {
99   memset(&m_socket_addr, 0, sizeof(m_socket_addr));
100 }
101 
102 bool SocketAddress::IsValid() const { return GetLength() != 0; }
103 
104 static socklen_t GetFamilyLength(sa_family_t family) {
105   switch (family) {
106   case AF_INET:
107     return sizeof(struct sockaddr_in);
108   case AF_INET6:
109     return sizeof(struct sockaddr_in6);
110   }
111   assert(0 && "Unsupported address family");
112   return 0;
113 }
114 
115 socklen_t SocketAddress::GetLength() const {
116 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
117   return m_socket_addr.sa.sa_len;
118 #else
119   return GetFamilyLength(GetFamily());
120 #endif
121 }
122 
123 socklen_t SocketAddress::GetMaxLength() { return sizeof(sockaddr_t); }
124 
125 sa_family_t SocketAddress::GetFamily() const {
126   return m_socket_addr.sa.sa_family;
127 }
128 
129 void SocketAddress::SetFamily(sa_family_t family) {
130   m_socket_addr.sa.sa_family = family;
131 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
132   m_socket_addr.sa.sa_len = GetFamilyLength(family);
133 #endif
134 }
135 
136 std::string SocketAddress::GetIPAddress() const {
137   char str[INET6_ADDRSTRLEN] = {0};
138   switch (GetFamily()) {
139   case AF_INET:
140     if (inet_ntop(GetFamily(), &m_socket_addr.sa_ipv4.sin_addr, str,
141                   sizeof(str)))
142       return str;
143     break;
144   case AF_INET6:
145     if (inet_ntop(GetFamily(), &m_socket_addr.sa_ipv6.sin6_addr, str,
146                   sizeof(str)))
147       return str;
148     break;
149   }
150   return "";
151 }
152 
153 uint16_t SocketAddress::GetPort() const {
154   switch (GetFamily()) {
155   case AF_INET:
156     return ntohs(m_socket_addr.sa_ipv4.sin_port);
157   case AF_INET6:
158     return ntohs(m_socket_addr.sa_ipv6.sin6_port);
159   }
160   return 0;
161 }
162 
163 bool SocketAddress::SetPort(uint16_t port) {
164   switch (GetFamily()) {
165   case AF_INET:
166     m_socket_addr.sa_ipv4.sin_port = htons(port);
167     return true;
168 
169   case AF_INET6:
170     m_socket_addr.sa_ipv6.sin6_port = htons(port);
171     return true;
172   }
173   return false;
174 }
175 
176 // SocketAddress assignment operator
177 const SocketAddress &SocketAddress::operator=(const SocketAddress &rhs) {
178   if (this != &rhs)
179     m_socket_addr = rhs.m_socket_addr;
180   return *this;
181 }
182 
183 const SocketAddress &SocketAddress::
184 operator=(const struct addrinfo *addr_info) {
185   Clear();
186   if (addr_info && addr_info->ai_addr && addr_info->ai_addrlen > 0 &&
187       size_t(addr_info->ai_addrlen) <= sizeof m_socket_addr) {
188     ::memcpy(&m_socket_addr, addr_info->ai_addr, addr_info->ai_addrlen);
189   }
190   return *this;
191 }
192 
193 const SocketAddress &SocketAddress::operator=(const struct sockaddr &s) {
194   m_socket_addr.sa = s;
195   return *this;
196 }
197 
198 const SocketAddress &SocketAddress::operator=(const struct sockaddr_in &s) {
199   m_socket_addr.sa_ipv4 = s;
200   return *this;
201 }
202 
203 const SocketAddress &SocketAddress::operator=(const struct sockaddr_in6 &s) {
204   m_socket_addr.sa_ipv6 = s;
205   return *this;
206 }
207 
208 const SocketAddress &SocketAddress::
209 operator=(const struct sockaddr_storage &s) {
210   m_socket_addr.sa_storage = s;
211   return *this;
212 }
213 
214 bool SocketAddress::getaddrinfo(const char *host, const char *service,
215                                 int ai_family, int ai_socktype, int ai_protocol,
216                                 int ai_flags) {
217   Clear();
218 
219   auto addresses = GetAddressInfo(host, service, ai_family, ai_socktype,
220                                   ai_protocol, ai_flags);
221   if (!addresses.empty())
222     *this = addresses[0];
223   return IsValid();
224 }
225 
226 std::vector<SocketAddress>
227 SocketAddress::GetAddressInfo(const char *hostname, const char *servname,
228                               int ai_family, int ai_socktype, int ai_protocol,
229                               int ai_flags) {
230   std::vector<SocketAddress> addr_list;
231 
232   struct addrinfo hints;
233   memset(&hints, 0, sizeof(hints));
234   hints.ai_family = ai_family;
235   hints.ai_socktype = ai_socktype;
236   hints.ai_protocol = ai_protocol;
237   hints.ai_flags = ai_flags;
238 
239   struct addrinfo *service_info_list = nullptr;
240   int err = ::getaddrinfo(hostname, servname, &hints, &service_info_list);
241   if (err == 0 && service_info_list) {
242     for (struct addrinfo *service_ptr = service_info_list;
243          service_ptr != nullptr; service_ptr = service_ptr->ai_next) {
244       addr_list.emplace_back(SocketAddress(service_ptr));
245     }
246   }
247 
248   if (service_info_list)
249     ::freeaddrinfo(service_info_list);
250   return addr_list;
251 }
252 
253 bool SocketAddress::SetToLocalhost(sa_family_t family, uint16_t port) {
254   switch (family) {
255   case AF_INET:
256     SetFamily(AF_INET);
257     if (SetPort(port)) {
258       m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
259       return true;
260     }
261     break;
262 
263   case AF_INET6:
264     SetFamily(AF_INET6);
265     if (SetPort(port)) {
266       m_socket_addr.sa_ipv6.sin6_addr = in6addr_loopback;
267       return true;
268     }
269     break;
270   }
271   Clear();
272   return false;
273 }
274 
275 bool SocketAddress::SetToAnyAddress(sa_family_t family, uint16_t port) {
276   switch (family) {
277   case AF_INET:
278     SetFamily(AF_INET);
279     if (SetPort(port)) {
280       m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_ANY);
281       return true;
282     }
283     break;
284 
285   case AF_INET6:
286     SetFamily(AF_INET6);
287     if (SetPort(port)) {
288       m_socket_addr.sa_ipv6.sin6_addr = in6addr_any;
289       return true;
290     }
291     break;
292   }
293   Clear();
294   return false;
295 }
296 
297 bool SocketAddress::IsAnyAddr() const {
298   return (GetFamily() == AF_INET)
299              ? m_socket_addr.sa_ipv4.sin_addr.s_addr == htonl(INADDR_ANY)
300              : 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, &in6addr_any, 16);
301 }
302 
303 bool SocketAddress::IsLocalhost() const {
304   return (GetFamily() == AF_INET)
305              ? m_socket_addr.sa_ipv4.sin_addr.s_addr == htonl(INADDR_LOOPBACK)
306              : 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, &in6addr_loopback,
307                            16);
308 }
309 
310 bool SocketAddress::operator==(const SocketAddress &rhs) const {
311   if (GetFamily() != rhs.GetFamily())
312     return false;
313   if (GetLength() != rhs.GetLength())
314     return false;
315   switch (GetFamily()) {
316   case AF_INET:
317     return m_socket_addr.sa_ipv4.sin_addr.s_addr ==
318            rhs.m_socket_addr.sa_ipv4.sin_addr.s_addr;
319   case AF_INET6:
320     return 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr,
321                        &rhs.m_socket_addr.sa_ipv6.sin6_addr, 16);
322   }
323   return false;
324 }
325 
326 bool SocketAddress::operator!=(const SocketAddress &rhs) const {
327   return !(*this == rhs);
328 }
329