Changeset 598ef67


Ignore:
Timestamp:
Nov 10, 2013 3:27:28 PM (6 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
90c2e08
Parents:
1b9d870
Message:

Tunnel fragments:

  • more checks, don't allow fragment #0 in follow-on fragment
  • Set message ID in constructor
Location:
router/java/src/net/i2p/router/tunnel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/tunnel/FragmentHandler.java

    r1b9d870 r598ef67  
    364364                msg = _fragmentedMessages.get(Long.valueOf(messageId));
    365365                if (msg == null) {
    366                     msg = new FragmentedMessage(_context);
     366                    msg = new FragmentedMessage(_context, messageId);
    367367                    _fragmentedMessages.put(Long.valueOf(messageId), msg);
    368368                }
     
    371371            // synchronized is required, fragments may be arriving in different threads
    372372            synchronized(msg) {
    373                 boolean ok = msg.receive(messageId, preprocessed, offset, size, false, router, tunnelId);
     373                boolean ok = msg.receive(preprocessed, offset, size, false, router, tunnelId);
    374374                if (!ok) return -1;
    375375                if (msg.isComplete()) {
     
    431431            msg = _fragmentedMessages.get(Long.valueOf(messageId));
    432432            if (msg == null) {
    433                 msg = new FragmentedMessage(_context);
     433                msg = new FragmentedMessage(_context, messageId);
    434434                _fragmentedMessages.put(Long.valueOf(messageId), msg);
    435435            }
     
    438438        // synchronized is required, fragments may be arriving in different threads
    439439        synchronized(msg) {
    440             boolean ok = msg.receive(messageId, fragmentNum, preprocessed, offset, size, isLast);
     440            boolean ok = msg.receive(fragmentNum, preprocessed, offset, size, isLast);
    441441            if (!ok) return -1;
    442442           
  • router/java/src/net/i2p/router/tunnel/FragmentedMessage.java

    r1b9d870 r598ef67  
    2020    private final I2PAppContext _context;
    2121    private final Log _log;
    22     private long _messageId;
     22    private final long _messageId;
    2323    private Hash _toRouter;
    2424    private TunnelId _toTunnel;
     
    3434    // 64 is pretty absurd, 32 is too, most likely
    3535    private static final int MAX_FRAGMENTS = 64;
    36    
    37     public FragmentedMessage(I2PAppContext ctx) {
     36    private static final int MAX_FRAGMENT_SIZE = 996;
     37   
     38    public FragmentedMessage(I2PAppContext ctx, long messageId) {
    3839        _context = ctx;
    3940        _log = ctx.logManager().getLog(FragmentedMessage.class);
    40         _messageId = -1;
     41        _messageId = messageId;
    4142        _fragments = new ByteArray[MAX_FRAGMENTS];
    4243        _highFragmentNum = -1;
     
    4950     * prior to the fragment # 0.
    5051     *
    51      * @param messageId what messageId is this fragment a part of
    52      * @param fragmentNum sequence number within the message (must be greater than 1)
    53      * @param payload data for the fragment
     52     * @param fragmentNum sequence number within the message (1 - 63)
     53     * @param payload data for the fragment non-null
    5454     * @param offset index into the payload where the fragment data starts (past headers/etc)
    5555     * @param length how much past the offset should we snag?
    5656     * @param isLast is this the last fragment in the message?
    5757     */
    58     public boolean receive(long messageId, int fragmentNum, byte payload[], int offset, int length, boolean isLast) {
    59         if (fragmentNum < 0) {
     58    public boolean receive(int fragmentNum, byte payload[], int offset, int length, boolean isLast) {
     59        if (fragmentNum <= 0 || fragmentNum >= MAX_FRAGMENTS) {
    6060            if (_log.shouldLog(Log.ERROR))
    61                 _log.error("Fragment # == " + fragmentNum + " for messageId " + messageId);
    62             return false;
    63         }
    64         if (payload == null) {
    65             if (_log.shouldLog(Log.ERROR))
    66                 _log.error("Payload is null for messageId " + messageId);
    67             return false;
    68         }
    69         if (length <= 0) {
     61                _log.error("Fragment # == " + fragmentNum + " for messageId " + _messageId);
     62            return false;
     63        }
     64        if (length <= 0 || length > MAX_FRAGMENT_SIZE) {
    7065            if (_log.shouldLog(Log.WARN))
    71                 _log.warn("Length is impossible (" + length + ") for messageId " + messageId);
     66                _log.warn("Length is impossible (" + length + ") for messageId " + _messageId);
    7267            return false;
    7368        }
    7469        if (offset + length > payload.length) {
    7570            if (_log.shouldLog(Log.WARN))
    76                 _log.warn("Length is impossible (" + length + "/" + offset + " out of " + payload.length + ") for messageId " + messageId);
     71                _log.warn("Length is impossible (" + length + "/" + offset + " out of " + payload.length + ") for messageId " + _messageId);
    7772            return false;
    7873        }
    7974        if (_log.shouldLog(Log.DEBUG))
    80             _log.debug("Receive message " + messageId + " fragment " + fragmentNum + " with " + length + " bytes (last? " + isLast + ") offset = " + offset);
    81         _messageId = messageId;
     75            _log.debug("Receive message " + _messageId + " fragment " + fragmentNum + " with " + length + " bytes (last? " + isLast + ") offset = " + offset);
    8276        // we should just use payload[] and use an offset/length on it
    8377        ByteArray ba = _cache.acquire(); //new ByteArray(payload, offset, length); //new byte[length]);
     
    9690        if (isLast && fragmentNum <= 0) {
    9791            if (_log.shouldLog(Log.ERROR))
    98                 _log.error("hmm, isLast and fragmentNum=" + fragmentNum + " for message " + messageId);
     92                _log.error("hmm, isLast and fragmentNum=" + fragmentNum + " for message " + _messageId);
    9993            return false;
    10094        }
     
    10397   
    10498    /**
    105      * Receive the first fragment and related metadata.  This may not be the first
     99     * Receive the first fragment (#0) and related metadata.  This may not be the first
    106100     * one to arrive at the endpoint however.
    107101     *
    108      * @param messageId what messageId is this fragment a part of
    109      * @param payload data for the fragment
     102     * @param payload data for the fragment non-null
    110103     * @param offset index into the payload where the fragment data starts (past headers/etc)
    111104     * @param length how much past the offset should we snag?
     
    114107     * @param toTunnel what tunnel is this destined for (may be null)
    115108     */
    116     public boolean receive(long messageId, byte payload[], int offset, int length, boolean isLast, Hash toRouter, TunnelId toTunnel) {
    117         if (payload == null) {
    118             if (_log.shouldLog(Log.ERROR))
    119                 _log.error("Payload is null for messageId " + messageId);
    120             return false;
    121         }
    122         if (length <= 0) {
     109    public boolean receive(byte payload[], int offset, int length, boolean isLast, Hash toRouter, TunnelId toTunnel) {
     110        if (length <= 0 || length > MAX_FRAGMENT_SIZE) {
    123111            if (_log.shouldLog(Log.WARN))
    124                 _log.warn("Length is impossible (" + length + ") for messageId " + messageId);
     112                _log.warn("Length is impossible (" + length + ") for messageId " + _messageId);
    125113            return false;
    126114        }
    127115        if (offset + length > payload.length) {
    128116            if (_log.shouldLog(Log.WARN))
    129                 _log.warn("Length is impossible (" + length + "/" + offset + " out of " + payload.length + ") for messageId " + messageId);
     117                _log.warn("Length is impossible (" + length + "/" + offset + " out of " + payload.length + ") for messageId " + _messageId);
    130118            return false;
    131119        }
    132120        if (_log.shouldLog(Log.DEBUG))
    133             _log.debug("Receive message " + messageId + " with " + length + " bytes (last? " + isLast + ") targetting " + toRouter + " / " + toTunnel + " offset=" + offset);
    134         _messageId = messageId;
     121            _log.debug("Receive message " + _messageId + " with " + length + " bytes (last? " + isLast + ") targetting " + toRouter + " / " + toTunnel + " offset=" + offset);
    135122        ByteArray ba = _cache.acquire(); // new ByteArray(payload, offset, length); // new byte[length]);
    136123        System.arraycopy(payload, offset, ba.getData(), 0, length);
Note: See TracChangeset for help on using the changeset viewer.