xref: /freebsd/contrib/llvm-project/lldb/source/Host/common/SocketAddress.cpp (revision 9e5787d2284e187abb5b654d924394a65772e004)
1 //===-- SocketAddress.cpp -------------------------------------------------===//
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::
178 operator=(const struct addrinfo *addr_info) {
179   Clear();
180   if (addr_info && addr_info->ai_addr && addr_info->ai_addrlen > 0 &&
181       size_t(addr_info->ai_addrlen) <= sizeof m_socket_addr) {
182     ::memcpy(&m_socket_addr, addr_info->ai_addr, addr_info->ai_addrlen);
183   }
184   return *this;
185 }
186 
187 const SocketAddress &SocketAddress::operator=(const struct sockaddr &s) {
188   m_socket_addr.sa = s;
189   return *this;
190 }
191 
192 const SocketAddress &SocketAddress::operator=(const struct sockaddr_in &s) {
193   m_socket_addr.sa_ipv4 = s;
194   return *this;
195 }
196 
197 const SocketAddress &SocketAddress::operator=(const struct sockaddr_in6 &s) {
198   m_socket_addr.sa_ipv6 = s;
199   return *this;
200 }
201 
202 const SocketAddress &SocketAddress::
203 operator=(const struct sockaddr_storage &s) {
204   m_socket_addr.sa_storage = s;
205   return *this;
206 }
207 
208 bool SocketAddress::getaddrinfo(const char *host, const char *service,
209                                 int ai_family, int ai_socktype, int ai_protocol,
210                                 int ai_flags) {
211   Clear();
212 
213   auto addresses = GetAddressInfo(host, service, ai_family, ai_socktype,
214                                   ai_protocol, ai_flags);
215   if (!addresses.empty())
216     *this = addresses[0];
217   return IsValid();
218 }
219 
220 std::vector<SocketAddress>
221 SocketAddress::GetAddressInfo(const char *hostname, const char *servname,
222                               int ai_family, int ai_socktype, int ai_protocol,
223                               int ai_flags) {
224   std::vector<SocketAddress> addr_list;
225 
226   struct addrinfo hints;
227   memset(&hints, 0, sizeof(hints));
228   hints.ai_family = ai_family;
229   hints.ai_socktype = ai_socktype;
230   hints.ai_protocol = ai_protocol;
231   hints.ai_flags = ai_flags;
232 
233   struct addrinfo *service_info_list = nullptr;
234   int err = ::getaddrinfo(hostname, servname, &hints, &service_info_list);
235   if (err == 0 && service_info_list) {
236     for (struct addrinfo *service_ptr = service_info_list;
237          service_ptr != nullptr; service_ptr = service_ptr->ai_next) {
238       addr_list.emplace_back(SocketAddress(service_ptr));
239     }
240   }
241 
242   if (service_info_list)
243     ::freeaddrinfo(service_info_list);
244   return addr_list;
245 }
246 
247 bool SocketAddress::SetToLocalhost(sa_family_t family, uint16_t port) {
248   switch (family) {
249   case AF_INET:
250     SetFamily(AF_INET);
251     if (SetPort(port)) {
252       m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
253       return true;
254     }
255     break;
256 
257   case AF_INET6:
258     SetFamily(AF_INET6);
259     if (SetPort(port)) {
260       m_socket_addr.sa_ipv6.sin6_addr = in6addr_loopback;
261       return true;
262     }
263     break;
264   }
265   Clear();
266   return false;
267 }
268 
269 bool SocketAddress::SetToAnyAddress(sa_family_t family, uint16_t port) {
270   switch (family) {
271   case AF_INET:
272     SetFamily(AF_INET);
273     if (SetPort(port)) {
274       m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_ANY);
275       return true;
276     }
277     break;
278 
279   case AF_INET6:
280     SetFamily(AF_INET6);
281     if (SetPort(port)) {
282       m_socket_addr.sa_ipv6.sin6_addr = in6addr_any;
283       return true;
284     }
285     break;
286   }
287   Clear();
288   return false;
289 }
290 
291 bool SocketAddress::IsAnyAddr() const {
292   return (GetFamily() == AF_INET)
293              ? m_socket_addr.sa_ipv4.sin_addr.s_addr == htonl(INADDR_ANY)
294              : 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, &in6addr_any, 16);
295 }
296 
297 bool SocketAddress::IsLocalhost() const {
298   return (GetFamily() == AF_INET)
299              ? m_socket_addr.sa_ipv4.sin_addr.s_addr == htonl(INADDR_LOOPBACK)
300              : 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, &in6addr_loopback,
301                            16);
302 }
303 
304 bool SocketAddress::operator==(const SocketAddress &rhs) const {
305   if (GetFamily() != rhs.GetFamily())
306     return false;
307   if (GetLength() != rhs.GetLength())
308     return false;
309   switch (GetFamily()) {
310   case AF_INET:
311     return m_socket_addr.sa_ipv4.sin_addr.s_addr ==
312            rhs.m_socket_addr.sa_ipv4.sin_addr.s_addr;
313   case AF_INET6:
314     return 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr,
315                        &rhs.m_socket_addr.sa_ipv6.sin6_addr, 16);
316   }
317   return false;
318 }
319 
320 bool SocketAddress::operator!=(const SocketAddress &rhs) const {
321   return !(*this == rhs);
322 }
323