/** * Creates an instance. * Initialize IP address of eligible router. */ public OspfEligibleRouter() { ipAddress = Ip4Address.valueOf("0.0.0.0"); }
/** * Checks if given id is valid or not. * * @param value input value * @return true if valid else false */ private static boolean isValidIpAddress(String value) { boolean status = true; try { Ip4Address ipAddress = Ip4Address.valueOf(value); } catch (Exception e) { log.debug("Invalid IP address string: {}", value); return false; } return status; }
/** * Converts an integer and a prefix length into an IPv4 prefix. * * @param address an integer representing the IPv4 address * @param prefixLength the prefix length * @return an IPv4 prefix * @throws IllegalArgumentException if the prefix length value is invalid */ public static Ip4Prefix valueOf(int address, int prefixLength) { return new Ip4Prefix(Ip4Address.valueOf(address), prefixLength); }
/** * Converts a byte array and a prefix length into an IPv4 prefix. * * @param address the IPv4 address value stored in network byte order * @param prefixLength the prefix length * @return an IPv4 prefix * @throws IllegalArgumentException if the prefix length value is invalid */ public static Ip4Prefix valueOf(byte[] address, int prefixLength) { return new Ip4Prefix(Ip4Address.valueOf(address), prefixLength); }
@Override public String toString() { return toStringHelper(getClass()) .add("hardwareType", Short.toString(hardwareType)) .add("protocolType", Short.toString(protocolType)) .add("hardwareAddressLength", Byte.toString(hardwareAddressLength)) .add("protocolAddressLength", Byte.toString(protocolAddressLength)) .add("opCode", Short.toString(opCode)) .add("senderHardwareAddress", MacAddress.valueOf(senderHardwareAddress)) .add("senderProtocolAddress", Ip4Address.valueOf(senderProtocolAddress)) .add("targetHardwareAddress", MacAddress.valueOf(targetHardwareAddress)) .add("targetProtocolAddress", Ip4Address.valueOf(targetProtocolAddress)) .toString(); } }
/** * Gets the {@link Ip4Address} view of the IP address. * * @return the {@link Ip4Address} view of the IP address if it is IPv4, * otherwise null */ public Ip4Address getIp4Address() { if (!isIp4()) { return null; } // Return this object itself if it is already instance of Ip4Address if (this instanceof Ip4Address) { return (Ip4Address) this; } return Ip4Address.valueOf(octets); }
/** * Converts a byte array and a given offset from the beginning of the * array into an IPv4 address. * <p> * The IP address is stored in network byte order (i.e., the most * significant byte first). * </p> * @param value the value to use * @param offset the offset in bytes from the beginning of the byte array * @return an IPv4 address * @throws IllegalArgumentException if the arguments are invalid */ public static Ip4Address valueOf(byte[] value, int offset) { IpAddress.checkArguments(VERSION, value, offset); byte[] bc = Arrays.copyOfRange(value, offset, value.length); return Ip4Address.valueOf(bc); }
/** * Converts an IPv4 string literal (e.g., "10.2.3.4") into an IP address. * * @param value an IPv4 address value in string form * @return an IPv4 address * @throws IllegalArgumentException if the argument is invalid */ public static Ip4Address valueOf(String value) { InetAddress inetAddress = null; try { inetAddress = InetAddresses.forString(value); } catch (IllegalArgumentException e) { final String msg = "Invalid IP address string: " + value; throw new IllegalArgumentException(msg); } return valueOf(inetAddress); }
/** * Creates an IPv4 address by masking it with a network mask of given * mask length. * * @param address the address to mask * @param prefixLength the length of the mask prefix. Must be in the * interval [0, 32] * @return a new IPv4 address that is masked with a mask prefix of the * specified length * @throws IllegalArgumentException if the prefix length is invalid */ public static Ip4Address makeMaskedAddress(final Ip4Address address, int prefixLength) { byte[] net = makeMaskedAddressArray(address, prefixLength); return Ip4Address.valueOf(net); } }
/** * Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16") * into an IPv4 prefix. * * @param address an IP prefix in string form (e.g., "10.1.0.0/16") * @return an IPv4 prefix * @throws IllegalArgumentException if the arguments are invalid */ public static Ip4Prefix valueOf(String address) { final String[] parts = address.split("/"); if (parts.length != 2) { String msg = "Malformed IPv4 prefix string: " + address + ". " + "Address must take form \"x.x.x.x/y\""; throw new IllegalArgumentException(msg); } Ip4Address ipAddress = Ip4Address.valueOf(parts[0]); int prefixLength = Integer.parseInt(parts[1]); return new Ip4Prefix(ipAddress, prefixLength); } }
@Override public Ip4Address read(Kryo kryo, Input input, Class<Ip4Address> type) { final int octLen = input.readInt(); checkArgument(octLen == Ip4Address.BYTE_LENGTH); byte[] octs = new byte[octLen]; input.readBytes(octs); return Ip4Address.valueOf(octs); } }
@Override public NiciraSetTunnelDst decode(ObjectNode json, CodecContext context) { if (json == null || !json.isObject()) { return null; } // parse tunnel destination IP address String dstIp = nullIsIllegal(json.get(TUNNEL_DST), TUNNEL_DST + MISSING_MEMBER_MESSAGE).asText(); Ip4Address tunnelDst = Ip4Address.valueOf(dstIp); return new NiciraSetTunnelDst(tunnelDst); } }
/** * Reads bytes from channel buffer . * * @param channelBuffer channel buffer instance * @throws Exception might throws exception while parsing buffer */ public void readFrom(ChannelBuffer channelBuffer) throws Exception { try { byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); this.setRouterAddress(Ip4Address.valueOf(tempByteArray)); } catch (Exception e) { log.debug("Error::RouterTLV:: {}", e.getMessage()); throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE); } }
/** * Reads the channel buffer and returns object of OSPFPseudonode. * * @param cb ChannelBuffer * @return object of OSPFPseudonode */ public static OspfPseudonode read(ChannelBuffer cb) { int routerID = cb.readInt(); Ip4Address drInterface = Ip4Address.valueOf(cb.readInt()); return OspfPseudonode.of(routerID, drInterface); }
/** * Reads from channel buffer and populate instance. * * @param channelBuffer channel buffer instance * @throws OspfParseException might throws exception while parsing buffer */ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { try { byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); this.setNetworkMask(Ip4Address.valueOf(tempByteArray)); //add all the attached routers while (channelBuffer.readableBytes() > 0) { tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); this.addAttachedRouter(Ip4Address.valueOf(tempByteArray)); } } catch (Exception e) { log.debug("Error::NetworkLSA::readFrom:: {}", e.getMessage()); throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE); } }
/** * Reads from channel buffer and populate instance. * * @param channelBuffer channelBuffer instance. * @throws OspfParseException might throws exception while parsing buffer */ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { try { byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); this.setNetworkMask(Ip4Address.valueOf(tempByteArray)); int unusedByte = channelBuffer.readByte(); this.setMetric(channelBuffer.readUnsignedMedium()); } catch (Exception e) { log.debug("Error::AsbrSummaryLsa:: {}", e.getMessage()); throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE); } }
/** * Reads from channel buffer and populate instance. * * @param channelBuffer channelBuffer instance * @throws OspfParseException might throws exception while parsing buffer */ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { try { byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); this.setNetworkMask(Ip4Address.valueOf(tempByteArray)); int unsedByte = channelBuffer.readByte(); this.setMetric(channelBuffer.readUnsignedMedium()); } catch (Exception e) { log.debug("Error::SummaryLsa:: {}", e.getMessage()); throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE); } }
/** * Reads the channel buffer and returns object of IPv4AddressTlv. * * @param cb channelBuffer * @param type address type * @return object of IPv4AddressTlv * @throws BgpParseException while parsing IPv4AddressTlv */ public static IPv4AddressTlv read(ChannelBuffer cb, short type) throws BgpParseException { InetAddress ipAddress = Validation.toInetAddress(LENGTH, cb); if (ipAddress.isMulticastAddress()) { throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null); } Ip4Address address = Ip4Address.valueOf(ipAddress); return IPv4AddressTlv.of(address, type); }
@Override public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { while (channelBuffer.readableBytes() >= OspfUtil.LSREQUEST_LENGTH) { LsRequestPacket lsRequestPacket = new LsRequestPacket(); lsRequestPacket.setLsType(channelBuffer.readInt()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); lsRequestPacket.setLinkStateId(Ip4Address.valueOf(tempByteArray).toString()); tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); lsRequestPacket.setOwnRouterId(Ip4Address.valueOf(tempByteArray).toString()); this.addLinkStateRequests(lsRequestPacket); } }
/** * Reads the IPv4 Router-ID. * * @param cb ChannelBuffer * @param sType tag type * @return object of BgpAttrRouterIdV4 * @throws BgpParseException while parsing BgpAttrRouterIdV4 */ public static BgpAttrRouterIdV4 read(ChannelBuffer cb, short sType) throws BgpParseException { short lsAttrLength = cb.readShort(); if ((lsAttrLength != 4) || (cb.readableBytes() < lsAttrLength)) { Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, lsAttrLength); } byte[] ipBytes = new byte[lsAttrLength]; cb.readBytes(ipBytes, 0, lsAttrLength); Ip4Address ip4RouterId = Ip4Address.valueOf(ipBytes); return BgpAttrRouterIdV4.of(ip4RouterId, sType); }