syncthing-bep.7 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. '\" t
  2. .\" Man page generated from reStructuredText.
  3. .
  4. .
  5. .nr rst2man-indent-level 0
  6. .
  7. .de1 rstReportMargin
  8. \\$1 \\n[an-margin]
  9. level \\n[rst2man-indent-level]
  10. level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
  11. -
  12. \\n[rst2man-indent0]
  13. \\n[rst2man-indent1]
  14. \\n[rst2man-indent2]
  15. ..
  16. .de1 INDENT
  17. .\" .rstReportMargin pre:
  18. . RS \\$1
  19. . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
  20. . nr rst2man-indent-level +1
  21. .\" .rstReportMargin post:
  22. ..
  23. .de UNINDENT
  24. . RE
  25. .\" indent \\n[an-margin]
  26. .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
  27. .nr rst2man-indent-level -1
  28. .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
  29. .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
  30. ..
  31. .TH "SYNCTHING-BEP" "7" "Jul 21, 2025" "v1.29.7" "Syncthing"
  32. .SH NAME
  33. syncthing-bep \- Block Exchange Protocol v1
  34. .SH INTRODUCTION AND DEFINITIONS
  35. .sp
  36. The Block Exchange Protocol (BEP) is used between two or more \fIdevices\fP thus
  37. forming a \fIcluster\fP\&. Each device has one or more \fIfolders\fP of files
  38. described by the \fIlocal model\fP, containing metadata and block hashes. The
  39. local model is sent to the other devices in the cluster. The union of all
  40. files in the local models, with files selected for highest change version,
  41. forms the \fIglobal model\fP\&. Each device strives to get its folders in sync
  42. with the global model by requesting missing or outdated blocks from the
  43. other devices in the cluster.
  44. .sp
  45. File data is described and transferred in units of \fIblocks\fP, each being from
  46. 128 KiB (131072 bytes) to 16 MiB in size, in steps of powers of two. The
  47. block size may vary between files but is constant in any given file, except
  48. for the last block which may be smaller.
  49. .sp
  50. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”,
  51. “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this
  52. document are to be interpreted as described in \X'tty: link https://datatracker.ietf.org/doc/html/rfc2119.html'\fI\%RFC 2119\fP <\fBhttps://datatracker.ietf.org/doc/html/rfc2119.html\fP>\X'tty: link'\&.
  53. .SH TRANSPORT AND AUTHENTICATION
  54. .sp
  55. BEP is deployed as the highest level in a protocol stack, with the lower
  56. level protocols providing encryption and authentication.
  57. .INDENT 0.0
  58. .INDENT 3.5
  59. .sp
  60. .EX
  61. +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+
  62. | Block Exchange Protocol |
  63. |\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-|
  64. | Encryption & Auth (TLS 1.3) |
  65. |\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-|
  66. | Reliable Transport |
  67. |\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-|
  68. v ... v
  69. .EE
  70. .UNINDENT
  71. .UNINDENT
  72. .sp
  73. The encryption and authentication layer SHALL use TLS 1.3 or a higher
  74. revision.
  75. .sp
  76. The exact nature of the authentication is up to the application, however
  77. it SHALL be based on the TLS certificate presented at the start of the
  78. connection. Possibilities include certificates signed by a common
  79. trusted CA, preshared certificates, preshared certificate fingerprints
  80. or certificate pinning combined with some out of band first
  81. verification. The reference implementation uses preshared certificate
  82. fingerprints (SHA\-256) referred to as “Device IDs”.
  83. .sp
  84. There is no required order or synchronization among BEP messages except
  85. as noted per message type \- any message type may be sent at any time and
  86. the sender need not await a response to one message before sending
  87. another.
  88. .sp
  89. The underlying transport protocol MUST guarantee reliable packet delivery.
  90. .sp
  91. In this document, in diagrams and text, “bit 0” refers to the \fImost
  92. significant\fP bit of a word; “bit 15” is thus the least significant bit of a
  93. 16 bit word (int16) and “bit 31” is the least significant bit of a 32 bit
  94. word (int32). Non protocol buffer integers are always represented in network
  95. byte order (i.e., big endian) and are signed unless stated otherwise, but
  96. when describing message lengths negative values do not make sense and the
  97. most significant bit MUST be zero.
  98. .sp
  99. The protocol buffer schemas in this document are in \fBproto3\fP syntax. This
  100. means, among other things, that all fields are optional and will assume
  101. their default value when missing. This does not necessarily mean that a
  102. message is \fIvalid\fP with all fields empty \- for example, an index entry for a
  103. file that does not have a name is not useful and MAY be rejected by the
  104. implementation. However the folder label is for human consumption only so an
  105. empty label should be accepted \- the implementation will have to choose some
  106. way to represent the folder, perhaps by using the ID in it’s place or
  107. automatically generating a label.
  108. .SH PRE-AUTHENTICATION MESSAGES
  109. .sp
  110. AFTER establishing a connection, but BEFORE performing any authentication,
  111. devices MUST exchange Hello messages.
  112. .sp
  113. Hello messages are used to carry additional information about the peer,
  114. which might be of interest to the user even if the peer is not permitted to
  115. communicate due to failing authentication. Note that the certificate based
  116. authentication may be considered part of the TLS handshake that precedes the
  117. Hello message exchange, but even in the case that a connection is rejected a
  118. Hello message must be sent before the connection is terminated.
  119. .sp
  120. Hello messages MUST be prefixed with an int32 containing the magic number
  121. \fB0x2EA7D90B\fP, followed by an int16 representing the size of the message,
  122. followed by the contents of the Hello message itself.
  123. .INDENT 0.0
  124. .INDENT 3.5
  125. .sp
  126. .EX
  127. 0 1
  128. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  129. +\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+
  130. | Magic |
  131. | (32 bits) |
  132. +\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+
  133. | Length |
  134. +\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+
  135. / /
  136. \e Hello \e
  137. / /
  138. +\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+
  139. .EE
  140. .UNINDENT
  141. .UNINDENT
  142. .sp
  143. The Hello message itself is in protocol buffer format with the following schema:
  144. .INDENT 0.0
  145. .INDENT 3.5
  146. .sp
  147. .EX
  148. message Hello {
  149. string device_name = 1;
  150. string client_name = 2;
  151. string client_version = 3;
  152. }
  153. .EE
  154. .UNINDENT
  155. .UNINDENT
  156. .SS Fields (Hello message)
  157. .sp
  158. The \fBdevice_name\fP is a human readable (configured or auto detected) device
  159. name or host name, for the remote device.
  160. .sp
  161. The \fBclient_name\fP and \fBclient_version\fP identifies the implementation. The
  162. values SHOULD be simple strings identifying the implementation name, as a
  163. user would expect to see it, and the version string in the same manner. An
  164. example client name is “syncthing” and an example client version is “v0.7.2”.
  165. The client version field SHOULD follow the patterns laid out in the \X'tty: link https://semver.org/'\fI\%Semantic
  166. Versioning\fP <\fBhttps://semver.org/\fP>\X'tty: link' standard.
  167. .sp
  168. Immediately after exchanging Hello messages, the connection MUST be dropped
  169. if the remote device does not pass authentication.
  170. .SH POST-AUTHENTICATION MESSAGES
  171. .sp
  172. Every message post authentication is made up of several parts:
  173. .INDENT 0.0
  174. .IP \(bu 2
  175. A header length word
  176. .IP \(bu 2
  177. A \fBHeader\fP
  178. .IP \(bu 2
  179. A message length word
  180. .IP \(bu 2
  181. A \fBMessage\fP
  182. .UNINDENT
  183. .INDENT 0.0
  184. .INDENT 3.5
  185. .sp
  186. .EX
  187. 0 1
  188. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  189. +\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+
  190. | Header Length |
  191. +\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+
  192. / /
  193. \e Header \e
  194. / /
  195. +\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+
  196. | Message Length |
  197. | (32 bits) |
  198. +\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+
  199. / /
  200. \e Message \e
  201. / /
  202. +\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+\-+
  203. .EE
  204. .UNINDENT
  205. .UNINDENT
  206. .sp
  207. The header length word is 16 bits. It indicates the length of the following
  208. \fBHeader\fP message. The Header is in protocol buffer format. The Header
  209. describes the type and compression status of the following message.
  210. .sp
  211. The message is preceded by the 32 bit message length word and is one of the
  212. concrete BEP messages described below, identified by the \fBtype\fP field of
  213. the Header.
  214. .sp
  215. As always, the length words are in network byte order (big endian).
  216. .INDENT 0.0
  217. .INDENT 3.5
  218. .sp
  219. .EX
  220. message Header {
  221. MessageType type = 1;
  222. MessageCompression compression = 2;
  223. }
  224. enum MessageType {
  225. CLUSTER_CONFIG = 0;
  226. INDEX = 1;
  227. INDEX_UPDATE = 2;
  228. REQUEST = 3;
  229. RESPONSE = 4;
  230. DOWNLOAD_PROGRESS = 5;
  231. PING = 6;
  232. CLOSE = 7;
  233. }
  234. enum MessageCompression {
  235. NONE = 0;
  236. LZ4 = 1;
  237. }
  238. .EE
  239. .UNINDENT
  240. .UNINDENT
  241. .sp
  242. When the \fBcompression\fP field is \fBNONE\fP, the message is directly in
  243. protocol buffer format.
  244. .sp
  245. When the compression field is \fBLZ4\fP, the message consists of a 32 bit
  246. integer describing the uncompressed message length followed by a single LZ4
  247. block. After decompressing the LZ4 block it should be interpreted as a
  248. protocol buffer message just as in the uncompressed case.
  249. .SH MESSAGE SUBTYPES
  250. .SS Cluster Config
  251. .sp
  252. This informational message provides information about the cluster
  253. configuration as it pertains to the current connection. A Cluster Config
  254. message MUST be the first post authentication message sent on a BEP
  255. connection. Additional Cluster Config messages MAY be sent after the initial
  256. exchange to change the connection in place with the update configuration. If an
  257. implementation does not support that, it SHOULD close the connection.
  258. .SS Protocol Buffer Schema
  259. .INDENT 0.0
  260. .INDENT 3.5
  261. .sp
  262. .EX
  263. message ClusterConfig {
  264. repeated Folder folders = 1;
  265. }
  266. message Folder {
  267. string id = 1;
  268. string label = 2;
  269. bool read_only = 3;
  270. bool ignore_permissions = 4;
  271. bool ignore_delete = 5;
  272. bool disable_temp_indexes = 6;
  273. bool paused = 7;
  274. repeated Device devices = 16;
  275. }
  276. message Device {
  277. bytes id = 1;
  278. string name = 2;
  279. repeated string addresses = 3;
  280. Compression compression = 4;
  281. string cert_name = 5;
  282. int64 max_sequence = 6;
  283. bool introducer = 7;
  284. uint64 index_id = 8;
  285. bool skip_introduction_removals = 9;
  286. bytes encryption_password_token = 10;
  287. }
  288. enum Compression {
  289. METADATA = 0;
  290. NEVER = 1;
  291. ALWAYS = 2;
  292. }
  293. .EE
  294. .UNINDENT
  295. .UNINDENT
  296. .SS Fields (Cluster Config Message)
  297. .sp
  298. The \fBfolders\fP field contains the list of folders that will be synchronized
  299. over the current connection.
  300. .SS Fields (Folder Message)
  301. .sp
  302. The \fBid\fP field contains the folder ID, which is the unique identifier of
  303. the folder.
  304. .sp
  305. The \fBlabel\fP field contains the folder label, the human readable name of
  306. the folder.
  307. .sp
  308. The \fBread_only\fP field is set for folders that the device will accept no
  309. updates from the network for.
  310. .sp
  311. The \fBignore_permissions\fP field is set for folders that the device will not
  312. accept or announce file permissions for.
  313. .sp
  314. The \fBignore_delete\fP field is set for folders that the device will ignore
  315. deletes for.
  316. .sp
  317. The \fBdisable_temp_indexes\fP field is set for folders that will not dispatch
  318. and do not wish to receive progress updates about partially downloaded files
  319. via Download Progress messages.
  320. .sp
  321. The \fBpaused\fP field is set for folders that are currently paused.
  322. .sp
  323. The \fBdevices\fP field is a list of devices participating in sharing this
  324. folder.
  325. .SS Fields (Device Message)
  326. .sp
  327. The device \fBid\fP field is a 32 byte number that uniquely identifies the
  328. device. For instance, the reference implementation uses the SHA\-256 of the
  329. device X.509 certificate.
  330. .sp
  331. The \fBname\fP field is a human readable name assigned to the described device
  332. by the sending device. It MAY be empty and it need not be unique.
  333. .sp
  334. The list of \fBaddresses\fP is that used by the sending device to connect to
  335. the described device.
  336. .sp
  337. The \fBcompression\fP field indicates the compression mode in use for this
  338. device and folder. The following values are valid:
  339. .INDENT 0.0
  340. .TP
  341. .B 0
  342. Compress metadata. This enables compression of metadata messages such as Index.
  343. .TP
  344. .B 1
  345. Compression disabled. No compression is used on any message.
  346. .TP
  347. .B 2
  348. Compress always. Metadata messages as well as Response messages are compressed.
  349. .UNINDENT
  350. .sp
  351. The \fBcert_name\fP field indicates the expected certificate name for this
  352. device. It is commonly blank, indicating to use the implementation default.
  353. .sp
  354. The \fBmax_sequence\fP field contains the highest sequence number of the files
  355. in the index. See \fI\%Delta Index Exchange\fP for the usage of this field.
  356. .sp
  357. The \fBintroducer\fP field is set for devices that are trusted as cluster
  358. introducers.
  359. .sp
  360. The \fBindex_id\fP field contains the unique identifier for the current set of
  361. index data. See \fI\%Delta Index Exchange\fP for the usage of this field.
  362. .sp
  363. The \fBskip_introduction_removals\fP field signifies if the remote device has
  364. opted to ignore introduction removals for the given device. This setting is
  365. copied across as we are being introduced to a new device.
  366. .sp
  367. The \fBencryption_password_token\fP field contains a token derived from the password, that is
  368. used to encrypt data sent to this device. If the device is the same as the
  369. device sending the message, it signifies that the device itself has encrypted
  370. data that was encrypted with the given token. It is empty or missing if there is
  371. no encryption. See \fI\%Untrusted Device Encryption\fP for details on the encryption scheme.
  372. .SS Index and Index Update
  373. .sp
  374. The Index and Index Update messages define the contents of the senders
  375. folder. An Index message represents the full contents of the folder and
  376. thus supersedes any previous index. An Index Update amends an existing
  377. index with new information, not affecting any entries not included in
  378. the message. An Index Update MAY NOT be sent unless preceded by an
  379. Index, unless a non\-zero Max Sequence has been announced for the
  380. given folder by the peer device.
  381. .sp
  382. The Index and Index Update messages are currently identical in format,
  383. although this is not guaranteed to be the case in the future.
  384. .SS Protocol Buffer Schema
  385. .INDENT 0.0
  386. .INDENT 3.5
  387. .sp
  388. .EX
  389. message Index {
  390. string folder = 1;
  391. repeated FileInfo files = 2;
  392. }
  393. message IndexUpdate {
  394. string folder = 1;
  395. repeated FileInfo files = 2;
  396. }
  397. message FileInfo {
  398. string name = 1;
  399. FileInfoType type = 2;
  400. int64 size = 3;
  401. uint32 permissions = 4;
  402. int64 modified_s = 5;
  403. int32 modified_ns = 11;
  404. uint64 modified_by = 12;
  405. bool deleted = 6;
  406. bool invalid = 7;
  407. bool no_permissions = 8;
  408. Vector version = 9;
  409. int64 sequence = 10;
  410. int32 block_size = 13;
  411. repeated BlockInfo Blocks = 16;
  412. string symlink_target = 17;
  413. }
  414. enum FileInfoType {
  415. FILE = 0;
  416. DIRECTORY = 1;
  417. SYMLINK_FILE = 2 [deprecated = true];
  418. SYMLINK_DIRECTORY = 3 [deprecated = true];
  419. SYMLINK = 4;
  420. }
  421. message BlockInfo {
  422. int64 offset = 1;
  423. int32 size = 2;
  424. bytes hash = 3;
  425. uint32 weak_hash = 4;
  426. }
  427. message Vector {
  428. repeated Counter counters = 1;
  429. }
  430. message Counter {
  431. uint64 id = 1;
  432. uint64 value = 2;
  433. }
  434. .EE
  435. .UNINDENT
  436. .UNINDENT
  437. .SS Fields (Index Message)
  438. .sp
  439. The \fBfolder\fP field identifies the folder that the index message pertains to.
  440. .sp
  441. The \fBfiles\fP field is a list of files making up the index information.
  442. .SS Fields (FileInfo Message)
  443. .sp
  444. The \fBname\fP is the file name path relative to the folder root. Like all
  445. strings in BEP, the Name is always in UTF\-8 NFC regardless of operating
  446. system or file system specific conventions. The name field uses the slash
  447. character (“/”) as path separator, regardless of the implementation’s
  448. operating system conventions. The combination of folder and name uniquely
  449. identifies each file in a cluster.
  450. .sp
  451. The \fBtype\fP field contains the type of the described item. The type is one
  452. of \fBfile (0)\fP, \fBdirectory (1)\fP, or \fBsymlink (4)\fP\&.
  453. .sp
  454. The \fBsize\fP field contains the size of the file, in bytes. For directories
  455. and symlinks the size is zero.
  456. .sp
  457. The \fBpermissions\fP field holds the common Unix permission bits. An
  458. implementation MAY ignore or interpret these as is suitable on the host
  459. operating system.
  460. .sp
  461. The \fBmodified_s\fP time is expressed as the number of seconds since the Unix
  462. Epoch (1970\-01\-01 00:00:00 UTC). The \fBmodified_ns\fP field holds the
  463. nanosecond part of the modification time.
  464. .sp
  465. The \fBmodified_by\fP field holds the short id of the client that last made
  466. any modification to the file whether add, change or delete. This will be
  467. overwritten every time a change is made to the file by the last client to do
  468. so and so does not hold history.
  469. .sp
  470. The \fBdeleted\fP field is set when the file has been deleted. The block list
  471. SHALL be of length zero and the modification time indicates the time of
  472. deletion or, if the time of deletion is not reliably determinable, the last
  473. known modification time.
  474. .sp
  475. The \fBinvalid\fP field is set when the file is invalid and unavailable for
  476. synchronization. A peer MAY set this bit to indicate that it can temporarily
  477. not serve data for the file.
  478. .sp
  479. The \fBno_permissions\fP field is set when there is no permission information
  480. for the file. This is the case when it originates on a file system which
  481. does not support permissions. Changes to only permission bits SHOULD be
  482. disregarded on files with this bit set. The permissions bits MUST be set to
  483. the octal value 0666.
  484. .sp
  485. The \fBversion\fP field is a version vector describing the updates performed
  486. to a file by all members in the cluster. Each counter in the version vector
  487. is an ID\-Value tuple. The ID is the first 64 bits of the device ID. The
  488. Value is a simple incrementing counter, starting at zero. The combination of
  489. Folder, Name and Version uniquely identifies the contents of a file at a
  490. given point in time.
  491. .sp
  492. The \fBsequence\fP field is the value of a device local monotonic clock at the
  493. time of last local database update to a file. The clock ticks on every local
  494. database update, thus forming a sequence number over database updates.
  495. .sp
  496. The \fBblock_size\fP field is the size, in bytes, of each individual block in
  497. the block list (except, possibly, the last block). If this field is missing
  498. or zero, the block size is assumed to be 128 KiB (131072 bytes). Valid
  499. values of this field are the powers of two from 128 KiB through 16 MiB. See
  500. also \fI\%Selection of Block Size\fP\&.
  501. .sp
  502. The \fBblocks\fP list contains the size and hash for each block in the file.
  503. Each block represents a \fBblock_size\fP\-sized slice of the file, except for
  504. the last block which may represent a smaller amount of data. The block list
  505. is empty for directories and symlinks.
  506. .sp
  507. The \fBsymlink_target\fP field contains the symlink target, for entries of
  508. symlink type. It is empty for all other entry types.
  509. .SS Request
  510. .sp
  511. The Request message expresses the desire to receive a data block
  512. corresponding to a part of a certain file in the peer’s folder.
  513. .SS Protocol Buffer Schema
  514. .INDENT 0.0
  515. .INDENT 3.5
  516. .sp
  517. .EX
  518. message Request {
  519. int32 id = 1;
  520. string folder = 2;
  521. string name = 3;
  522. int64 offset = 4;
  523. int32 size = 5;
  524. bytes hash = 6;
  525. bool from_temporary = 7;
  526. }
  527. .EE
  528. .UNINDENT
  529. .UNINDENT
  530. .SS Fields
  531. .sp
  532. The \fBid\fP is the request identifier. It will be matched in the
  533. corresponding \fBResponse\fP message. Each outstanding request must have a
  534. unique ID.
  535. .sp
  536. The \fBfolder\fP and \fBname\fP fields are as documented for the Index message.
  537. The \fBoffset\fP and \fBsize\fP fields specify the region of the file to be
  538. transferred. This SHOULD equate to exactly one block as seen in an Index
  539. message.
  540. .sp
  541. The \fIhash\fP field MAY be set to the expected hash value of the block. If set,
  542. the other device SHOULD ensure that the transmitted block matches the
  543. requested hash. The other device MAY reuse a block from a different file and
  544. offset having the same size and hash, if one exists.
  545. .sp
  546. The \fBfrom_temporary\fP field is set to indicate that the read should be
  547. performed from the temporary file (converting name to it’s temporary form)
  548. and falling back to the non temporary file if any error occurs. Knowledge of
  549. contents of temporary files comes from DownloadProgress messages.
  550. .SS Response
  551. .sp
  552. The Response message is sent in response to a Request message.
  553. .SS Protocol Buffer Schema
  554. .INDENT 0.0
  555. .INDENT 3.5
  556. .sp
  557. .EX
  558. message Response {
  559. int32 id = 1;
  560. bytes data = 2;
  561. ErrorCode code = 3;
  562. }
  563. enum ErrorCode {
  564. NO_ERROR = 0;
  565. GENERIC = 1;
  566. NO_SUCH_FILE = 2;
  567. INVALID_FILE = 3;
  568. }
  569. .EE
  570. .UNINDENT
  571. .UNINDENT
  572. .SS Fields
  573. .sp
  574. The \fBid\fP field is the request identifier. It must match the ID of the
  575. \fBRequest\fP that is being responded to.
  576. .sp
  577. The \fBdata\fP field contains either the requested data block or is empty if
  578. the requested block is not available.
  579. .sp
  580. The \fBcode\fP field contains an error code describing the reason a Request
  581. could not be fulfilled, in the case where zero length data was returned. The
  582. following values are defined:
  583. .INDENT 0.0
  584. .TP
  585. .B 0
  586. No Error (data should be present)
  587. .TP
  588. .B 1
  589. Generic Error
  590. .TP
  591. .B 2
  592. No Such File (the requested file does not exist, or the offset is
  593. outside the acceptable range for the file)
  594. .TP
  595. .B 3
  596. Invalid (file exists but has invalid bit set or is otherwise
  597. unavailable)
  598. .UNINDENT
  599. .SS DownloadProgress
  600. .sp
  601. The DownloadProgress message is used to notify remote devices about partial
  602. availability of files. By default, these messages are sent every 5 seconds,
  603. and only in the cases where progress or state changes have been detected.
  604. Each DownloadProgress message is addressed to a specific folder and MUST
  605. contain zero or more FileDownloadProgressUpdate messages.
  606. .SS Protocol Buffer Schema
  607. .INDENT 0.0
  608. .INDENT 3.5
  609. .sp
  610. .EX
  611. message DownloadProgress {
  612. string folder = 1;
  613. repeated FileDownloadProgressUpdate updates = 2;
  614. }
  615. message FileDownloadProgressUpdate {
  616. FileDownloadProgressUpdateType update_type = 1;
  617. string name = 2;
  618. Vector version = 3;
  619. repeated int32 block_indexes = 4;
  620. }
  621. enum FileDownloadProgressUpdateType {
  622. APPEND = 0;
  623. FORGET = 1;
  624. }
  625. .EE
  626. .UNINDENT
  627. .UNINDENT
  628. .SS Fields (DownloadProgress Message)
  629. .sp
  630. The \fBfolder\fP field represents the ID of the folder for which the update is
  631. being provided.
  632. .sp
  633. The \fBupdates\fP field is a list of progress update messages.
  634. .SS Fields (FileDownloadProgressUpdate Message)
  635. .sp
  636. The \fBupdate_type\fP indicates whether the update is of type \fBappend (0)\fP
  637. (new blocks are available) or \fBforget (1)\fP (the file transfer has
  638. completed or failed).
  639. .sp
  640. The \fBname\fP field defines the file name from the global index for which
  641. this update is being sent.
  642. .sp
  643. The \fBversion\fP message defines the version of the file for which this
  644. update is being sent.
  645. .sp
  646. The \fBblock_indexes\fP field is a list of positive integers, where each
  647. integer represents the index of the block in the FileInfo message Blocks
  648. array that has become available for download.
  649. .sp
  650. For example an integer with value 3 represents that the data defined in the
  651. fourth BlockInfo message of the FileInfo message of that file is now
  652. available. Please note that matching should be done on \fBname\fP AND
  653. \fBversion\fP\&. Furthermore, each update received is incremental, for example
  654. the initial update message might contain indexes 0, 1, 2, an update 5
  655. seconds later might contain indexes 3, 4, 5 which should be appended to the
  656. original list, which implies that blocks 0\-5 are currently available.
  657. .sp
  658. Block indexes MAY be added in any order. An implementation MUST NOT assume
  659. that block indexes are added in any specific order.
  660. .sp
  661. The \fBforget\fP field being set implies that previously advertised file is no
  662. longer available, therefore the list of block indexes should be truncated.
  663. .sp
  664. Messages with the \fBforget\fP field set MUST NOT have any block indexes.
  665. .sp
  666. Any update message which is being sent for a different \fBversion\fP of the
  667. same file name must be preceded with an update message for the old version
  668. of that file with the \fBforget\fP field set.
  669. .sp
  670. As a safeguard on the receiving side, the value of \fBversion\fP changing
  671. between update messages implies that the file has changed and that any
  672. indexes previously advertised are no longer available. The list of available
  673. block indexes MUST be replaced (rather than appended) with the indexes
  674. specified in this message.
  675. .SS Ping
  676. .sp
  677. The Ping message is used to determine that a connection is alive, and to
  678. keep connections alive through state tracking network elements such as
  679. firewalls and NAT gateways. A Ping message is sent every 90 seconds, if no
  680. other message has been sent in the preceding 90 seconds.
  681. .SS Protocol Buffer Schema
  682. .INDENT 0.0
  683. .INDENT 3.5
  684. .sp
  685. .EX
  686. message Ping {
  687. }
  688. .EE
  689. .UNINDENT
  690. .UNINDENT
  691. .SS Close
  692. .sp
  693. The Close message MAY be sent to indicate that the connection will be torn
  694. down due to an error condition. A Close message MUST NOT be followed by
  695. further messages.
  696. .SS Protocol Buffer Schema
  697. .INDENT 0.0
  698. .INDENT 3.5
  699. .sp
  700. .EX
  701. message Close {
  702. string reason = 1;
  703. }
  704. .EE
  705. .UNINDENT
  706. .UNINDENT
  707. .SS Fields
  708. .sp
  709. The \fBreason\fP field contains a human readable description of the error
  710. condition.
  711. .SH SHARING MODES
  712. .SS Trusted
  713. .sp
  714. Trusted mode is the default sharing mode. Updates are exchanged in both
  715. directions.
  716. .INDENT 0.0
  717. .INDENT 3.5
  718. .sp
  719. .EX
  720. +\-\-\-\-\-\-\-\-\-\-\-\-+ Updates /\-\-\-\-\-\-\-\-\-\e
  721. | | \-\-\-\-\-\-\-\-\-\-\-> / \e
  722. | Device | | Cluster |
  723. | | <\-\-\-\-\-\-\-\-\-\-\- \e /
  724. +\-\-\-\-\-\-\-\-\-\-\-\-+ Updates \e\-\-\-\-\-\-\-\-\-/
  725. .EE
  726. .UNINDENT
  727. .UNINDENT
  728. .SS Send Only
  729. .sp
  730. In send only mode, a device does not apply any updates from the cluster, but
  731. publishes changes of its local folder to the cluster as usual.
  732. .INDENT 0.0
  733. .INDENT 3.5
  734. .sp
  735. .EX
  736. +\-\-\-\-\-\-\-\-\-\-\-\-+ Updates /\-\-\-\-\-\-\-\-\-\e
  737. | | \-\-\-\-\-\-\-\-\-\-\-> / \e
  738. | Device | | Cluster |
  739. | | \e /
  740. +\-\-\-\-\-\-\-\-\-\-\-\-+ \e\-\-\-\-\-\-\-\-\-/
  741. .EE
  742. .UNINDENT
  743. .UNINDENT
  744. .SS Receive Only
  745. .sp
  746. In receive only mode, a device does not send any updates to the cluster, but
  747. accepts changes to its local folder from the cluster as usual.
  748. .INDENT 0.0
  749. .INDENT 3.5
  750. .sp
  751. .EX
  752. +\-\-\-\-\-\-\-\-\-\-\-\-+ Updates /\-\-\-\-\-\-\-\-\-\e
  753. | | <\-\-\-\-\-\-\-\-\-\-\- / \e
  754. | Device | | Cluster |
  755. | | \e /
  756. +\-\-\-\-\-\-\-\-\-\-\-\-+ \e\-\-\-\-\-\-\-\-\-/
  757. .EE
  758. .UNINDENT
  759. .UNINDENT
  760. .SH DELTA INDEX EXCHANGE
  761. .sp
  762. Index data must be exchanged whenever two devices connect so that one knows
  763. the files available on the other. In the most basic case this happens by way
  764. of sending an \fBIndex\fP message followed by one or more \fBIndex Update\fP
  765. messages. Any previous index data known for a remote device is removed and
  766. replaced with the new index data received in an \fBIndex\fP message, while the
  767. contents of an \fBIndex Update\fP message is simply added to the existing
  768. index data.
  769. .sp
  770. For situations with large indexes or frequent reconnects this can be quite
  771. inefficient. A mechanism can then be used to retain index data between
  772. connections and only transmit any changes since that data on connection
  773. start. This is called “delta indexes”. To enable this mechanism the
  774. \fBsequence\fP and \fBindex ID\fP fields are used.
  775. .INDENT 0.0
  776. .TP
  777. .B Sequence:
  778. Each index item (i.e., file, directory or symlink) has a sequence number
  779. field. It contains the value of a counter at the time the index item was
  780. updated. The counter increments by one for each change. That is, as files
  781. are scanned and added to the index they get assigned sequence numbers
  782. 1, 2, 3 and so on. The next file to be changed or detected gets sequence
  783. number 4, and future updates continue in the same fashion.
  784. .TP
  785. .B Index ID:
  786. Each folder has an Index ID. This is a 64 bit random identifier set at
  787. index creation time.
  788. .UNINDENT
  789. .sp
  790. Given the above, we know that the tuple {index ID, maximum sequence number}
  791. uniquely identifies a point in time of a given index. Any further changes
  792. will increase the sequence number of some item, and thus the maximum
  793. sequence number for the index itself. Should the index be reset or removed
  794. (i.e., the sequence number reset to zero), a new index ID must be generated.
  795. .sp
  796. By letting a device know the {index ID, maximum sequence number} we have for
  797. their index data, that device can arrange to only transmit \fBIndex Update\fP
  798. messages for items with a higher sequence number. This is the delta index
  799. mechanism.
  800. .sp
  801. The index ID and maximum sequence number known for each device is
  802. transmitted in the \fBCluster Config\fP message at connection start.
  803. .sp
  804. For this mechanism to be reliable it is essential that outgoing index
  805. information is ordered by increasing sequence number. Devices announcing a
  806. non\-zero index ID in the \fBCluster Config\fP message MUST send all index data
  807. ordered by increasing sequence number. Devices not intending to participate
  808. in delta index exchange MUST send a zero index ID or, equivalently, not send
  809. the \fBindex_id\fP attribute at all.
  810. .SH MESSAGE LIMITS
  811. .sp
  812. An implementation MAY impose reasonable limits on the length of messages and
  813. message fields to aid robustness in the face of corruption or broken
  814. implementations. An implementation should strive to keep messages short
  815. and to the point, favouring more and smaller messages over fewer and larger.
  816. For example, favour a smaller Index message followed by one or more Index
  817. Update messages rather than sending a very large Index message.
  818. .sp
  819. The Syncthing implementation imposes a hard limit of 500,000,000 bytes on
  820. all messages. Attempting to send or receive a larger message will result in
  821. a connection close. This size was chosen to accommodate Index messages
  822. containing a large block list. It’s intended that the limit may be further
  823. reduced in a future protocol update supporting variable block sizes (and
  824. thus shorter block lists for large files).
  825. .SH SELECTION OF BLOCK SIZE
  826. .sp
  827. The desired block size for any given file is the smallest block size that
  828. results in fewer than 2000 blocks, or the maximum block size for larger
  829. files. This rule results in the following table of block sizes per file
  830. size:
  831. .TS
  832. box center;
  833. l|l.
  834. T{
  835. File Size
  836. T} T{
  837. Block Size
  838. T}
  839. _
  840. T{
  841. 0 \- 250 MiB
  842. T} T{
  843. 128 KiB
  844. T}
  845. _
  846. T{
  847. 250 MiB \- 500 MiB
  848. T} T{
  849. 256 KiB
  850. T}
  851. _
  852. T{
  853. 500 MiB \- 1 GiB
  854. T} T{
  855. 512 KiB
  856. T}
  857. _
  858. T{
  859. 1 GiB \- 2 GiB
  860. T} T{
  861. 1 MiB
  862. T}
  863. _
  864. T{
  865. 2 GiB \- 4 GiB
  866. T} T{
  867. 2 MiB
  868. T}
  869. _
  870. T{
  871. 4 GiB \- 8 GiB
  872. T} T{
  873. 4 MiB
  874. T}
  875. _
  876. T{
  877. 8 GiB \- 16 GiB
  878. T} T{
  879. 8 MiB
  880. T}
  881. _
  882. T{
  883. 16 GiB \- up
  884. T} T{
  885. 16 MiB
  886. T}
  887. .TE
  888. .sp
  889. An implementation MAY deviate from the block size rule when there is good
  890. reason to do so. For example, if a file has been indexed at a certain block
  891. size and grows beyond 2000 blocks it may be retained at the current block
  892. size for practical reasons. When there is no overriding reason to the
  893. contrary, such as when indexing a new file for the first time, the block
  894. size rule above SHOULD be followed.
  895. .sp
  896. An implementation MUST therefore accept files with a block size differing
  897. from the above rule. This does not mean that arbitrary block sizes are
  898. allowed. The block size used MUST be exactly one of the power\-of\-two block
  899. sizes listed in the table above.
  900. .SH EXAMPLE EXCHANGE
  901. .TS
  902. box center;
  903. l|l|l.
  904. T{
  905. #
  906. T} T{
  907. A
  908. T} T{
  909. B
  910. T}
  911. _
  912. T{
  913. 1
  914. T} T{
  915. ClusterConfiguration\->
  916. T} T{
  917. <\-ClusterConfiguration
  918. T}
  919. _
  920. T{
  921. 2
  922. T} T{
  923. Index\->
  924. T} T{
  925. <\-Index
  926. T}
  927. _
  928. T{
  929. 3
  930. T} T{
  931. IndexUpdate\->
  932. T} T{
  933. <\-IndexUpdate
  934. T}
  935. _
  936. T{
  937. 4
  938. T} T{
  939. IndexUpdate\->
  940. T} T{
  941. T}
  942. _
  943. T{
  944. 5
  945. T} T{
  946. Request\->
  947. T} T{
  948. T}
  949. _
  950. T{
  951. 6
  952. T} T{
  953. Request\->
  954. T} T{
  955. T}
  956. _
  957. T{
  958. 7
  959. T} T{
  960. Request\->
  961. T} T{
  962. T}
  963. _
  964. T{
  965. 8
  966. T} T{
  967. Request\->
  968. T} T{
  969. T}
  970. _
  971. T{
  972. 9
  973. T} T{
  974. T} T{
  975. <\-Response
  976. T}
  977. _
  978. T{
  979. 10
  980. T} T{
  981. T} T{
  982. <\-Response
  983. T}
  984. _
  985. T{
  986. 11
  987. T} T{
  988. T} T{
  989. <\-Response
  990. T}
  991. _
  992. T{
  993. 12
  994. T} T{
  995. T} T{
  996. <\-Response
  997. T}
  998. _
  999. T{
  1000. 13
  1001. T} T{
  1002. Index Update\->
  1003. T} T{
  1004. T}
  1005. _
  1006. T{
  1007. T} T{
  1008. T} T{
  1009. T}
  1010. _
  1011. T{
  1012. 14
  1013. T} T{
  1014. T} T{
  1015. <\-Ping
  1016. T}
  1017. _
  1018. T{
  1019. 15
  1020. T} T{
  1021. Ping\->
  1022. T} T{
  1023. T}
  1024. .TE
  1025. .sp
  1026. The connection is established and at 1. both peers send ClusterConfiguration
  1027. messages and then Index records. The Index records are received and both
  1028. peers recompute their knowledge of the data in the cluster. In this example,
  1029. peer A has four missing or outdated blocks. At 5 through 8 peer A sends
  1030. requests for these blocks. The requests are received by peer B, who
  1031. retrieves the data from the folder and transmits Response records (9 through
  1032. 12). Device A updates their folder contents and transmits an Index Update
  1033. message (13). Both peers enter idle state after 13. At some later time 14,
  1034. the ping timer on device B expires and a Ping message is sent. The same
  1035. process occurs for device A at 15.
  1036. .SH AUTHOR
  1037. The Syncthing Authors
  1038. .SH COPYRIGHT
  1039. 2014-2019, The Syncthing Authors
  1040. .\" Generated by docutils manpage writer.
  1041. .