From a827b0a839c15a9a955efb7deb2525d99ae055e4 Mon Sep 17 00:00:00 2001 From: drbob Date: Sat, 7 Jun 2008 11:19:09 +0000 Subject: [PATCH] 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 --- libretroshare/src/util/rsnet.cc | 59 +++++++++++++++++++++++++++++++++ libretroshare/src/util/rsnet.h | 7 ++++ 2 files changed, 66 insertions(+) diff --git a/libretroshare/src/util/rsnet.cc b/libretroshare/src/util/rsnet.cc index d53d9120a..938f15c98 100644 --- a/libretroshare/src/util/rsnet.cc +++ b/libretroshare/src/util/rsnet.cc @@ -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; +} + + + diff --git a/libretroshare/src/util/rsnet.h b/libretroshare/src/util/rsnet.h index 47430ccbe..4a007eebb 100644 --- a/libretroshare/src/util/rsnet.h +++ b/libretroshare/src/util/rsnet.h @@ -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 */