Class NTCPTransport

  • All Implemented Interfaces:

    public class NTCPTransport
    extends TransportImpl
    The NIO TCP transport
    • Method Detail

      • inboundEstablished

        NTCPConnection inboundEstablished​(NTCPConnection con)
        con - that is established
        the previous connection to the same peer, must be closed by caller, null if no such.
      • outboundMessageReady

        protected void outboundMessageReady()
        Description copied from class: TransportImpl
        This message is called whenever a new message is added to the send pool, and it should not block Only used by NTCP. SSU throws UOE.
        Specified by:
        outboundMessageReady in class TransportImpl
      • afterSend

        public void afterSend​(OutNetMessage msg,
                              boolean sendSuccessful,
                              boolean allowRequeue,
                              long msToSend)
        Description copied from class: TransportImpl
        The transport is done sending this message. This is the method that actually does all of the cleanup - firing off jobs, requeueing, updating stats, etc.
        afterSend in class TransportImpl
        msg - message in question
        sendSuccessful - true if the peer received it
        allowRequeue - true if we should try other transports if available
        msToSend - how long it took to transfer the data to the peer
      • bid

        public TransportBid bid​(RouterInfo toAddress,
                                int dataSize)
        dataSize - assumes full 16-byte header, transports should adjust as necessary
        a bid or null if unwilling to send
      • allowConnection

        public boolean allowConnection()
      • sendComplete

        void sendComplete​(OutNetMessage msg)
        queue up afterSend call, which can take some time w/ jobs, etc
      • forceDisconnect

        public void forceDisconnect​(Hash peer)
        Tell the transport to disconnect from this peer.
      • removeCon

        NTCPConnection removeCon​(NTCPConnection con)
        usually the con passed in, but possibly a second connection with the same peer... only con or null as of 0.9.37
      • getPeers

        public Collection<NTCPConnection> getPeers()
        For /peers UI only. Not a public API, not for external use.
        not a copy, do not modify
      • getEstablished

        public Set<Hash> getEstablished()
        Connected peers.
        a copy, modifiable
      • countActivePeers

        public int countActivePeers()
        How many peers have we talked to in the last 5 minutes? As of 0.9.20, actually returns active peer count, not total.
        Specified by:
        countActivePeers in interface Transport
        Specified by:
        countActivePeers in class TransportImpl
      • setLastBadSkew

        void setLastBadSkew​(long skew)
        A positive number means our clock is ahead of theirs.
        skew - in seconds
      • getClockSkews

        public List<Long> getClockSkews()
        Return our peer clock skews on this transport. List composed of Long, each element representing a peer skew in seconds. A positive number means our clock is ahead of theirs.
        Specified by:
        getClockSkews in interface Transport
        getClockSkews in class TransportImpl
      • isHXHIValid

        boolean isHXHIValid​(byte[] hxhi)
        Incoming connection replay detection. As there is no timestamp in the first message, we can't detect something long-delayed. To be fixed in next version of NTCP.
        hxhi - using first 8 bytes only
      • startListening

        public void startListening()
        Called by TransportManager. Caller should stop the transport first, then verify stopped with isAlive() Unfortunately TransportManager doesn't do that, so we check here to prevent two pumpers.
      • isAlive

        public boolean isAlive()
      • getReader

        Reader getReader()
        Hook for NTCPConnection
      • getWriter

        Writer getWriter()
        Hook for NTCPConnection
      • getStyle

        public String getStyle()
        Description copied from interface: Transport
        The unique identity of this Transport
        always "NTCP"
      • getAltStyle

        public String getAltStyle()
        An alternate supported style
        getAltStyle in class TransportImpl
        "NTCP2" always
      • getPumper

        EventPumper getPumper()
        Hook for NTCPConnection
      • getXDHFactory

        X25519KeyFactory getXDHFactory()
        null if not configured for NTCP2
      • establishing

        void establishing​(NTCPConnection con)
        add us to the establishment timeout process
      • expireTimedOut

        void expireTimedOut()
        called in the EventPumper no more than once a second or so, closing any unconnected/unestablished connections
      • getNTCP2StaticPubkey

        byte[] getNTCP2StaticPubkey()
        The static priv key
      • getNTCP2StaticPrivkey

        byte[] getNTCP2StaticPrivkey()
        The static priv key
      • getNTCP2StaticIV

        byte[] getNTCP2StaticIV()
        The static IV
      • externalAddressReceived

        public void externalAddressReceived​(Transport.AddressSource source,
                                            byte[] ip,
                                            int port)
        UDP changed addresses, tell NTCP and (possibly) restart
        Specified by:
        externalAddressReceived in interface Transport
        Specified by:
        externalAddressReceived in class TransportImpl
        ip - typ. IPv4 or IPv6 non-local; may be null to indicate IPv4 failure or port info only
        source - defined in
        port - 0 for unknown or unchanged
        IPv6 moved from CSFI.notifyReplaceAddress()
      • externalAddressRemoved

        public void externalAddressRemoved​(Transport.AddressSource source,
                                           boolean ipv6)
        Notify a transport of an external address change. This may be from a local interface, UPnP, a config change, etc. This should not be called if the ip didn't change (from that source's point of view), or is a local address. May be called multiple times for IPv4 or IPv6. The transport should also do its own checking on whether to accept notifications from this source. This can be called after the transport is running. TODO externalAddressRemoved(source, ip, port)
        Specified by:
        externalAddressRemoved in interface Transport
        externalAddressRemoved in class TransportImpl
        source - defined in
      • forwardPortStatus

        public void forwardPortStatus​(byte[] ip,
                                      int port,
                                      int externalPort,
                                      boolean success,
                                      String reason)
        If we didn't used to be forwarded, and we have an address, and we are configured to use UPnP, update our RouterAddress Don't do anything now. If it fails, we don't know if it's because there is no firewall, or if the firewall rejected the request. So we just use the SSU reachability status to decide whether to enable inbound NTCP. SSU will have CSFI build a new NTCP address when it transitions to OK.
        Specified by:
        forwardPortStatus in interface Transport
        forwardPortStatus in class TransportImpl
        ip - may be null
        port - the internal port
        externalPort - the external port, which for now should always be the same as the internal port if the forwarding was successful.
      • getRequestedPort

        public int getRequestedPort()
        Description copied from class: TransportImpl
        What INTERNAL port would the transport like to have forwarded by UPnP. This can't be passed via getCurrentAddress(), as we have to open the port before we can publish the address, and that's the external port anyway.
        Specified by:
        getRequestedPort in interface Transport
        getRequestedPort in class TransportImpl
        current IPv4 port, else NTCP configured port, else -1 (but not UDP port if auto)
      • getReachabilityStatus

        public CommSystemFacade.Status getReachabilityStatus()
        Maybe we should trust UPnP here and report OK if it opened the port, but for now we don't. Just go through and if we have one inbound connection, we must be good. As we drop idle connections pretty quickly, this will be fairly accurate. We have to be careful here because much of the router console code assumes that the reachability status is really just the UDP status. This only returns OK, DISABLED, or UNKNOWN for IPv4 and IPv6. We leave the FIREWALLED status for UDP. Previously returned short, now enum as of 0.9.20
        Specified by:
        getReachabilityStatus in interface Transport
        Specified by:
        getReachabilityStatus in class TransportImpl
      • stopListening

        public void stopListening()
        This doesn't (completely) block, caller should check isAlive() before calling startListening() or restartListening()