/** @details A network address defines the "bottom-most" address for a given network. * @param ip An IP address on the network. * @param netmask The netmask defining the network range. * @returns The network address. */ _CGUL_EXPORT CGUL::Network::IPAddress CGUL::Network::IPAddress::CalculateNetwork(const IPAddress& ip, const IPAddress& netmask) { if (!ip.IsValid() || ip.GetType() != netmask.GetType()) { throw NetworkException(NetworkExceptionCode::FAILED_CALCULATE_ADDRESS, NetworkExceptionReason::ADDRESS_MISMATCH); } if (ip.GetType() == IPAddressType::IPV4) { return IPAddress(ip.ToUInt32() & netmask.ToUInt32()); } else { UInt64 network[2]; network[0] = ip.address[0] & netmask.address[0]; network[1] = ip.address[1] & netmask.address[1]; return IPAddress(network); } }
/** @details The broadcast address can only be calculated on a IPv4 network. While it is * technically possible to calculate an IPv6 broadcast address, the IPv6 protocol has dropped * support for broadcasting. Since broadcasting is not supported in IPv6, this method will result * in an exception given an IPv6 address. * @param ip An IP address on the network. * @param netmask The netmask defining the network range. * @returns The broadcast address. */ _CGUL_EXPORT CGUL::Network::IPAddress CGUL::Network::IPAddress::CalculateBroadcast(const IPAddress& ip, const IPAddress& netmask) { if (ip.GetType() == IPAddressType::IPV6 || netmask.GetType() == IPAddressType::IPV6) { throw NetworkException(NetworkExceptionCode::FAILED_CALCULATE_ADDRESS, NetworkExceptionReason::ADDRESS_INVALID); } if (!ip.IsValid() || ip.GetType() != netmask.GetType()) { throw NetworkException(NetworkExceptionCode::FAILED_CALCULATE_ADDRESS, NetworkExceptionReason::ADDRESS_MISMATCH); } return IPAddress(ip.ToUInt32() | (~netmask.ToUInt32())); }