/** * Sets the data of this option. * * @param data the data to set */ public void setData(byte[] data) { try { this.payload = Data.deserializer().deserialize(data, 0, data.length); } catch (DeserializationException e) { throw new IllegalArgumentException("Invalid data"); } }
@Override public void setData(byte[] data) { try { Dhcp6Duid duid = Dhcp6Duid.deserializer().deserialize(data, 0, data.length); this.setDuid(duid); } catch (DeserializationException e) { throw new IllegalArgumentException("Invalid DUID"); } }
/** * Deserializer function for IP packets. * * @return deserializer function */ public static Deserializer<? extends IP> deserializer() { return (data, offset, length) -> { final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); byte version = (byte) (bb.get() >> 4 & 0xf); switch (version) { case 4: return IPv4.deserializer().deserialize(data, offset, length); case 6: return IPv6.deserializer().deserialize(data, offset, length); default: throw new DeserializationException("Invalid IP version"); } }; } }
/** * Make an exact copy of the ARP packet. * * @return copy of the packet */ public ARP duplicate() { try { byte[] data = serialize(); return deserializer().deserialize(data, 0, data.length); } catch (DeserializationException dex) { // If we can't make an object out of the serialized data, its a defect throw new IllegalStateException(dex); } }
/** * Make an exact copy of the ethernet packet. * * @return copy of the packet */ public Ethernet duplicate() { try { byte[] data = serialize(); return deserializer().deserialize(data, 0, data.length); } catch (DeserializationException dex) { // If we can't make an object out of the serialized data, its a defect throw new IllegalStateException(dex); } } }
@Override public Ethernet parsed() { checkPermission(PACKET_READ); try { return Ethernet.deserializer().deserialize(pktin.getData(), 0, pktin.getData().length); } catch (BufferUnderflowException | NullPointerException | DeserializationException e) { Logger log = LoggerFactory.getLogger(getClass()); log.error("packet deserialization problem : {}", e.getMessage()); return null; } }
/** * Returns the deserializer closure (used by upper layer deserializer). * * @return the deserializer closure */ public static Deserializer<VXLAN> deserializer() { return (data, offset, length) -> { checkInput(data, offset, length, VXLAN_HEADER_LENGTH); VXLAN vxlan = new VXLAN(); ByteBuffer bb = ByteBuffer.wrap(data, offset, length); vxlan.flags = bb.get(); bb.get(vxlan.rsvd1); bb.get(vxlan.vni); vxlan.rsvd2 = bb.get(); Deserializer<? extends IPacket> deserializer = Data.deserializer(); vxlan.payload = deserializer.deserialize(data, bb.position(), bb.limit() - bb.position()); vxlan.payload.setParent(vxlan); return vxlan; }; }
/** * Deserializer function for RIPng packets. * * @return deserializer function */ public static Deserializer<RIPng> deserializer() { return (data, offset, length) -> { RIPng ripng = new RIPng(); checkInput(data, offset, length, MIN_HEADER_LENGTH); ByteBuffer bb = ByteBuffer.wrap(data, offset, length); ripng.cmdType = bb.get(); ripng.version = bb.get(); ripng.reserved = bb.getShort(); // read route entries while (bb.hasRemaining() && (bb.remaining() >= RIPngEntry.ENTRY_LEN)) { RIPngEntry rtEntry; byte[] rtData = new byte[RIPngEntry.ENTRY_LEN]; bb.get(rtData); rtEntry = RIPngEntry.deserializer().deserialize(rtData, 0, rtData.length); ripng.rtEntries.add(rtEntry); } return ripng; }; } /*
/** * Deserializer function for encapsulated security payload headers. * * @return deserializer function */ public static Deserializer<EncapSecurityPayload> deserializer() { return (data, offset, length) -> { checkInput(data, offset, length, HEADER_LENGTH); EncapSecurityPayload encapSecurityPayload = new EncapSecurityPayload(); ByteBuffer bb = ByteBuffer.wrap(data, offset, length); encapSecurityPayload.securityParamIndex = bb.getInt(); encapSecurityPayload.sequence = bb.getInt(); encapSecurityPayload.payload = Data.deserializer().deserialize( data, bb.position(), bb.limit() - bb.position()); encapSecurityPayload.payload.setParent(encapSecurityPayload); return encapSecurityPayload; }; }
/** * Deserializer function for MPLS packets. * * @return deserializer function */ public static Deserializer<MPLS> deserializer() { return (data, offset, length) -> { checkInput(data, offset, length, HEADER_LENGTH); MPLS mpls = new MPLS(); ByteBuffer bb = ByteBuffer.wrap(data, offset, length); int mplsheader = bb.getInt(); mpls.label = ((mplsheader & 0xfffff000) >>> 12); mpls.bos = (byte) ((mplsheader & 0x00000100) >> 8); mpls.ttl = (byte) (mplsheader & 0x000000ff); mpls.protocol = (mpls.bos == 1) ? PROTOCOL_IPV4 : PROTOCOL_MPLS; Deserializer<? extends IPacket> deserializer; if (protocolDeserializerMap.containsKey(mpls.protocol)) { deserializer = protocolDeserializerMap.get(mpls.protocol); } else { deserializer = Data.deserializer(); } mpls.payload = deserializer.deserialize(data, bb.position(), bb.limit() - bb.position()); mpls.payload.setParent(mpls); return mpls; }; }
/** * Decapsulates an EAP packet from the RADIUS packet. * * @return An EAP object containing the reassembled EAP message * @throws DeserializationException if packet deserialization fails */ public EAP decapsulateMessage() throws DeserializationException { EAP message = new EAP(); ByteArrayOutputStream messageStream = new ByteArrayOutputStream(); // Iterating through EAP-Message attributes to concatenate their value for (RADIUSAttribute ra : this.getAttributeList(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE)) { try { messageStream.write(ra.getValue()); } catch (IOException e) { log.error("Error while reassembling EAP message: {}", e.getMessage()); } } // Assembling EAP object from the concatenated stream message = EAP.deserializer().deserialize(messageStream.toByteArray(), 0, messageStream.size()); return message; }
/** * Deserializer function for Slow packets. * * @return deserializer function */ public static Deserializer<Slow> deserializer() { return (data, offset, length) -> { checkInput(data, offset, length, HEADER_LENGTH); Slow slow = new Slow(); ByteBuffer bb = ByteBuffer.wrap(data, offset, length); slow.setSubtype(bb.get()); Deserializer<? extends IPacket> deserializer; if (Slow.PROTOCOL_DESERIALIZER_MAP.containsKey(slow.subtype)) { deserializer = Slow.PROTOCOL_DESERIALIZER_MAP.get(slow.subtype); } else { throw new DeserializationException("Unsupported slow protocol subtype " + Byte.toString(slow.subtype)); } int remainingLength = bb.limit() - bb.position(); slow.payload = deserializer.deserialize(data, bb.position(), remainingLength); slow.payload.setParent(slow); return slow; }; }
/** * Deserializer function for redirect packets. * * @return deserializer function */ public static Deserializer<Redirect> deserializer() { return (data, offset, length) -> { checkInput(data, offset, length, HEADER_LENGTH); Redirect redirect = new Redirect(); ByteBuffer bb = ByteBuffer.wrap(data, offset, length); bb.getInt(); bb.get(redirect.targetAddress, 0, Ip6Address.BYTE_LENGTH); bb.get(redirect.destinationAddress, 0, Ip6Address.BYTE_LENGTH); if (bb.limit() - bb.position() > 0) { NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer() .deserialize(data, bb.position(), bb.limit() - bb.position()); for (NeighborDiscoveryOptions.Option option : options.options()) { redirect.addOption(option.type(), option.data()); } } return redirect; }; }
/** * Deserializer function for IPv4 packets. * * @return deserializer function */ public static Deserializer<PIM> deserializer() { return (data, offset, length) -> { checkInput(data, offset, length, PIM_HEADER_LEN); PIM pim = new PIM(); final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); byte versionByte = bb.get(); pim.version = (byte) (versionByte >> 4 & 0xf); pim.setPIMType((byte) (versionByte & 0xf)); pim.reserved = bb.get(); pim.checksum = bb.getShort(); Deserializer<? extends IPacket> deserializer; if (PIM.PROTOCOL_DESERIALIZER_MAP.containsKey(pim.getPimMsgType())) { deserializer = PIM.PROTOCOL_DESERIALIZER_MAP.get(pim.getPimMsgType()); } else { deserializer = Data.deserializer(); } pim.payload = deserializer.deserialize(data, bb.position(), bb.limit() - bb.position()); pim.payload.setParent(pim); return pim; }; }
/** * Deserializer function for neighbor solicitation packets. * * @return deserializer function */ public static Deserializer<NeighborSolicitation> deserializer() { return (data, offset, length) -> { checkInput(data, offset, length, HEADER_LENGTH); NeighborSolicitation neighborSolicitation = new NeighborSolicitation(); ByteBuffer bb = ByteBuffer.wrap(data, offset, length); bb.getInt(); bb.get(neighborSolicitation.targetAddress, 0, Ip6Address.BYTE_LENGTH); if (bb.limit() - bb.position() > 0) { NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer() .deserialize(data, bb.position(), bb.limit() - bb.position()); for (NeighborDiscoveryOptions.Option option : options.options()) { neighborSolicitation.addOption(option.type(), option.data()); } } return neighborSolicitation; }; }
/** * Deserializer function for router solicitation packets. * * @return deserializer function */ public static Deserializer<RouterSolicitation> deserializer() { return (data, offset, length) -> { checkInput(data, offset, length, HEADER_LENGTH); RouterSolicitation routerSolicitation = new RouterSolicitation(); ByteBuffer bb = ByteBuffer.wrap(data, offset, length); bb.getInt(); if (bb.limit() - bb.position() > 0) { NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer() .deserialize(data, bb.position(), bb.limit() - bb.position()); for (NeighborDiscoveryOptions.Option option : options.options()) { routerSolicitation.addOption(option.type(), option.data()); } } return routerSolicitation; }; }
/** * Gets deserializer. * * @return the deserializer */ public static Deserializer<Dhcp6Option> deserializer() { return (data, offset, length) -> { Dhcp6Option dhcp6Option = Dhcp6Option.deserializer().deserialize(data, offset, length); if (dhcp6Option.getLength() < DEFAULT_LEN) { throw new DeserializationException("Invalid CLT option data"); } Dhcp6CLTOption cltOption = new Dhcp6CLTOption(dhcp6Option); byte[] optionData = cltOption.getData(); ByteBuffer bb = ByteBuffer.wrap(optionData); cltOption.clt = bb.getInt(); return cltOption; }; }
/** * Gets deserializer for DHCPv6 relay option. * * @return the deserializer */ public static Deserializer<Dhcp6Option> deserializer() { return (data, offset, len) -> { Dhcp6Option dhcp6Option = Dhcp6Option.deserializer().deserialize(data, offset, len); IPacket payload = DHCP6.deserializer() .deserialize(dhcp6Option.getData(), 0, dhcp6Option.getLength()); Dhcp6RelayOption relayOption = new Dhcp6RelayOption(dhcp6Option); relayOption.setPayload(payload); payload.setParent(relayOption); return relayOption; }; }
public static Deserializer<Dhcp6Option> deserializer() { return (data, offset, length) -> { Dhcp6Option dhcp6Option = Dhcp6Option.deserializer().deserialize(data, offset, length); Dhcp6ClientIdOption clientIdentifier = new Dhcp6ClientIdOption(); if (dhcp6Option.getLength() < DEFAULT_LEN) { throw new DeserializationException("Invalid length of Client Id option"); } Dhcp6Duid duid = Dhcp6Duid.deserializer().deserialize(dhcp6Option.getData(), 0, dhcp6Option.getLength()); clientIdentifier.setPayload(duid); return clientIdentifier; }; }
@Override public void send() { if (!this.block()) { if (outPacket() == null) { sendPacket(null); } else { try { Ethernet eth = Ethernet.deserializer() .deserialize(outPacket().data().array(), 0, outPacket().data().array().length); sendPacket(eth); } catch (DeserializationException e) { log.warn("Unable to deserialize packet"); } } } }