pcap.codec.ndp.Redirect Maven / Gradle / Ivy
/** This code is licenced under the GPL version 2. */
package pcap.codec.ndp;
import pcap.codec.AbstractPacket;
import pcap.codec.Packet;
import pcap.codec.UnknownPacket;
import pcap.common.annotation.Inclubating;
import pcap.common.memory.Memory;
import pcap.common.net.Inet6Address;
import pcap.common.util.NamedNumber;
/** @author Ardika Rommy Sanjaya */
@Inclubating
public class Redirect extends AbstractPacket {
private final Header header;
private final Packet payload;
/**
* Build Redirect packet.
*
* @param builder builder.
*/
public Redirect(Builder builder) {
this.header = new Header(builder);
this.payload = null;
this.payloadBuffer = builder.payloadBuffer;
}
@Override
public Header header() {
return header;
}
@Override
public Packet payload() {
return payload;
}
public static class Header extends AbstractPacket.Header {
public static final byte REDIRECT_HEADER_LENGTH = 36;
private final Inet6Address targetAddress;
private final Inet6Address destinationAddress;
private final NeighborDiscoveryOptions options;
private final Builder builder;
private Header(Builder builder) {
this.targetAddress = builder.targetAddress;
this.destinationAddress = builder.destinationAddress;
this.options = builder.options;
this.buffer = slice(builder.buffer, length());
this.builder = builder;
}
public Inet6Address targetAddress() {
return targetAddress;
}
public Inet6Address destinationAddress() {
return destinationAddress;
}
public NeighborDiscoveryOptions options() {
return options;
}
@SuppressWarnings("TypeParameterUnusedInFormals")
@Override
public T payloadType() {
return (T) UnknownPacket.UNKNOWN_PAYLOAD_TYPE;
}
@Override
public int length() {
return REDIRECT_HEADER_LENGTH + options.header().length();
}
@Override
public Memory buffer() {
if (buffer == null) {
buffer = ALLOCATOR.allocate(length());
buffer.writeInt(0);
buffer.writeBytes(targetAddress.address());
buffer.writeBytes(destinationAddress.address());
buffer.writeBytes(options.header().buffer());
}
return buffer;
}
@Override
public Builder builder() {
return builder;
}
@Override
public String toString() {
return new StringBuilder()
.append("\ttargetAddress: ")
.append(targetAddress)
.append('\n')
.append("\tdestinationAddress: ")
.append(destinationAddress)
.append('\n')
.append("\toptions: ")
.append(options)
.append('\n')
.toString();
}
}
@Override
public String toString() {
return new StringBuilder("[ Redirect Header (")
.append(header().length())
.append(" bytes) ]")
.append('\n')
.append(header)
.append("\tpayload: ")
.append(payload != null ? payload.getClass().getSimpleName() : "")
.toString();
}
public static class Builder extends AbstractPacket.Builder {
private Inet6Address targetAddress = Inet6Address.ZERO;
private Inet6Address destinationAddress = Inet6Address.ZERO;
private NeighborDiscoveryOptions options;
private Memory buffer;
private Memory payloadBuffer;
public Builder targetAddrss(Inet6Address targetAddress) {
this.targetAddress = targetAddress;
return this;
}
public Builder destinationAddress(Inet6Address destinationAddress) {
this.destinationAddress = destinationAddress;
return this;
}
@Override
public Packet build() {
return new Redirect(this);
}
@Override
public Packet build(Memory buffer) {
buffer.readInt();
byte[] target = new byte[Inet6Address.IPV6_ADDRESS_LENGTH];
buffer.readBytes(target);
this.targetAddress = Inet6Address.valueOf(target);
byte[] destination = new byte[Inet6Address.IPV6_ADDRESS_LENGTH];
buffer.readBytes(destination);
this.destinationAddress = Inet6Address.valueOf(destination);
this.options =
(NeighborDiscoveryOptions) new NeighborDiscoveryOptions.Builder().build(buffer);
this.buffer = buffer;
this.payloadBuffer = buffer.slice();
return new Redirect(this);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy