/** * Checks given IPs are in same network or not. * * @param ip1 IP address * @param ip2 IP address * @param mask network mask * @return true if both are in same network else false * @throws Exception might throws exception while parsing ip address */ public static boolean sameNetwork(Ip4Address ip1, Ip4Address ip2, Ip4Address mask) throws Exception { byte[] a1 = ip1.toOctets(); byte[] a2 = ip2.toOctets(); byte[] m = mask.toOctets(); for (int i = 0; i < a1.length; i++) { if ((a1[i] & m[i]) != (a2[i] & m[i])) { return false; } } return true; }
@Override public void writeTo(ByteBuf byteBuf, LispIpv4Address address) throws LispWriterException { byte[] ipByte = address.getAddress().getIp4Address().toOctets(); byteBuf.writeBytes(ipByte); } }
@Override public void write(Kryo kryo, Output output, Ip4Address object) { byte[] octs = object.toOctets(); // It is always Ip4Address.BYTE_LENGTH output.writeInt(octs.length); output.writeBytes(octs); }
/** * Gets tlv body as byte array. * * @return tlv body as byte array */ public byte[] getTlvBodyAsByteArray() { List<Byte> bodyLst = new ArrayList<>(); bodyLst.addAll(Bytes.asList(this.routerAddress().toOctets())); return Bytes.toArray(bodyLst); }
/** * Gets LSA body as byte array. * * @return LSA body as byte array * @throws OspfParseException might throws exception while parsing packet */ public byte[] getLsaBodyAsByteArray() throws OspfParseException { List<Byte> bodyLst = new ArrayList<>(); try { bodyLst.addAll(Bytes.asList(this.networkMask().toOctets())); //add each attachedRouters details for (Ip4Address attachedRouter : attachedRouters) { //attached router bodyLst.addAll(Bytes.asList(attachedRouter.toOctets())); } } catch (Exception e) { log.debug("Error::NetworkLSA::getLsrBodyAsByteArray {}", e.getMessage()); throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE); } return Bytes.toArray(bodyLst); }
/** * Get LSA body as byte array. * * @return byte array contains lsa body * @throws OspfParseException might throws exception while parsing packet */ public byte[] getLsaBodyAsByteArray() throws OspfParseException { List<Byte> bodyLst = new ArrayList<>(); try { bodyLst.addAll(Bytes.asList(this.networkMask().toOctets())); bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.metric()))); } catch (Exception e) { log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage()); throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE); } return Bytes.toArray(bodyLst); }
/** * Get the LSA body. * * @return LSA body */ public byte[] getLsaBodyAsByteArray() { List<Byte> bodyLst = new ArrayList<>(); try { bodyLst.addAll(Bytes.asList(this.networkMask().toOctets())); bodyLst.add((byte) 0); bodyLst.addAll(Bytes.asList(OspfUtil.convertToThreeBytes(this.metric()))); } catch (Exception e) { log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage()); return Bytes.toArray(bodyLst); } return Bytes.toArray(bodyLst); }
/** * Gets hello body as byte array. * * @return hello body as byte array */ public byte[] getHelloBodyAsByteArray() { List<Byte> bodyLst = new ArrayList<>(); try { bodyLst.addAll(Bytes.asList(this.networkMask().toOctets())); bodyLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.helloInterval()))); bodyLst.add((byte) this.options()); bodyLst.add((byte) this.routerPriority()); bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.routerDeadInterval()))); bodyLst.addAll(Bytes.asList(this.dr().toOctets())); bodyLst.addAll(Bytes.asList(this.bdr().toOctets())); for (Ip4Address neighbour : neighborAddress) { bodyLst.addAll(Bytes.asList(neighbour.toOctets())); } } catch (Exception e) { log.debug("Error::getHelloBodyAsByteArray {}", e.getMessage()); return Bytes.toArray(bodyLst); } return Bytes.toArray(bodyLst); }
/** * Adds metadata to ospf packet like whether to join multi cast group and destination IP. * * @param interfaceIndex interface index * @param ospfPacket OSPF packet * @param allDroutersValue whether to join multi cast or not * @param destinationIp destination ip address * @return byte array */ public static byte[] addMetadata(int interfaceIndex, byte[] ospfPacket, int allDroutersValue, Ip4Address destinationIp) { byte[] packet; byte[] interfaceIndexByteVal = {(byte) interfaceIndex}; byte[] allDroutersByteVal = {(byte) allDroutersValue}; byte[] destIpAsBytes = destinationIp.toOctets(); byte[] metadata = Bytes.concat(interfaceIndexByteVal, allDroutersByteVal); metadata = Bytes.concat(metadata, destIpAsBytes); packet = Bytes.concat(ospfPacket, metadata); return packet; } }
/** * Gets LSA body as byte array. * * @return byte array contains LSA body * @throws OspfParseException might throws exception while parsing buffer */ public byte[] getLsaBodyAsByteArray() throws OspfParseException { List<Byte> bodyLst = new ArrayList<>(); try { bodyLst.addAll(Bytes.asList(this.networkMask().toOctets())); //add each OSPFExternalDestination details for (OspfExternalDestination externalDest : externalDestinations) { if (externalDest.isType1orType2Metric()) { //add 1 followed by 7 zeros equals to decimal 128 bodyLst.add((byte) 128); } else { bodyLst.add((byte) 0); } bodyLst.addAll(Bytes.asList(OspfUtil.convertToThreeBytes(externalDest.metric()))); bodyLst.addAll(Bytes.asList(externalDest.forwardingAddress().toOctets())); bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(externalDest.externalRouterTag()))); } } catch (Exception e) { log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage()); return Bytes.toArray(bodyLst); } return Bytes.toArray(bodyLst); }
/** * Gets LS request packet header as byte array. * * @return LS request packet header as byte array */ public byte[] getLsrHeaderAsByteArray() { List<Byte> headerLst = new ArrayList<>(); try { headerLst.add((byte) this.ospfVersion()); headerLst.add((byte) this.ospfType()); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength()))); headerLst.addAll(Bytes.asList(this.routerId().toOctets())); headerLst.addAll(Bytes.asList(this.areaId().toOctets())); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum()))); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType()))); //Authentication is 0 always. Total 8 bytes consist of zero byte[] auth = new byte[OspfUtil.EIGHT_BYTES]; headerLst.addAll(Bytes.asList(auth)); } catch (Exception e) { log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage()); return Bytes.toArray(headerLst); } return Bytes.toArray(headerLst); }
/** * Gets the LSA header as bytes. * * @return LSA header as bytes */ public byte[] getLsaHeaderAsByteArray() { List<Byte> headerLst = new ArrayList<>(); try { headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.age()))); headerLst.add((byte) this.options()); headerLst.add((byte) this.lsType()); headerLst.addAll(Bytes.asList(InetAddress.getByName(this.linkStateId()).getAddress())); headerLst.addAll(Bytes.asList(this.advertisingRouter().toOctets())); headerLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.lsSequenceNo()))); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsCheckSum()))); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsPacketLen()))); } catch (Exception e) { log.debug("Error::getLsaHeaderAsByteArray {}", e.getMessage()); return Bytes.toArray(headerLst); } return Bytes.toArray(headerLst); }
/** * Gets hello header as byte array. * * @return hello header */ public byte[] getHelloHeaderAsByteArray() { List<Byte> headerLst = new ArrayList<>(); try { headerLst.add((byte) this.ospfVersion()); headerLst.add((byte) this.ospfType()); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength()))); headerLst.addAll(Bytes.asList(this.routerId().toOctets())); headerLst.addAll(Bytes.asList(this.areaId().toOctets())); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum()))); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType()))); //Authentication is 0 always. Total 8 bytes consist of zero byte[] auth = new byte[OspfUtil.EIGHT_BYTES]; headerLst.addAll(Bytes.asList(auth)); } catch (Exception e) { log.debug("Error::getHelloHeaderAsByteArray {}", e.getMessage()); return Bytes.toArray(headerLst); } return Bytes.toArray(headerLst); }
/** * Gets lsu header. * * @return lsu header as byte array */ public byte[] getLsuHeaderAsByteArray() { List<Byte> headerLst = new ArrayList<>(); try { headerLst.add((byte) this.ospfVersion()); headerLst.add((byte) this.ospfType()); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength()))); headerLst.addAll(Bytes.asList(this.routerId().toOctets())); headerLst.addAll(Bytes.asList(this.areaId().toOctets())); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum()))); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType()))); //Authentication is 0 always. Total 8 bytes consist of zero byte[] auth = new byte[OspfUtil.EIGHT_BYTES]; headerLst.addAll(Bytes.asList(auth)); } catch (Exception e) { log.debug("Error::LSUpdate::getLsuHeaderAsByteArray:: {}", e.getMessage()); return Bytes.toArray(headerLst); } return Bytes.toArray(headerLst); }
/** * Gets DD Header as byte array. * * @return dd header as byte array. */ public byte[] getDdHeaderAsByteArray() { List<Byte> headerLst = new ArrayList<>(); try { headerLst.add((byte) this.ospfVersion()); headerLst.add((byte) this.ospfType()); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength()))); headerLst.addAll(Bytes.asList(this.routerId().toOctets())); headerLst.addAll(Bytes.asList(this.areaId().toOctets())); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum()))); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType()))); //Authentication is 0 always. Total 8 bytes consist of zero byte[] auth = new byte[OspfUtil.EIGHT_BYTES]; headerLst.addAll(Bytes.asList(auth)); } catch (Exception e) { log.debug("Error"); } return Bytes.toArray(headerLst); }
/** * Gets LSAcknowledge as byte array. * * @return byte array */ public byte[] getLsAckAsByteArray() { List<Byte> headerLst = new ArrayList<>(); try { headerLst.add((byte) this.ospfVersion()); headerLst.add((byte) this.ospfType()); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength()))); headerLst.addAll(Bytes.asList(this.routerId().toOctets())); headerLst.addAll(Bytes.asList(this.areaId().toOctets())); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum()))); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType()))); //Authentication is 0 always. Total 8 bytes consist of zero byte[] auth = new byte[OspfUtil.EIGHT_BYTES]; headerLst.addAll(Bytes.asList(auth)); } catch (Exception e) { log.debug("Error::LsAckPacket:: {}", e.getMessage()); return Bytes.toArray(headerLst); } return Bytes.toArray(headerLst); }
@Override public void write(Kryo kryo, Output output, Ip4Prefix object) { byte[] octs = object.address().toOctets(); // It is always Ip4Address.BYTE_LENGTH output.writeInt(octs.length); output.writeBytes(octs); output.writeInt(object.prefixLength()); }
/** * Gets header as byte array. * * @return header as byte array */ public byte[] getOpaqueLsaHeaderAsByteArray() { List<Byte> headerLst = new ArrayList<>(); try { headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.age()))); headerLst.add((byte) this.options()); headerLst.add((byte) this.lsType()); headerLst.add((byte) this.opaqueType()); headerLst.addAll(Bytes.asList(OspfUtil.convertToThreeBytes(this.opaqueId()))); headerLst.addAll(Bytes.asList(this.advertisingRouter().toOctets())); headerLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.lsSequenceNo()))); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsCheckSum()))); headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsPacketLen()))); } catch (Exception e) { log.debug("Error::getLsaHeaderAsByteArray {}", e.getMessage()); return Bytes.toArray(headerLst); } return Bytes.toArray(headerLst); }