//--------------------------------------------------------------------------- bool IPAddress::isIPv6() const { if (isIPv4Compatible()) return true; // an IPv4 compatible address is also an IPv6 address - both return true return !isIPv4(); }
String IPAddress::string(bool inIncludePort) const { WORD port = getPort(); if (!inIncludePort) port = 0; if (isIPv4()) { char buffer[sizeof("255.255.255.255:65535")+1]; const char *format = "%u.%u.%u.%u"; if (0 != port) format = "%u.%u.%u.%u:%u"; int bytePos = 12; if (isIPv46to4()) bytePos = 2; sprintf( buffer, format, (UINT)mIPAddress.by[bytePos], (UINT)mIPAddress.by[bytePos+1], (UINT)mIPAddress.by[bytePos+2], (UINT)mIPAddress.by[bytePos+3], (UINT)port ); String temp(buffer); return temp; } return stringAsIPv6(inIncludePort); }
//--------------------------------------------------------------------------- bool IPAddress::isAddressEqualIgnoringIPv4Format(const IPAddress &inIPAddress) const { if (isIPv4()) { if (!inIPAddress.isIPv4()) return false; return getIPv4AddressAsDWORD() == inIPAddress.getIPv4AddressAsDWORD(); } return isAddressEqual(inIPAddress); }
//--------------------------------------------------------------------------- bool IPAddress::isAddressEqualIgnoringIPv4Format(const IPv6PortPair &inPortPair) const { if (isIPv4()) { IPAddress temp(inPortPair); if (!temp.isIPv4()) return false; return getIPv4AddressAsDWORD() == temp.getIPv4AddressAsDWORD(); } return isAddressEqual(inPortPair); }
//--------------------------------------------------------------------------- // Unicast IPv6 addresses // http://technet.microsoft.com/en-us/library/cc759208%28WS.10%29.aspx // Link-local addresses, identified by the FP of 1111 1110 10, are used by // nodes when communicating with neighboring nodes on the same link. For // example, on a single link IPv6 network with no router, link-local // addresses are used to communicate between hosts on the link. Link-local // addresses are equivalent to Automatic Private IP Addressing (APIPA) IPv4 // addresses (using the 169.254.0.0/16 prefix). The scope of a link-local // address is the local link. A link-local address is required for Neighbor // Discovery processes and is always automatically configured, even in the // absence of all other unicast addresses. For more information about the // address autoconfiguration process for link-local addresses, see IPv6 // address autoconfiguration. // // Link-local addresses always begin with FE80. With the 64-bit interface // identifier, the prefix for link-local addresses is always FE80::/64. An // IPv6 router never forwards link-local traffic beyond the link. bool IPAddress::isLinkLocal() const { if (isIPv4()) { IPAddress temp; temp.mIPAddress.dw[3] = htonl(getIPv4AddressAsDWORD()); // see http://en.wikipedia.org/wiki/Private_network if (169 == (temp.mIPAddress.by[12])) return (254 == temp.mIPAddress.by[13]); return false; } return ((0xFE == (mIPAddress.by[0])) && (0x80 == (mIPAddress.by[1] & 0xC0))); }
//--------------------------------------------------------------------------- bool IPAddress::isPrivate() const { // see http://en.wikipedia.org/wiki/Private_network if (isLinkLocal()) return true; if (isIPv4()) { IPAddress temp; temp.mIPAddress.dw[3] = htonl(getIPv4AddressAsDWORD()); switch (temp.mIPAddress.by[12]) { case 10: return true; case 172: return (16 == (temp.mIPAddress.by[13] & (0xF0))); case 192: return (168 == temp.mIPAddress.by[13]); // case 169: return (254 == temp.by[13]); // considered link local } } return (0xFC == (0xFe & mIPAddress.by[0])); }
result_t net_base::isIP(exlib::string ip, int32_t& retVal) { retVal = 0; bool is = false; isIPv4(ip, is); if (is) { retVal = 4; return 0; } isIPv6(ip, is); if (is) retVal = 6; return 0; }