/** * Converts a byte array into an IP address. * * @param version the IP address version * @param value the IP address value stored in network byte order * (i.e., the most significant byte first) * @return an IP address * @throws IllegalArgumentException if the arguments are invalid */ public static IpAddress valueOf(Version version, byte[] value) { return new IpAddress(version, value); }
/** * Converts an integer into an IPv4 address. * * @param value an integer representing an IPv4 address value * @return an IP address */ public static IpAddress valueOf(int value) { byte[] bytes = ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array(); return new IpAddress(Version.INET, bytes); }
/** * Converts an InetAddress into an IP address. * * @param inetAddress the InetAddress value to use * @return an IP address * @throws IllegalArgumentException if the argument is invalid */ public static IpAddress valueOf(InetAddress inetAddress) { byte[] bytes = inetAddress.getAddress(); if (inetAddress instanceof Inet4Address) { return new IpAddress(Version.INET, bytes); } if (inetAddress instanceof Inet6Address) { return new IpAddress(Version.INET6, bytes); } // Use the number of bytes as a hint if (bytes.length == INET_BYTE_LENGTH) { return new IpAddress(Version.INET, bytes); } if (bytes.length == INET6_BYTE_LENGTH) { return new IpAddress(Version.INET6, bytes); } final String msg = "Unrecognized IP version address string: " + inetAddress.toString(); throw new IllegalArgumentException(msg); }
/** * Creates an IP network mask prefix. * * @param version the IP address version * @param prefixLength the length of the mask prefix. Must be in the * interval [0, 32] for IPv4, or [0, 128] for IPv6 * @return a new IP address that contains a mask prefix of the * specified length * @throws IllegalArgumentException if the arguments are invalid */ public static IpAddress makeMaskPrefix(Version version, int prefixLength) { byte[] mask = makeMaskPrefixArray(version, prefixLength); return new IpAddress(version, mask); }
/** * Generates an IP prefix. * * @return the IP prefix of the IP address */ public IpPrefix toIpPrefix() { if (isIp4()) { return IpPrefix.valueOf(new IpAddress(Version.INET, octets), Ip4Address.BIT_LENGTH); } else { return IpPrefix.valueOf(new IpAddress(Version.INET6, octets), Ip6Address.BIT_LENGTH); } }
pdu.setGenericTrap(PDUv1.ENTERPRISE_SPECIFIC); pdu.setSpecificTrap(1); pdu.setAgentAddress(new IpAddress(ipAddress)); long sysUpTime = 111111; pdu.setTimestamp(sysUpTime); pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(sysUpTime))); pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(trapOid))); pdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress(ipAddress)));