Moved several network related functions from pqinetwork to here.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@581 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2008-06-07 11:19:09 +00:00
parent bba3c20045
commit a827b0a839
2 changed files with 66 additions and 0 deletions

View File

@ -66,3 +66,62 @@ void sockaddr_clear(struct sockaddr_in *addr)
addr->sin_family = AF_INET;
}
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;
}

View File

@ -60,4 +60,11 @@ uint64_t htonll(uint64_t x);
/* blank a network address */
void sockaddr_clear(struct sockaddr_in *addr);
/* determine network type (moved from pqi/pqinetwork.cc) */
bool isValidNet(struct in_addr *addr);
bool isLoopbackNet(struct in_addr *addr);
bool isPrivateNet(struct in_addr *addr);
bool isExternalNet(struct in_addr *addr);
#endif /* RS_UNIVERSAL_NETWORK_HEADER */