class Packet extends Object
Contain a single packet transferred as part of a streaming connection. The data format is as follows:
sendStreamId [4 byte value]receiveStreamId [4 byte value]sequenceNum [4 byte unsigned integer]ackThrough [4 byte unsigned integer]NACKsresendDelay [1 byte integer]The flags field above specifies some metadata about the packet, and in turn may require certain additional data to be included. The flags are as follows (with any data structures specified added to the options area in the given order):
FLAG_SYNCHRONIZE: no option dataFLAG_CLOSE: no option dataFLAG_RESET: no option dataFLAG_SIGNATURE_INCLUDED: SignatureFLAG_SIGNATURE_REQUESTED: no option dataFLAG_FROM_INCLUDED: DestinationFLAG_DELAY_REQUESTED: 2 byte integerFLAG_MAX_PACKET_SIZE_INCLUDED: 2 byte integerFLAG_PROFILE_INTERACTIVE: no option dataFLAG_ECHO: no option dataFLAG_NO_ACK: no option data - this appears to be unused, we always ack, even for the first packetIf the signature is included, it uses the Destination's DSA key to sign the entire header and payload with the space in the options for the signature being set to all zeroes.
If the sequenceNum is 0 and the SYN is not set, this is a plain ACK packet that should not be ACKed
NOTE: All setters unsynchronized.| Modifier and Type | Field and Description |
|---|---|
protected long[] | _nacks_nacks. |
protected Signature | _offlineSignature_offlineSignature. |
protected Destination | _optionFrom_optionFrom. |
protected Signature | _optionSignature_optionSignature. |
protected I2PSession | _session_session. |
protected long | _transientExpires_transientExpires. |
protected SigningPublicKey | _transientSigningPublicKey_transientSigningPublicKey. |
static int | DEFAULT_MAX_SIZEDEFAULT_MAX_SIZE. |
static int | FLAG_CLOSEThe sender of this packet will not be sending any more payload data. |
static int | FLAG_DELAY_REQUESTEDThis packet includes an explicit request for the recipient to delay
sending any packets with data for a given amount of time. |
static int | FLAG_ECHOIf set, this packet is a ping (if sendStreamId is set) or a
ping reply (if receiveStreamId is set). |
static int | FLAG_FROM_INCLUDEDThis packet includes the full I2P destination of the packet's sender. |
static int | FLAG_MAX_PACKET_SIZE_INCLUDEDThis packet includes a request that the recipient not send any
subsequent packets with payloads greater than a specific size. |
static int | FLAG_NO_ACKIf set, this packet doesn't really want to ack anything |
static int | FLAG_PROFILE_INTERACTIVEIf set, this packet is travelling as part of an interactive flow,
meaning it is more lag sensitive than throughput sensitive. |
static int | FLAG_RESETThis packet is being sent to signify that the socket does not exist
(or, if in response to an initial synchronize packet, that the
connection was refused). |
static int | FLAG_SIGNATURE_INCLUDEDThis packet contains a DSA signature from the packet's sender. |
static int | FLAG_SIGNATURE_OFFLINEIf set, an offline signing block is in the options. |
static int | FLAG_SIGNATURE_REQUESTEDThis packet wants the recipient to include signatures on subsequent
packets sent to the creator of this packet. |
static int | FLAG_SYNCHRONIZEThis packet is creating a new socket connection (if the receiveStreamId
is STREAM_ID_UNKNOWN) or it is acknowledging a request to
create a connection and in turn is accepting the socket. |
protected static int | MAX_DELAY_REQUESTMAX_DELAY_REQUEST. |
static int | MAX_PAYLOAD_SIZEMAX_PAYLOAD_SIZE. |
static long | MAX_STREAM_IDMAX_STREAM_ID. |
static int | MIN_DELAY_CHOKEMIN_DELAY_CHOKE. |
static int | SEND_DELAY_CHOKESEND_DELAY_CHOKE. |
static long | STREAM_ID_UNKNOWNThe receiveStreamId will be set to this when the packet doesn't know
what ID will be assigned by the remote peer (aka this is the initial
synchronize packet) |
| Constructor and Description |
|---|
Packet(I2PSession session)Does no initialization. |
| Modifier and Type | Method and Description |
|---|---|
ByteArray | acquirePayload() |
protected StringBuilder | formatAsString()formatAsString. |
long | getAckThrough()The highest packet sequence number that received on the receiveStreamId. |
int | getLocalPort() |
long[] | getNacks()List of packet sequence numbers below the getAckThrough() value
have not been received. |
int | getOptionalDelay()How many milliseconds the sender of this packet wants the recipient
to wait before sending any more data (only valid if the flag for it is
set) |
Destination | getOptionalFrom()the sender of the packet (only included if the flag for it is set) |
int | getOptionalMaxSize()What is the largest payload the sender of this packet wants to receive? |
Signature | getOptionalSignature()The signature on the packet (only included if the flag for it is set)
Warning, may be typed wrong on incoming packets for EdDSA
before verifySignature() is called. |
ByteArray | getPayload()get the actual payload of the message. |
int | getPayloadSize() |
long | getReceiveStreamId()stream the replies should be sent on. |
int | getRemotePort() |
int | getResendDelay()How long is the creator of this packet going to wait before
resending this packet (if it hasn't yet been ACKed). |
long | getSendStreamId()what stream do we send data to the peer on? |
long | getSequenceNum()0-indexed sequence number for this Packet in the sendStream |
I2PSession | getSession() |
SigningPublicKey | getTransientSPK()Only if an offline signing block was included, else null |
boolean | isFlagSet(int flag)is a particular flag set on this packet? |
void | logTCPDump(Connection con)Generate a pcap/tcpdump-compatible format,
so we can use standard debugging tools. |
void | readPacket(byte[] buffer,
int offset,
int length)Read the packet from the buffer (starting at the offset) and return
the number of bytes read. |
void | releasePayload()does nothing right now |
void | setAckThrough(long id) |
void | setFlag(int flag) |
void | setFlag(int flag,
boolean set) |
void | setLocalPort(int port)Must be called to change the port, not set by readPacket()
as the port is out-of-band in the I2CP header. |
void | setNacks(long[] nacks) |
void | setOptionalDelay(int delayMs)Caller must also call setFlag(FLAG_DELAY_REQUESTED) |
void | setOptionalMaxSize(int numBytes)This also sets flag FLAG_MAX_PACKET_SIZE_INCLUDED |
void | setOptionalSignature(Signature sig)This also sets flag FLAG_SIGNATURE_INCLUDED |
void | setPayload(ByteArray payload) |
void | setReceiveStreamId(long id) |
void | setRemotePort(int port)Must be called to change the port, not set by readPacket()
as the port is out-of-band in the I2CP header. |
void | setResendDelay(int numSeconds)Unused. |
void | setSendStreamId(long id) |
void | setSequenceNum(long num) |
(package private) static String | toId(long id) |
String | toString()toString. |
boolean | verifySignature(I2PAppContext ctx,
byte[] buffer)Determine whether the signature on the data is valid. |
boolean | verifySignature(I2PAppContext ctx,
SigningPublicKey altSPK,
byte[] buffer)Determine whether the signature on the data is valid. |
int | writePacket(byte[] buffer,
int offset)Write the packet to the buffer (starting at the offset) and return
the number of bytes written. |
protected int | writePacket(byte[] buffer,
int offset,
int fakeSigLen) |
protected long[] _nacks
protected Signature _offlineSignature
protected Destination _optionFrom
protected Signature _optionSignature
protected final I2PSession _session
protected long _transientExpires
protected SigningPublicKey _transientSigningPublicKey
public static final int DEFAULT_MAX_SIZE
public static final int FLAG_CLOSE
public static final int FLAG_DELAY_REQUESTED
public static final int FLAG_ECHO
public static final int FLAG_FROM_INCLUDED
public static final int FLAG_MAX_PACKET_SIZE_INCLUDED
public static final int FLAG_NO_ACK
public static final int FLAG_PROFILE_INTERACTIVE
public static final int FLAG_RESET
public static final int FLAG_SIGNATURE_INCLUDED
public static final int FLAG_SIGNATURE_OFFLINE
public static final int FLAG_SIGNATURE_REQUESTED
public static final int FLAG_SYNCHRONIZE
protected static final int MAX_DELAY_REQUEST
public static final int MAX_PAYLOAD_SIZE
public static final long MAX_STREAM_ID
public static final int MIN_DELAY_CHOKE
public static final int SEND_DELAY_CHOKE
public static final long STREAM_ID_UNKNOWN
public Packet(I2PSession session)
public ByteArray acquirePayload()
protected StringBuilder formatAsString()
public long getAckThrough()
public int getLocalPort()
public long[] getNacks()
public int getOptionalDelay()
public Destination getOptionalFrom()
public int getOptionalMaxSize()
public Signature getOptionalSignature()
public ByteArray getPayload()
public int getPayloadSize()
public long getReceiveStreamId()
public int getRemotePort()
public int getResendDelay()
public long getSendStreamId()
public long getSequenceNum()
public I2PSession getSession()
public SigningPublicKey getTransientSPK()
public boolean isFlagSet(int flag)
flag - bitmask of any flag(s)public void logTCPDump(Connection con)
con - the connection, may be null for unknown connectionspublic void readPacket(byte[] buffer,
int offset,
int length)
throws IllegalArgumentExceptionbuffer - packet buffer containing the dataoffset - index into the buffer to start readignlength - how many bytes within the buffer past the offset are
part of the packet?IllegalArgumentException - if the data is b0rkedIndexOutOfBoundsException - if the data is b0rkedpublic void releasePayload()
public void setAckThrough(long id)
id - the ack through value to set, if < 0 sets FLAG_NO_ACKpublic void setFlag(int flag)
flag - bitmask of any flag(s)public void setFlag(int flag,
boolean set)flag - bitmask of any flag(s)set - true to set, false to clearpublic void setLocalPort(int port)
public void setNacks(long[] nacks)
nacks - list of packet sequence numbers not ACKed, or null if there are none.public void setOptionalDelay(int delayMs)
public void setOptionalMaxSize(int numBytes)
public void setOptionalSignature(Signature sig)
public void setPayload(ByteArray payload)
payload - the payload to set, or null to clearIllegalArgumentException - if the payload exceeds MAX_PAYLOAD_SIZEpublic void setReceiveStreamId(long id)
id - the receive stream ID to setRuntimeException - if the receive stream ID has already been set to a different valuepublic void setRemotePort(int port)
public void setResendDelay(int numSeconds)
public void setSendStreamId(long id)
id - the send stream ID to setRuntimeException - if the send stream ID has already been set to a different valuepublic void setSequenceNum(long num)
num - the sequence number to setstatic final String toId(long id)
id - the stream ID to formatpublic boolean verifySignature(I2PAppContext ctx, byte[] buffer)
ctx - Application contextbuffer - data to validate with signature, or null to use our own buffer.public boolean verifySignature(I2PAppContext ctx, SigningPublicKey altSPK, byte[] buffer)
ctx - Application contextaltSPK - Signing key to verify with, ONLY if there is no FROM field in this packet.
May be the SPK from a FROM field or offline sig field from a previous packet on this connection.
Ignored if this packet contains a FROM option block.
Null ok if none available.buffer - data to validate with signature, or null to use our own buffer.public int writePacket(byte[] buffer,
int offset)
throws IllegalStateExceptionbuffer - bytes to write to a destinationoffset - starting point in the buffer to sendIllegalStateException - if there is data missing or otherwise b0rkedprotected int writePacket(byte[] buffer,
int offset,
int fakeSigLen)
throws IllegalStateExceptionbuffer - bytes to write to a destinationoffset - starting point in the buffer to sendfakeSigLen - if 0, include the real signature in _optionSignature;
if nonzero, leave space for that many bytesIllegalStateException - if there is data missing or otherwise b0rked