Changeset 1a56d5a


Ignore:
Timestamp:
Jun 26, 2018 3:23:55 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
435bf819
Parents:
3d21ad0
Message:

NTCP2: I2NP support, fixes, cleanups

Location:
router/java/src/net/i2p/data/i2np
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/data/i2np/I2NPMessage.java

    r3d21ad0 r1a56d5a  
    128128     * the data is formatted so as to be self contained, with the type, size,
    129129     * expiration, unique id, as well as a checksum bundled along. 
    130      * Full 16 byte header.
     130     * Full 16 byte header for NTCP 1.
     131     *
     132     * @return the length written
    131133     */
    132134    public int toByteArray(byte buffer[]);
     135
     136    /**
     137     * write the message to the buffer, returning the number of bytes written.
     138     * the data is formatted so as to be self contained, with the type, size,
     139     * expiration, unique id, as well as a checksum bundled along. 
     140     * Full 16 byte header for NTCP 1.
     141     *
     142     * @param off the offset to start writing at
     143     * @return the new offset (NOT the length)
     144     * @since 0.9.36
     145     */
     146    public int toByteArray(byte buffer[], int off);
    133147
    134148    /**
     
    136150     * the data is is not self contained - it does not include the size,
    137151     * unique id, or any checksum, but does include the type and expiration.
    138      * Short 5 byte header.
     152     * Short 5 byte header for SSU.
     153     *
     154     * @return the length written
    139155     */
    140156    public int toRawByteArray(byte buffer[]);
     157
     158    /**
     159     * write the message to the buffer, returning the number of bytes written.
     160     * the data is is not self contained - it does not include the size,
     161     * unique id, or any checksum, but does include the type and expiration.
     162     * Short 9 byte header for NTCP 2.
     163     *
     164     * @param off the offset to start writing at
     165     * @return the new offset (NOT the length)
     166     * @since 0.9.36
     167     */
     168    public int toRawByteArrayNTCP2(byte buffer[], int off);
    141169}
  • router/java/src/net/i2p/data/i2np/I2NPMessageHandler.java

    r3d21ad0 r1a56d5a  
    5454            _lastReadBegin = System.currentTimeMillis();
    5555            I2NPMessage msg = I2NPMessageImpl.createMessage(_context, type);
    56             // can't be null
    57             //if (msg == null)
    58             //    throw new I2NPMessageException("The type "+ type + " is an unknown I2NP message");
    5956            try {
    6057                _lastSize = msg.readBytes(in, type, _messageBuffer);
     
    115112        _lastReadBegin = System.currentTimeMillis();
    116113        I2NPMessage msg = I2NPMessageImpl.createMessage(_context, type);
    117         // can't be null
    118         //if (msg == null) {
    119         //    int sz = data.length-offset;
    120         //   boolean allZero = false;
    121         //    for (int i = offset; i < data.length; i++) {
    122         //        if (data[i] != 0) {
    123         //            allZero = false;
    124         //            break;
    125         //        }
    126         //    }
    127         //    throw new I2NPMessageException("The type "+ type + " is an unknown I2NP message (remaining sz="
    128         //                                   + sz + " all zeros? " + allZero + ")");
    129         //}
    130114        try {
    131115            _lastSize = msg.readBytes(data, type, cur, maxLen - 1);
  • router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java

    r3d21ad0 r1a56d5a  
    5050                        + 2 // payload length
    5151                        + CHECKSUM_LENGTH;
    52 
    53     // Whether SSU used the full header or a truncated header.
    54     // We are stuck with the short header, can't change it now.
    55     //private static final boolean RAW_FULL_SIZE = false;
    5652
    5753    /** unused */
     
    293289     */
    294290    public synchronized int getRawMessageSize() {
    295         //if (RAW_FULL_SIZE)
    296         //    return getMessageSize();
    297         //else
    298291            return calculateWrittenLength()+5;
    299292    }
     
    311304    }
    312305
     306    /**
     307     * write the message to the buffer, returning the number of bytes written.
     308     * the data is formatted so as to be self contained, with the type, size,
     309     * expiration, unique id, as well as a checksum bundled along. 
     310     * Full 16 byte header for NTCP 1.
     311     *
     312     * @return the length written
     313     */
    313314    public int toByteArray(byte buffer[]) {
    314         try {
    315             int writtenLen = writeMessageBody(buffer, HEADER_LENGTH);
    316             int payloadLen = writtenLen - HEADER_LENGTH;
     315        return toByteArray(buffer, 0);
     316    }
     317
     318    /**
     319     * write the message to the buffer, returning the number of bytes written.
     320     * the data is formatted so as to be self contained, with the type, size,
     321     * expiration, unique id, as well as a checksum bundled along. 
     322     * Full 16 byte header for NTCP 1.
     323     *
     324     * @param off the offset to start writing at
     325     * @return the new offset (NOT the length)
     326     * @since 0.9.36 with off param
     327     */
     328    public int toByteArray(byte buffer[], int off) {
     329        int start = off;
     330        try {
     331            int rv = writeMessageBody(buffer, off + HEADER_LENGTH);
     332            int payloadLen = rv - (off + HEADER_LENGTH);
    317333            byte[] h = SimpleByteCache.acquire(Hash.HASH_LENGTH);
    318             _context.sha().calculateHash(buffer, HEADER_LENGTH, payloadLen, h, 0);
    319 
    320             int off = 0;
     334            _context.sha().calculateHash(buffer, off + HEADER_LENGTH, payloadLen, h, 0);
     335
    321336            DataHelper.toLong(buffer, off, 1, getType());
    322             off += 1;
     337            off++;
    323338
    324339            // Lazy initialization of value
     
    336351            SimpleByteCache.release(h);
    337352
    338             return writtenLen;
     353            return rv;
    339354        } catch (I2NPMessageException ime) {
    340355            _context.logManager().getLog(getClass()).log(Log.CRIT, "Error writing", ime);
     
    348363    /**
    349364     * write the message body to the output array, starting at the given index.
    350      * @return the index into the array after the last byte written
     365     * @return the index into the array after the last byte written (NOT the length)
    351366     */
    352367    protected abstract int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException;
    353 
    354     /*
    355     protected int toByteArray(byte out[], byte[][] prefix, byte[][] suffix) throws I2NPMessageException {
    356         int curIndex = 0;
    357         for (int i = 0; i < prefix.length; i++) {
    358             System.arraycopy(prefix[i], 0, out, curIndex, prefix[i].length);
    359             curIndex += prefix[i].length;
    360         }
    361 
    362         curIndex = writeMessageBody(out, curIndex);
    363 
    364         for (int i = 0; i < suffix.length; i++) {
    365             System.arraycopy(suffix[i], 0, out, curIndex, suffix[i].length);
    366             curIndex += suffix[i].length;
    367         }
    368 
    369         return curIndex;
    370     }
    371      */
    372 
    373368
    374369    /**
     
    376371     *  THe header consists of a one-byte type and a 4-byte expiration in seconds only.
    377372     *  Used by SSU only!
     373     *
     374     *  @return the new written length
    378375     */
    379376    public int toRawByteArray(byte buffer[]) {
    380         //if (RAW_FULL_SIZE)
    381         //    return toByteArray(buffer);
    382377        try {
    383378            int off = 0;
     
    395390    }
    396391
     392    /**
     393     * write the message to the buffer, returning the number of bytes written.
     394     * the data is is not self contained - it does not include the size,
     395     * unique id, or any checksum, but does include the type and expiration.
     396     * Short 9 byte header for NTCP 2.
     397     *
     398     * @param off the offset to start writing at
     399     * @return the new offset (NOT the length)
     400     * @since 0.9.36
     401     */
     402    public int toRawByteArrayNTCP2(byte buffer[], int off) {
     403        try {
     404            DataHelper.toLong(buffer, off, 1, getType());
     405            off += 1;
     406            // Lazy initialization of value
     407            if (_uniqueId < 0) {
     408                _uniqueId = _context.random().nextLong(MAX_ID_VALUE);
     409            }
     410            DataHelper.toLong(buffer, off, 4, _uniqueId);
     411            off += 4;
     412            // January 19 2038? No, unsigned, good until Feb. 7 2106
     413            // in seconds, round up so we don't lose time every hop
     414            DataHelper.toLong(buffer, off, 4, (_expiration + 500) / 1000);
     415            off += 4;
     416            return writeMessageBody(buffer, off);
     417        } catch (I2NPMessageException ime) {
     418            _context.logManager().getLog(getClass()).log(Log.CRIT, "Error writing", ime);
     419            throw new IllegalStateException("Unable to serialize the message " + getClass().getSimpleName(), ime);
     420        }
     421    }
     422
    397423    public void readMessage(byte data[], int offset, int dataSize, int type, I2NPMessageHandler handler) throws I2NPMessageException {
    398424        // ignore the handler (overridden in subclasses if necessary
     
    421447        offset++;
    422448        I2NPMessage msg = createMessage(ctx, type);
    423         if (msg == null)
    424             throw new I2NPMessageException("Unknown message type: " + type);
    425         //if (RAW_FULL_SIZE) {
    426         //    try {
    427         //        msg.readBytes(buffer, type, offset);
    428         //    } catch (IOException ioe) {
    429         //        throw new I2NPMessageException("Error reading the " + msg, ioe);
    430         //    }
    431         //    return msg;
    432         //}
    433449
    434450        try {
     
    457473        offset++;
    458474        I2NPMessage msg = createMessage(ctx, type);
    459         if (msg == null)
    460             throw new I2NPMessageException("Unknown message type: " + type);
    461475
    462476        try {
Note: See TracChangeset for help on using the changeset viewer.