diff options
Diffstat (limited to 'node')
-rw-r--r-- | node/InetAddress.hpp | 20 | ||||
-rw-r--r-- | node/Utils.hpp | 16 |
2 files changed, 30 insertions, 6 deletions
diff --git a/node/InetAddress.hpp b/node/InetAddress.hpp index b60a5a3a..1d171ba7 100644 --- a/node/InetAddress.hpp +++ b/node/InetAddress.hpp @@ -231,7 +231,6 @@ struct InetAddress : public sockaddr_storage * @param port Port, 0 to 65535 */ inline void setPort(unsigned int port) - throw() { switch(ss_family) { case AF_INET: @@ -244,6 +243,25 @@ struct InetAddress : public sockaddr_storage } /** + * @return True if this network/netmask route describes a default route (e.g. 0.0.0.0/0) + */ + inline bool isDefaultRoute() + { + switch(ss_family) { + case AF_INET: + return ( (reinterpret_cast<struct sockaddr_in *>(this)->sin_addr.s_addr == 0) && (reinterpret_cast<struct sockaddr_in *>(this)->sin_port == 0) ); + case AF_INET6: + const uint8_t *ipb = reinterpret_cast<const uint8_t *>(reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_addr.s6_addr); + for(int i=0;i<16;++i) { + if (ipb[i]) + return false; + } + return (reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_port == 0); + } + return false; + } + + /** * @return ASCII IP/port format representation */ std::string toString() const; diff --git a/node/Utils.hpp b/node/Utils.hpp index 3f4cc765..04838f1c 100644 --- a/node/Utils.hpp +++ b/node/Utils.hpp @@ -225,18 +225,24 @@ public: } /** - * Perform a safe C string copy + * Perform a safe C string copy, ALWAYS null-terminating the result * - * @param dest Destination buffer - * @param len Length of buffer - * @param src Source string + * This will never ever EVER result in dest[] not being null-terminated + * regardless of any input parameter (other than len==0 which is invalid). + * + * @param dest Destination buffer (must not be NULL) + * @param len Length of dest[] (if zero, false is returned and nothing happens) + * @param src Source string (if NULL, dest will receive a zero-length string and true is returned) * @return True on success, false on overflow (buffer will still be 0-terminated) */ static inline bool scopy(char *dest,unsigned int len,const char *src) - throw() { if (!len) return false; // sanity check + if (!src) { + *dest = (char)0; + return true; + } char *end = dest + len; while ((*dest++ = *src++)) { if (dest == end) { |