2018-05-30 15:19:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/util: rsnet.cc *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2004-2006 Robert Fernie <retroshare@lunamutt.com> *
|
|
|
|
* Copyright 2015-2018 Gioacchino Mazzurco <gio@eigenlab.org> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2008-02-03 07:07:59 -05:00
|
|
|
|
|
|
|
#include "util/rsnet.h"
|
2010-07-10 16:34:03 -04:00
|
|
|
#include "util/rsthreads.h"
|
2012-04-15 10:37:44 -04:00
|
|
|
#include "util/rsstring.h"
|
2015-10-07 21:24:31 -04:00
|
|
|
#include "util/rsmemory.h"
|
2010-04-22 16:03:47 -04:00
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
#else
|
2010-04-22 14:49:27 -04:00
|
|
|
#include <netdb.h>
|
2010-04-22 16:03:47 -04:00
|
|
|
#endif
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2018-11-08 07:14:29 -05:00
|
|
|
#include <cstring>
|
|
|
|
|
2008-03-03 09:41:15 -05:00
|
|
|
/* enforce LITTLE_ENDIAN on Windows */
|
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
#define BYTE_ORDER 1234
|
|
|
|
#define LITTLE_ENDIAN 1234
|
|
|
|
#define BIG_ENDIAN 4321
|
|
|
|
#endif
|
2008-02-03 07:07:59 -05:00
|
|
|
|
2015-12-21 11:25:02 -05:00
|
|
|
#ifndef ntohll
|
2008-02-03 07:07:59 -05:00
|
|
|
uint64_t ntohll(uint64_t x)
|
|
|
|
{
|
|
|
|
#ifdef BYTE_ORDER
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
return x;
|
|
|
|
#elif BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
|
|
|
|
uint32_t top = (uint32_t) (x >> 32);
|
|
|
|
uint32_t bot = (uint32_t) (0x00000000ffffffffULL & x);
|
|
|
|
|
|
|
|
uint64_t rev = ((uint64_t) ntohl(top)) | (((uint64_t) ntohl(bot)) << 32);
|
|
|
|
|
|
|
|
return rev;
|
|
|
|
#else
|
|
|
|
#error "ENDIAN determination Failed"
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#error "ENDIAN determination Failed (BYTE_ORDER not defined)"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
2015-12-21 11:25:02 -05:00
|
|
|
#endif
|
|
|
|
#ifndef htonll
|
2008-02-03 07:07:59 -05:00
|
|
|
uint64_t htonll(uint64_t x)
|
|
|
|
{
|
|
|
|
return ntohll(x);
|
|
|
|
}
|
2015-12-21 11:25:02 -05:00
|
|
|
#endif
|
2008-02-03 07:07:59 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
void sockaddr_clear(struct sockaddr_in *addr)
|
|
|
|
{
|
|
|
|
memset(addr, 0, sizeof(struct sockaddr_in));
|
|
|
|
addr->sin_family = AF_INET;
|
|
|
|
}
|
2008-02-03 07:07:59 -05:00
|
|
|
|
2015-10-07 21:24:31 -04:00
|
|
|
bool rsGetHostByName(const std::string& hostname, in_addr& returned_addr)
|
|
|
|
{
|
2018-11-08 07:14:29 -05:00
|
|
|
addrinfo hint; memset(&hint, 0, sizeof(hint));
|
|
|
|
hint.ai_family = AF_INET;
|
|
|
|
addrinfo* info = nullptr;
|
|
|
|
int res = getaddrinfo(hostname.c_str(), nullptr, &hint, &info);
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
if(res > 0 || !info || !info->ai_addr)
|
|
|
|
{
|
|
|
|
std::cerr << __PRETTY_FUNCTION__ << "(EE) getaddrinfo returned error "
|
|
|
|
<< res << " on string \"" << hostname << "\"" << std::endl;
|
|
|
|
returned_addr.s_addr = 0;
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
returned_addr.s_addr = ((sockaddr_in*)info->ai_addr)->sin_addr.s_addr;
|
|
|
|
|
|
|
|
if(info) freeaddrinfo(info);
|
|
|
|
|
|
|
|
return ok;
|
2015-10-07 21:24:31 -04:00
|
|
|
}
|
2008-06-07 07:19:09 -04:00
|
|
|
|
2010-05-23 18:27:10 -04:00
|
|
|
bool isValidNet(const struct in_addr *addr)
|
2008-06-07 07:19:09 -04:00
|
|
|
{
|
|
|
|
// invalid address.
|
|
|
|
if((*addr).s_addr == INADDR_NONE)
|
|
|
|
return false;
|
|
|
|
if((*addr).s_addr == 0)
|
|
|
|
return false;
|
|
|
|
// should do more tests.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-23 18:27:10 -04:00
|
|
|
bool isLoopbackNet(const struct in_addr *addr)
|
2008-06-07 07:19:09 -04:00
|
|
|
{
|
|
|
|
in_addr_t taddr = ntohl(addr->s_addr);
|
|
|
|
return (taddr == (127 << 24 | 1));
|
|
|
|
}
|
|
|
|
|
2018-02-19 17:23:15 -05:00
|
|
|
bool isPrivateNet(const struct in_addr *addr)
|
2008-06-07 07:19:09 -04:00
|
|
|
{
|
|
|
|
in_addr_t taddr = ntohl(addr->s_addr);
|
|
|
|
|
2018-02-19 17:23:15 -05:00
|
|
|
if ( (taddr>>24 == 10) || // 10.0.0.0/8
|
|
|
|
(taddr>>20 == (172<<4 | 16>>4)) || // 172.16.0.0/12
|
|
|
|
(taddr>>16 == (192<<8 | 168)) || // 192.168.0.0/16
|
|
|
|
(taddr>>16 == (169<<8 | 254)) ) // 169.254.0.0/16
|
2008-06-07 07:19:09 -04:00
|
|
|
return true;
|
2018-02-19 17:23:15 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isLinkLocalNet(const struct in_addr *addr)
|
|
|
|
{
|
|
|
|
in_addr_t taddr = ntohl(addr->s_addr);
|
|
|
|
if ( taddr>>16 == (169<<8 | 254) ) return true; // 169.254.0.0/16
|
|
|
|
|
|
|
|
return false;
|
2008-06-07 07:19:09 -04:00
|
|
|
}
|
|
|
|
|
2010-05-23 18:27:10 -04:00
|
|
|
bool isExternalNet(const struct in_addr *addr)
|
2008-06-07 07:19:09 -04:00
|
|
|
{
|
|
|
|
if (!isValidNet(addr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isLoopbackNet(addr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isPrivateNet(addr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-10 09:42:49 -04:00
|
|
|
std::ostream &operator<<(std::ostream &out, const struct sockaddr_in &addr)
|
|
|
|
{
|
2012-03-17 21:33:25 -04:00
|
|
|
out << "[" << rs_inet_ntoa(addr.sin_addr) << ":";
|
2010-06-10 09:42:49 -04:00
|
|
|
out << htons(addr.sin_port) << "]";
|
|
|
|
return out;
|
|
|
|
}
|
2010-07-10 16:34:03 -04:00
|
|
|
|
2019-04-28 07:38:16 -04:00
|
|
|
std::ostream& operator<<(std::ostream& o, const sockaddr_storage& addr)
|
|
|
|
{ return o << sockaddr_storage_tostring(addr); }
|
|
|
|
|
2010-07-10 16:34:03 -04:00
|
|
|
/* thread-safe version of inet_ntoa */
|
|
|
|
|
|
|
|
std::string rs_inet_ntoa(struct in_addr in)
|
|
|
|
{
|
2012-04-15 10:37:44 -04:00
|
|
|
std::string str;
|
2011-06-29 14:02:44 -04:00
|
|
|
uint8_t *bytes = (uint8_t *) &(in.s_addr);
|
2012-04-15 10:37:44 -04:00
|
|
|
rs_sprintf(str, "%u.%u.%u.%u", (int) bytes[0], (int) bytes[1], (int) bytes[2], (int) bytes[3]);
|
|
|
|
return str;
|
2010-07-10 16:34:03 -04:00
|
|
|
}
|