Changeset a3e16614


Ignore:
Timestamp:
Nov 8, 2015 6:30:51 PM (5 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
e120a8a
Parents:
bdde11c
Message:

SSU: Prep for extended options

Location:
router/java/src/net/i2p/router/transport/udp
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/transport/udp/PacketBuilder.java

    rbdde11c ra3e16614  
    784784     */
    785785    public UDPPacket buildSessionRequestPacket(OutboundEstablishState state) {
     786        // TODO
     787        //byte[] options = new byte[3];
     788        //UDPPacket packet = buildPacketHeader(SESSION_REQUEST_FLAG_BYTE, options);
    786789        UDPPacket packet = buildPacketHeader(SESSION_REQUEST_FLAG_BYTE);
    787790        DatagramPacket pkt = packet.getPacket();
    788791        byte data[] = pkt.getData();
    789         int off = HEADER_SIZE;
     792        int off = HEADER_SIZE; // + 1 + options.length;
    790793
    791794        byte toIP[] = state.getSentIP();
     
    14341437     *  Create a new packet and add the flag byte and the time stamp.
    14351438     *  Caller should add data starting at HEADER_SIZE.
    1436      *  At this point, adding support for extended options and rekeying is unlikely,
    1437      *  but if we do, we'll have to change this.
     1439     *  Does not include extended options or rekeying.
    14381440     *
    14391441     *  @param flagByte contains type and flags
     
    14411443     */
    14421444    private UDPPacket buildPacketHeader(byte flagByte) {
     1445        return buildPacketHeader(flagByte, null);
     1446    }
     1447
     1448    /**
     1449     *  Create a new packet and add the flag byte and the time stamp.
     1450     *  Caller should add data starting at HEADER_SIZE.
     1451     *  (if extendedOptions != null, at HEADER_SIZE + 1 + extendedOptions.length)
     1452     *  Does not include rekeying.
     1453     *
     1454     *  @param flagByte contains type and flags
     1455     *  @param extendedOptions May be null. If non-null, we will add the associated flag here.
     1456     *                         255 bytes max.
     1457     *  @since 0.9.24
     1458     */
     1459    private UDPPacket buildPacketHeader(byte flagByte, byte[] extendedOptions) {
    14431460        UDPPacket packet = UDPPacket.acquire(_context, false);
    14441461        byte data[] = packet.getPacket().getData();
     
    14471464       
    14481465        // header
     1466        if (extendedOptions != null)
     1467            flagByte |= UDPPacket.HEADER_FLAG_EXTENDED_OPTIONS;
    14491468        data[off] = flagByte;
    14501469        off++;
    14511470        long now = (_context.clock().now() + 500) / 1000;
    14521471        DataHelper.toLong(data, off, 4, now);
    1453         // todo: add support for rekeying and extended options
     1472        // todo: add support for rekeying
     1473        // extended options
     1474        if (extendedOptions != null) {
     1475            off+= 4;
     1476            int len = extendedOptions.length;
     1477            if (len > 255)
     1478                throw new IllegalArgumentException();
     1479            data[off++] = (byte) len;
     1480            System.arraycopy(extendedOptions, 0, data, off, len);
     1481        }
    14541482        return packet;
    14551483    }
  • router/java/src/net/i2p/router/transport/udp/UDPPacket.java

    rbdde11c ra3e16614  
    9090    public static final int PAYLOAD_TYPE_SESSION_DESTROY = 8;
    9191   
     92    // various flag fields for use in the header
     93    /**
     94     *  Defined in the spec from the beginning, Unused
     95     *  @since 0.9.24
     96     */
     97    public static final byte HEADER_FLAG_REKEY = (1 << 3);
     98    /**
     99     *  Defined in the spec from the beginning, Used starting in 0.9.24
     100     *  @since 0.9.24
     101     */
     102    public static final byte HEADER_FLAG_EXTENDED_OPTIONS = (1 << 2);
     103
    92104    // various flag fields for use in the data packets
    93105    public static final byte DATA_FLAG_EXPLICIT_ACK = (byte)(1 << 7);
    94106    public static final byte DATA_FLAG_ACK_BITFIELDS = (1 << 6);
    95     // unused
     107    /** unused */
    96108    public static final byte DATA_FLAG_ECN = (1 << 4);
    97109    public static final byte DATA_FLAG_WANT_ACKS = (1 << 3);
    98110    public static final byte DATA_FLAG_WANT_REPLY = (1 << 2);
    99     // unused
     111    /** unused */
    100112    public static final byte DATA_FLAG_EXTENDED = (1 << 1);
    101113   
  • router/java/src/net/i2p/router/transport/udp/UDPPacketReader.java

    rbdde11c ra3e16614  
    6868    }
    6969   
    70     /** does this packet include rekeying data? */
    71     public boolean readRekeying() {
    72         return (_message[_payloadBeginOffset] & (1 << 3)) != 0;
    73     }
    74    
    75     public boolean readExtendedOptionsIncluded() {
    76         return (_message[_payloadBeginOffset] & (1 << 2)) != 0;
     70    /**
     71     * Does this packet include rekeying data in the header?
     72     * Unused, should always be false.
     73     */
     74    public boolean isRekeyingIncluded() {
     75        return (_message[_payloadBeginOffset] & UDPPacket.HEADER_FLAG_REKEY) != 0;
     76    }
     77   
     78    /**
     79     * Does this packet include extended options in the header?
     80     */
     81    public boolean isExtendedOptionsIncluded() {
     82        return (_message[_payloadBeginOffset] & UDPPacket.HEADER_FLAG_EXTENDED_OPTIONS) != 0;
    7783    }
    7884   
     
    8288    }
    8389   
    84     public void readKeyingMaterial(byte target[], int targetOffset) {
    85         if (!readRekeying())
    86             throw new IllegalStateException("This packet is not rekeying!");
    87         System.arraycopy(_message, _payloadBeginOffset + 1 + 4, target, targetOffset, KEYING_MATERIAL_LENGTH);
     90    /**
     91     * Returns rekeying data (64 bytes), or null if none.
     92     * Unused, should always return null.
     93     *
     94     * @deprecated unused
     95     */
     96    @Deprecated
     97    public byte[] readKeyingMaterial() {
     98        if (!isRekeyingIncluded())
     99            return null;
     100        byte[] rv = new byte[KEYING_MATERIAL_LENGTH];
     101        System.arraycopy(_message, _payloadBeginOffset + 1 + 4, rv, 0, KEYING_MATERIAL_LENGTH);
     102        return rv;
     103    }
     104   
     105    /**
     106     * Returns extended option data, 0-255 bytes, or null if none.
     107     *
     108     * @return extended options or null if none is included
     109     * @since 0.9.24
     110     */
     111    public byte[] readExtendedOptions() {
     112        if (!isExtendedOptionsIncluded())
     113            return null;
     114        int offset = _payloadBeginOffset + 1 + 4;
     115        if (isRekeyingIncluded())
     116            offset += KEYING_MATERIAL_LENGTH;
     117        int optionsSize = _message[offset++] & 0xff;
     118        byte[] rv = new byte[optionsSize];
     119        System.arraycopy(_message, offset, rv, 0, optionsSize);
     120        return rv;
    88121    }
    89122   
     
    91124    private int readBodyOffset() {
    92125        int offset = _payloadBeginOffset + 1 + 4;
    93         if (readRekeying())
     126        if (isRekeyingIncluded())
    94127            offset += KEYING_MATERIAL_LENGTH;
    95         if (readExtendedOptionsIncluded()) {
    96             int optionsSize = (int)DataHelper.fromLong(_message, offset, 1);
     128        if (isExtendedOptionsIncluded()) {
     129            int optionsSize = _message[offset] & 0xff;
    97130            offset += optionsSize + 1;
    98131        }
Note: See TracChangeset for help on using the changeset viewer.