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]NACKs
resendDelay
[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
: Signature
FLAG_SIGNATURE_REQUESTED
: no option dataFLAG_FROM_INCLUDED
: Destination
FLAG_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 |
protected Destination |
_optionFrom |
protected Signature |
_optionSignature |
protected I2PSession |
_session |
static int |
DEFAULT_MAX_SIZE |
static int |
FLAG_CLOSE
The sender of this packet will not be sending any more payload data.
|
static int |
FLAG_DELAY_REQUESTED
This packet includes an explicit request for the recipient to delay
sending any packets with data for a given amount of time.
|
static int |
FLAG_ECHO
If set, this packet is a ping (if sendStreamId is set) or a
ping reply (if receiveStreamId is set).
|
static int |
FLAG_FROM_INCLUDED
This packet includes the full I2P destination of the packet's sender.
|
static int |
FLAG_MAX_PACKET_SIZE_INCLUDED
This packet includes a request that the recipient not send any
subsequent packets with payloads greater than a specific size.
|
static int |
FLAG_NO_ACK
If set, this packet doesn't really want to ack anything
|
static int |
FLAG_PROFILE_INTERACTIVE
If set, this packet is travelling as part of an interactive flow,
meaning it is more lag sensitive than throughput sensitive.
|
static int |
FLAG_RESET
This 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_INCLUDED
This packet contains a DSA signature from the packet's sender.
|
static int |
FLAG_SIGNATURE_REQUESTED
This packet wants the recipient to include signatures on subsequent
packets sent to the creator of this packet.
|
static int |
FLAG_SYNCHRONIZE
This 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_REQUEST |
static int |
MAX_PAYLOAD_SIZE |
static long |
MAX_STREAM_ID |
static int |
MIN_DELAY_CHOKE |
static int |
SEND_DELAY_CHOKE |
static long |
STREAM_ID_UNKNOWN
The 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() |
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() |
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 |
setOptionalFrom()
This sets the from field in the packet to the Destination for the session
provided in the constructor.
|
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() |
boolean |
verifySignature(I2PAppContext ctx,
Destination from,
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 final I2PSession _session
protected long[] _nacks
protected Signature _optionSignature
protected Destination _optionFrom
public static final long STREAM_ID_UNKNOWN
public static final long MAX_STREAM_ID
public static final int FLAG_SYNCHRONIZE
public static final int FLAG_CLOSE
public static final int FLAG_RESET
public static final int FLAG_SIGNATURE_INCLUDED
public static final int FLAG_SIGNATURE_REQUESTED
public static final int FLAG_FROM_INCLUDED
public static final int FLAG_DELAY_REQUESTED
public static final int FLAG_MAX_PACKET_SIZE_INCLUDED
public static final int FLAG_PROFILE_INTERACTIVE
public static final int FLAG_ECHO
public static final int FLAG_NO_ACK
public static final int DEFAULT_MAX_SIZE
protected static final int MAX_DELAY_REQUEST
public static final int MIN_DELAY_CHOKE
public static final int SEND_DELAY_CHOKE
public static final int MAX_PAYLOAD_SIZE
public Packet(I2PSession session)
public I2PSession getSession()
public long getSendStreamId()
public void setSendStreamId(long id)
public long getReceiveStreamId()
public void setReceiveStreamId(long id)
public long getSequenceNum()
public void setSequenceNum(long num)
public long getAckThrough()
public void setAckThrough(long id)
id
- if < 0, sets FLAG_NO_ACKpublic long[] getNacks()
public void setNacks(long[] nacks)
public int getResendDelay()
public void setResendDelay(int numSeconds)
public ByteArray getPayload()
public void setPayload(ByteArray payload)
public int getPayloadSize()
public void releasePayload()
public ByteArray acquirePayload()
public boolean isFlagSet(int flag)
flag
- bitmask of any flag(s)public 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 Signature getOptionalSignature()
public void setOptionalSignature(Signature sig)
public Destination getOptionalFrom()
public void setOptionalFrom()
public int getOptionalDelay()
public void setOptionalDelay(int delayMs)
public int getOptionalMaxSize()
public void setOptionalMaxSize(int numBytes)
public int getLocalPort()
public void setLocalPort(int port)
public int getRemotePort()
public void setRemotePort(int port)
public int writePacket(byte[] buffer, int offset) throws IllegalStateException
buffer
- 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 IllegalStateException
fakeSigLen
- if 0, include the real signature in _optionSignature;
if nonzero, leave space for that many bytesIllegalStateException
public void readPacket(byte[] buffer, int offset, int length) throws IllegalArgumentException
buffer
- 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 b0rkedpublic boolean verifySignature(I2PAppContext ctx, Destination from, byte[] buffer)
ctx
- Application contextfrom
- the Destination the data came frombuffer
- data to validate with signatureprotected StringBuilder formatAsString()
static final String toId(long id)
public void logTCPDump(Connection con)