moved isLocalNet() etc to util/rsnet.cc

Fixed FAILURE in net_test1



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@582 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2008-06-07 11:21:00 +00:00
parent a827b0a839
commit cbd0b216a4
3 changed files with 7 additions and 95 deletions

View File

@ -86,6 +86,7 @@ int test_isExternalNet()
struct in_addr localnet4_addr;
struct in_addr external_addr;
struct in_addr invalid_addr;
struct in_addr invalid_addr2;
inet_aton(loopback_addrstr, &loopback_addr);
inet_aton(localnet1_addrstr, &localnet1_addr);
@ -93,7 +94,8 @@ int test_isExternalNet()
inet_aton(localnet3_addrstr, &localnet3_addr);
inet_aton(localnet4_addrstr, &localnet4_addr);
inet_aton(external_addrstr, &external_addr);
inet_aton(invalid_addrstr, &invalid_addr);
invalid_addr.s_addr = 0;
invalid_addr2.s_addr = -1;
CHECK(isExternalNet(&loopback_addr)==false);
CHECK(isExternalNet(&localnet1_addr)==false);
@ -102,6 +104,7 @@ int test_isExternalNet()
CHECK(isExternalNet(&localnet4_addr)==false);
CHECK(isExternalNet(&external_addr)==true);
CHECK(isExternalNet(&invalid_addr)==false);
CHECK(isExternalNet(&invalid_addr2)==false);
REPORT("isExternalNet()");

View File

@ -27,6 +27,7 @@
#include "pqi/pqinetwork.h"
#include "util/rsnet.h"
#include <errno.h>
#include <iostream>
@ -492,80 +493,6 @@ int inaddr_cmp(struct sockaddr_in addr1, unsigned long addr2)
return 1;
}
bool isLoopbackNet(struct in_addr *addr)
{
if (0 == strcmp(inet_ntoa(*addr), "127.0.0.1"))
{
return true;
}
return false;
}
// returns all possible addrs.
// all processing is done in network byte order.
bool isPrivateNet(struct in_addr *addr)
{
bool ret = false;
std::ostringstream out;
in_addr_t taddr = htonl(addr->s_addr);
out << "isPrivateNet(" << inet_ntoa(*addr) << ") ? ";
out << std::endl;
struct in_addr addr2;
addr2.s_addr = taddr;
out << "Checking (" << inet_ntoa(addr2) << ") Against: " << std::endl;
// shouldn't need to rotate these...h -> n (in network already.
// check text output....
addr2.s_addr = htonl(inet_network("10.0.0.0"));
out << "\t(" << inet_ntoa(addr2) << ")" << std::endl;
addr2.s_addr = htonl(inet_network("172.16.0.0"));
out << "\t(" << inet_ntoa(addr2) << ")" << std::endl;
addr2.s_addr = htonl(inet_network("192.168.0.0"));
out << "\t(" << inet_ntoa(addr2) << ")" << std::endl;
addr2.s_addr = htonl(inet_network("169.254.0.0"));
out << "\t(" << inet_ntoa(addr2) << ")" << std::endl;
// 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)))
{
out << " True" << std::endl;
ret = true;
}
else
{
out << " False" << std::endl;
ret = false;
}
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out.str());
return ret;
}
bool isExternalNet(struct in_addr *addr)
{
if (!isValidNet(addr))
{
return false;
}
if (isLoopbackNet(addr))
{
return false;
}
if (isPrivateNet(addr))
{
return false;
}
return true;
}
struct in_addr getPreferredInterface() // returns best addr.
{
@ -666,17 +593,6 @@ bool sameNet(struct in_addr *addr, struct in_addr *addr2)
}
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 isSameSubnet(struct in_addr *addr1, struct in_addr *addr2)
{
/*

View File

@ -108,19 +108,12 @@ int sockaddr_cmp(struct sockaddr_in &addr1, struct sockaddr_in &addr2 );
int inaddr_cmp(struct sockaddr_in addr1, struct sockaddr_in addr1 );
int inaddr_cmp(struct sockaddr_in addr1, unsigned long);
struct in_addr getPreferredInterface(); // returns best addr.
std::list<std::string> getLocalInterfaces(); // returns all possible addrs.
bool isExternalNet(struct in_addr *addr); // if Valid & is not Private or Loopback.
bool isPrivateNet(struct in_addr *addr); // if inside 10.0.0.0 or
// other then firewalled.
bool isLoopbackNet(struct in_addr *addr);
bool sameNet(struct in_addr *addr, struct in_addr *addr2);
bool isValidNet(struct in_addr *addr);
// checks (addr1 & 255.255.255.0) == (addr2 & 255.255.255.0)
bool isSameSubnet(struct in_addr *addr1, struct in_addr *addr2);
struct in_addr getPreferredInterface(); // returns best addr.
bool sameNet(struct in_addr *addr, struct in_addr *addr2);
in_addr_t pqi_inet_netof(struct in_addr addr); // our implementation.