java.lang.Object
net.i2p.router.transport.ntcp.NTCP2Payload
NTCP 2 payload generation and parsing utilities.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBase class for NTCP 2 payload blocks to be transmitted.static classBlock containing the current date and time.static classBlock containing I2NP message.static classBlock containing options data.static classBlock containing padding data.static interfaceFor all callbacks, recommend throwing exceptions only from the handshake.static classBlock containing router information.static classBlock containing termination data.Field Summary
Modifier and TypeFieldDescriptionprivate static final intBlock type of the date and time.static final intBlock header size in bytes.private static final intBlock type of the I2NP message.private static final intBlock type of the session options.private static final intBlock type of the padding.private static final intBlock type of the router info.private static final intBlock type of the termination reason.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate static voidcheckBlockOrder(boolean isHandshake, int blocks, boolean gotPadding, boolean gotTermination, int type) Check the payload-wide ordering rules for the next block: no block may follow padding, only padding may follow termination, and the first block of a handshake frame must be a RouterInfo.private static voidcheckFrameOverflow(int off, int length, int end, int i, int len, int blocks, int type) Check that the block fits inside the frame.private static voidcheckHandshakeOnly(boolean isHandshake, int type) Check that the block type is not allowed in a handshake frame.private static voidprocessDateTime(NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len) Parse a DATETIME block (exactly 4 bytes) and report it to the callback.private static voidprocessI2NP(I2PAppContext ctx, NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len) Parse an I2NP block (at least 9 bytes) and report it to the callback.private static voidprocessOptions(NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len, boolean isHandshake) Copy an OPTIONS block and report it to the callback.static intprocessPayload(I2PAppContext ctx, NTCP2Payload.PayloadCallback cb, byte[] payload, int off, int length, boolean isHandshake) Incoming payload.private static voidprocessRouterInfo(I2PAppContext ctx, NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len, boolean isHandshake) Deserialize a ROUTERINFO block (a 1-byte flag then the RouterInfo) and report it to the callback.private static booleanprocessTermination(NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len) Parse a TERMINATION block (at least 9 bytes) and report it to the callback.static intwritePayload(byte[] payload, int off, List<NTCP2Payload.Block> blocks) Write the blocks out to the frame, in list order.
Field Details
BLOCK_HEADER_SIZE
BLOCK_DATETIME
private static final int BLOCK_DATETIMEBlock type of the date and time.- See Also:
BLOCK_OPTIONS
private static final int BLOCK_OPTIONSBlock type of the session options.- See Also:
BLOCK_ROUTERINFO
private static final int BLOCK_ROUTERINFOBlock type of the router info.- See Also:
BLOCK_I2NP
BLOCK_TERMINATION
private static final int BLOCK_TERMINATIONBlock type of the termination reason.- See Also:
BLOCK_PADDING
Constructor Details
NTCP2Payload
NTCP2Payload()Not instantiated - every member below is static.
Method Details
processPayload
public static int processPayload(I2PAppContext ctx, NTCP2Payload.PayloadCallback cb, byte[] payload, int off, int length, boolean isHandshake) throws IOException, DataFormatException, I2NPMessageException Incoming payload. Calls the callback for each received block.- Parameters:
ctx- used to deserialize the RouterInfo and I2NP blockscb- receives each block in the order it appears in the framepayload- the frame bytes, holding a sequence of blocksoff- index in payload of the first block's type bytelength- how many bytes of payload to parse, bounding the block walkisHandshake- true only for message 3 part 2, which allows only RouterInfo, options and padding- Returns:
- number of blocks processed
- Throws:
IOException- on major errorsDataFormatException- on parsing of individual blocksI2NPMessageException- on parsing of I2NP block
checkBlockOrder
private static void checkBlockOrder(boolean isHandshake, int blocks, boolean gotPadding, boolean gotTermination, int type) throws IOException Check the payload-wide ordering rules for the next block: no block may follow padding, only padding may follow termination, and the first block of a handshake frame must be a RouterInfo.- Parameters:
isHandshake- true for the handshake frame, where the block is not allowedblocks- blocks already processed in this framegotPadding- whether padding was already seengotTermination- whether termination was already seentype- the next block type- Throws:
IOException- if ordering is violated
checkFrameOverflow
private static void checkFrameOverflow(int off, int length, int end, int i, int len, int blocks, int type) throws IOException Check that the block fits inside the frame. Called with i pointing just past the 3-byte header, so the block's offset isi - 3 - off. The frame contents are deliberately not included, they may be decrypted data; callers that want a dump log it at DEBUG.- Parameters:
off- index in payload of the first block's type byte, for the error messagelength- frame size in bytes, for the error messageend- exclusive end index of the frame, i.e. off + lengthi- index of the block's first data byte, just past its 3-byte headerlen- the block's data length, as read from its headerblocks- blocks already processed in this frame, for the error messagetype- the current block's type, for the error message- Throws:
IOException- if the block runs over the end of the frame
checkHandshakeOnly
Check that the block type is not allowed in a handshake frame.- Parameters:
isHandshake- true for the handshake frame, where the block is not allowedtype- the block's type, for the error message- Throws:
IOException- if the block appears in a handshake frame
processDateTime
private static void processDateTime(NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len) throws IOException, DataFormatException Parse a DATETIME block (exactly 4 bytes) and report it to the callback.- Parameters:
cb- receives the peer's clock, in millisecondspayload- the frame bytesi- index of the block's first data bytelen- the block's data length, must be exactly 4- Throws:
IOException- if the length is not 4DataFormatException- if the callback cannot process the block
processOptions
private static void processOptions(NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len, boolean isHandshake) throws DataFormatException Copy an OPTIONS block and report it to the callback.- Parameters:
cb- receives a copy of the block's datapayload- the frame bytesi- index of the block's first data bytelen- the block's data lengthisHandshake- true for the handshake frame, passed on to the callback- Throws:
DataFormatException- if the callback cannot process the block
processRouterInfo
private static void processRouterInfo(I2PAppContext ctx, NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len, boolean isHandshake) throws IOException, DataFormatException Deserialize a ROUTERINFO block (a 1-byte flag then the RouterInfo) and report it to the callback.- Parameters:
ctx- used to deserialize the RouterInfocb- receives the RouterInfo and whether the peer is a floodfillpayload- the frame bytesi- index of the block's first data byte, the 1-byte flaglen- the block's data length, including the flag byteisHandshake- true for the handshake frame, passed on to the callback- Throws:
IOException- if the RouterInfo cannot be readDataFormatException- if the RouterInfo is too big, or the callback rejects it
processI2NP
private static void processI2NP(I2PAppContext ctx, NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len) throws IOException, I2NPMessageException Parse an I2NP block (at least 9 bytes) and report it to the callback.- Parameters:
ctx- used to deserialize the messagecb- receives the messagepayload- the frame bytesi- index of the block's first data bytelen- the block's data length, at least 9- Throws:
IOException- if the block is shorter than 9 bytesI2NPMessageException- if the message cannot be deserialized, or the callback cannot process it
processTermination
private static boolean processTermination(NTCP2Payload.PayloadCallback cb, byte[] payload, int i, int len) throws IOException Parse a TERMINATION block (at least 9 bytes) and report it to the callback.- Parameters:
cb- receives the reason and the peer's last received date and timepayload- the frame bytesi- index of the block's first data bytelen- the block's data length, at least 9- Returns:
- true, so the caller records that termination has been seen
- Throws:
IOException- if the block is shorter than 9 bytes
writePayload
Write the blocks out to the frame, in list order.- Parameters:
payload- writes to it starting at offoff- index in payload to write the first block's header atblocks- each one written at the offset the previous one ended at- Returns:
- the new offset