2008-02-03 07:07:59 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/util: rsnet.cc
|
|
|
|
*
|
|
|
|
* Universal Networking Header for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2007-2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util/rsnet.h"
|
2008-02-07 11:18:34 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t htonll(uint64_t x)
|
|
|
|
{
|
|
|
|
return ntohll(x);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-06-07 07:19:09 -04:00
|
|
|
|
|
|
|
bool isValidNet(struct in_addr *addr)
|
|
|
|
{
|
|
|
|
// invalid address.
|
|
|
|
if((*addr).s_addr == INADDR_NONE)
|
|
|
|
return false;
|
|
|
|
if((*addr).s_addr == 0)
|
|
|
|
return false;
|
|
|
|
// should do more tests.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isLoopbackNet(struct in_addr *addr)
|
|
|
|
{
|
|
|
|
in_addr_t taddr = ntohl(addr->s_addr);
|
|
|
|
return (taddr == (127 << 24 | 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isPrivateNet(struct in_addr *addr)
|
|
|
|
{
|
|
|
|
in_addr_t taddr = ntohl(addr->s_addr);
|
|
|
|
|
|
|
|
// 10.0.0.0/8
|
|
|
|
// 172.16.0.0/12
|
|
|
|
// 192.168.0.0/16
|
|
|
|
// 169.254.0.0/16
|
|
|
|
if ((taddr>>24 == 10) ||
|
|
|
|
(taddr>>20 == (172<<4 | 16>>4)) ||
|
|
|
|
(taddr>>16 == (192<<8 | 168)) ||
|
|
|
|
(taddr>>16 == (169<<8 | 254)))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isExternalNet(struct in_addr *addr)
|
|
|
|
{
|
|
|
|
if (!isValidNet(addr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isLoopbackNet(addr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isPrivateNet(addr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-22 14:49:08 -04:00
|
|
|
bool getIPAddressFromString (const char *addr_str, struct in_addr *addr)
|
|
|
|
{
|
|
|
|
if (addr_str && addr) {
|
|
|
|
hostent *pHost = gethostbyname (addr_str);
|
|
|
|
if (pHost) {
|
|
|
|
addr->s_addr = *(unsigned long*) (pHost->h_addr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|