Changeset 3622501


Ignore:
Timestamp:
Feb 2, 2014 4:47:29 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
e9b3577
Parents:
b7207fd2
Message:
  • Streaming:
    • Set ports on many packets that were missing them
    • Use connection throttling methods on pings too (ticket #1142)
    • Add methods to set ports on pings
    • Argument checking on ping methods
Location:
apps
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketManager.java

    rb7207fd2 r3622501  
    127127     * the timeout specified, false otherwise.  This call blocks.
    128128     *
     129     * Uses the ports from the default options.
     130     *
    129131     * @param peer Destination to ping
    130      * @param timeoutMs timeout in ms
     132     * @param timeoutMs timeout in ms, greater than zero
     133     * @throws IllegalArgumentException
    131134     * @return success or failure
    132135     */
    133136    public boolean ping(Destination peer, long timeoutMs);
     137
     138    /**
     139     * Ping the specified peer, returning true if they replied to the ping within
     140     * the timeout specified, false otherwise.  This call blocks.
     141     *
     142     * Uses the ports specified.
     143     *
     144     * @param peer Destination to ping
     145     * @param localPort 0 - 65535
     146     * @param remotePort 0 - 65535
     147     * @param timeoutMs timeout in ms, greater than zero
     148     * @return success or failure
     149     * @throws IllegalArgumentException
     150     * @since 0.9.12
     151     */
     152    public boolean ping(Destination peer, int localPort, int remotePort, long timeoutMs);
    134153
    135154    public String getName();
  • apps/streaming/java/src/net/i2p/client/streaming/impl/Connection.java

    rb7207fd2 r3622501  
    308308        reply.setReceiveStreamId(_receiveStreamId);
    309309        reply.setOptionalFrom(_connectionManager.getSession().getMyDestination());
     310        reply.setLocalPort(_localPort);
     311        reply.setRemotePort(_remotePort);
    310312        // this just sends the packet - no retries or whatnot
    311313        if (_outboundQueue.enqueue(reply)) {
  • apps/streaming/java/src/net/i2p/client/streaming/impl/ConnectionDataReceiver.java

    rb7207fd2 r3622501  
    207207            packet.setOptionalFrom(con.getSession().getMyDestination());
    208208            packet.setOptionalMaxSize(con.getOptions().getMaxMessageSize());
    209             packet.setLocalPort(con.getLocalPort());
    210             packet.setRemotePort(con.getPort());
    211         }
     209        }
     210        packet.setLocalPort(con.getLocalPort());
     211        packet.setRemotePort(con.getPort());
    212212        if (con.getSendStreamId() == Packet.STREAM_ID_UNKNOWN) {
    213213            packet.setFlag(Packet.FLAG_NO_ACK);
  • apps/streaming/java/src/net/i2p/client/streaming/impl/ConnectionManager.java

    rb7207fd2 r3622501  
    263263            reply.setReceiveStreamId(0);
    264264            reply.setOptionalFrom(_session.getMyDestination());
     265            reply.setLocalPort(synPacket.getLocalPort());
     266            reply.setRemotePort(synPacket.getRemotePort());
    265267            // this just sends the packet - no retries or whatnot
    266268            _outboundQueue.enqueue(reply);
     
    283285        _context.statManager().addRateData("stream.connectionReceived", 1, 0);
    284286        return con;
     287    }
     288   
     289    /**
     290     *  Process a ping by checking for throttling, etc., then sending a pong.
     291     *
     292     *  @param con null if unknown
     293     *  @param ping Ping packet to process, must have From and Sig fields,
     294     *              with signature already verified, only if answerPings() returned true
     295     *  @return true if we sent a pong
     296     *  @since 0.9.12 from PacketHandler.receivePing()
     297     */
     298    public boolean receivePing(Connection con, Packet ping) {
     299        Destination dest = ping.getOptionalFrom();
     300        if (dest == null)
     301            return false;
     302        if (con == null) {
     303            // Use the same throttling as for connections
     304            String why = shouldRejectConnection(ping);
     305            if (why != null) {
     306                if ((!_defaultOptions.getDisableRejectLogging()) || _log.shouldLog(Log.WARN))
     307                    _log.logAlways(Log.WARN, "Dropping ping since peer is " + why + ": " + dest.calculateHash());
     308                return false;
     309            }
     310        } else {
     311            // in-connection ping to a 3rd party ???
     312            if (!dest.equals(con.getRemotePeer())) {
     313                _log.logAlways(Log.WARN, "Dropping ping from " + con.getRemotePeer().calculateHash() +
     314                                         " to " + dest.calculateHash());
     315                return false;
     316            }
     317        }
     318        PacketLocal pong = new PacketLocal(_context, dest);
     319        pong.setFlag(Packet.FLAG_ECHO | Packet.FLAG_NO_ACK);
     320        pong.setReceiveStreamId(ping.getSendStreamId());
     321        pong.setLocalPort(ping.getLocalPort());
     322        pong.setRemotePort(ping.getRemotePort());
     323        _outboundQueue.enqueue(pong);
     324        return true;
    285325    }
    286326   
     
    573613    }
    574614
    575     /** blocking */
    576     public boolean ping(Destination peer, long timeoutMs) {
    577         return ping(peer, timeoutMs, true, null);
    578     }
    579     public boolean ping(Destination peer, long timeoutMs, boolean blocking) {
    580         return ping(peer, timeoutMs, blocking, null);
    581     }
    582 
    583     /**
    584      * @deprecated I2PSession ignores tags, use non-tag variant
    585      * @param keyToUse ignored
    586      * @param tagsToSend ignored
    587      */
    588     public boolean ping(Destination peer, long timeoutMs, boolean blocking, SessionKey keyToUse, Set<?> tagsToSend, PingNotifier notifier) {
    589         return ping(peer, timeoutMs, blocking, notifier);
    590     }
    591 
    592     public boolean ping(Destination peer, long timeoutMs, boolean blocking, PingNotifier notifier) {
     615    /**
     616     *  blocking
     617     *
     618     *  @param timeoutMs greater than zero
     619     *  @return true if pong received
     620     *  @since 0.9.12 added port args
     621     */
     622    public boolean ping(Destination peer, int fromPort, int toPort, long timeoutMs) {
     623        return ping(peer, fromPort, toPort, timeoutMs, true, null);
     624    }
     625
     626    /**
     627     *  @param timeoutMs greater than zero
     628     *  @return true if blocking and pong received
     629     *  @since 0.9.12 added port args
     630     */
     631    public boolean ping(Destination peer, int fromPort, int toPort, long timeoutMs, boolean blocking) {
     632        return ping(peer, fromPort, toPort, timeoutMs, blocking, null);
     633    }
     634
     635    /**
     636     *  @param timeoutMs greater than zero
     637     *  @param notifier may be null
     638     *  @return true if blocking and pong received
     639     *  @since 0.9.12 added port args
     640     */
     641    public boolean ping(Destination peer, int fromPort, int toPort, long timeoutMs,
     642                        boolean blocking, PingNotifier notifier) {
    593643        Long id = Long.valueOf(_context.random().nextLong(Packet.MAX_STREAM_ID-1)+1);
    594644        PacketLocal packet = new PacketLocal(_context, peer);
     
    598648                       Packet.FLAG_SIGNATURE_INCLUDED);
    599649        packet.setOptionalFrom(_session.getMyDestination());
     650        packet.setLocalPort(fromPort);
     651        packet.setRemotePort(toPort);
    600652        //if ( (keyToUse != null) && (tagsToSend != null) ) {
    601653        //    packet.setKeyUsed(keyToUse);
     
    603655        //}
    604656        if (_log.shouldLog(Log.INFO)) {
    605             _log.info(String.format("about to ping %s timeout=%d blocking=%b",
    606                     peer,timeoutMs,blocking));
     657            _log.info(String.format("about to ping %s port %d from port %d timeout=%d blocking=%b",
     658                      peer.calculateHash().toString(), toPort, fromPort, timeoutMs, blocking));
    607659        }
    608660           
     
    659711        private final PingNotifier _notifier;
    660712
     713        /** @param notifier may be null */
    661714        public PingRequest(PingNotifier notifier) {
    662715            _notifier = notifier;
  • apps/streaming/java/src/net/i2p/client/streaming/impl/I2PSocketManagerFull.java

    rb7207fd2 r3622501  
    149149     * the timeout specified, false otherwise.  This call blocks.
    150150     *
     151     * Uses the ports from the default options.
    151152     *
    152153     * @param peer
    153      * @param timeoutMs
     154     * @param timeoutMs timeout in ms, greater than zero
    154155     * @return true on success, false on failure
     156     * @throws IllegalArgumentException
    155157     */
    156158    public boolean ping(Destination peer, long timeoutMs) {
    157         return _connectionManager.ping(peer, timeoutMs);
     159        if (timeoutMs <= 0)
     160            throw new IllegalArgumentException("bad timeout");
     161        return _connectionManager.ping(peer, _defaultOptions.getLocalPort(),
     162                                       _defaultOptions.getPort(), timeoutMs);
     163    }
     164
     165    /**
     166     * Ping the specified peer, returning true if they replied to the ping within
     167     * the timeout specified, false otherwise.  This call blocks.
     168     *
     169     * Uses the ports specified.
     170     *
     171     * @param peer Destination to ping
     172     * @param localPort 0 - 65535
     173     * @param remotePort 0 - 65535
     174     * @param timeoutMs timeout in ms, greater than zero
     175     * @return success or failure
     176     * @throws IllegalArgumentException
     177     * @since 0.9.12
     178     */
     179    public boolean ping(Destination peer, int localPort, int remotePort, long timeoutMs) {
     180        if (localPort < 0 || localPort > 65535 ||
     181            remotePort < 0 || remotePort > 65535)
     182            throw new IllegalArgumentException("bad port");
     183        if (timeoutMs <= 0)
     184            throw new IllegalArgumentException("bad timeout");
     185        return _connectionManager.ping(peer, localPort, remotePort, timeoutMs);
    158186    }
    159187
  • apps/streaming/java/src/net/i2p/client/streaming/impl/PacketHandler.java

    rb7207fd2 r3622501  
    137137            if (packet.getSendStreamId() > 0) {
    138138                if (con.getOptions().getAnswerPings())
    139                     receivePing(packet);
     139                    receivePing(con, packet);
    140140                else if (_log.shouldLog(Log.WARN))
    141141                    _log.warn("Dropping Echo packet on existing con: " + packet);
     
    248248        reply.setReceiveStreamId(packet.getSendStreamId());
    249249        reply.setOptionalFrom(_manager.getSession().getMyDestination());
     250        reply.setLocalPort(packet.getLocalPort());
     251        reply.setRemotePort(packet.getRemotePort());
    250252        // this just sends the packet - no retries or whatnot
    251253        _manager.getPacketQueue().enqueue(reply);
     
    256258            if (packet.getSendStreamId() > 0) {
    257259                if (_manager.answerPings())
    258                     receivePing(packet);
     260                    receivePing(null, packet);
    259261                else if (_log.shouldLog(Log.WARN))
    260262                    _log.warn("Dropping Echo packet on unknown con: " + packet);
     
    336338    }
    337339   
    338     private void receivePing(Packet packet) {
     340    /**
     341     *  @param con null if unknown
     342     */
     343    private void receivePing(Connection con, Packet packet) {
    339344        boolean ok = packet.verifySignature(_context, packet.getOptionalFrom(), null);
    340345        if (!ok) {
     
    349354            }
    350355        } else {
    351             PacketLocal pong = new PacketLocal(_context, packet.getOptionalFrom());
    352             pong.setFlag(Packet.FLAG_ECHO | Packet.FLAG_NO_ACK);
    353             pong.setReceiveStreamId(packet.getSendStreamId());
    354             _manager.getPacketQueue().enqueue(pong);
     356            _manager.receivePing(con, packet);
    355357        }
    356358    }
Note: See TracChangeset for help on using the changeset viewer.