Search in sources :

Example 1 with IPAddress

use of inet.ipaddr.IPAddress in project jcasbin by casbin.

the class BuiltInFunctions method ipMatch.

/**
 * ipMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be
 * an IP address or a CIDR pattern. For example, "192.168.2.123" matches "192.168.2.0/24"
 *
 * @param ip1 the first argument.
 * @param ip2 the second argument.
 * @return whether ip1 matches ip2.
 */
public static boolean ipMatch(String ip1, String ip2) {
    IPAddressString ipas1 = new IPAddressString(ip1);
    try {
        ipas1.validateIPv4();
    } catch (AddressStringException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("invalid argument: ip1 in IPMatch() function is not an IP address.");
    }
    IPAddressString ipas2 = new IPAddressString(ip2);
    try {
        ipas2.validate();
    } catch (AddressStringException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("invalid argument: ip2 in IPMatch() function is neither an IP address nor a CIDR.");
    }
    if (ipas1.equals(ipas2)) {
        return true;
    }
    IPAddress ipa1;
    IPAddress ipa2;
    try {
        ipa1 = ipas1.toAddress();
        ipa2 = ipas2.toAddress();
    } catch (AddressStringException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("invalid argument: ip1 or ip2 in IPMatch() function is not an IP address.");
    }
    Integer prefix = ipa2.getNetworkPrefixLength();
    IPAddress mask = ipa2.getNetwork().getNetworkMask(prefix, false);
    return ipa1.mask(mask).equals(ipa2.mask(mask));
}
Also used : AddressStringException(inet.ipaddr.AddressStringException) IPAddress(inet.ipaddr.IPAddress) IPAddressString(inet.ipaddr.IPAddressString)

Example 2 with IPAddress

use of inet.ipaddr.IPAddress in project ArTEMiS by ls1intum.

the class HttpRequestUtils method getIpAddressFromRequest.

/**
 * Extract Client IP Address from Http Request as IPAddress Object
 *
 * @param request Http Request
 * @return IPAddress Object
 */
public static Optional<IPAddress> getIpAddressFromRequest(@NotNull HttpServletRequest request) {
    final String ipString = getIpStringFromRequest(request);
    final IPAddress ipAddress = new IPAddressString(ipString).getAddress();
    return Optional.ofNullable(ipAddress);
}
Also used : IPAddressString(inet.ipaddr.IPAddressString) IPAddress(inet.ipaddr.IPAddress) IPAddressString(inet.ipaddr.IPAddressString)

Example 3 with IPAddress

use of inet.ipaddr.IPAddress in project onos by opennetworkinglab.

the class KubevirtIpPool method getRangedIps.

/**
 * Obtains the IP address list from the given start and end range.
 *
 * @param start start range
 * @param end   end range
 * @return IP address list from the given start and end range
 * @throws AddressStringException exception
 */
public Set<IpAddress> getRangedIps(String start, String end) throws AddressStringException {
    Set<IpAddress> ips = new HashSet<>();
    IPAddress lower = new IPAddressString(start).toAddress();
    IPAddress upper = new IPAddressString(end).toAddress();
    IPAddressSeqRange range = lower.toSequentialRange(upper);
    for (IPAddress addr : range.getIterable()) {
        ips.add(IpAddress.valueOf(addr.toString()));
    }
    return ips;
}
Also used : IPAddressSeqRange(inet.ipaddr.IPAddressSeqRange) IpAddress(org.onlab.packet.IpAddress) IPAddress(inet.ipaddr.IPAddress) HashSet(java.util.HashSet) IPAddressString(inet.ipaddr.IPAddressString)

Aggregations

IPAddress (inet.ipaddr.IPAddress)3 IPAddressString (inet.ipaddr.IPAddressString)3 AddressStringException (inet.ipaddr.AddressStringException)1 IPAddressSeqRange (inet.ipaddr.IPAddressSeqRange)1 HashSet (java.util.HashSet)1 IpAddress (org.onlab.packet.IpAddress)1