java.lang.Object
net.i2p.router.CommSystemFacade
All Implemented Interfaces:
Service
Direct Known Subclasses:
CommSystemFacadeImpl, VMCommSystem

public abstract class CommSystemFacadeextends Object implements Service
Manages peer communication subsystem including transport protocols, connection handling, and network address management for router-to-router messaging.
  • Field Details

    • ROUTER_BUNDLE_NAME

      protected static final String ROUTER_BUNDLE_NAME
      Bundle holding the translatable messages this subsystem logs.
      See Also:
    • STATUS_OK

      public static final short STATUS_OK
      These must be increasing in "badness" (see TransportManager.java), but UNKNOWN must be last.

      We are able to receive unsolicited connections on all enabled transports.

      See Also:
    • STATUS_IPV4_OK_IPV6_UNKNOWN

      public static final short STATUS_IPV4_OK_IPV6_UNKNOWN
      We have an IPv6 transport enabled and a public IPv6 address. We can receive unsolicited connections on IPv4. We might be able to receive unsolicited connections on IPv6.
      See Also:
    • STATUS_IPV4_OK_IPV6_FIREWALLED

      public static final short STATUS_IPV4_OK_IPV6_FIREWALLED
      We have an IPv6 transport enabled and a public IPv6 address. We can receive unsolicited connections on IPv4. We cannot receive unsolicited connections on IPv6.
      See Also:
    • STATUS_IPV4_UNKNOWN_IPV6_OK

      public static final short STATUS_IPV4_UNKNOWN_IPV6_OK
      We have an IPv6 transport enabled and a public IPv6 address. We may be able to receive unsolicited connections on IPv4. We can receive unsolicited connections on IPv6.
      See Also:
    • STATUS_IPV4_FIREWALLED_IPV6_OK

      public static final short STATUS_IPV4_FIREWALLED_IPV6_OK
      We have an IPv6 transport enabled and a public IPv6 address. We cannot receive unsolicited connections on IPv4. We can receive unsolicited connections on IPv6.
      See Also:
    • STATUS_IPV4_DISABLED_IPV6_OK

      public static final short STATUS_IPV4_DISABLED_IPV6_OK
      We have an IPv6 transport enabled and a public IPv6 address. IPv4 is disabled. We can receive unsolicited connections on IPv6.
      See Also:
    • STATUS_IPV4_SNAT_IPV6_OK

      public static final short STATUS_IPV4_SNAT_IPV6_OK
      We are behind a symmetric NAT which will make our 'from' address look differently when we talk to multiple people We can receive unsolicited connections on IPv6.
      See Also:
    • STATUS_DIFFERENT

      public static final short STATUS_DIFFERENT
      We are behind a symmetric NAT which will make our 'from' address look differently when we talk to multiple people.
      See Also:
    • STATUS_IPV4_SNAT_IPV6_UNKNOWN

      public static final short STATUS_IPV4_SNAT_IPV6_UNKNOWN
      We are behind a symmetric NAT which will make our 'from' address look differently when we talk to multiple people We might be able to receive unsolicited connections on IPv6.
      See Also:
    • STATUS_IPV4_FIREWALLED_IPV6_UNKNOWN

      public static final short STATUS_IPV4_FIREWALLED_IPV6_UNKNOWN
      We have an IPv6 transport enabled and a public IPv6 address. We cannot receive unsolicited connections on IPv4. We might be able to receive unsolicited connections on IPv6.
      See Also:
    • STATUS_REJECT_UNSOLICITED

      public static final short STATUS_REJECT_UNSOLICITED
      We are able to talk to peers that we initiate communication with, but cannot receive unsolicited connections, i.e. Firewalled, on all enabled transports.
      See Also:
    • STATUS_IPV4_UNKNOWN_IPV6_FIREWALLED

      public static final short STATUS_IPV4_UNKNOWN_IPV6_FIREWALLED
      We have an IPv6 transport enabled and a public IPv6 address. We may be able to receive unsolicited connections on IPv4. We cannot receive unsolicited connections on IPv6.
      See Also:
    • STATUS_IPV4_DISABLED_IPV6_UNKNOWN

      public static final short STATUS_IPV4_DISABLED_IPV6_UNKNOWN
      We have an IPv6 transport enabled and a public IPv6 address. IPv4 is disabled. We might be able to receive unsolicited connections on IPv6.
      See Also:
    • STATUS_IPV4_DISABLED_IPV6_FIREWALLED

      public static final short STATUS_IPV4_DISABLED_IPV6_FIREWALLED
      We have an IPv6 transport enabled and a public IPv6 address. IPv4 is disabled. We can receive unsolicited connections on IPv6.
      See Also:
    • STATUS_DISCONNECTED

      public static final short STATUS_DISCONNECTED
      We have no network interface at all enabled transports.
      See Also:
    • STATUS_HOSED

      public static final short STATUS_HOSED
      Our detection system is broken (SSU bind port failed).
      See Also:
    • STATUS_UNKNOWN

      public static final short STATUS_UNKNOWN
      Our reachability is unknown on all.
      See Also:
  • Constructor Details

    • CommSystemFacade

      protected CommSystemFacade()
      Not instantiable; a RouterContext subclass supplies the implementation.
  • Method Details

    • processMessage

      public abstract void processMessage(OutNetMessage msg)
      Queue an outbound message for delivery through the appropriate transport.
      Parameters:
      msg - the message to send
    • renderStatusHTML

      public void renderStatusHTML(Writer out, String urlBase, int sortFlags) throws IOException
      Render the transport status section of the router console page.
      Parameters:
      out - receives the HTML for this transport's status section
      urlBase - base URL for links, may be null
      sortFlags - flags controlling sort order
      Throws:
      IOException - on write error
    • renderStatusHTML

      public void renderStatusHTML(Writer out) throws IOException
      Render the transport status section of the router console page with default flags.
      Specified by:
      renderStatusHTML in interface Service
      Parameters:
      out - receives the HTML for this transport's status section
      Throws:
      IOException - on write error
    • createAddresses

      public List<RouterAddress> createAddresses()
      Create the list of RouterAddress structures based on the router's config.
      Returns:
      the published addresses, empty when none are configured
    • countActivePeers

      public abstract int countActivePeers()
      How many peers are we currently connected to, that we have sent a message to or received a message from in the last five minutes.
      Returns:
      the number of active peers
    • countActiveSendPeers

      public abstract int countActiveSendPeers()
      How many peers we have sent a message to in the last minute.

      Narrower than countActivePeers() in both respects that matter: send only, and a one-minute window rather than the transport's active window. The pair answers different questions - how many peers we can reach against how many we are actually driving traffic to right now.

      Returns:
      the number of peers sent to in the last minute
    • haveInboundCapacity

      public boolean haveInboundCapacity(int pct)
      Report whether inbound bandwidth has capacity for the given percentage load.
      Parameters:
      pct - percentage of bandwidth currently in use
      Returns:
      true if capacity is available
    • haveOutboundCapacity

      public boolean haveOutboundCapacity(int pct)
      Report whether outbound bandwidth has capacity for the given percentage load.
      Parameters:
      pct - percentage of bandwidth currently in use
      Returns:
      true if capacity is available
    • haveHighOutboundCapacity

      public boolean haveHighOutboundCapacity()
      Report whether outbound bandwidth is operating well below its limit.
      Returns:
      true if high outbound capacity is available
    • getMostRecentErrorMessages

      public List<String> getMostRecentErrorMessages()
      Retrieve recent transport-related error messages for display in the console.
      Returns:
      list of error message strings, non-null
    • getMedianPeerClockSkew

      public Long getMedianPeerClockSkew()
      Median clock skew of connected peers in seconds, or null if we cannot answer. CommSystemFacadeImpl overrides this.
      Returns:
      the median peer clock skew
    • getFramedAveragePeerClockSkew

      public long getFramedAveragePeerClockSkew(int percentToInclude)
      Return framed average clock skew of connected peers in seconds, or null if we cannot answer. CommSystemFacadeImpl overrides this.
      Parameters:
      percentToInclude - percentage of peers to include in the frame
      Returns:
      average skew in seconds
    • getStatus

      public CommSystemFacade.Status getStatus()
      Determine under what conditions we are remotely reachable.
      Returns:
      the reachability CommSystemFacade.Status, CommSystemFacade.Status.OK unless an implementation overrides this
    • getLocalizedStatusString

      public String getLocalizedStatusString()
      Localized reachability status string.
      Returns:
      the localized status string
    • isBacklogged

      public boolean isBacklogged(Hash peer)
      Check whether the given peer has excessive pending outbound messages.
      Parameters:
      peer - the router to test, identified by its router identity hash
      Returns:
      true if the peer is backlogged
    • wasUnreachable

      public boolean wasUnreachable(Hash peer)
      Check whether the given peer was recently unreachable.
      Parameters:
      peer - the router to test, identified by its router identity hash
      Returns:
      true if the peer was unreachable
    • isEstablished

      public abstract boolean isEstablished(Hash peer)
      Check whether a transport connection exists with the given peer.
      Parameters:
      peer - the router to test, identified by its router identity hash
      Returns:
      true if a connection is established
    • wasFailsafeClosed

      public abstract boolean wasFailsafeClosed(Hash peer, long withinMs)
      Whether any transport closed a session to the given peer for idleness within the given window.

      True means this router declined to keep talking to the peer, which is the opposite of the peer having gone away. Callers attribute a later loss of connectivity with it, and must treat a false as "no information" rather than as evidence the peer is at fault: only a transport that remembers the close can answer, and an answer of false covers never-established, still-open, and closed-for-other-reasons alike.

      Parameters:
      peer - the peer to ask about, non-null
      withinMs - how far back counts as recent in ms; non-positive means nothing is recent
      Returns:
      true when some transport closed a session to the peer that recently
    • isConnecting

      public boolean isConnecting(Hash peer)
      Check if any transport has a connection attempt in progress for the given peer (handshake started but not yet complete).
      Parameters:
      peer - hash of the peer to check
      Returns:
      true if at least one transport is currently establishing
    • getIP

      public byte[] getIP(Hash dest)
      Get the IP address associated with the given destination.
      Parameters:
      dest - the destination hash to resolve; no entry means null is returned
      Returns:
      IP address bytes or null if unknown
    • queueLookup

      public void queueLookup(byte[] ip)
      Queue a reverse-DNS lookup for the given IP address.
      Parameters:
      ip - IP address bytes to look up
    • mayDisconnect

      public void mayDisconnect(Hash peer)
      Tell the comm system that we may disconnect from this peer. This is advisory only.
      Parameters:
      peer - the peer we may drop
    • forceDisconnect

      public void forceDisconnect(Hash peer)
      Tell the comm system to disconnect from this peer.
      Parameters:
      peer - the peer to disconnect from
    • forceDisconnect

      public void forceDisconnect(Hash peer, String reason)
      Tell the comm system to disconnect from this peer with a reason.
      Parameters:
      peer - the router to disconnect from, identified by its router identity hash
      reason - reason for disconnection (for logging), may be null
    • getOurCountry

      public String getOurCountry()
      Our two-letter country code determined from our external IP.
      Returns:
      the our country
    • isInStrictCountry

      public boolean isInStrictCountry()
      Whether country blocking is enabled at all.
      Returns:
      whether in strict country
    • isInStrictCountry

      public boolean isInStrictCountry(Hash peer)
      Whether the given peer's country is blocked.
      Parameters:
      peer - peer to check
      Returns:
      whether in strict country
    • isInStrictCountry

      public boolean isInStrictCountry(RouterInfo ri)
      Whether the given router's country is blocked.
      Parameters:
      ri - router info to check
      Returns:
      whether in strict country
    • getCountry

      public String getCountry(Hash peer)
      Get the two-letter country code for the given peer's IP address.
      Parameters:
      peer - the router whose current IP address is looked up
      Returns:
      two-letter country code or null if unknown
    • getCountry

      public String getCountry(String ip)
      Get the two-letter country code for the given IP address.
      Parameters:
      ip - the IP address to look up
      Returns:
      two-letter country code or null if unknown
    • getCountryName

      public String getCountryName(String code)
      Resolve a two-letter country code to its full country name.
      Parameters:
      code - two-letter country code
      Returns:
      country name, or the code itself if unknown
    • getCountries

      public Map<String,String> getCountries()
      The map of all country codes.
      Returns:
      Map of two-letter lower case code to untranslated country name, unmodifiable
    • renderPeerHTML

      public String renderPeerHTML(Hash peer, boolean extended)
      Render an HTML snippet identifying the given peer, optionally with extended details.
      Parameters:
      peer - the router to describe, identified by its router identity hash
      extended - if true include extended information
      Returns:
      HTML string
    • renderPeerFlag

      public String renderPeerFlag(Hash peer)
      Render the country flag for the given peer as HTML.
      Parameters:
      peer - the router whose country is shown, identified by its router identity hash
      Returns:
      HTML string for the flag
    • renderPeerCaps

      public String renderPeerCaps(Hash peer, boolean inline)
      Render the peer's capabilities as HTML, optionally inline.
      Parameters:
      peer - the router whose capabilities are shown, identified by its router identity hash
      inline - if true render inline
      Returns:
      HTML string
    • getCanonicalHostName

      public String getCanonicalHostName(String ipAddress)
      Look up the canonical hostname for the given IP, blocking on DNS if necessary.
      Parameters:
      ipAddress - the IP address to look up
      Returns:
      hostname, or the IP itself if not resolvable
    • getCanonicalHostNameSync

      public String getCanonicalHostNameSync(String ipAddress)
      Synchronous canonical hostname lookup for the given IP.
      Parameters:
      ipAddress - the IP address to look up
      Returns:
      hostname, or the IP itself if not resolvable
    • getLocalHostName

      public String getLocalHostName(String ipAddress)
      Fast hostname lookup that never blocks on network DNS. Returns cached result if available, otherwise does a local ASN lookup. Queues background async RDNS if enabled, updating the cache on success.
      Parameters:
      ipAddress - the address to resolve, as text
      Returns:
      hostname from cache, ASN org name, or null if not resolvable
    • enableReverseLookups

      public abstract boolean enableReverseLookups()
      Whether reverse DNS lookups of router IPs are enabled. Single source of truth for the routerconsole.enableReverseLookups setting; used by the comm system, netDb, and the console UI.
      Returns:
      true if reverse lookups are enabled
    • getTransports

      public SortedMap<String, Transport> getTransports()
      Registered transports by style name.
      Returns:
      SortedMap of style to Transport (a copy)
    • getEstablished

      public abstract List<Hash> getEstablished()
      Get all the peers we are connected to. This should be more efficient than repeated calls to isEstablished(Hash) if you have to check a lot.
      Returns:
      the hashes of all the routers we are connected to, non-null
    • isDummy

      public boolean isDummy()
      Whether this is a dummy implementation with no real transports.
      Returns:
      true for this base implementation, which carries no transports
    • isRunning

      public boolean isRunning()
      Whether the comm system is fully initialized and running.
      Returns:
      true when the comm system is running
    • notifyReplaceAddress

      public void notifyReplaceAddress(RouterAddress address)
      Tell other transports our address changed.
      Parameters:
      address - the address that replaced the previous one
    • notifyRemoveAddress

      public void notifyRemoveAddress(RouterAddress address)
      Tell other transports our address changed.
      Parameters:
      address - the address that was removed
    • notifyRemoveAddress

      public void notifyRemoveAddress(boolean ipv6)
      Tell other transports our address changed.
      Parameters:
      ipv6 - true to remove the IPv6 address, false for IPv4
    • registerTransport

      public void registerTransport(Transport t)
      Register a transport for use by the comm system.
      Parameters:
      t - the transport to register
    • unregisterTransport

      public void unregisterTransport(Transport t)
      Unregister a transport from the comm system.
      Parameters:
      t - the transport to unregister
    • getXDHFactory

      public X25519KeyFactory getXDHFactory()
      Factory for making X25519 key pairs.
      Returns:
      the XDHE factory
    • initGeoIP

      public void initGeoIP()
      Router must call after netdb is initialized.
    • exemptIncoming

      public void exemptIncoming(Hash peer)
      Exempt this router hash from any incoming throttles or rejections.
      Parameters:
      peer - the router to exempt
    • isExemptIncoming

      public boolean isExemptIncoming(String ip)
      Is this IP exempt from any incoming throttles or rejections.
      Parameters:
      ip - the address to test
      Returns:
      whether exempt incoming
    • removeExemption

      public void removeExemption(String ip)
      Remove this IP from the exemptions.
      Parameters:
      ip - the address to remove from the exemptions