Changeset a69267dc


Ignore:
Timestamp:
Sep 1, 2011 1:24:31 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
aff250f
Parents:
c968764
Message:

UDP cleanups

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

Legend:

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

    rc968764 ra69267dc  
    6363    public void shutdown() {
    6464        _alive = false;
    65         PeerState poison = new PeerState(_context, _transport);
     65        PeerState poison = new PeerState(_context, _transport, null, 0, null, false);
    6666        poison.setTheyRelayToUsAs(POISON_PS);
    6767        _peersToACK.offer(poison);
  • router/java/src/net/i2p/router/transport/udp/EstablishmentManager.java

    rc968764 ra69267dc  
    286286            // we are offering introductions.
    287287            if ((!_context.router().isHidden()) && (!_transport.introducersRequired()) && _transport.haveCapacity()) {
    288                 long tag = _context.random().nextLong(MAX_TAG_VALUE);
     288                // ensure > 0
     289                long tag = 1 + _context.random().nextLong(MAX_TAG_VALUE);
    289290                state.setSentRelayTag(tag);
    290291                if (_log.shouldLog(Log.INFO))
     
    461462        long now = _context.clock().now();
    462463        RouterIdentity remote = state.getConfirmedIdentity();
    463         PeerState peer = new PeerState(_context, _transport);
     464        PeerState peer = new PeerState(_context, _transport,
     465                                       state.getSentIP(), state.getSentPort(), remote.calculateHash(), true);
    464466        peer.setCurrentCipherKey(state.getCipherKey());
    465467        peer.setCurrentMACKey(state.getMACKey());
    466         peer.setCurrentReceiveSecond(now - (now % 1000));
    467         peer.setKeyEstablishedTime(now);
    468         peer.setLastReceiveTime(now);
    469         peer.setLastSendTime(now);
    470         peer.setRemoteAddress(state.getSentIP(), state.getSentPort());
    471         peer.setRemotePeer(remote.calculateHash());
    472468        peer.setWeRelayToThemAs(state.getSentRelayTag());
    473         peer.setTheyRelayToUsAs(0);
    474         peer.setInbound();
     469        // 0 is the default
     470        //peer.setTheyRelayToUsAs(0);
    475471       
    476472        if (_log.shouldLog(Log.DEBUG))
     
    548544        long now = _context.clock().now();
    549545        RouterIdentity remote = state.getRemoteIdentity();
    550         PeerState peer = new PeerState(_context, _transport);
     546        PeerState peer = new PeerState(_context, _transport,
     547                                       state.getSentIP(), state.getSentPort(), remote.calculateHash(), false);
    551548        peer.setCurrentCipherKey(state.getCipherKey());
    552549        peer.setCurrentMACKey(state.getMACKey());
    553         peer.setCurrentReceiveSecond(now - (now % 1000));
    554         peer.setKeyEstablishedTime(now);
    555         peer.setLastReceiveTime(now);
    556         peer.setLastSendTime(now);
    557         peer.setRemoteAddress(state.getSentIP(), state.getSentPort());
    558         peer.setRemotePeer(remote.calculateHash());
    559550        peer.setTheyRelayToUsAs(state.getReceivedRelayTag());
    560         peer.setWeRelayToThemAs(0);
     551        // 0 is the default
     552        //peer.setWeRelayToThemAs(0);
    561553       
    562554        if (_log.shouldLog(Log.DEBUG))
     
    599591    }
    600592   
     593    /** the relay tag is a 4-byte field in the protocol */
    601594    public static final long MAX_TAG_VALUE = 0xFFFFFFFFl;
    602595   
     
    611604            // (perhaps we should check our bw usage and/or how many peers we are
    612605            //  already offering introducing?)
    613             if (state.getSentRelayTag() < 0) {
    614                 state.setSentRelayTag(_context.random().nextLong(MAX_TAG_VALUE));
     606            if (state.getSentRelayTag() == 0) {
     607                // ensure > 0
     608                state.setSentRelayTag(1 + _context.random().nextLong(MAX_TAG_VALUE));
    615609            } else {
    616610                // don't change it, since we've already prepared our sig
  • router/java/src/net/i2p/router/transport/udp/PacketBuilder.java

    rc968764 ra69267dc  
    635635            // BUG: NPE here if null signature
    636636            System.arraycopy(state.getSentSignature().getData(), 0, data, off, Signature.SIGNATURE_BYTES);
    637             packet.getPacket().setLength(off + Signature.SIGNATURE_BYTES);
    638             authenticate(packet, state.getCipherKey(), state.getMACKey());
     637            off += Signature.SIGNATURE_BYTES;
    639638        } else {
    640639            // nothing more to add beyond the identity fragment, though we can
     
    645644            if ( (off % 16) != 0)
    646645                off += 16 - (off % 16);
    647             packet.getPacket().setLength(off);
    648             authenticate(packet, state.getCipherKey(), state.getMACKey());
    649646        }
     647        packet.getPacket().setLength(off);
     648        authenticate(packet, state.getCipherKey(), state.getMACKey());
    650649       
    651650        setTo(packet, to, state.getSentPort());
  • router/java/src/net/i2p/router/transport/udp/PeerState.java

    rc968764 ra69267dc  
    2222/**
    2323 * Contain all of the state about a UDP connection to a peer.
    24  *
     24 * This is instantiated only after a connection is fully established.
    2525 */
    2626class PeerState {
     
    3333     * is established.
    3434     */
    35     private Hash _remotePeer;
     35    private final Hash _remotePeer;
    3636    /**
    3737     * The AES key used to verify packets, set only after the connection is
     
    119119    private int _receiveBps;
    120120    private int _receiveBytes;
    121     private int _sendACKBps;
    122     private int _sendACKBytes;
    123     private int _receiveACKBps;
    124     private int _receiveACKBytes;
     121    //private int _sendACKBps;
     122    //private int _sendZACKBytes;
     123    //private int _receiveACKBps;
     124    //private int _receiveACKBytes;
    125125    private long _receivePeriodBegin;
    126126    private volatile long _lastCongestionOccurred;
     
    132132    private volatile int _slowStartThreshold;
    133133    /** what IP is the peer sending and receiving packets on? */
    134     private byte[] _remoteIP;
     134    private final byte[] _remoteIP;
    135135    /** cached IP address */
    136136    private transient InetAddress _remoteIPAddress;
    137137    /** what port is the peer sending and receiving packets on? */
    138     private int _remotePort;
     138    private final int _remotePort;
    139139    /** cached RemoteHostId, used to find the peerState by remote info */
    140     private RemoteHostId _remoteHostId;
     140    private final RemoteHostId _remoteHostId;
     141
    141142    /** if we need to contact them, do we need to talk to an introducer? */
    142143    private boolean _remoteRequiresIntroduction;
     
    209210    private volatile int _consecutiveRejections = 0;
    210211    /** is it inbound? **/
    211     private boolean _isInbound;
     212    private final boolean _isInbound;
    212213    /** Last time it was made an introducer **/
    213214    private long _lastIntroducerTime;
     
    249250    private static final String PROP_DEFAULT_MTU = "i2np.udp.mtu";
    250251   
    251     public PeerState(RouterContext ctx, UDPTransport transport) {
     252    public PeerState(RouterContext ctx, UDPTransport transport,
     253                     byte[] remoteIP, int remotePort, Hash remotePeer, boolean isInbound) {
    252254        _context = ctx;
    253255        _log = ctx.logManager().getLog(PeerState.class);
    254256        _transport = transport;
    255         _keyEstablishedTime = -1;
    256         _currentReceiveSecond = -1;
    257         _lastSendTime = -1;
    258         _lastReceiveTime = -1;
     257        long now = ctx.clock().now();
     258        _keyEstablishedTime = now;
     259        _currentReceiveSecond = now - (now % 1000);
     260        _lastSendTime = now;
     261        _lastReceiveTime = now;
    259262        _currentACKs = new ConcurrentHashSet();
    260263        _currentACKsResend = new LinkedBlockingQueue();
     
    262265        _sendWindowBytesRemaining = DEFAULT_SEND_WINDOW_BYTES;
    263266        _slowStartThreshold = MAX_SEND_WINDOW_BYTES/2;
    264         _lastSendRefill = _context.clock().now();
    265         _receivePeriodBegin = _lastSendRefill;
     267        _lastSendRefill = now;
     268        _receivePeriodBegin = now;
    266269        _lastCongestionOccurred = -1;
    267         _remotePort = -1;
     270        _remotePort = remotePort;
    268271        _mtu = getDefaultMTU();
    269272        _mtuReceive = _mtu;
     
    276279        _outboundMessages = new ArrayList(32);
    277280        // all createRateStat() moved to EstablishmentManager
     281        _remoteIP = remoteIP;
     282        _remotePeer = remotePeer;
     283        _isInbound = isInbound;
     284        _remoteHostId = new RemoteHostId(remoteIP, remotePort);
    278285    }
    279286   
     
    304311     */
    305312    public SessionKey getNextMACKey() { return _nextMACKey; }
     313
    306314    /**
    307315     * The pending AES key for encrypting/decrypting packets if we are
     
    310318     */
    311319    public SessionKey getNextCipherKey() { return _nextCipherKey; }
     320
    312321    /**
    313322     * The keying material used for the rekeying, or null if we are not in
    314323     * the process of rekeying.
     324     * @deprecated unused
    315325     */
    316326    public byte[] getNextKeyingMaterial() { return _nextKeyingMaterial; }
     327
    317328    /** true if we began the current rekeying, false otherwise */
    318329    public boolean getRekeyBeganLocally() { return _rekeyBeganLocally; }
     
    349360    /** what IP is the peer sending and receiving packets on? */
    350361    public byte[] getRemoteIP() { return _remoteIP; }
     362
     363    /**
     364     *  @return may be null if IP is invalid
     365     */
    351366    public InetAddress getRemoteIPAddress() {
    352367        if (_remoteIPAddress == null) {
     
    360375        return _remoteIPAddress;
    361376    }
     377
    362378    /** what port is the peer sending and receiving packets on? */
    363379    public int getRemotePort() { return _remotePort; }
     380
    364381    /** if we need to contact them, do we need to talk to an introducer? */
    365382    public boolean getRemoteRequiresIntroduction() { return _remoteRequiresIntroduction; }
     383
    366384    /**
    367385     * if we are serving as an introducer to them, this is the the tag that
    368386     * they can publish that, when presented to us, will cause us to send
    369387     * a relay introduction to the current peer
     388     * @return 0 (no relay) if unset previously
    370389     */
    371390    public long getWeRelayToThemAs() { return _weRelayToThemAs; }
     391
    372392    /**
    373393     * If they have offered to serve as an introducer to us, this is the tag
    374394     * we can use to publish that fact.
     395     * @return 0 (no relay) if unset previously
    375396     */
    376397    public long getTheyRelayToUsAs() { return _theyRelayToUsAs; }
     398
    377399    /** what is the largest packet we can send to the peer? */
    378400    public int getMTU() { return _mtu; }
     
    392414  ****/
    393415   
    394     /**
    395      * The peer are we talking to.  This should be set as soon as this
    396      * state is created if we are initiating a connection, but if we are
    397      * receiving the connection this will be set only after the connection
    398      * is established.
    399      */
    400     public void setRemotePeer(Hash peer) { _remotePeer = peer; }
    401416    /**
    402417     * The AES key used to verify packets, set only after the connection is
     
    404419     */
    405420    public void setCurrentMACKey(SessionKey key) { _currentMACKey = key; }
     421
    406422    /**
    407423     * The AES key used to encrypt/decrypt packets, set only after the
     
    409425     */
    410426    public void setCurrentCipherKey(SessionKey key) { _currentCipherKey = key; }
     427
    411428    /**
    412429     * The pending AES key for verifying packets if we are rekeying the
    413430     * connection, or null if we are not in the process of rekeying.
     431     * @deprecated unused
    414432     */
    415433    public void setNextMACKey(SessionKey key) { _nextMACKey = key; }
     434
    416435    /**
    417436     * The pending AES key for encrypting/decrypting packets if we are
    418437     * rekeying the connection, or null if we are not in the process
    419438     * of rekeying.
     439     * @deprecated unused
    420440     */
    421441    public void setNextCipherKey(SessionKey key) { _nextCipherKey = key; }
     442
    422443    /**
    423444     * The keying material used for the rekeying, or null if we are not in
    424445     * the process of rekeying.
     446     * @deprecated unused
    425447     */
    426448    public void setNextKeyingMaterial(byte data[]) { _nextKeyingMaterial = data; }
    427     /** true if we began the current rekeying, false otherwise */
     449
     450    /**
     451     * @param local true if we began the current rekeying, false otherwise
     452     * @deprecated unused
     453     */
    428454    public void setRekeyBeganLocally(boolean local) { _rekeyBeganLocally = local; }
    429     /** when were the current cipher and MAC keys established/rekeyed? */
     455
     456    /**
     457     * when were the current cipher and MAC keys established/rekeyed?
     458     * @deprecated unused
     459     */
    430460    public void setKeyEstablishedTime(long when) { _keyEstablishedTime = when; }
    431461
     
    470500   
    471501    /** how fast we are sending *ack* packets */
    472     public int getSendACKBps() { return _sendACKBps; }
    473     public int getReceiveACKBps() { return _receiveACKBps; }
     502    //public int getSendACKBps() { return _sendACKBps; }
     503    //public int getReceiveACKBps() { return _receiveACKBps; }
    474504   
    475505    /**
     
    499529            _sendBytes += size;
    500530            _sendBps = (int)(0.9f*(float)_sendBps + 0.1f*((float)_sendBytes * (1000f/(float)duration)));
    501             if (isForACK) {
    502                 _sendACKBytes += size;
    503                 _sendACKBps = (int)(0.9f*(float)_sendACKBps + 0.1f*((float)_sendACKBytes * (1000f/(float)duration)));
    504             }
     531            //if (isForACK) {
     532            //    _sendACKBytes += size;
     533            //    _sendACKBps = (int)(0.9f*(float)_sendACKBps + 0.1f*((float)_sendACKBytes * (1000f/(float)duration)));
     534            //}
    505535            _sendBytes = 0;
    506             _sendACKBytes = 0;
     536            //_sendACKBytes = 0;
    507537            _lastSendRefill = now;
    508538        }
     
    523553            _sendBytes += size;
    524554            _lastSendTime = now;
    525             if (isForACK)
    526                 _sendACKBytes += size;
     555            //if (isForACK)
     556            //    _sendACKBytes += size;
    527557            return true;
    528558        } else {
     
    531561    }
    532562   
    533     /** what IP+port is the peer sending and receiving packets on? */
    534     public void setRemoteAddress(byte ip[], int port) {
    535         _remoteIP = ip;
    536         _remoteIPAddress = null;
    537         _remotePort = port;
    538         _remoteHostId = new RemoteHostId(ip, port);
    539     }
    540563    /** if we need to contact them, do we need to talk to an introducer? */
    541564    public void setRemoteRequiresIntroduction(boolean required) { _remoteRequiresIntroduction = required; }
     565
    542566    /**
    543567     * if we are serving as an introducer to them, this is the the tag that
    544568     * they can publish that, when presented to us, will cause us to send
    545569     * a relay introduction to the current peer
     570     * @param tag 1 to Integer.MAX_VALUE, or 0 if relaying disabled
    546571     */
    547572    public void setWeRelayToThemAs(long tag) { _weRelayToThemAs = tag; }
     573
    548574    /**
    549575     * If they have offered to serve as an introducer to us, this is the tag
    550576     * we can use to publish that fact.
     577     * @param tag 1 to Integer.MAX_VALUE, or 0 if relaying disabled
    551578     */
    552579    public void setTheyRelayToUsAs(long tag) { _theyRelayToUsAs = tag; }
     
    565592    public int getConsecutiveSendRejections() { return _consecutiveRejections; }
    566593    public boolean isInbound() { return _isInbound; }
    567     public void setInbound() { _isInbound = true; }
    568594    public long getIntroducerTime() { return _lastIntroducerTime; }
    569595    public void setIntroducerTime() { _lastIntroducerTime = _context.clock().now(); }
     
    574600        if (bytes > 0) {
    575601            _receiveBytes += bytes;
    576             if (isForACK)
    577                 _receiveACKBytes += bytes;
     602            //if (isForACK)
     603            //    _receiveACKBytes += bytes;
    578604        } else {
    579605            if (true || _retransmissionPeriodStart + 1000 < _context.clock().now()) {
     
    589615        if (duration >= 1000) {
    590616            _receiveBps = (int)(0.9f*(float)_receiveBps + 0.1f*((float)_receiveBytes * (1000f/(float)duration)));
    591             if (isForACK)
    592                 _receiveACKBps = (int)(0.9f*(float)_receiveACKBps + 0.1f*((float)_receiveACKBytes * (1000f/(float)duration)));
    593             _receiveACKBytes = 0;
     617            //if (isForACK)
     618            //    _receiveACKBps = (int)(0.9f*(float)_receiveACKBps + 0.1f*((float)_receiveACKBytes * (1000f/(float)duration)));
     619            //_receiveACKBytes = 0;
    594620            _receiveBytes = 0;
    595621            _receivePeriodBegin = now;
  • router/java/src/net/i2p/router/transport/udp/UDPPacketReader.java

    rc968764 ra69267dc  
    190190        }
    191191       
    192         /** write out the 4 byte relayAs tag */
     192        /** read in the 4 byte relayAs tag */
    193193        public long readRelayTag() {
    194194            int offset = readBodyOffset() + Y_LENGTH + 1 + readIPSize() + 2;
Note: See TracChangeset for help on using the changeset viewer.