From: <sv...@ww...> - 2004-12-18 10:33:54
|
Author: mkrose Date: 2004-12-18 02:33:41 -0800 (Sat, 18 Dec 2004) New Revision: 1400 Modified: trunk/CSP/SimNet/NetworkNode.cpp trunk/CSP/SimNet/NetworkNode.h Log: Minor improvements to the NetworkNode api. Also provides stream output operators that are useful for logging. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1400 Modified: trunk/CSP/SimNet/NetworkNode.cpp =================================================================== --- trunk/CSP/SimNet/NetworkNode.cpp 2004-12-18 10:31:07 UTC (rev 1399) +++ trunk/CSP/SimNet/NetworkNode.cpp 2004-12-18 10:33:41 UTC (rev 1400) @@ -74,18 +74,10 @@ return m_addr; } -ost::tpport_t NetworkNode::getPort() const { - return m_port; -} - const char * NetworkNode::getHostname() const { return m_addr.getHostname(); } -simdata::uint32 NetworkNode::getIp() const { - return m_addr.getAddress().s_addr; -} - std::string NetworkNode::ipToString(simdata::uint32 addr) { std::ostringstream os; os << (addr & 0xff) << "." << ((addr >> 8) & 0xff) << "." << ((addr >> 16) & 0xff) << "." << (addr >> 24); Modified: trunk/CSP/SimNet/NetworkNode.h =================================================================== --- trunk/CSP/SimNet/NetworkNode.h 2004-12-18 10:31:07 UTC (rev 1399) +++ trunk/CSP/SimNet/NetworkNode.h 2004-12-18 10:33:41 UTC (rev 1400) @@ -100,7 +100,7 @@ /** Get the host receive port. */ - ost::tpport_t getPort() const; + inline ost::tpport_t getPort() const { return m_port; } /** Get the host ip address. */ @@ -109,23 +109,38 @@ /** Convert ip address and port to a ConnectionPoint. */ inline ConnectionPoint getConnectionPoint() const { - return ConnectionPoint(m_addr.getAddress().s_addr, m_port); + return ConnectionPoint(getIp(), getPort()); } /** Get the ip address as a 32-bit int, in network byte-order. */ - simdata::uint32 getIp() const; + inline simdata::uint32 getIp() const { + return m_addr.getAddress().s_addr; + } /** Get the host name. */ const char * getHostname() const; - /** Test if an ip address is unroutable (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). + /** Get ip address as a dotted-quad string. + */ + inline std::string getIpString() const { + return ipToString(getIp()); + } + + /** Return true if this ip is routable (ie, not 127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). + */ + inline bool isRoutable() const { + return isRoutable(getIp()); + } + + /** Test if an ip address is routable (ie, not 127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). * @param addr 32-bit ipv4 address in network byte-order. */ static bool isRoutable(simdata::uint32 addr) { return ((addr & 0xffff) != 43200) && // 192.168. 0. 0 - 192.168.255.255 ((addr & 0x00ff) != 10) && // 10. 0. 0. 0 - 10.255.255.255 + ((addr & 0x00ff) != 127) && // 127. 0. 0. 0 - 127.255.255.255 ((addr & 0xf0ff) != 4268); // 172. 16. 0. 0 - 172. 31.255.255 } @@ -136,6 +151,14 @@ }; +inline std::ostream &operator<<(std::ostream &os, NetworkNode const &node) { + return os << node.getIpString() << ':' << node.getPort() << " (" << node.getHostname() << ")"; +} + +inline std::ostream &operator<<(std::ostream &os, ConnectionPoint const &point) { + return os << NetworkNode(point); +} + } // namespace simnet #endif // __SIMNET_NETWORKNODE_H__ |