]> Cypherpunks.ru repositories - nncp.git/blob - doc/sp.texi
@example is more correct way to give examples
[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
58 @verbatim
59 +------+
60 | HALT |
61 +------+
62 @end verbatim
63
64 @item INFO
65     Information about the file we have for transmission.
66
67 @verbatim
68 +------+--------------------+
69 | INFO | NICE | SIZE | HASH |
70 +------+--------------------+
71 @end verbatim
72
73     @multitable @columnfractions 0.2 0.3 0.5
74     @headitem @tab XDR type @tab Value
75     @item Niceness @tab
76         unsigned integer @tab
77         1-255, file niceness level
78     @item Size @tab
79         unsigned hyper integer @tab
80         File size
81     @item Hash @tab
82         32-byte, fixed length opaque data @tab
83         Unique file identifier, its checksum
84     @end multitable
85
86 @item FREQ
87     File transmission request. Ask remote side to queue the file for
88     transmission.
89
90 @verbatim
91 +------+---------------+
92 | FREQ | HASH | OFFSET |
93 +------+---------------+
94 @end verbatim
95
96     @multitable @columnfractions 0.2 0.3 0.5
97     @headitem @tab XDR type @tab Value
98     @item Hash @tab
99         32-byte, fixed length opaque data @tab
100         Unique file identifier, its checksum
101     @item Offset @tab
102         unsigned hyper integer @tab
103         Offset from which remote side must transmit the file
104     @end multitable
105
106 @item FILE
107     Chunk of file.
108
109 @verbatim
110 +------+-------------------------+
111 | FILE | HASH | OFFSET | PAYLOAD |
112 +------+-------------------------+
113 @end verbatim
114
115     @multitable @columnfractions 0.2 0.3 0.5
116     @headitem @tab XDR type @tab Value
117     @item Hash @tab
118         32-byte, fixed length opaque data @tab
119         Unique file identifier, its checksum
120     @item Offset @tab
121         unsigned hyper integer @tab
122         Offset from which transmission goes
123     @item Payload @tab
124         variable length opaque data @tab
125         Chunk of file itself
126     @end multitable
127
128 @item DONE
129     Signal remote side that we have successfully downloaded the file.
130
131 @verbatim
132 +------+------+
133 | DONE | HASH |
134 +------+------+
135 @end verbatim
136
137     @multitable @columnfractions 0.2 0.3 0.5
138     @headitem @tab XDR type @tab Value
139     @item Hash @tab
140         32-byte, fixed length opaque data @tab
141         Unique file identifier, its checksum
142     @end multitable
143
144 @end table
145
146 Typical peer's behaviour is following:
147
148 @verbatiminclude sp.utxt
149
150 @enumerate
151 @item Perform @emph{Noise-IK} handshake:
152
153     @table @strong
154     @item Initiator
155     Collects all @emph{tx}-related files information and prepares
156     payload filled with @emph{INFO}s for including in the @strong{first}
157     handshake message.
158     @item Responder
159     After receiving the first handshake message, it gains remote
160     identity knowledge and similarly prepares the payload for including
161     in the @strong{second} handshake message.
162     @end table
163
164     All payloads are padded to maximal message size with @emph{HALT}s.
165
166 @item If queued @emph{INFO}s are not sent completely in handshake
167 payloads, then send all of remaining in the transport stage.
168
169 @item When @emph{INFO} packet received:
170
171     @itemize
172     @item Check that it has an acceptable niceness level.
173     Ignore it if it is too nice.
174     @item If already downloaded file exists, then queue @emph{DONE}
175     sending.
176     @item If @file{.seen} exists, then queue @emph{DONE} sending.
177     @item If @file{.part} exists, then queue @emph{FREQ} sending with
178     corresponding offset.
179     @end itemize
180
181 @item When @emph{FREQ} packet received, insert it to current sending
182 queue with niceness level sort: higher priority packets will be sent
183 first. Sending queue contains files with offsets that are needed to be
184 sent.
185
186 @item While sending queue is not empty, send @emph{FILE} packets.
187 @emph{FREQ} could contain offset equal to size -- anyway sent
188 @emph{FILE} packet with an empty payload. @emph{FILE} sending is
189 performed only if no other outgoing packets are queued: @emph{INFO}s
190 have higher priority.
191
192 @item When @emph{FILE} packet received, check if it is completely
193 downloaded (comparing to @emph{INFO}'s packet size information). If so,
194 then run background integrity checker on it. If check succeeds, then
195 delete @file{.part} suffix from file's name and send @emph{DONE} packet.
196
197 @item When @emph{DONE} packet received, delete corresponding file.
198 @item When @emph{HALT} packet received, empty file sending queue.
199
200 @item Each second, node checks: are there any new @emph{tx} packets
201 appeared and queues corresponding @emph{INFO} packets.
202
203 @item If no packets are sent and received during @ref{CfgOnlineDeadline,
204 onlinedeadline} duration, then close the connection. There is no
205 explicit indication that session is over.
206
207 @end enumerate