protocol.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package protocol
  5. import (
  6. "encoding/binary"
  7. "encoding/hex"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "sync"
  12. "time"
  13. lz4 "github.com/bkaradzic/go-lz4"
  14. )
  15. const (
  16. BlockSize = 128 * 1024
  17. )
  18. const (
  19. messageTypeClusterConfig = 0
  20. messageTypeIndex = 1
  21. messageTypeRequest = 2
  22. messageTypeResponse = 3
  23. messageTypePing = 4
  24. messageTypePong = 5
  25. messageTypeIndexUpdate = 6
  26. messageTypeClose = 7
  27. )
  28. const (
  29. stateInitial = iota
  30. stateCCRcvd
  31. stateIdxRcvd
  32. )
  33. const (
  34. FlagDeleted uint32 = 1 << 12
  35. FlagInvalid = 1 << 13
  36. FlagDirectory = 1 << 14
  37. FlagNoPermBits = 1 << 15
  38. )
  39. const (
  40. FlagShareTrusted uint32 = 1 << 0
  41. FlagShareReadOnly = 1 << 1
  42. FlagIntroducer = 1 << 2
  43. FlagShareBits = 0x000000ff
  44. )
  45. var (
  46. ErrClusterHash = fmt.Errorf("configuration error: mismatched cluster hash")
  47. ErrClosed = errors.New("connection closed")
  48. )
  49. type Model interface {
  50. // An index was received from the peer node
  51. Index(nodeID NodeID, repo string, files []FileInfo)
  52. // An index update was received from the peer node
  53. IndexUpdate(nodeID NodeID, repo string, files []FileInfo)
  54. // A request was made by the peer node
  55. Request(nodeID NodeID, repo string, name string, offset int64, size int) ([]byte, error)
  56. // A cluster configuration message was received
  57. ClusterConfig(nodeID NodeID, config ClusterConfigMessage)
  58. // The peer node closed the connection
  59. Close(nodeID NodeID, err error)
  60. }
  61. type Connection interface {
  62. ID() NodeID
  63. Name() string
  64. Index(repo string, files []FileInfo) error
  65. IndexUpdate(repo string, files []FileInfo) error
  66. Request(repo string, name string, offset int64, size int) ([]byte, error)
  67. ClusterConfig(config ClusterConfigMessage)
  68. Statistics() Statistics
  69. }
  70. type rawConnection struct {
  71. id NodeID
  72. name string
  73. receiver Model
  74. state int
  75. cr *countingReader
  76. cw *countingWriter
  77. awaiting [4096]chan asyncResult
  78. awaitingMut sync.Mutex
  79. idxMut sync.Mutex // ensures serialization of Index calls
  80. nextID chan int
  81. outbox chan hdrMsg
  82. closed chan struct{}
  83. once sync.Once
  84. compressionThreshold int // compress messages larger than this many bytes
  85. rdbuf0 []byte // used & reused by readMessage
  86. rdbuf1 []byte // used & reused by readMessage
  87. }
  88. type asyncResult struct {
  89. val []byte
  90. err error
  91. }
  92. type hdrMsg struct {
  93. hdr header
  94. msg encodable
  95. }
  96. type encodable interface {
  97. AppendXDR([]byte) []byte
  98. }
  99. const (
  100. pingTimeout = 30 * time.Second
  101. pingIdleTime = 60 * time.Second
  102. )
  103. func NewConnection(nodeID NodeID, reader io.Reader, writer io.Writer, receiver Model, name string, compress bool) Connection {
  104. cr := &countingReader{Reader: reader}
  105. cw := &countingWriter{Writer: writer}
  106. compThres := 1<<31 - 1 // compression disabled
  107. if compress {
  108. compThres = 128 // compress messages that are 128 bytes long or larger
  109. }
  110. c := rawConnection{
  111. id: nodeID,
  112. name: name,
  113. receiver: nativeModel{receiver},
  114. state: stateInitial,
  115. cr: cr,
  116. cw: cw,
  117. outbox: make(chan hdrMsg),
  118. nextID: make(chan int),
  119. closed: make(chan struct{}),
  120. compressionThreshold: compThres,
  121. }
  122. go c.readerLoop()
  123. go c.writerLoop()
  124. go c.pingerLoop()
  125. go c.idGenerator()
  126. return wireFormatConnection{&c}
  127. }
  128. func (c *rawConnection) ID() NodeID {
  129. return c.id
  130. }
  131. func (c *rawConnection) Name() string {
  132. return c.name
  133. }
  134. // Index writes the list of file information to the connected peer node
  135. func (c *rawConnection) Index(repo string, idx []FileInfo) error {
  136. select {
  137. case <-c.closed:
  138. return ErrClosed
  139. default:
  140. }
  141. c.idxMut.Lock()
  142. c.send(-1, messageTypeIndex, IndexMessage{repo, idx})
  143. c.idxMut.Unlock()
  144. return nil
  145. }
  146. // IndexUpdate writes the list of file information to the connected peer node as an update
  147. func (c *rawConnection) IndexUpdate(repo string, idx []FileInfo) error {
  148. select {
  149. case <-c.closed:
  150. return ErrClosed
  151. default:
  152. }
  153. c.idxMut.Lock()
  154. c.send(-1, messageTypeIndexUpdate, IndexMessage{repo, idx})
  155. c.idxMut.Unlock()
  156. return nil
  157. }
  158. // Request returns the bytes for the specified block after fetching them from the connected peer.
  159. func (c *rawConnection) Request(repo string, name string, offset int64, size int) ([]byte, error) {
  160. var id int
  161. select {
  162. case id = <-c.nextID:
  163. case <-c.closed:
  164. return nil, ErrClosed
  165. }
  166. c.awaitingMut.Lock()
  167. if ch := c.awaiting[id]; ch != nil {
  168. panic("id taken")
  169. }
  170. rc := make(chan asyncResult, 1)
  171. c.awaiting[id] = rc
  172. c.awaitingMut.Unlock()
  173. ok := c.send(id, messageTypeRequest, RequestMessage{repo, name, uint64(offset), uint32(size)})
  174. if !ok {
  175. return nil, ErrClosed
  176. }
  177. res, ok := <-rc
  178. if !ok {
  179. return nil, ErrClosed
  180. }
  181. return res.val, res.err
  182. }
  183. // ClusterConfig send the cluster configuration message to the peer and returns any error
  184. func (c *rawConnection) ClusterConfig(config ClusterConfigMessage) {
  185. c.send(-1, messageTypeClusterConfig, config)
  186. }
  187. func (c *rawConnection) ping() bool {
  188. var id int
  189. select {
  190. case id = <-c.nextID:
  191. case <-c.closed:
  192. return false
  193. }
  194. rc := make(chan asyncResult, 1)
  195. c.awaitingMut.Lock()
  196. c.awaiting[id] = rc
  197. c.awaitingMut.Unlock()
  198. ok := c.send(id, messageTypePing, nil)
  199. if !ok {
  200. return false
  201. }
  202. res, ok := <-rc
  203. return ok && res.err == nil
  204. }
  205. func (c *rawConnection) readerLoop() (err error) {
  206. defer func() {
  207. c.close(err)
  208. }()
  209. for {
  210. select {
  211. case <-c.closed:
  212. return ErrClosed
  213. default:
  214. }
  215. hdr, msg, err := c.readMessage()
  216. if err != nil {
  217. return err
  218. }
  219. switch hdr.msgType {
  220. case messageTypeIndex:
  221. if c.state < stateCCRcvd {
  222. return fmt.Errorf("protocol error: index message in state %d", c.state)
  223. }
  224. c.handleIndex(msg.(IndexMessage))
  225. c.state = stateIdxRcvd
  226. case messageTypeIndexUpdate:
  227. if c.state < stateIdxRcvd {
  228. return fmt.Errorf("protocol error: index update message in state %d", c.state)
  229. }
  230. c.handleIndexUpdate(msg.(IndexMessage))
  231. case messageTypeRequest:
  232. if c.state < stateIdxRcvd {
  233. return fmt.Errorf("protocol error: request message in state %d", c.state)
  234. }
  235. // Requests are handled asynchronously
  236. go c.handleRequest(hdr.msgID, msg.(RequestMessage))
  237. case messageTypeResponse:
  238. if c.state < stateIdxRcvd {
  239. return fmt.Errorf("protocol error: response message in state %d", c.state)
  240. }
  241. c.handleResponse(hdr.msgID, msg.(ResponseMessage))
  242. case messageTypePing:
  243. c.send(hdr.msgID, messageTypePong, EmptyMessage{})
  244. case messageTypePong:
  245. c.handlePong(hdr.msgID)
  246. case messageTypeClusterConfig:
  247. if c.state != stateInitial {
  248. return fmt.Errorf("protocol error: cluster config message in state %d", c.state)
  249. }
  250. go c.receiver.ClusterConfig(c.id, msg.(ClusterConfigMessage))
  251. c.state = stateCCRcvd
  252. case messageTypeClose:
  253. return errors.New(msg.(CloseMessage).Reason)
  254. default:
  255. return fmt.Errorf("protocol error: %s: unknown message type %#x", c.id, hdr.msgType)
  256. }
  257. }
  258. }
  259. func (c *rawConnection) readMessage() (hdr header, msg encodable, err error) {
  260. if cap(c.rdbuf0) < 8 {
  261. c.rdbuf0 = make([]byte, 8)
  262. } else {
  263. c.rdbuf0 = c.rdbuf0[:8]
  264. }
  265. _, err = io.ReadFull(c.cr, c.rdbuf0)
  266. if err != nil {
  267. return
  268. }
  269. hdr = decodeHeader(binary.BigEndian.Uint32(c.rdbuf0[0:4]))
  270. msglen := int(binary.BigEndian.Uint32(c.rdbuf0[4:8]))
  271. if debug {
  272. l.Debugf("read header %v (msglen=%d)", hdr, msglen)
  273. }
  274. if cap(c.rdbuf0) < msglen {
  275. c.rdbuf0 = make([]byte, msglen)
  276. } else {
  277. c.rdbuf0 = c.rdbuf0[:msglen]
  278. }
  279. _, err = io.ReadFull(c.cr, c.rdbuf0)
  280. if err != nil {
  281. return
  282. }
  283. if debug {
  284. l.Debugf("read %d bytes", len(c.rdbuf0))
  285. }
  286. msgBuf := c.rdbuf0
  287. if hdr.compression {
  288. c.rdbuf1 = c.rdbuf1[:cap(c.rdbuf1)]
  289. c.rdbuf1, err = lz4.Decode(c.rdbuf1, c.rdbuf0)
  290. if err != nil {
  291. return
  292. }
  293. msgBuf = c.rdbuf1
  294. if debug {
  295. l.Debugf("decompressed to %d bytes", len(msgBuf))
  296. }
  297. }
  298. if debug {
  299. if len(msgBuf) > 1024 {
  300. l.Debugf("message data:\n%s", hex.Dump(msgBuf[:1024]))
  301. } else {
  302. l.Debugf("message data:\n%s", hex.Dump(msgBuf))
  303. }
  304. }
  305. switch hdr.msgType {
  306. case messageTypeIndex, messageTypeIndexUpdate:
  307. var idx IndexMessage
  308. err = idx.UnmarshalXDR(msgBuf)
  309. msg = idx
  310. case messageTypeRequest:
  311. var req RequestMessage
  312. err = req.UnmarshalXDR(msgBuf)
  313. msg = req
  314. case messageTypeResponse:
  315. var resp ResponseMessage
  316. err = resp.UnmarshalXDR(msgBuf)
  317. msg = resp
  318. case messageTypePing, messageTypePong:
  319. msg = EmptyMessage{}
  320. case messageTypeClusterConfig:
  321. var cc ClusterConfigMessage
  322. err = cc.UnmarshalXDR(msgBuf)
  323. msg = cc
  324. case messageTypeClose:
  325. var cm CloseMessage
  326. err = cm.UnmarshalXDR(msgBuf)
  327. msg = cm
  328. default:
  329. err = fmt.Errorf("protocol error: %s: unknown message type %#x", c.id, hdr.msgType)
  330. }
  331. return
  332. }
  333. func (c *rawConnection) handleIndex(im IndexMessage) {
  334. if debug {
  335. l.Debugf("Index(%v, %v, %d files)", c.id, im.Repository, len(im.Files))
  336. }
  337. c.receiver.Index(c.id, im.Repository, im.Files)
  338. }
  339. func (c *rawConnection) handleIndexUpdate(im IndexMessage) {
  340. if debug {
  341. l.Debugf("queueing IndexUpdate(%v, %v, %d files)", c.id, im.Repository, len(im.Files))
  342. }
  343. c.receiver.IndexUpdate(c.id, im.Repository, im.Files)
  344. }
  345. func (c *rawConnection) handleRequest(msgID int, req RequestMessage) {
  346. data, _ := c.receiver.Request(c.id, req.Repository, req.Name, int64(req.Offset), int(req.Size))
  347. c.send(msgID, messageTypeResponse, ResponseMessage{data})
  348. }
  349. func (c *rawConnection) handleResponse(msgID int, resp ResponseMessage) {
  350. c.awaitingMut.Lock()
  351. if rc := c.awaiting[msgID]; rc != nil {
  352. c.awaiting[msgID] = nil
  353. rc <- asyncResult{resp.Data, nil}
  354. close(rc)
  355. }
  356. c.awaitingMut.Unlock()
  357. }
  358. func (c *rawConnection) handlePong(msgID int) {
  359. c.awaitingMut.Lock()
  360. if rc := c.awaiting[msgID]; rc != nil {
  361. c.awaiting[msgID] = nil
  362. rc <- asyncResult{}
  363. close(rc)
  364. }
  365. c.awaitingMut.Unlock()
  366. }
  367. func (c *rawConnection) send(msgID int, msgType int, msg encodable) bool {
  368. if msgID < 0 {
  369. select {
  370. case id := <-c.nextID:
  371. msgID = id
  372. case <-c.closed:
  373. return false
  374. }
  375. }
  376. hdr := header{
  377. version: 0,
  378. msgID: msgID,
  379. msgType: msgType,
  380. }
  381. select {
  382. case c.outbox <- hdrMsg{hdr, msg}:
  383. return true
  384. case <-c.closed:
  385. return false
  386. }
  387. }
  388. func (c *rawConnection) writerLoop() {
  389. var msgBuf = make([]byte, 8) // buffer for wire format message, kept and reused
  390. var uncBuf []byte // buffer for uncompressed message, kept and reused
  391. for {
  392. var tempBuf []byte
  393. var err error
  394. select {
  395. case hm := <-c.outbox:
  396. if hm.msg != nil {
  397. // Uncompressed message in uncBuf
  398. uncBuf = hm.msg.AppendXDR(uncBuf[:0])
  399. if len(uncBuf) >= c.compressionThreshold {
  400. // Use compression for large messages
  401. hm.hdr.compression = true
  402. // Make sure we have enough space for the compressed message plus header in msgBug
  403. msgBuf = msgBuf[:cap(msgBuf)]
  404. if maxLen := lz4.CompressBound(len(uncBuf)) + 8; maxLen > len(msgBuf) {
  405. msgBuf = make([]byte, maxLen)
  406. }
  407. // Compressed is written to msgBuf, we keep tb for the length only
  408. tempBuf, err = lz4.Encode(msgBuf[8:], uncBuf)
  409. binary.BigEndian.PutUint32(msgBuf[4:8], uint32(len(tempBuf)))
  410. msgBuf = msgBuf[0 : len(tempBuf)+8]
  411. if debug {
  412. l.Debugf("write compressed message; %v (len=%d)", hm.hdr, len(tempBuf))
  413. }
  414. } else {
  415. // No point in compressing very short messages
  416. hm.hdr.compression = false
  417. msgBuf = msgBuf[:cap(msgBuf)]
  418. if l := len(uncBuf) + 8; l > len(msgBuf) {
  419. msgBuf = make([]byte, l)
  420. }
  421. binary.BigEndian.PutUint32(msgBuf[4:8], uint32(len(uncBuf)))
  422. msgBuf = msgBuf[0 : len(uncBuf)+8]
  423. copy(msgBuf[8:], uncBuf)
  424. if debug {
  425. l.Debugf("write uncompressed message; %v (len=%d)", hm.hdr, len(uncBuf))
  426. }
  427. }
  428. } else {
  429. if debug {
  430. l.Debugf("write empty message; %v", hm.hdr)
  431. }
  432. binary.BigEndian.PutUint32(msgBuf[4:8], 0)
  433. msgBuf = msgBuf[:8]
  434. }
  435. binary.BigEndian.PutUint32(msgBuf[0:4], encodeHeader(hm.hdr))
  436. if err == nil {
  437. var n int
  438. n, err = c.cw.Write(msgBuf)
  439. if debug {
  440. l.Debugf("wrote %d bytes on the wire", n)
  441. }
  442. }
  443. if err != nil {
  444. c.close(err)
  445. return
  446. }
  447. case <-c.closed:
  448. return
  449. }
  450. }
  451. }
  452. func (c *rawConnection) close(err error) {
  453. c.once.Do(func() {
  454. close(c.closed)
  455. c.awaitingMut.Lock()
  456. for i, ch := range c.awaiting {
  457. if ch != nil {
  458. close(ch)
  459. c.awaiting[i] = nil
  460. }
  461. }
  462. c.awaitingMut.Unlock()
  463. go c.receiver.Close(c.id, err)
  464. })
  465. }
  466. func (c *rawConnection) idGenerator() {
  467. nextID := 0
  468. for {
  469. nextID = (nextID + 1) & 0xfff
  470. select {
  471. case c.nextID <- nextID:
  472. case <-c.closed:
  473. return
  474. }
  475. }
  476. }
  477. func (c *rawConnection) pingerLoop() {
  478. var rc = make(chan bool, 1)
  479. ticker := time.Tick(pingIdleTime / 2)
  480. for {
  481. select {
  482. case <-ticker:
  483. if d := time.Since(c.cr.Last()); d < pingIdleTime {
  484. if debug {
  485. l.Debugln(c.id, "ping skipped after rd", d)
  486. }
  487. continue
  488. }
  489. if d := time.Since(c.cw.Last()); d < pingIdleTime {
  490. if debug {
  491. l.Debugln(c.id, "ping skipped after wr", d)
  492. }
  493. continue
  494. }
  495. go func() {
  496. if debug {
  497. l.Debugln(c.id, "ping ->")
  498. }
  499. rc <- c.ping()
  500. }()
  501. select {
  502. case ok := <-rc:
  503. if debug {
  504. l.Debugln(c.id, "<- pong")
  505. }
  506. if !ok {
  507. c.close(fmt.Errorf("ping failure"))
  508. }
  509. case <-time.After(pingTimeout):
  510. c.close(fmt.Errorf("ping timeout"))
  511. case <-c.closed:
  512. return
  513. }
  514. case <-c.closed:
  515. return
  516. }
  517. }
  518. }
  519. type Statistics struct {
  520. At time.Time
  521. InBytesTotal uint64
  522. OutBytesTotal uint64
  523. }
  524. func (c *rawConnection) Statistics() Statistics {
  525. return Statistics{
  526. At: time.Now(),
  527. InBytesTotal: c.cr.Tot(),
  528. OutBytesTotal: c.cw.Tot(),
  529. }
  530. }
  531. func IsDeleted(bits uint32) bool {
  532. return bits&FlagDeleted != 0
  533. }
  534. func IsInvalid(bits uint32) bool {
  535. return bits&FlagInvalid != 0
  536. }
  537. func IsDirectory(bits uint32) bool {
  538. return bits&FlagDirectory != 0
  539. }
  540. func HasPermissionBits(bits uint32) bool {
  541. return bits&FlagNoPermBits == 0
  542. }