{% extends "global/layout.html" %} {% block title %}{% trans %}Streaming Library{% endtrans %}{% endblock %} {% block lastupdated %}{% trans %}November 2012{% endtrans %}{% endblock %} {% block accuratefor %}0.9.3{% endblock %} {% block content %}

{% trans %}Overview{% endtrans %}

{% trans datagrams=site_url('docs/spec/datagrams') -%} The streaming library is technically part of the "application" layer, as it is not a core router function. In practice, however, it provides a vital function for almost all existing I2P applications, by providing a TCP-like streams over I2P, and allowing existing apps to be easily ported to I2P. The other end-to-end transport library for client communication is the datagram library. {%- endtrans %}

{% trans i2cp=site_url('docs/protocol/i2cp') -%} The streaming library is a layer on top of the core I2CP API that allows reliable, in-order, and authenticated streams of messages to operate across an unreliable, unordered, and unauthenticated message layer. Just like the TCP to IP relationship, this streaming functionality has a whole series of tradeoffs and optimizations available, but rather than embed that functionality into the base I2P code, it has been factored off into its own library both to keep the TCP-esque complexities separate and to allow alternative optimized implementations. {%- endtrans %}

{% trans -%} In consideration of the relatively high cost of messages, the streaming library's protocol for scheduling and delivering those messages has been optimized to allow individual messages passed to contain as much information as is available. For instance, a small HTTP transaction proxied through the streaming library can be completed in a single round trip - the first messages bundle a SYN, FIN, and the small HTTP request payload, and the reply bundles the SYN, FIN, ACK, and the HTTP response payload. While an additional ACK must be transmitted to tell the HTTP server that the SYN/FIN/ACK has been received, the local HTTP proxy can often deliver the full response to the browser immediately. {%- endtrans %}

{% trans -%} The streaming library bears much resemblance to an abstraction of TCP, with its sliding windows, congestion control algorithms (both slow start and congestion avoidance), and general packet behavior (ACK, SYN, FIN, RST, rto calculation, etc). {%- endtrans %}

{% trans -%} The streaming library is a robust library which is optimized for operation over I2P. It has a one-phase setup, and it contains a full windowing implementation. {%- endtrans %}

{% trans %}API{% endtrans %}

{% trans i2cp=site_url('docs/protocol/i2cp') -%} The streaming library API provides a standard socket paradigm to Java applications. The lower-level I2CP API is completely hidden, except that applications may pass I2CP parameters through the streaming library, to be interpreted by I2CP. {%- endtrans %}

{% trans i2cp=site_url('docs/protocol/i2cp'), i2psktmf='http://docs.i2p-projekt.de/javadoc/net/i2p/client/streaming/I2PSocketManagerFactory.html', i2psktm='http://docs.i2p-projekt.de/javadoc/net/i2p/client/streaming/I2PSocketManager.html', i2psess='http://docs.i2p-projekt.de/javadoc/net/i2p/client/streaming/I2PSession.html', i2pskt='http://docs.i2p-projekt.de/javadoc/net/i2p/client/streaming/I2PSocket.html', i2psskt='http://docs.i2p-projekt.de/javadoc/net/i2p/client/streaming/I2PServerSocket.html' -%} The standard interface to the streaming lib is for the application to use the I2PSocketManagerFactory to create an I2PSocketManager. The application then asks the socket manager for an I2PSession, which will cause a connection to the router via I2CP. The application can then setup connections with an I2PSocket or receive connections with an I2PServerSocket. {%- endtrans %}

{% trans url='http://docs.i2p-projekt.de/javadoc/net/i2p/client/streaming/package-summary.html' -%} Here are the full streaming library Javadocs. {%- endtrans %}

{% trans -%} For a good example of usage, see the i2psnark code. {%- endtrans %}

{% trans %}Options and Defaults{% endtrans %}

