Changeset 5c3f264 for router


Ignore:
Timestamp:
Jun 3, 2018 1:12:21 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
98019bb
Parents:
02855a0
Message:

NTCP: More efficient copying of inbound establish state data
More prep for NTCP2

Location:
router/java/src/net/i2p/router/transport/ntcp
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/transport/ntcp/EventPumper.java

    r02855a0 r5c3f264  
    409409        }
    410410    }
    411    
     411
    412412    /**
    413413     *  Called by the connection when it has data ready to write.
     
    416416     */
    417417    public void wantsWrite(NTCPConnection con, byte data[]) {
    418         ByteBuffer buf = ByteBuffer.wrap(data);
    419         FIFOBandwidthLimiter.Request req = _context.bandwidthLimiter().requestOutbound(data.length, 0, "NTCP write");//con, buf);
     418        wantsWrite(con, data, 0, data.length);
     419    }
     420
     421    /**
     422     *  Called by the connection when it has data ready to write.
     423     *  If we have bandwidth, calls con.Write() which calls wantsWrite(con).
     424     *  If no bandwidth, calls con.queuedWrite().
     425     *
     426     *  @since 0.9.35 off/len version
     427     */
     428    public void wantsWrite(NTCPConnection con, byte data[], int off, int len) {
     429        ByteBuffer buf = ByteBuffer.wrap(data, off, len);
     430        FIFOBandwidthLimiter.Request req = _context.bandwidthLimiter().requestOutbound(len, 0, "NTCP write");//con, buf);
    420431        if (req.getPendingRequested() > 0) {
    421432            if (_log.shouldLog(Log.INFO))
    422                 _log.info("queued write on " + con + " for " + data.length);
     433                _log.info("queued write on " + con + " for " + len);
    423434            _context.statManager().addRateData("ntcp.wantsQueuedWrite", 1);
    424435            con.queuedWrite(buf, req);
  • router/java/src/net/i2p/router/transport/ntcp/InboundEstablishState.java

    r02855a0 r5c3f264  
    2828class InboundEstablishState extends EstablishBase {
    2929
    30     /**
    31      * next index in _curEncrypted to write to (equals _curEncrypted length if the block is
    32      * ready to decrypt)
    33      */
    34     private int _curEncryptedOffset;
    35 
    3630    /** current encrypted block we are reading (IB only) or an IV buf used at the end for OB */
    3731    private byte _curEncrypted[];
     
    4135
    4236    /** contains the decrypted aliceIndexSize + aliceIdent + tsA + padding + aliceSig */
    43     private ByteArrayOutputStream _sz_aliceIdent_tsA_padding_aliceSig;
     37    private final ByteArrayOutputStream _sz_aliceIdent_tsA_padding_aliceSig;
    4438
    4539    /** how long we expect _sz_aliceIdent_tsA_padding_aliceSig to be when its full */
    4640    private int _sz_aliceIdent_tsA_padding_aliceSigSize;
     41
     42    private static final int NTCP1_MSG1_SIZE = XY_SIZE + HXY_SIZE;
    4743   
    4844    public InboundEstablishState(RouterContext ctx, NTCPTransport transport, NTCPConnection con) {
     
    7672     *  @since 0.9.35
    7773     */
    78     public int getVersion() { return 1; }
     74    public int getVersion() {
     75        if (!_transport.isNTCP2Enabled())
     76            return 1;
     77        synchronized (_stateLock) {
     78            if (_state == State.IB_INIT)
     79                return 0;
     80            // TODO NTCP2 states
     81            return 1;
     82        }
     83    }
    7984
    8085    /**
     
    9196     */
    9297    private void receiveInbound(ByteBuffer src) {
    93         while (_state == State.IB_INIT && src.hasRemaining()) {
    94             byte c = src.get();
    95             _X[_received++] = c;
    96             if (_received >= XY_SIZE)
    97                 changeState(State.IB_GOT_X);
     98        if (_state == State.IB_INIT && src.hasRemaining()) {
     99            int remaining = src.remaining();
     100            //if (remaining < NTCP1_MSG1_SIZE && _transport.isNTCP2Enabled()) {
     101            //    // NTCP2
     102            //}
     103            int toGet = Math.min(remaining, XY_SIZE - _received);
     104            src.get(_X, _received, toGet);
     105            _received += toGet;
     106            if (_received < XY_SIZE)
     107                return;
     108            changeState(State.IB_GOT_X);
     109            _received = 0;
    98110        }
    99         while (_state == State.IB_GOT_X && src.hasRemaining()) {
    100             int i = _received - XY_SIZE;
    101             _received++;
    102             byte c = src.get();
    103             _hX_xor_bobIdentHash[i] = c;
    104             if (i >= HXY_SIZE - 1)
    105                 changeState(State.IB_GOT_HX);
     111
     112        if (_state == State.IB_GOT_X && src.hasRemaining()) {
     113            int toGet = Math.min(src.remaining(), HXY_SIZE - _received);
     114            src.get(_hX_xor_bobIdentHash, _received, toGet);
     115            _received += toGet;
     116            if (_received < HXY_SIZE)
     117                return;
     118            changeState(State.IB_GOT_HX);
     119            _received = 0;
    106120        }
    107121
     
    185199
    186200                // Collect a 16-byte block
    187                 while (_curEncryptedOffset < AES_SIZE && src.hasRemaining()) {
    188                     _curEncrypted[_curEncryptedOffset++] = src.get();
    189                     _received++;
     201                if (_received < AES_SIZE && src.hasRemaining()) {
     202                    int toGet = Math.min(src.remaining(), AES_SIZE - _received);
     203                    src.get(_curEncrypted, _received, toGet);
     204                    _received += toGet;
     205                    if (_received < AES_SIZE) {
     206                        // no more bytes available in the buffer, and only a partial
     207                        // block was read, so we can't decrypt it.
     208                        if (_log.shouldLog(Log.DEBUG))
     209                            _log.debug(prefix() + "end of available data with only a partial block read (" +
     210                                       + _received + ")");
     211                        return;
     212                    }
    190213                }
    191214                // Decrypt the 16-byte block
    192                 if (_curEncryptedOffset >= AES_SIZE) {
     215                if (_received >= AES_SIZE) {
    193216                    _context.aes().decrypt(_curEncrypted, 0, _curDecrypted, 0, _dh.getSessionKey(),
    194217                                           _prevEncrypted, 0, AES_SIZE);
     
    197220                    _prevEncrypted = _curEncrypted;
    198221                    _curEncrypted = swap;
    199                     _curEncryptedOffset = 0;
     222                    _received = 0;
    200223
    201224                    if (_state == State.IB_SENT_Y) { // we are on the first decrypted block
     
    270293                    }
    271294                } else {
    272                     // no more bytes available in the buffer, and only a partial
    273                     // block was read, so we can't decrypt it.
    274                     if (_log.shouldLog(Log.DEBUG))
    275                         _log.debug(prefix() + "end of available data with only a partial block read (" +
    276                                    _curEncryptedOffset + ", " + _received + ")");
    277295                }
    278296        }
  • router/java/src/net/i2p/router/transport/ntcp/NTCP2Payload.java

    r02855a0 r5c3f264  
    125125     *  @return the new offset
    126126     */
    127     public int writePayload(byte[] payload, int off, List<Block> blocks) {
     127    public static int writePayload(byte[] payload, int off, List<Block> blocks) {
    128128        for (Block block : blocks) {
    129129            off = block.write(payload, off);
  • router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java

    r02855a0 r5c3f264  
    121121    private static final String NTCP2_PROTO_SHORT = "NXK2CS";
    122122    private static final String OPT_NTCP2_SK = 'N' + NTCP2_PROTO_SHORT + "2s";
    123     private static final int NTCP2_INT_VERSION = 2;
     123    static final int NTCP2_INT_VERSION = 2;
    124124    private static final String NTCP2_VERSION = Integer.toString(NTCP2_INT_VERSION);
    125125    /** b64 static private key */
     
    10921092
    10931093    /**
     1094     * The static priv key
     1095     *
     1096     * @since 0.9.35
     1097     */
     1098    byte[] getNTCP2StaticPrivkey() {
     1099        return _ntcp2StaticPrivkey;
     1100    }
     1101
     1102    /**
    10941103     * Get the valid NTCP version of this NTCP address.
    10951104     *
Note: See TracChangeset for help on using the changeset viewer.