blob: 8bda17425f007bed8c87643ff8c19bb2ec7fa0d9 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Adrien Béraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraud612b55b2023-05-29 10:42:04 -040016 */
Adrien Béraud612b55b2023-05-29 10:42:04 -040017#include "ip_utils.h"
Adrien Béraud80c14e12023-07-18 16:13:15 -040018#include "sip_utils.h"
19#include "string_utils.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040020
Adrien Béraud80c14e12023-07-18 16:13:15 -040021#include <fmt/format.h>
Adrien Béraud612b55b2023-05-29 10:42:04 -040022
23#include <sys/types.h>
24#include <unistd.h>
25#include <limits.h>
26
27#ifdef _WIN32
28#define InetPtonA inet_pton
29WINSOCK_API_LINKAGE INT WSAAPI InetPtonA(INT Family, LPCSTR pStringBuf, PVOID pAddr);
30#else
31#include <arpa/inet.h>
32#include <arpa/nameser.h>
33#include <resolv.h>
34#include <netdb.h>
35#include <netinet/ip.h>
36#include <net/if.h>
37#include <ifaddrs.h>
38#include <sys/ioctl.h>
39#endif
40
41#ifndef HOST_NAME_MAX
42#ifdef MAX_COMPUTERNAME_LENGTH
43#define HOST_NAME_MAX MAX_COMPUTERNAME_LENGTH
44#else
45// Max 255 chars as per RFC 1035
46#define HOST_NAME_MAX 255
47#endif
48#endif
49
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040050namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040051
52std::string_view
53sip_strerror(pj_status_t code)
54{
55 thread_local char err_msg[PJ_ERR_MSG_SIZE];
Adrien Béraud80c14e12023-07-18 16:13:15 -040056 return sip_utils::as_view(pj_strerror(code, err_msg, sizeof err_msg));
Adrien Béraud612b55b2023-05-29 10:42:04 -040057}
58
59
60std::string
61ip_utils::getHostname()
62{
63 char hostname[HOST_NAME_MAX];
64 if (gethostname(hostname, HOST_NAME_MAX))
65 return {};
66 return hostname;
67}
68
69int
70ip_utils::getHostName(char* out, size_t out_len)
71{
72 char tempstr[INET_ADDRSTRLEN];
73 const char* p = NULL;
74#ifdef _WIN32
75 struct hostent* h = NULL;
76 struct sockaddr_in localAddr;
77 memset(&localAddr, 0, sizeof(localAddr));
78 gethostname(out, out_len);
79 h = gethostbyname(out);
80 if (h != NULL) {
81 memcpy(&localAddr.sin_addr, h->h_addr_list[0], 4);
82 p = inet_ntop(AF_INET, &localAddr.sin_addr, tempstr, sizeof(tempstr));
83 if (p)
84 strncpy(out, p, out_len);
85 else
86 return -1;
87 } else {
88 return -1;
89 }
90#elif (defined(BSD) && BSD >= 199306) || defined(__FreeBSD_kernel__)
91 int retVal = 0;
92 struct ifaddrs* ifap;
93 struct ifaddrs* ifa;
94 if (getifaddrs(&ifap) != 0)
95 return -1;
96 // Cycle through available interfaces.
97 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
98 // Skip loopback, point-to-point and down interfaces.
99 // except don't skip down interfaces if we're trying to get
100 // a list of configurable interfaces.
101 if ((ifa->ifa_flags & IFF_LOOPBACK) || (!(ifa->ifa_flags & IFF_UP)))
102 continue;
103 if (ifa->ifa_addr->sa_family == AF_INET) {
104 if (((struct sockaddr_in*) (ifa->ifa_addr))->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) {
105 // We don't want the loopback interface. Go to next one.
106 continue;
107 }
108 p = inet_ntop(AF_INET,
109 &((struct sockaddr_in*) (ifa->ifa_addr))->sin_addr,
110 tempstr,
111 sizeof(tempstr));
112 if (p)
113 strncpy(out, p, out_len);
114 else
115 retVal = -1;
116 break;
117 }
118 }
119 freeifaddrs(ifap);
120 retVal = ifa ? 0 : -1;
121 return retVal;
122#else
123 struct ifconf ifConf;
124 struct ifreq ifReq;
125 struct sockaddr_in localAddr;
126 char szBuffer[MAX_INTERFACE * sizeof(struct ifreq)];
127 int nResult;
128 int localSock;
129 memset(&ifConf, 0, sizeof(ifConf));
130 memset(&ifReq, 0, sizeof(ifReq));
131 memset(szBuffer, 0, sizeof(szBuffer));
132 memset(&localAddr, 0, sizeof(localAddr));
133 // Create an unbound datagram socket to do the SIOCGIFADDR ioctl on.
134 localSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
135 if (localSock == INVALID_SOCKET)
136 return -1;
137 /* Get the interface configuration information... */
138 ifConf.ifc_len = (int) sizeof szBuffer;
139 ifConf.ifc_ifcu.ifcu_buf = (caddr_t) szBuffer;
140 nResult = ioctl(localSock, SIOCGIFCONF, &ifConf);
141 if (nResult < 0) {
142 close(localSock);
143 return -1;
144 }
145 unsigned int i;
146 unsigned int j = 0;
147 // Cycle through the list of interfaces looking for IP addresses.
148 for (i = 0u; i < (unsigned int) ifConf.ifc_len && j < MIN_INTERFACE;) {
149 struct ifreq* pifReq = (struct ifreq*) ((caddr_t) ifConf.ifc_req + i);
150 i += sizeof *pifReq;
151 // See if this is the sort of interface we want to deal with.
152 memset(ifReq.ifr_name, 0, sizeof(ifReq.ifr_name));
153 strncpy(ifReq.ifr_name, pifReq->ifr_name, sizeof(ifReq.ifr_name));
154 ioctl(localSock, SIOCGIFFLAGS, &ifReq);
155 // Skip loopback, point-to-point and down interfaces.
156 // except don't skip down interfaces if we're trying to get
157 // a list of configurable interfaces.
158 if ((ifReq.ifr_flags & IFF_LOOPBACK) || (!(ifReq.ifr_flags & IFF_UP)))
159 continue;
160 if (pifReq->ifr_addr.sa_family == AF_INET) {
161 memcpy(&localAddr, &pifReq->ifr_addr, sizeof pifReq->ifr_addr);
162 if (localAddr.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) {
163 // We don't want the loopback interface. Go to the next one.
164 continue;
165 }
166 }
167 j++; // Increment j if we found an address which is not loopback and is up.
168 }
169 close(localSock);
170 p = inet_ntop(AF_INET, &localAddr.sin_addr, tempstr, sizeof(tempstr));
171 if (p)
172 strncpy(out, p, out_len);
173 else
174 return -1;
175#endif
176 return 0;
177}
178std::string
179ip_utils::getGateway(char* localHost, ip_utils::subnet_mask prefix)
180{
181 std::string_view localHostStr(localHost);
182 if (prefix == ip_utils::subnet_mask::prefix_32bit)
183 return std::string(localHostStr);
184 std::string defaultGw {};
185 // Make a vector of each individual number in the ip address.
186 std::vector<std::string_view> tokens = split_string(localHostStr, '.');
187 // Build a gateway address from the individual ip components.
188 for (unsigned i = 0; i <= (unsigned) prefix; i++)
Adrien Béraud80c14e12023-07-18 16:13:15 -0400189 defaultGw = fmt::format("{:s}{:s}.", defaultGw, tokens[i]);
Adrien Béraud612b55b2023-05-29 10:42:04 -0400190 for (unsigned i = (unsigned) ip_utils::subnet_mask::prefix_32bit;
191 i > (unsigned) prefix + 1;
192 i--)
193 defaultGw += "0.";
194 defaultGw += "1";
195 return defaultGw;
196}
197
198IpAddr
199ip_utils::getLocalGateway()
200{
201 char localHostBuf[INET_ADDRSTRLEN];
202 if (ip_utils::getHostName(localHostBuf, INET_ADDRSTRLEN) < 0) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400203 // JAMI_WARN("Couldn't find local host");
Adrien Béraud612b55b2023-05-29 10:42:04 -0400204 return {};
205 } else {
206 return IpAddr(ip_utils::getGateway(localHostBuf, ip_utils::subnet_mask::prefix_24bit));
207 }
208}
209
210std::vector<IpAddr>
211ip_utils::getAddrList(std::string_view name, pj_uint16_t family)
212{
213 std::vector<IpAddr> ipList;
214 if (name.empty())
215 return ipList;
216 if (IpAddr::isValid(name, family)) {
217 ipList.emplace_back(name);
218 return ipList;
219 }
220
221 static constexpr unsigned MAX_ADDR_NUM = 128;
222 pj_addrinfo res[MAX_ADDR_NUM];
223 unsigned addr_num = MAX_ADDR_NUM;
224 const pj_str_t pjname(sip_utils::CONST_PJ_STR(name));
225 auto status = pj_getaddrinfo(family, &pjname, &addr_num, res);
226 if (status != PJ_SUCCESS) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400227 // JAMI_ERR("Error resolving %.*s : %s",
228 // (int) name.size(),
229 // name.data(),
230 // sip_utils::sip_strerror(status).c_str());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400231 return ipList;
232 }
233
234 for (unsigned i = 0; i < addr_num; i++) {
235 bool found = false;
236 for (const auto& ip : ipList)
237 if (!pj_sockaddr_cmp(&ip, &res[i].ai_addr)) {
238 found = true;
239 break;
240 }
241 if (!found)
242 ipList.emplace_back(res[i].ai_addr);
243 }
244
245 return ipList;
246}
247
248bool
249ip_utils::haveCommonAddr(const std::vector<IpAddr>& a, const std::vector<IpAddr>& b)
250{
251 for (const auto& i : a) {
252 for (const auto& j : b) {
253 if (i == j)
254 return true;
255 }
256 }
257 return false;
258}
259
260IpAddr
261ip_utils::getLocalAddr(pj_uint16_t family)
262{
263 IpAddr ip_addr {};
264 pj_status_t status = pj_gethostip(family, ip_addr.pjPtr());
265 if (status == PJ_SUCCESS) {
266 return ip_addr;
267 }
Morteza Namvar5f639522023-07-04 17:08:58 -0400268 // JAMI_WARN("Could not get preferred address familly (%s)",
269 // (family == pj_AF_INET6()) ? "IPv6" : "IPv4");
Adrien Béraud612b55b2023-05-29 10:42:04 -0400270 family = (family == pj_AF_INET()) ? pj_AF_INET6() : pj_AF_INET();
271 status = pj_gethostip(family, ip_addr.pjPtr());
272 if (status == PJ_SUCCESS) {
273 return ip_addr;
274 }
Morteza Namvar5f639522023-07-04 17:08:58 -0400275 // JAMI_ERR("Could not get local IP");
Adrien Béraud612b55b2023-05-29 10:42:04 -0400276 return ip_addr;
277}
278
279IpAddr
280ip_utils::getInterfaceAddr(const std::string& interface, pj_uint16_t family)
281{
282 if (interface == DEFAULT_INTERFACE)
283 return getLocalAddr(family);
284
285 IpAddr addr {};
286
287#ifndef _WIN32
288 const auto unix_family = family == pj_AF_INET() ? AF_INET : AF_INET6;
289
290 int fd = socket(unix_family, SOCK_DGRAM, 0);
291 if (fd < 0) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400292 // JAMI_ERR("Could not open socket: %m");
Adrien Béraud612b55b2023-05-29 10:42:04 -0400293 return addr;
294 }
295
296 if (unix_family == AF_INET6) {
297 int val = family != pj_AF_UNSPEC();
298 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void*) &val, sizeof(val)) < 0) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400299 // JAMI_ERR("Could not setsockopt: %m");
Adrien Béraud612b55b2023-05-29 10:42:04 -0400300 close(fd);
301 return addr;
302 }
303 }
304
305 ifreq ifr;
306 strncpy(ifr.ifr_name, interface.c_str(), sizeof ifr.ifr_name);
307 // guarantee that ifr_name is NULL-terminated
308 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
309
310 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
311 ifr.ifr_addr.sa_family = unix_family;
312
313 ioctl(fd, SIOCGIFADDR, &ifr);
314 close(fd);
315
316 addr = ifr.ifr_addr;
317 if (addr.isUnspecified())
318 return getLocalAddr(addr.getFamily());
319#else // _WIN32
320 struct addrinfo hints;
321 struct addrinfo* result = NULL;
322 struct sockaddr_in* sockaddr_ipv4;
323 struct sockaddr_in6* sockaddr_ipv6;
324
325 ZeroMemory(&hints, sizeof(hints));
326
327 DWORD dwRetval = getaddrinfo(interface.c_str(), "0", &hints, &result);
328 if (dwRetval != 0) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400329 // JAMI_ERR("getaddrinfo failed with error: %lu", dwRetval);
Adrien Béraud612b55b2023-05-29 10:42:04 -0400330 return addr;
331 }
332
333 switch (result->ai_family) {
334 sockaddr_ipv4 = (struct sockaddr_in*) result->ai_addr;
335 addr = sockaddr_ipv4->sin_addr;
336 break;
337 case AF_INET6:
338 sockaddr_ipv6 = (struct sockaddr_in6*) result->ai_addr;
339 addr = sockaddr_ipv6->sin6_addr;
340 break;
341 default:
342 break;
343 }
344
345 if (addr.isUnspecified())
346 return getLocalAddr(addr.getFamily());
347#endif // !_WIN32
348
349 return addr;
350}
351
352std::vector<std::string>
353ip_utils::getAllIpInterfaceByName()
354{
355 std::vector<std::string> ifaceList;
356 ifaceList.push_back("default");
357#ifndef _WIN32
358 static ifreq ifreqs[20];
359 ifconf ifconf;
360
361 ifconf.ifc_buf = (char*) (ifreqs);
362 ifconf.ifc_len = sizeof(ifreqs);
363
364 int sock = socket(AF_INET6, SOCK_STREAM, 0);
365
366 if (sock >= 0) {
367 if (ioctl(sock, SIOCGIFCONF, &ifconf) >= 0)
368 for (unsigned i = 0; i < ifconf.ifc_len / sizeof(ifreq); ++i)
369 ifaceList.push_back(std::string(ifreqs[i].ifr_name));
370
371 close(sock);
372 }
373
374#else
Morteza Namvar5f639522023-07-04 17:08:58 -0400375 // JAMI_ERR("Not implemented yet. (iphlpapi.h problem)");
Adrien Béraud612b55b2023-05-29 10:42:04 -0400376#endif
377 return ifaceList;
378}
379
380std::vector<std::string>
381ip_utils::getAllIpInterface()
382{
383 pj_sockaddr addrList[16];
384 unsigned addrCnt = PJ_ARRAY_SIZE(addrList);
385
386 std::vector<std::string> ifaceList;
387
388 if (pj_enum_ip_interface(pj_AF_UNSPEC(), &addrCnt, addrList) == PJ_SUCCESS) {
389 for (unsigned i = 0; i < addrCnt; i++) {
390 char addr[PJ_INET6_ADDRSTRLEN];
391 pj_sockaddr_print(&addrList[i], addr, sizeof(addr), 0);
392 ifaceList.push_back(std::string(addr));
393 }
394 }
395
396 return ifaceList;
397}
398
399std::vector<IpAddr>
400ip_utils::getLocalNameservers()
401{
402 std::vector<IpAddr> res;
403#if defined __ANDROID__ || defined _WIN32 || TARGET_OS_IPHONE
404#ifdef _MSC_VER
405#pragma message(__FILE__ "(" STR2(__LINE__) ") : -NOTE- " \
406 "Not implemented")
407#else
408#warning "Not implemented"
409#endif
410#else
411 if (not(_res.options & RES_INIT))
412 res_init();
413 res.insert(res.end(), _res.nsaddr_list, _res.nsaddr_list + _res.nscount);
414#endif
415 return res;
416}
417
418bool
419IpAddr::isValid(std::string_view address, pj_uint16_t family)
420{
421 const pj_str_t pjstring(sip_utils::CONST_PJ_STR(address));
422 pj_str_t ret_str;
423 pj_uint16_t ret_port;
424 int ret_family;
425 auto status = pj_sockaddr_parse2(pj_AF_UNSPEC(), 0, &pjstring, &ret_str, &ret_port, &ret_family);
426 if (status != PJ_SUCCESS || (family != pj_AF_UNSPEC() && ret_family != family))
427 return false;
428
429 char buf[PJ_INET6_ADDRSTRLEN];
430 pj_str_t addr_with_null = {buf, 0};
431 pj_strncpy_with_null(&addr_with_null, &ret_str, sizeof(buf));
432 struct sockaddr sa;
433 return inet_pton(ret_family == pj_AF_INET6() ? AF_INET6 : AF_INET, buf, &(sa.sa_data)) == 1;
434}
435
436bool
437IpAddr::isUnspecified() const
438{
439 switch (addr.addr.sa_family) {
440 case AF_INET:
441 return IN_IS_ADDR_UNSPECIFIED(&addr.ipv4.sin_addr);
442 case AF_INET6:
443 return IN6_IS_ADDR_UNSPECIFIED(reinterpret_cast<const in6_addr*>(&addr.ipv6.sin6_addr));
444 default:
445 return true;
446 }
447}
448
449bool
450IpAddr::isLoopback() const
451{
452 switch (addr.addr.sa_family) {
453 case AF_INET: {
454 auto addr_host = ntohl(addr.ipv4.sin_addr.s_addr);
455 uint8_t b1 = (uint8_t)(addr_host >> 24);
456 return b1 == 127;
457 }
458 case AF_INET6:
459 return IN6_IS_ADDR_LOOPBACK(reinterpret_cast<const in6_addr*>(&addr.ipv6.sin6_addr));
460 default:
461 return false;
462 }
463}
464
465bool
466IpAddr::isPrivate() const
467{
468 if (isLoopback()) {
469 return true;
470 }
471 switch (addr.addr.sa_family) {
472 case AF_INET: {
473 auto addr_host = ntohl(addr.ipv4.sin_addr.s_addr);
474 uint8_t b1, b2;
475 b1 = (uint8_t)(addr_host >> 24);
476 b2 = (uint8_t)((addr_host >> 16) & 0x0ff);
477 // 10.x.y.z
478 if (b1 == 10)
479 return true;
480 // 172.16.0.0 - 172.31.255.255
481 if ((b1 == 172) && (b2 >= 16) && (b2 <= 31))
482 return true;
483 // 192.168.0.0 - 192.168.255.255
484 if ((b1 == 192) && (b2 == 168))
485 return true;
486 return false;
487 }
488 case AF_INET6: {
489 const pj_uint8_t* addr6 = reinterpret_cast<const pj_uint8_t*>(&addr.ipv6.sin6_addr);
490 if (addr6[0] == 0xfc)
491 return true;
492 return false;
493 }
494 default:
495 return false;
496 }
497}
498} // namespace jami