{% trans i2psktmf='http://docs.i2p-projekt.de/javadoc/net/i2p/client/streaming/I2PSocketManagerFactory.html' -%} The options and current default values are listed below. Options are case-sensitive and may be set for the whole router, for a particular client, or for an individual socket on a per-connection basis. Many values are tuned for HTTP performance over typical I2P conditions. Other applications such as peer-to-peer services are strongly encouraged to modify as necessary, by setting the options and passing them via the call to I2PSocketManagerFactory.createManager(_i2cpHost, _i2cpPort, opts). Time values are in ms. {%- endtrans %}

{% trans samv3=site_url('docs/api/samv3'), bob=site_url('docs/api/bob'), i2ptunnel=site_url('docs/api/i2ptunnel') -%} Note that higher-layer APIs, such as SAM, BOB, and I2PTunnel, may override these defaults with their own defaults. Also note that many options only apply to servers listening for incoming connections. {%- endtrans %}

{% trans -%} As of release 0.9.1, most, but not all, options may be changed on an active socket manager or session. See the javadocs for details. {%- endtrans %}

{{ _('Option') }}{{ _('Default') }}{{ _('Notes') }}
i2cp.accessListnull{% trans -%} Comma- or space-separated list of Base64 peer Hashes used for either access list or blacklist. {%- endtrans %} {% trans release='0.7.13' -%} As of release {{ release }}. {%- endtrans %}
i2cp.enableAccessListfalse{% trans -%} Use the access list as a whitelist for incoming connections. {%- endtrans %} {% trans release='0.7.13' -%} As of release {{ release }}. {%- endtrans %}
i2cp.enableBlackListfalse{% trans -%} Use the access list as a blacklist for incoming connections. {%- endtrans %} {% trans release='0.7.13' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.answerPingstrue{% trans -%} Whether to respond to incoming pings {%- endtrans %}
i2p.streaming.blacklistnull{% trans -%} Comma- or space-separated list of Base64 peer Hashes to be blacklisted for incoming connections to ALL destinations in the context. This option must be set in the context properties, NOT in the createManager() options argument. Note that setting this in the router context will not affect clients outside the router in a separate JVM and context. {%- endtrans %} {% trans release='0.9.3' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.bufferSize64K{% trans -%} How much transmit data (in bytes) will be accepted that hasn't been written out yet. {%- endtrans %}
i2p.streaming.congestionAvoidanceGrowthRateFactor1{% trans -%} When we're in congestion avoidance, we grow the window size at the rate of 1/(windowSize*factor). In standard TCP, window sizes are in bytes, while in I2P, window sizes are in messages. A higher number means slower growth. {%- endtrans %}
i2p.streaming.connectDelay-1{% trans -%} How long to wait after instantiating a new con before actually attempting to connect. If this is <= 0, connect immediately with no initial data. If greater than 0, wait until the output stream is flushed, the buffer fills, or that many milliseconds pass, and include any initial data with the SYN. {%- endtrans %}
i2p.streaming.connectTimeout5*60*1000{% trans -%} How long to block on connect, in milliseconds. Negative means indefinitely. Default is 5 minutes. {%- endtrans %}
i2p.streaming.disableRejectLoggingfalse{% trans -%} Whether to disable warnings in the logs when an incoming connection is rejected due to connection limits. {%- endtrans %} {% trans release='0.9.4' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.enforceProtocolfalse{% trans -%} Whether to listen only for the streaming protocol. Setting to true will prohibit communication with Destinations earlier than release 0.7.1 (released March 2009). Set to true if running multiple protocols on this Destination. {%- endtrans %} {% trans release='0.9.1' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.inactivityAction2 (send) {% trans -%} (0=noop, 1=disconnect) What to do on an inactivity timeout - do nothing, disconnect, or send a duplicate ack. {%- endtrans %}
i2p.streaming.inactivityTimeout90*1000
i2p.streaming.initialAckDelay2000
i2p.streaming.initialResendDelay1000{% trans -%} The initial value of the resend delay field in the packet header, times 1000. Not fully implemented; see below. {%- endtrans %}
i2p.streaming.initialRTT8000 ({% trans %}if no sharing data available{% endtrans %})
i2p.streaming.initialWindowSize6({% trans %}if no sharing data available{% endtrans %}) {% trans -%} In standard TCP, window sizes are in bytes, while in I2P, window sizes are in messages. {%- endtrans %}
i2p.streaming.maxConcurrentStreams-1 {% trans -%} (0 or negative value means unlimited) This is a total limit for incoming and outgoing combined. {%- endtrans %}
i2p.streaming.maxConnsPerMinute0 {% trans -%} Incoming connection limit (per peer; 0 means disabled) {%- endtrans %} {% trans release='0.7.14' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.maxConnsPerHour0 {% trans -%} (per peer; 0 means disabled) {%- endtrans %} {% trans release='0.7.14' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.maxConnsPerDay0 {% trans -%} (per peer; 0 means disabled) {%- endtrans %} {% trans release='0.7.14' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.maxMessageSize1730{% trans -%} The MTU in bytes. {%- endtrans %}
i2p.streaming.maxResends8{% trans -%} Maximum number of retransmissions before failure. {%- endtrans %}
i2p.streaming.maxTotalConnsPerMinute0 {% trans -%} Incoming connection limit (all peers; 0 means disabled) {%- endtrans %} {% trans release='0.7.14' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.maxTotalConnsPerHour0 {% trans -%} (all peers; 0 means disabled) Use with caution as exceeding this will disable a server for a long time. {%- endtrans %} {% trans release='0.7.14' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.maxTotalConnsPerDay0 {% trans -%} (all peers; 0 means disabled) Use with caution as exceeding this will disable a server for a long time. {%- endtrans %} {% trans release='0.7.14' -%} As of release {{ release }}. {%- endtrans %}
i2p.streaming.maxWindowSize128
i2p.streaming.profile1 (bulk){% trans -%} (2=interactive not supported) This doesn't currently do anything, but setting it to a value other than 1 will cause an error. {%- endtrans %}
i2p.streaming.readTimeout-1{% trans -%} How long to block on read, in milliseconds. Negative means indefinitely. {%- endtrans %}
i2p.streaming.slowStartGrowthRateFactor1{% trans -%} When we're in slow start, we grow the window size at the rate of 1/(factor). In standard TCP, window sizes are in bytes, while in I2P, window sizes are in messages. A higher number means slower growth. {%- endtrans %}
i2p.streaming.writeTimeout-1{% trans -%} How long to block on write/flush, in milliseconds. Negative means indefinitely. {%- endtrans %}

{% trans %}Protocol Specification{% endtrans %}

{% trans %}Packet Format{% endtrans %}

{% trans -%} The format of a single packet in the streaming protocol is: {%- endtrans %}


+----+----+----+----+----+----+----+----+
| send Stream ID    | rcv Stream ID     |
+----+----+----+----+----+----+----+----+
| sequence  Num     | ack Through       |
+----+----+----+----+----+----+----+----+
| nc |   NACKs ...
+----+----+----+----+----+----+----+----+
     | rd |  flags  | opt size| opt data
+----+----+----+----+----+----+----+----+
   ...                                  |
+----+----+----+----+----+----+----+----+
|   payload ...
+----+----+----+----//


{{ _('Field') }}{{ _('Length') }}{{ _('Contents') }}
sendStreamId 4 byte IntegerRandom number selected by the connection recipient and constant for the life of the connection. 0 in the SYN message sent by the originator, and in subsequent messages, until a SYN reply is received, containing the peer's stream ID.
receiveStreamId 4 byte IntegerRandom number selected by the connection originator and constant for the life of the connection. May be 0 if unknown, for example in a RESET packet.
sequenceNum 4 byte Integer The sequence for this message, starting at 0 in the SYN message, and incremented by 1 in each message except for plain ACKs and retransmissions. If the sequenceNum is 0 and the SYN flag is not set, this is a plain ACK packet that should not be ACKed.
ackThrough 4 byte Integer The highest packet sequence number that was received on the receiveStreamId. This field is ignored on the initial connection packet (where receiveStreamId is the unknown id) or if the NO_ACK flag set. All packets up to and including this sequence number are ACKed, EXCEPT for those listed in NACKs below.
NACK count1 byte Integer The number of 4-byte NACKs in the next field
NACKs n * 4 byte Integers Sequence numbers less than ackThrough that are not yet received. Two NACKs of a packet is a request for a 'fast retransmit' of that packet.
resendDelay1 byte Integer How long is the creator of this packet going to wait before resending this packet (if it hasn't yet been ACKed). The value is seconds since the packet was created. Currently ignored on receive.
flags 2 byte value See below.
option size2 byte Integer The number of bytes in the next field
option data0 or more bytes As specified by the flags. See below.
payload remaining packet size

{% trans %}Flags and Option Data Fields{% endtrans %}

{% trans -%} The flags field above specifies some metadata about the packet, and in turn may require certain additional data to be included. The flags are as follows. Any data structures specified must be added to the options area in the given order. {%- endtrans %}

Bit order: 15....0 (15 is MSB)

BitFlagOption DataFunction
0SYNCHRONIZE-- Similar to TCP SYN. Set in the initial packet and in the first response. FROM_INCLUDED and SIGNATURE_INCLUDED must be set also.
1CLOSE-- Similar to TCP FIN. If the response to a SYNCHRONIZE fits in a single message, the response will contain both SYNCHRONIZE and CLOSE. SIGNATURE_INCLUDED must be set also.
2RESET-- Abnormal close. SIGNATURE_INCLUDED must be set also.
3SIGNATURE_INCLUDED40 byte DSA Signature Currently sent only with SYNCHRONIZE, CLOSE, and RESET, where it is required. The signature uses the Destination's DSA signing keys to sign the entire header and payload with the 40-byte space in the option data field for the signature being set to all zeroes.
4SIGNATURE_REQUESTED-- Unused. Requests every packet in the other direction to have SIGNATURE_INCLUDED
5FROM_INCLUDED387+ byte Destination Currently sent only with SYNCHRONIZE, where it is required.
6DELAY_REQUESTED2 byte Integer Optional delay. How many milliseconds the sender of this packet wants the recipient to wait before sending any more data. A value greater than 60000 indicates choking.
7MAX_PACKET_SIZE_INCLUDED2 byte Integer Currently sent with SYNCHRONIZE only. Was also sent in retransmitted packets until release 0.9.1.
8PROFILE_INTERACTIVE-- Unused or ignored; the interactive profile is unimplemented.
9ECHO-- Unused except by ping programs
10NO_ACK-- This flag simply tells the recipient to ignore the ackThrough field in the header. Currently unused, the ackThrough field is always valid.
11-15unused

{% trans %}Implementation Details{% endtrans %}

{% trans %}Setup{% endtrans %}

{% trans -%} The initiator sends a packet with the SYNCHRONIZE flag set. This packet may contain the initial data as well. The peer replies with a packet with the SYNCHRONIZE flag set. This packet may contain the initial response data as well. {%- endtrans %}

{% trans -%} The initiator may send additional data packets, up to the initial window size, before receiving the SYNCHRONIZE response. These packets will also have the send Stream ID field set to 0. Recipients must buffer packets received on unknown streams for a short period of time, as they may arrive out of order, in advance of the SYNCHRONIZE packet. {%- endtrans %}

{% trans %}MTU Selection and Negotiation{% endtrans %}

{% trans -%} The maximum message size (also called the MTU / MRU) is negotiated to the lower value supported by the two peers. As tunnel messages are padded to 1KB, a poor MTU selection will lead to a large amount of overhead. The MTU is specified by the option i2p.streaming.maxMessageSize. The current default MTU of 1730 was chosen to fit precisely into two 1K I2NP tunnel messages, including overhead for the typical case. {%- endtrans %}

{% trans -%} The first message in a connection includes a 387 byte (typical) Destination added by the streaming layer, and usually a 898 byte (typical) LeaseSet, and Session keys, bundled in the Garlic message by the router. (The LeaseSet and Session Keys will not be bundled if an ElGamal Session was previously established). Therefore, the goal of fitting a complete HTTP request in a single 1KB I2NP message is not always attainable. However, the selection of the MTU, together with careful implementation of fragmentation and batching strategies in the tunnel gateway processor, are important factors in network bandwidth, latency, reliability, and efficiency, especially for long-lived connections. {%- endtrans %}

{% trans %}Data Integrity{% endtrans %}

{% trans i2cp=site_url('docs/protocol/i2cp') -%} Data integrity is assured by the gzip CRC-32 checksum implemented in the I2CP layer. There is no checksum field in the streaming protocol. {%- endtrans %}

{% trans %}Packet Encapsulation{% endtrans %}

{% trans garlicrouting=site_url('docs/how/garlic-routing'), i2cp=site_url('docs/protocol/i2cp'), i2np=site_url('docs/protocol/i2np'), tunnelmessage=site_url('docs/spec/tunnel-message') -%} Each packet is sent through I2P as a single message (or as an individual clove in a Garlic Message). Message encapsulation is implemented in the underlying I2CP, I2NP, and tunnel message layers. There is no packet delimiter mechanism or payload length field in the streaming protocol. {%- endtrans %}

{% trans %}Windowing{% endtrans %}

{% trans -%} The streaming lib uses standard slow-start (exponential window growth) and congestion avoidance (linear window growth) phases, with exponential backoff. Windowing and acknowledgments use packet count, not byte count. {%- endtrans %}

{% trans %}Close{% endtrans %}

{% trans -%} Any packet, including one with the SYNCHRONIZE flag set, may have the CLOSE flag sent as well. The connection is not closed until the peer responds with the CLOSE flag. CLOSE packets may contain data as well. {%- endtrans %}

{% trans %}Control Block Sharing{% endtrans %}

{% trans -%} The streaming lib supports "TCP" Control Block sharing. This shares two important streaming lib parameters (window size and round trip time) across connections to the same remote peer. This is used for "temporal" sharing at connection open/close time, not "ensemble" sharing during a connection (See RFC 2140). There is a separate share per ConnectionManager (i.e. per local Destination) so that there is no information leakage to other Destinations on the same router. The share data for a given peer expires after a few minutes. {%- endtrans %}

{% trans %}Other Parameters{% endtrans %}

{% trans -%} The following parameters are hardcoded, but may be of interest for analysis: {%- endtrans %}

{% trans %}History{% endtrans %}

{% trans -%} The streaming library has grown organically for I2P - first mihi implemented the "mini streaming library" as part of I2PTunnel, which was limited to a window size of 1 message (requiring an ACK before sending the next one), and then it was refactored out into a generic streaming interface (mirroring TCP sockets) and the full streaming implementation was deployed with a sliding window protocol and optimizations to take into account the high bandwidth x delay product. Individual streams may adjust the maximum packet size and other options. The default message size is selected to fit precisely in two 1K I2NP tunnel messages, and is a reasonable tradeoff between the bandwidth costs of retransmitting lost messages, and the latency and overhead of multiple messages. {%- endtrans %}

{% trans %}Future Work{% endtrans %}

{% trans -%} The behavior of the streaming library has a profound impact on application-level performance, and as such, is an important area for further analysis. {%- endtrans %}

{% endblock %}