net.i2p.router.transport

## Class TransportImpl

- java.lang.Object
- - net.i2p.router.transport.TransportImpl

- All Implemented Interfaces:
    Transport
  Direct Known Subclasses:
    NTCPTransport, UDPTransport

      public abstract class TransportImpl
      extends Object
      implements Transport

  Defines a way to send a message to another peer and start listening for messages

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from interface net.i2p.router.transport.Transport

      `Transport.AddressSource`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected RouterContext` | `_context`
    `protected static boolean` | `ADJUST_COST` Do we increase the advertised cost when approaching conn limits?
    `static String` | `CAP_IPV4`
    `static String` | `CAP_IPV4_IPV6`
    `static String` | `CAP_IPV6`
    `protected static int` | `CONGESTION_COST_ADJUSTMENT` TODO change to 2
    `protected static String` | `PROP_BOOST_CONNECTION_LIMITS`
    `protected static String` | `PROP_IPV6_FIREWALLED`
    `protected static String` | `PROP_THROTTLE_NTCP_ATTACK`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `TransportImpl(RouterContext context)` Initialize the new transport

  - ### Method Summary

    All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected String` | `_t(String s)` Translate
    `protected String` | `_t(String s, Object o)` Translate
    `protected void` | `afterSend(OutNetMessage msg, boolean sendSuccessful)` The transport is done sending this message
    `protected void` | `afterSend(OutNetMessage msg, boolean sendSuccessful, boolean allowRequeue)` The transport is done sending this message
    `protected void` | `afterSend(OutNetMessage msg, boolean sendSuccessful, boolean allowRequeue, long msToSend)` The transport is done sending this message.
    `protected void` | `afterSend(OutNetMessage msg, boolean sendSuccessful, long msToSend)` The transport is done sending this message
    `boolean` | `allowLocal()` Are we allowed to connect to local addresses?
    `(package private) static void` | `clearCaches()`
    `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.
    `abstract int` | `countActiveSendPeers()` How many peers are we currently connected to, that we have sent a message to in the last minute.
    `abstract int` | `countPeers()` How many peers are we connected to?
    `abstract void` | `externalAddressReceived(Transport.AddressSource source, byte[] ip, int port)` Notify a transport of an external address change.
    `void` | `externalAddressRemoved(Transport.AddressSource source, boolean ipv6)` Notify a transport of an external address change.
    `void` | `forwardPortStatus(byte[] ip, int port, int externalPort, boolean success, String reason)` Notify a transport of the results of trying to forward a port.
    `String` | `getAltStyle()` An alternate supported style, or null.
    `List<Long>` | `getClockSkews()` Return our peer clock skews on a transport.
    `RouterAddress` | `getCurrentAddress(boolean ipv6)` What address are we currently listening to? Replaces getCurrentAddress() Note: An address without a host is considered IPv4.
    `List<RouterAddress>` | `getCurrentAddresses()` What addresses are we currently listening to? Replaces getCurrentAddress()
    `static byte[]` | `getIP(Hash peer)` IP of the peer from the last connection (in or out, any transport).
    `TransportUtil.IPv6Config` | `getIPv6Config()`
    `int` | `getMaxConnections()` Per-transport connection limit
    `List<String>` | `getMostRecentErrorMessages()`
    `protected OutNetMessage` | `getNextMessage()` Nonblocking call to pull the next outbound message off the queue.
    `abstract CommSystemFacade.Status` | `getReachabilityStatus()` Previously returned short, now enum as of 0.9.20
    `int` | `getRequestedPort()` What INTERNAL port would the transport like to have forwarded by UPnP.
    `protected Collection<InetAddress>` | `getSavedLocalAddresses()` Return and then clear all saved local addresses.
    `List<RouterAddress>` | `getTargetAddresses(RouterInfo target)` Get all available address we can use, shuffled and then sorted by cost/preference.
    `static int` | `getTransportMaxConnections(RouterContext ctx, String style)`
    `boolean` | `hasCurrentAddress()` Do we have any current address?
    `boolean` | `haveCapacity()` Can we initiate or accept a connection to another peer, saving some margin
    `boolean` | `haveCapacity(int pct)`
    `boolean` | `isBacklogged(Hash peer)`
    `boolean` | `isEstablished(Hash peer)`
    `boolean` | `isIPv4Firewalled()` This returns true if the force-firewalled setting is configured, false otherwise.
    `boolean` | `isIPv6Firewalled()` This returns true if the force-firewalled setting is configured, false otherwise.
    `protected boolean` | `isPubliclyRoutable(byte[] addr)` Allows IPv6 only if the transport is configured for it.
    `boolean` | `isUnreachable(Hash peer)`
    `void` | `markReachable(Hash peer, boolean isInbound)` called when we establish a peer connection (outbound or inbound)
    `void` | `markUnreachable(Hash peer)` called when we can't reach a peer
    `void` | `mayDisconnect(Hash peer)` Tell the transport that we may disconnect from this peer.
    `void` | `messageReceived(I2NPMessage inMsg, RouterIdentity remoteIdent, Hash remoteIdentHash, long msToReceive, int bytesReceived)` Message received from the I2NPMessageReader - send it to the listener
    `protected String` | `ngettext(String s, String p, int n)` Translate
    `protected abstract void` | `outboundMessageReady()` This message is called whenever a new message is added to the send pool, and it should not block Only used by NTCP.
    `protected void` | `removeAddress(boolean ipv6)` Remove all existing addresses with the specified IP length (4 or 16).
    `protected void` | `removeAddress(RouterAddress address)` Remove only this address.
    `void` | `renderStatusHTML(Writer out)` Make this stuff pretty
    `void` | `renderStatusHTML(Writer out, String urlBase, int sortFlags)`
    `protected void` | `replaceAddress(RouterAddress address)` Replace any existing addresses for the current transport with the same IP length (4 or 16) with the given one.
    `protected void` | `saveLocalAddress(InetAddress address)` Save a local address we were notified about before we started.
    `void` | `send(OutNetMessage msg)` Asynchronously send the message as requested in the message and, if the send is successful, queue up any msg.getOnSendJob job, and register it with the OutboundMessageRegistry (if it has a reply selector).
    `void` | `setIP(Hash peer, byte[] ip)` IP of the peer from the last connection (in or out, any transport).
    `void` | `setListener(TransportEventListener listener)` Who to notify on message availability
    `List<RouterAddress>` | `updateAddress()` Ask the transport to update its address based on current information and return it Transports should override.
    `boolean` | `wasUnreachable(Hash peer)` Was the peer Unreachable (outbound only) the last time we tried it? This is NOT reset if the peer contacts us.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface net.i2p.router.transport.Transport

      `bid, forceDisconnect, getEstablished, getPeerCounts, getStyle, startListening, stopListening`

- - ### Field Detail

    - #### _context

          protected final RouterContext _context

    - #### ADJUST_COST

          protected static final boolean ADJUST_COST

      Do we increase the advertised cost when approaching conn limits?
      See Also:
        Constant Field Values

    - #### CAP_IPV4

          public static final String CAP_IPV4

      See Also:
        Constant Field Values

    - #### CAP_IPV4_IPV6

          public static final String CAP_IPV4_IPV6

      See Also:
        Constant Field Values

    - #### CAP_IPV6

          public static final String CAP_IPV6

      See Also:
        Constant Field Values

    - #### CONGESTION_COST_ADJUSTMENT

          protected static final int CONGESTION_COST_ADJUSTMENT

      TODO change to 2
      See Also:
        Constant Field Values

    - #### PROP_BOOST_CONNECTION_LIMITS

          protected static final String PROP_BOOST_CONNECTION_LIMITS

      See Also:
        Constant Field Values

    - #### PROP_IPV6_FIREWALLED

          protected static final String PROP_IPV6_FIREWALLED

      See Also:
        Constant Field Values

    - #### PROP_THROTTLE_NTCP_ATTACK

          protected static final String PROP_THROTTLE_NTCP_ATTACK

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### TransportImpl

          public TransportImpl(RouterContext context)

      Initialize the new transport

  - ### Method Detail

    - #### _t

          protected String _t(String s)

      Translate

    - #### _t

          protected String _t(String s,
                              Object o)

      Translate

    - #### afterSend

          protected void afterSend(OutNetMessage msg,
                                   boolean sendSuccessful)

      The transport is done sending this message
      Parameters:
        `msg` - message in question
        `sendSuccessful` - true if the peer received it

    - #### afterSend

          protected void afterSend(OutNetMessage msg,
                                   boolean sendSuccessful,
                                   boolean allowRequeue)

      The transport is done sending this message
      Parameters:
        `msg` - message in question
        `sendSuccessful` - true if the peer received it
        `allowRequeue` - true if we should try other transports if available

    - #### afterSend

          protected void afterSend(OutNetMessage msg,
                                   boolean sendSuccessful,
                                   boolean allowRequeue,
                                   long msToSend)

      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.
      Parameters:
        `msg` - message in question
        `sendSuccessful` - true if the peer received it
        `msToSend` - how long it took to transfer the data to the peer
        `allowRequeue` - true if we should try other transports if available

    - #### afterSend

          protected void afterSend(OutNetMessage msg,
                                   boolean sendSuccessful,
                                   long msToSend)

      The transport is done sending this message
      Parameters:
        `msg` - message in question
        `sendSuccessful` - true if the peer received it
        `msToSend` - how long it took to transfer the data to the peer

    - #### allowLocal

          public boolean allowLocal()

      Are we allowed to connect to local addresses?

    - #### clearCaches

          static void clearCaches()

    - #### 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.
      Specified by:
        `countActivePeers` in interface `Transport`

    - #### countActiveSendPeers

          public abstract int countActiveSendPeers()

      How many peers are we currently connected to, that we have sent a message to in the last minute. Unused for anything, to be removed.
      Specified by:
        `countActiveSendPeers` in interface `Transport`

    - #### countPeers

          public abstract int countPeers()

      How many peers are we connected to?
      Specified by:
        `countPeers` in interface `Transport`

    - #### externalAddressReceived

          public abstract void externalAddressReceived(Transport.AddressSource source,
                                                       byte[] ip,
                                                       int port)

      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 before startListening() to set an initial address, or after the transport is running.
      Specified by:
        `externalAddressReceived` in interface `Transport`
      Parameters:
        `source` - defined in Transport.java
        `ip` - typ. IPv4 or IPv6 non-local; may be null to indicate IPv4 failure or port info only
        `port` - 0 for unknown or unchanged

    - #### 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. This implementation does nothing. Transports should override if they want notification.
      Specified by:
        `externalAddressRemoved` in interface `Transport`
      Parameters:
        `source` - defined in Transport.java
        `ipv6` - true for IPv6, false for IPv4

    - #### forwardPortStatus

          public void forwardPortStatus(byte[] ip,
                                        int port,
                                        int externalPort,
                                        boolean success,
                                        String reason)

      Notify a transport of the results of trying to forward a port. This implementation does nothing. Transports should override if they want notification.
      Specified by:
        `forwardPortStatus` in interface `Transport`
      Parameters:
        `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.

    - #### getAltStyle

          public String getAltStyle()

      An alternate supported style, or null.
      Returns:
        null, override to add support

    - #### getClockSkews

          public List<Long> getClockSkews()

      Return our peer clock skews on a transport. List composed of Long, each element representing a peer skew in seconds. Dummy version. Transports override it.
      Specified by:
        `getClockSkews` in interface `Transport`
      Returns:
        list of clock skew measurements in milliseconds, may be empty if no measurements available

    - #### getCurrentAddress

          public RouterAddress getCurrentAddress(boolean ipv6)

      What address are we currently listening to? Replaces getCurrentAddress() Note: An address without a host is considered IPv4.
      Specified by:
        `getCurrentAddress` in interface `Transport`
      Parameters:
        `ipv6` - true for IPv6 only; false for IPv4 only
      Returns:
        first matching address or null

    - #### getCurrentAddresses

          public List<RouterAddress> getCurrentAddresses()

      What addresses are we currently listening to? Replaces getCurrentAddress()
      Specified by:
        `getCurrentAddresses` in interface `Transport`
      Returns:
        a copy of all addresses, non-null

    - #### getIP

          public static byte[] getIP(Hash peer)

      IP of the peer from the last connection (in or out, any transport).
      Returns:
        IPv4 or IPv6 or null

    - #### getIPv6Config

          public TransportUtil.IPv6Config getIPv6Config()

      Specified by:
        `getIPv6Config` in interface `Transport`

    - #### getMaxConnections

          public int getMaxConnections()

      Per-transport connection limit

    - #### getMostRecentErrorMessages

          public List<String> getMostRecentErrorMessages()

      Specified by:
        `getMostRecentErrorMessages` in interface `Transport`

    - #### getNextMessage

          protected OutNetMessage getNextMessage()

      Nonblocking call to pull the next outbound message off the queue. Only used by NTCP. SSU does not call.
      Returns:
        the next message or null if none are available

    - #### getReachabilityStatus

          public abstract CommSystemFacade.Status getReachabilityStatus()

      Previously returned short, now enum as of 0.9.20
      Specified by:
        `getReachabilityStatus` in interface `Transport`
      Returns:
        current reachability status indicating transport's network accessibility

    - #### getRequestedPort

          public int getRequestedPort()

      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`
      Returns:
        port or -1 for none or 0 for any

    - #### getSavedLocalAddresses

          protected Collection<InetAddress> getSavedLocalAddresses()

      Return and then clear all saved local addresses.

    - #### getTargetAddresses

          public List<RouterAddress> getTargetAddresses(RouterInfo target)

      Get all available address we can use, shuffled and then sorted by cost/preference. Lowest cost (most preferred) first.
      Returns:
        non-null, possibly empty

    - #### getTransportMaxConnections

          public static int getTransportMaxConnections(RouterContext ctx,
                                                       String style)

    - #### hasCurrentAddress

          public boolean hasCurrentAddress()

      Do we have any current address?
      Specified by:
        `hasCurrentAddress` in interface `Transport`

    - #### haveCapacity

          public boolean haveCapacity()

      Can we initiate or accept a connection to another peer, saving some margin
      Specified by:
        `haveCapacity` in interface `Transport`

    - #### haveCapacity

          public boolean haveCapacity(int pct)

      Specified by:
        `haveCapacity` in interface `Transport`
      Parameters:
        `pct` - are we under x% 0-100

    - #### isBacklogged

          public boolean isBacklogged(Hash peer)

      Specified by:
        `isBacklogged` in interface `Transport`

    - #### isEstablished

          public boolean isEstablished(Hash peer)

      Specified by:
        `isEstablished` in interface `Transport`

    - #### isIPv4Firewalled

          public boolean isIPv4Firewalled()

      This returns true if the force-firewalled setting is configured, false otherwise.
      Specified by:
        `isIPv4Firewalled` in interface `Transport`

    - #### isIPv6Firewalled

          public boolean isIPv6Firewalled()

      This returns true if the force-firewalled setting is configured, false otherwise.
      Specified by:
        `isIPv6Firewalled` in interface `Transport`

    - #### isPubliclyRoutable

          protected boolean isPubliclyRoutable(byte[] addr)

      Allows IPv6 only if the transport is configured for it. Caller must check if we actually have a public IPv6 address.
      Parameters:
        `addr` - non-null

    - #### isUnreachable

          public boolean isUnreachable(Hash peer)

      Specified by:
        `isUnreachable` in interface `Transport`

    - #### markReachable

          public void markReachable(Hash peer,
                                    boolean isInbound)

      called when we establish a peer connection (outbound or inbound)

    - #### markUnreachable

          public void markUnreachable(Hash peer)

      called when we can't reach a peer

    - #### mayDisconnect

          public void mayDisconnect(Hash peer)

      Tell the transport that we may disconnect from this peer. This is advisory only.
      Specified by:
        `mayDisconnect` in interface `Transport`
      Parameters:
        `peer` - hash of the peer that may be disconnected

    - #### messageReceived

          public void messageReceived(I2NPMessage inMsg,
                                      RouterIdentity remoteIdent,
                                      Hash remoteIdentHash,
                                      long msToReceive,
                                      int bytesReceived)

      Message received from the I2NPMessageReader - send it to the listener
      Parameters:
        `inMsg` - non-null
        `remoteIdent` - may be null
        `remoteIdentHash` - may be null, calculated from remoteIdent if null

    - #### ngettext

          protected String ngettext(String s,
                                    String p,
                                    int n)

      Translate

    - #### outboundMessageReady

          protected abstract void outboundMessageReady()

      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.

    - #### removeAddress

          protected void removeAddress(boolean ipv6)

      Remove all existing addresses with the specified IP length (4 or 16). Calls listener.transportAddressChanged(). To remove all IPv4 and IPv6 addresses, use replaceAddress(null).
      Parameters:
        `ipv6` - true to remove all IPv6 addresses, false to remove all IPv4 addresses

    - #### removeAddress

          protected void removeAddress(RouterAddress address)

      Remove only this address. Calls listener.transportAddressChanged(). To remove all IPv4 or IPv6 addresses, use removeAddress(boolean). To remove all IPv4 and IPv6 addresses, use replaceAddress(null).

    - #### renderStatusHTML

          public void renderStatusHTML(Writer out)
                                throws IOException

      Make this stuff pretty
      Throws:
        `IOException`

    - #### renderStatusHTML

          public void renderStatusHTML(Writer out,
                                       String urlBase,
                                       int sortFlags)
                                throws IOException

      Specified by:
        `renderStatusHTML` in interface `Transport`
      Throws:
        `IOException`

    - #### replaceAddress

          protected void replaceAddress(RouterAddress address)

      Replace any existing addresses for the current transport with the same IP length (4 or 16) with the given one. TODO: Allow multiple addresses of the same length. Calls listener.transportAddressChanged() To remove all IPv4 or IPv6 addresses, use removeAddress(boolean).
      Parameters:
        `address` - null to remove all

    - #### saveLocalAddress

          protected void saveLocalAddress(InetAddress address)

      Save a local address we were notified about before we started.

    - #### send

          public void send(OutNetMessage msg)

      Asynchronously send the message as requested in the message and, if the send is successful, queue up any msg.getOnSendJob job, and register it with the OutboundMessageRegistry (if it has a reply selector). If the send fails, queue up any msg.getOnFailedSendJob Only used by NTCP. SSU overrides. Note that this adds to the queue and then takes it back off in the same thread, so it actually blocks, and we don't need a big queue. TODO: Override in NTCP also and get rid of queue?
      Specified by:
        `send` in interface `Transport`
      Parameters:
        `msg` - the message to send, containing destination, payload, and callbacks

    - #### setIP

          public void setIP(Hash peer,
                            byte[] ip)

      IP of the peer from the last connection (in or out, any transport).
      Parameters:
        `ip` - IPv4 or IPv6, non-null

    - #### setListener

          public void setListener(TransportEventListener listener)

      Who to notify on message availability
      Specified by:
        `setListener` in interface `Transport`
      Parameters:
        `listener` - the event listener to receive transport notifications

    - #### updateAddress

          public List<RouterAddress> updateAddress()

      Ask the transport to update its address based on current information and return it Transports should override.
      Specified by:
        `updateAddress` in interface `Transport`
      Returns:
        a copy of all addresses, non-null

    - #### wasUnreachable

          public boolean wasUnreachable(Hash peer)

      Was the peer Unreachable (outbound only) the last time we tried it? This is NOT reset if the peer contacts us.
      Specified by:
        `wasUnreachable` in interface `Transport`
      Parameters:
        `peer` - hash of the peer to check reachability for
      Returns:
        true if peer was unreachable on last outbound attempt