message.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (C) 2014 The Protocol Authors.
  2. //go:generate -command genxdr go run ../../vendor/github.com/calmh/xdr/cmd/genxdr/main.go
  3. //go:generate genxdr -o message_xdr.go message.go
  4. package protocol
  5. import (
  6. "bytes"
  7. "crypto/sha256"
  8. "fmt"
  9. )
  10. var (
  11. sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
  12. HelloMessageMagic uint32 = 0x9F79BC40
  13. )
  14. type HelloMessage struct {
  15. DeviceName string // max:64
  16. ClientName string // max:64
  17. ClientVersion string // max:64
  18. }
  19. type IndexMessage struct {
  20. Folder string // max:256
  21. Files []FileInfo // max:1000000
  22. Flags uint32
  23. Options []Option // max:64
  24. }
  25. type FileInfo struct {
  26. Name string // max:8192
  27. Flags uint32
  28. Modified int64
  29. Version Vector
  30. LocalVersion int64
  31. CachedSize int64 // noencode (cache only)
  32. Blocks []BlockInfo // max:10000000
  33. }
  34. func (f FileInfo) String() string {
  35. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%v, Size:%d, Blocks:%v}",
  36. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.Blocks)
  37. }
  38. func (f FileInfo) Size() (bytes int64) {
  39. if f.IsDeleted() || f.IsDirectory() {
  40. return 128
  41. }
  42. if f.CachedSize > 0 {
  43. return f.CachedSize
  44. }
  45. for _, b := range f.Blocks {
  46. bytes += int64(b.Size)
  47. }
  48. f.CachedSize = bytes
  49. return
  50. }
  51. func (f FileInfo) IsDeleted() bool {
  52. return f.Flags&FlagDeleted != 0
  53. }
  54. func (f FileInfo) IsInvalid() bool {
  55. return f.Flags&FlagInvalid != 0
  56. }
  57. func (f FileInfo) IsDirectory() bool {
  58. return f.Flags&FlagDirectory != 0
  59. }
  60. func (f FileInfo) IsSymlink() bool {
  61. return f.Flags&FlagSymlink != 0
  62. }
  63. func (f FileInfo) HasPermissionBits() bool {
  64. return f.Flags&FlagNoPermBits == 0
  65. }
  66. // WinsConflict returns true if "f" is the one to choose when it is in
  67. // conflict with "other".
  68. func (f FileInfo) WinsConflict(other FileInfo) bool {
  69. // If a modification is in conflict with a delete, we pick the
  70. // modification.
  71. if !f.IsDeleted() && other.IsDeleted() {
  72. return true
  73. }
  74. if f.IsDeleted() && !other.IsDeleted() {
  75. return false
  76. }
  77. // The one with the newer modification time wins.
  78. if f.Modified > other.Modified {
  79. return true
  80. }
  81. if f.Modified < other.Modified {
  82. return false
  83. }
  84. // The modification times were equal. Use the device ID in the version
  85. // vector as tie breaker.
  86. return f.Version.Compare(other.Version) == ConcurrentGreater
  87. }
  88. type BlockInfo struct {
  89. Offset int64 // noencode (cache only)
  90. Size int32
  91. Hash []byte // max:64
  92. }
  93. func (b BlockInfo) String() string {
  94. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  95. }
  96. // IsEmpty returns true if the block is a full block of zeroes.
  97. func (b BlockInfo) IsEmpty() bool {
  98. return b.Size == BlockSize && bytes.Equal(b.Hash, sha256OfEmptyBlock[:])
  99. }
  100. type RequestMessage struct {
  101. Folder string // max:256
  102. Name string // max:8192
  103. Offset int64
  104. Size int32
  105. Hash []byte // max:64
  106. Flags uint32
  107. Options []Option // max:64
  108. }
  109. type ResponseMessage struct {
  110. Data []byte
  111. Code int32
  112. }
  113. type ClusterConfigMessage struct {
  114. Folders []Folder // max:1000000
  115. Options []Option // max:64
  116. }
  117. type DownloadProgressMessage struct {
  118. Folder string // max:64
  119. Updates []FileDownloadProgressUpdate // max:1000000
  120. Flags uint32
  121. Options []Option // max:64
  122. }
  123. func (o *ClusterConfigMessage) GetOption(key string) string {
  124. for _, option := range o.Options {
  125. if option.Key == key {
  126. return option.Value
  127. }
  128. }
  129. return ""
  130. }
  131. type Folder struct {
  132. ID string // max:256
  133. Label string // max:256
  134. Devices []Device // max:1000000
  135. Flags uint32
  136. Options []Option // max:64
  137. }
  138. type Device struct {
  139. ID []byte // max:32
  140. Name string // max:64
  141. Addresses []string // max:64,2083
  142. Compression uint32
  143. CertName string // max:64
  144. MaxLocalVersion int64
  145. Flags uint32
  146. Options []Option // max:64
  147. }
  148. type FileDownloadProgressUpdate struct {
  149. UpdateType uint32
  150. Name string // max:8192
  151. Version Vector
  152. BlockIndexes []int32 // max:1000000
  153. }
  154. type Option struct {
  155. Key string // max:64
  156. Value string // max:1024
  157. }
  158. type CloseMessage struct {
  159. Reason string // max:1024
  160. Code int32
  161. }
  162. type EmptyMessage struct{}