protocol.go 14 KB

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