]> Cypherpunks.ru repositories - nncp.git/blob - doc/sp.texi
Merge branch 'develop'
[nncp.git] / doc / sp.texi
1 @node Sync
2 @unnumbered Synchronization protocol
3
4 So-called synchronization protocol (SP) is used in current TCP daemon's
5 implementation. It is used for synchronizing @ref{Spool, spool}
6 directory contents between two nodes.
7
8 It is aimed to be very simple and effective. It uses reliable transport
9 like TCP connections. It must be effective both on single-duplex and
10 full-duplex links: for example satellites have very high throughput but
11 high-delay links, so acknowledging of each received packet, like
12 @url{https://en.wikipedia.org/wiki/XMODEM, XMODEM} does, causes
13 unacceptable performance degradation.
14
15 SP works on top of
16 @url{http://noiseprotocol.org/noise.html#interactive-patterns,
17 @code{Noise_IK_25519_ChaChaPoly_BLAKE2b}} protocol. Each Noise packet
18 is sent inside an @url{https://tools.ietf.org/html/rfc4506, XDR} envelope:
19
20 @verbatim
21 +-----------------+
22 | MAGIC | PAYLOAD |
23 +-----------------+
24 @end verbatim
25
26 @multitable @columnfractions 0.2 0.3 0.5
27 @headitem @tab XDR type @tab Value
28 @item Magic number @tab
29     8-byte, fixed length opaque data @tab
30     @verb{|N N C P S 0x00 0x00 0x01|}
31 @item Payload @tab
32     variable length opaque data @tab
33     Noise packet itself
34 @end multitable
35
36 Peers static keys are specified as @ref{Configuration, @emph{noisepub}}
37 configuration entry.
38
39 Payload inside Noise packets has maximum size of @emph{64 KiB - 256 B =
40 65280 B}. It is sent immediately in the first message by each side. The
41 very first payload (that is carried inside handshake messages) is always
42 padded to the maximum size with @emph{HALT} packets (read below), for
43 hiding actual number of @emph{INFO} packets (number of files available
44 for transmission).
45
46 Each SP payload is a concatenation of SP packets. Each packet has
47 XDR-encoded header and then corresponding XDR-encoded body. Header is
48 just an unsigned integer telling what body structure follows.
49
50 @table @emph
51
52 @item HALT
53     Stop file transmission, empty sending queue on the remote side.
54     Actually @emph{HALT} packet does not have any body, only the header
55     with the type. It is also used in the first payload for padding to
56     the maximum size.
57 @verbatim
58 +------+
59 | HALT |
60 +------+
61 @end verbatim
62
63 @item INFO
64     Information about the file we have for transmission.
65 @verbatim
66 +------+--------------------+
67 | INFO | NICE | SIZE | HASH |
68 +------+--------------------+
69 @end verbatim
70     @multitable @columnfractions 0.2 0.3 0.5
71     @headitem @tab XDR type @tab Value
72     @item Niceness @tab
73         unsigned integer @tab
74         1-255, file niceness level
75     @item Size @tab
76         unsigned hyper integer @tab
77         File size
78     @item Hash @tab
79         32-byte, fixed length opaque data @tab
80         Unique file identifier, its checksum
81     @end multitable
82
83 @item FREQ
84     File transmission request. Ask remote side to queue the file for
85     transmission.
86 @verbatim
87 +------+---------------+
88 | FREQ | HASH | OFFSET |
89 +------+---------------+
90 @end verbatim
91     @multitable @columnfractions 0.2 0.3 0.5
92     @headitem @tab XDR type @tab Value
93     @item Hash @tab
94         32-byte, fixed length opaque data @tab
95         Unique file identifier, its checksum
96     @item Offset @tab
97         unsigned hyper integer @tab
98         Offset from which remote side must transmit the file
99     @end multitable
100
101 @item FILE
102     Chunk of file.
103 @verbatim
104 +------+-------------------------+
105 | FILE | HASH | OFFSET | PAYLOAD |
106 +------+-------------------------+
107 @end verbatim
108     @multitable @columnfractions 0.2 0.3 0.5
109     @headitem @tab XDR type @tab Value
110     @item Hash @tab
111         32-byte, fixed length opaque data @tab
112         Unique file identifier, its checksum
113     @item Offset @tab
114         unsigned hyper integer @tab
115         Offset from which transmission goes
116     @item Payload @tab
117         variable length opaque data @tab
118         Chunk of file itself
119     @end multitable
120
121 @item DONE
122     Signal remote side that we have successfully downloaded the file.
123 @verbatim
124 +------+------+
125 | DONE | HASH |
126 +------+------+
127 @end verbatim
128     @multitable @columnfractions 0.2 0.3 0.5
129     @headitem @tab XDR type @tab Value
130     @item Hash @tab
131         32-byte, fixed length opaque data @tab
132         Unique file identifier, its checksum
133     @end multitable
134
135 @end table
136
137 Typical peer's behaviour is following:
138
139 @verbatiminclude sp.utxt
140
141 @enumerate
142 @item Perform @emph{Noise-IK} handshake:
143
144     @table @strong
145     @item Initiator
146     Collects all @emph{tx}-related files information and prepares
147     payload filled with @emph{INFO}s for including in the @strong{first}
148     handshake message.
149     @item Responder
150     After receiving the first handshake message, it gains remote
151     identity knowledge and similarly prepares the payload for including
152     in the @strong{second} handshake message.
153     @end table
154
155     All payloads are padded to maximal message size with @emph{HALT}s.
156
157 @item If queued @emph{INFO}s are not sent completely in handshake
158 payloads, then send all of remaining in the transport stage.
159
160 @item When @emph{INFO} packet received:
161
162     @itemize
163     @item Check that it has an acceptable niceness level.
164     Ignore it if it is too nice.
165     @item If already downloaded file exists, then queue @emph{DONE}
166     sending.
167     @item If @file{.seen} exists, then queue @emph{DONE} sending.
168     @item If @file{.part} exists, then queue @emph{FREQ} sending with
169     corresponding offset.
170     @end itemize
171
172 @item When @emph{FREQ} packet received, insert it to current sending
173 queue with niceness level sort: higher priority packets will be sent
174 first. Sending queue contains files with offsets that are needed to be
175 sent.
176
177 @item While sending queue is not empty, send @emph{FILE} packets.
178 @emph{FREQ} could contain offset equal to size -- anyway sent
179 @emph{FILE} packet with an empty payload. @emph{FILE} sending is
180 performed only if no other outgoing packets are queued: @emph{INFO}s
181 have higher priority.
182
183 @item When @emph{FILE} packet received, check if it is completely
184 downloaded (comparing to @emph{INFO}'s packet size information). If so,
185 then run background integrity checker on it. If check succeeds, then
186 delete @file{.part} suffix from file's name and send @emph{DONE} packet.
187
188 @item When @emph{DONE} packet received, delete corresponding file.
189 @item When @emph{HALT} packet received, empty file sending queue.
190
191 @item Each second, node checks: are there any new @emph{tx} packets
192 appeared and queues corresponding @emph{INFO} packets.
193
194 @item If no packets are sent and received during @ref{CfgOnlineDeadline,
195 onlinedeadline} duration, then close the connection. There is no
196 explicit indication that session is over.
197
198 @end enumerate