2 NNCP -- Node-to-Node CoPy
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 "github.com/davecgh/go-xdr/xdr2"
35 "github.com/flynn/noise"
39 MaxLLPSize = 2<<15 - 256
45 MagicNNCPLv1 [8]byte = [8]byte{'N', 'N', 'C', 'P', 'L', 1, 0, 0}
51 LLPHaltMarshalized []byte
53 NoiseCipherSuite noise.CipherSuite = noise.NewCipherSuite(
55 noise.CipherChaChaPoly,
63 LLPTypeInfo LLPType = iota
64 LLPTypeFreq LLPType = iota
65 LLPTypeFile LLPType = iota
66 LLPTypeDone LLPType = iota
67 LLPTypeHalt LLPType = iota
102 llpHead := LLPHead{Type: LLPTypeHalt}
103 if _, err := xdr.Marshal(&buf, llpHead); err != nil {
106 copy(LLPHaltMarshalized, buf.Bytes())
107 LLPHeadOverhead = buf.Len()
110 llpInfo := LLPInfo{Nice: 123, Size: 123, Hash: new([32]byte)}
111 if _, err := xdr.Marshal(&buf, llpInfo); err != nil {
114 LLPInfoOverhead = buf.Len()
117 llpFreq := LLPFreq{Hash: new([32]byte), Offset: 123}
118 if _, err := xdr.Marshal(&buf, llpFreq); err != nil {
121 LLPFreqOverhead = buf.Len()
124 llpFile := LLPFile{Hash: new([32]byte), Offset: 123}
125 if _, err := xdr.Marshal(&buf, llpFile); err != nil {
128 LLPFileOverhead = buf.Len()
131 func MarshalLLP(typ LLPType, llp interface{}) []byte {
134 if _, err = xdr.Marshal(&buf, LLPHead{typ}); err != nil {
137 if _, err = xdr.Marshal(&buf, llp); err != nil {
143 func payloadsSplit(payloads [][]byte) [][]byte {
144 var outbounds [][]byte
145 outbound := make([]byte, 0, MaxLLPSize)
146 for i, payload := range payloads {
147 outbound = append(outbound, payload...)
148 if i+1 < len(payloads) && len(outbound)+len(payloads[i+1]) > MaxLLPSize {
149 outbounds = append(outbounds, outbound)
150 outbound = make([]byte, 0, MaxLLPSize)
153 if len(outbound) > 0 {
154 outbounds = append(outbounds, outbound)
159 type LLPState struct {
163 hs *noise.HandshakeState
164 csOur *noise.CipherState
165 csTheir *noise.CipherState
167 infosTheir map[[32]byte]*LLPInfo
168 queueTheir []*LLPFreq
175 Duration time.Duration
184 func (state *LLPState) isDead() bool {
186 return now.Sub(state.RxLastSeen).Seconds() >= DeadlineDuration && now.Sub(state.TxLastSeen).Seconds() >= DeadlineDuration
189 func (state *LLPState) dirUnlock() {
190 state.ctx.UnlockDir(state.rxLock)
191 state.ctx.UnlockDir(state.txLock)
194 func (state *LLPState) WriteLLP(dst io.Writer, payload []byte) error {
195 n, err := xdr.Marshal(dst, LLPRaw{Magic: MagicNNCPLv1, Payload: payload})
197 state.TxLastSeen = time.Now()
198 state.TxBytes += int64(n)
203 func (state *LLPState) ReadLLP(src io.Reader) ([]byte, error) {
205 n, err := xdr.Unmarshal(src, &llp)
209 state.RxLastSeen = time.Now()
210 state.RxBytes += int64(n)
211 if llp.Magic != MagicNNCPLv1 {
214 return llp.Payload, nil
217 func (ctx *Ctx) infosOur(nodeId *NodeId, nice uint8) [][]byte {
220 for job := range ctx.Jobs(nodeId, TTx) {
222 if job.PktEnc.Nice > nice {
225 totalSize += job.Size
226 infos = append(infos, &LLPInfo{
227 Nice: job.PktEnc.Nice,
228 Size: uint64(job.Size),
232 sort.Sort(ByNice(infos))
233 var payloads [][]byte
234 for _, info := range infos {
235 payloads = append(payloads, MarshalLLP(LLPTypeInfo, info))
236 ctx.LogD("llp-info-our", SDS{
238 "name": ToBase32(info.Hash[:]),
239 "size": strconv.FormatInt(int64(info.Size), 10),
242 ctx.LogI("llp-infos", SDS{
245 "pkts": strconv.Itoa(len(payloads)),
246 "size": strconv.FormatInt(totalSize, 10),
248 return payloadsSplit(payloads)
251 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
252 dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
253 if err := os.MkdirAll(dirPath, os.FileMode(0700)); err != nil {
254 ctx.LogE("llp-ensure", SDS{"dir": dirPath, "err": err}, "")
257 fd, err := os.Open(dirPath)
259 ctx.LogE("llp-ensure", SDS{"dir": dirPath, "err": err}, "")
266 func (ctx *Ctx) StartI(conn net.Conn, nodeId *NodeId, nice uint8, xxOnly *TRxTx) (*LLPState, error) {
267 err := ctx.ensureRxDir(nodeId)
272 if xxOnly != nil && *xxOnly == TRx {
273 rxLock, err = ctx.LockDir(nodeId, TRx)
279 if xxOnly != nil && *xxOnly == TTx {
280 txLock, err = ctx.LockDir(nodeId, TTx)
285 started := time.Now()
286 conf := noise.Config{
287 CipherSuite: NoiseCipherSuite,
288 Pattern: noise.HandshakeIK,
290 StaticKeypair: noise.DHKey{
291 Private: ctx.Self.NoisePrv[:],
292 Public: ctx.Self.NoisePub[:],
294 PeerStatic: ctx.Neigh[*nodeId].NoisePub[:],
298 hs: noise.NewHandshakeState(conf),
301 payloads: make(chan []byte),
302 infosTheir: make(map[[32]byte]*LLPInfo),
309 var infosPayloads [][]byte
310 if xxOnly == nil || *xxOnly != TTx {
311 infosPayloads = ctx.infosOur(nodeId, nice)
313 var firstPayload []byte
314 if len(infosPayloads) > 0 {
315 firstPayload = infosPayloads[0]
317 for i := 0; i < (MaxLLPSize-len(firstPayload))/LLPHeadOverhead; i++ {
318 firstPayload = append(firstPayload, LLPHaltMarshalized...)
323 buf, _, _ = state.hs.WriteMessage(nil, firstPayload)
324 sds := SDS{"node": nodeId, "nice": strconv.Itoa(int(nice))}
325 ctx.LogD("llp-start", sds, "sending first message")
326 conn.SetWriteDeadline(time.Now().Add(DeadlineDuration * time.Second))
327 if err = state.WriteLLP(conn, buf); err != nil {
328 ctx.LogE("llp-start", SdsAdd(sds, SDS{"err": err}), "")
332 ctx.LogD("llp-start", sds, "waiting for first message")
333 conn.SetReadDeadline(time.Now().Add(DeadlineDuration * time.Second))
334 if buf, err = state.ReadLLP(conn); err != nil {
335 ctx.LogE("llp-start", SdsAdd(sds, SDS{"err": err}), "")
339 payload, state.csOur, state.csTheir, err = state.hs.ReadMessage(nil, buf)
341 ctx.LogE("llp-start", SdsAdd(sds, SDS{"err": err}), "")
345 ctx.LogD("llp-start", sds, "starting workers")
346 err = state.StartWorkers(conn, infosPayloads, payload)
348 ctx.LogE("llp-start", SdsAdd(sds, SDS{"err": err}), "")
355 func (ctx *Ctx) StartR(conn net.Conn, nice uint8, xxOnly *TRxTx) (*LLPState, error) {
356 started := time.Now()
357 conf := noise.Config{
358 CipherSuite: NoiseCipherSuite,
359 Pattern: noise.HandshakeIK,
361 StaticKeypair: noise.DHKey{
362 Private: ctx.Self.NoisePrv[:],
363 Public: ctx.Self.NoisePub[:],
368 hs: noise.NewHandshakeState(conf),
370 payloads: make(chan []byte),
371 infosTheir: make(map[[32]byte]*LLPInfo),
380 SDS{"nice": strconv.Itoa(int(nice))},
381 "waiting for first message",
383 conn.SetReadDeadline(time.Now().Add(DeadlineDuration * time.Second))
384 if buf, err = state.ReadLLP(conn); err != nil {
385 ctx.LogE("llp-start", SDS{"err": err}, "")
388 if payload, _, _, err = state.hs.ReadMessage(nil, buf); err != nil {
389 ctx.LogE("llp-start", SDS{"err": err}, "")
394 for _, node := range ctx.Neigh {
395 if subtle.ConstantTimeCompare(state.hs.PeerStatic(), node.NoisePub[:]) == 1 {
401 peerId := ToBase32(state.hs.PeerStatic())
402 ctx.LogE("llp-start", SDS{"peer": peerId}, "unknown")
403 return nil, errors.New("Unknown peer: " + peerId)
405 state.NodeId = nodeId
406 sds := SDS{"node": nodeId, "nice": strconv.Itoa(int(nice))}
408 if ctx.ensureRxDir(nodeId); err != nil {
412 if xxOnly != nil && *xxOnly == TRx {
413 rxLock, err = ctx.LockDir(nodeId, TRx)
418 state.rxLock = rxLock
420 if xxOnly != nil && *xxOnly == TTx {
421 txLock, err = ctx.LockDir(nodeId, TTx)
426 state.txLock = txLock
428 var infosPayloads [][]byte
429 if xxOnly == nil || *xxOnly != TTx {
430 infosPayloads = ctx.infosOur(nodeId, nice)
432 var firstPayload []byte
433 if len(infosPayloads) > 0 {
434 firstPayload = infosPayloads[0]
436 for i := 0; i < (MaxLLPSize-len(firstPayload))/LLPHeadOverhead; i++ {
437 firstPayload = append(firstPayload, LLPHaltMarshalized...)
440 ctx.LogD("llp-start", sds, "sending first message")
441 buf, state.csTheir, state.csOur = state.hs.WriteMessage(nil, firstPayload)
442 conn.SetWriteDeadline(time.Now().Add(DeadlineDuration * time.Second))
443 if err = state.WriteLLP(conn, buf); err != nil {
444 ctx.LogE("llp-start", SdsAdd(sds, SDS{"err": err}), "")
448 ctx.LogD("llp-start", sds, "starting workers")
449 err = state.StartWorkers(conn, infosPayloads, payload)
457 func (state *LLPState) StartWorkers(conn net.Conn, infosPayloads [][]byte, payload []byte) error {
458 sds := SDS{"node": state.NodeId, "nice": strconv.Itoa(int(state.nice))}
459 if len(infosPayloads) > 1 {
461 for _, payload := range infosPayloads[1:] {
464 SdsAdd(sds, SDS{"size": strconv.Itoa(len(payload))}),
465 "queuing remaining payload",
467 state.payloads <- payload
473 SdsAdd(sds, SDS{"size": strconv.Itoa(len(payload))}),
474 "processing first payload",
476 replies, err := state.ProcessLLP(payload)
478 state.ctx.LogE("llp-work", SdsAdd(sds, SDS{"err": err}), "")
482 for _, reply := range replies {
485 SdsAdd(sds, SDS{"size": strconv.Itoa(len(reply))}),
488 state.payloads <- reply
493 defer state.wg.Done()
500 case payload = <-state.payloads:
503 SdsAdd(sds, SDS{"size": strconv.Itoa(len(payload))}),
510 if len(state.queueTheir) == 0 {
511 state.ctx.LogD("llp-xmit", sds, "file queue is empty")
513 time.Sleep(100 * time.Millisecond)
516 freq := state.queueTheir[0]
518 sdsp := SdsAdd(sds, SDS{
520 "hash": ToBase32(freq.Hash[:]),
521 "size": strconv.FormatInt(int64(freq.Offset), 10),
523 state.ctx.LogD("llp-file", sdsp, "queueing")
524 fd, err := os.Open(filepath.Join(
526 state.NodeId.String(),
528 ToBase32(freq.Hash[:]),
531 state.ctx.LogE("llp-file", SdsAdd(sdsp, SDS{"err": err}), "")
536 state.ctx.LogE("llp-file", SdsAdd(sdsp, SDS{"err": err}), "")
539 fullSize := uint64(fi.Size())
541 if freq.Offset < fullSize {
542 state.ctx.LogD("llp-file", sdsp, "seeking")
543 if _, err = fd.Seek(int64(freq.Offset), 0); err != nil {
544 state.ctx.LogE("llp-file", SdsAdd(sdsp, SDS{"err": err}), "")
547 buf = make([]byte, MaxLLPSize-LLPHeadOverhead-LLPFileOverhead)
548 n, err := fd.Read(buf)
550 state.ctx.LogE("llp-file", SdsAdd(sdsp, SDS{"err": err}), "")
556 SdsAdd(sdsp, SDS{"size": strconv.Itoa(n)}),
561 payload = MarshalLLP(LLPTypeFile, LLPFile{
566 ourSize := freq.Offset + uint64(len(buf))
567 sdsp["size"] = strconv.FormatInt(int64(ourSize), 10)
568 sdsp["fullsize"] = strconv.FormatInt(int64(fullSize), 10)
569 state.ctx.LogP("llp-file", sdsp, "")
571 if len(state.queueTheir) > 0 && *state.queueTheir[0].Hash == *freq.Hash {
572 if ourSize == fullSize {
573 state.ctx.LogD("llp-file", sdsp, "finished")
574 if len(state.queueTheir) > 1 {
575 state.queueTheir = state.queueTheir[1:]
577 state.queueTheir = state.queueTheir[:0]
580 state.queueTheir[0].Offset += uint64(len(buf))
583 state.ctx.LogD("llp-file", sdsp, "queue disappeared")
589 SdsAdd(sds, SDS{"size": strconv.Itoa(len(payload))}),
592 conn.SetWriteDeadline(time.Now().Add(DeadlineDuration * time.Second))
593 if err := state.WriteLLP(conn, state.csOur.Encrypt(nil, nil, payload)); err != nil {
594 state.ctx.LogE("llp-xmit", SdsAdd(sds, SDS{"err": err}), "")
601 defer state.wg.Done()
606 state.ctx.LogD("llp-recv", sds, "waiting for payload")
607 conn.SetReadDeadline(time.Now().Add(DeadlineDuration * time.Second))
608 payload, err := state.ReadLLP(conn)
610 unmarshalErr := err.(*xdr.UnmarshalError)
611 netErr, ok := unmarshalErr.Err.(net.Error)
612 if (ok && netErr.Timeout()) || unmarshalErr.ErrorCode == xdr.ErrIO {
615 state.ctx.LogE("llp-recv", SdsAdd(sds, SDS{"err": err}), "")
621 SdsAdd(sds, SDS{"size": strconv.Itoa(len(payload))}),
624 payload, err = state.csTheir.Decrypt(nil, nil, payload)
626 state.ctx.LogE("llp-recv", SdsAdd(sds, SDS{"err": err}), "")
631 SdsAdd(sds, SDS{"size": strconv.Itoa(len(payload))}),
634 replies, err := state.ProcessLLP(payload)
636 state.ctx.LogE("llp-recv", SdsAdd(sds, SDS{"err": err}), "")
640 for _, reply := range replies {
643 SdsAdd(sds, SDS{"size": strconv.Itoa(len(reply))}),
646 state.payloads <- reply
654 func (state *LLPState) Wait() {
657 state.Duration = time.Now().Sub(state.started)
658 state.RxSpeed = state.RxBytes
659 state.TxSpeed = state.TxBytes
660 rxDuration := int64(state.RxLastSeen.Sub(state.started).Seconds())
661 txDuration := int64(state.TxLastSeen.Sub(state.started).Seconds())
663 state.RxSpeed = state.RxBytes / rxDuration
666 state.TxSpeed = state.TxBytes / txDuration
670 func (state *LLPState) ProcessLLP(payload []byte) ([][]byte, error) {
671 sds := SDS{"node": state.NodeId, "nice": strconv.Itoa(int(state.nice))}
672 r := bytes.NewReader(payload)
677 state.ctx.LogD("llp-process", sds, "unmarshaling header")
679 if _, err = xdr.Unmarshal(r, &head); err != nil {
680 state.ctx.LogE("llp-process", SdsAdd(sds, SDS{"err": err}), "")
686 sdsp := SdsAdd(sds, SDS{"type": "info"})
687 state.ctx.LogD("llp-process", sdsp, "unmarshaling packet")
689 if _, err = xdr.Unmarshal(r, &info); err != nil {
690 state.ctx.LogE("llp-process", SdsAdd(sdsp, SDS{"err": err}), "")
693 sdsp = SdsAdd(sds, SDS{
694 "hash": ToBase32(info.Hash[:]),
695 "size": strconv.FormatInt(int64(info.Size), 10),
697 if info.Nice > state.nice {
698 state.ctx.LogD("llp-process", sdsp, "too nice")
701 state.ctx.LogD("llp-process", sdsp, "received")
702 if state.xxOnly != nil && *state.xxOnly == TTx {
706 state.infosTheir[*info.Hash] = &info
708 state.ctx.LogD("llp-process", sdsp, "stating part")
709 if _, err = os.Stat(filepath.Join(
711 state.NodeId.String(),
713 ToBase32(info.Hash[:]),
715 state.ctx.LogD("llp-process", sdsp, "already done")
716 replies = append(replies, MarshalLLP(LLPTypeDone, LLPDone{info.Hash}))
719 fi, err := os.Stat(filepath.Join(
721 state.NodeId.String(),
723 ToBase32(info.Hash[:])+PartSuffix,
730 SdsAdd(sdsp, SDS{"offset": strconv.FormatInt(offset, 10)}),
734 replies = append(replies, MarshalLLP(
736 LLPFreq{info.Hash, uint64(offset)},
741 SdsAdd(sds, SDS{"type": "file"}),
742 "unmarshaling packet",
745 if _, err = xdr.Unmarshal(r, &file); err != nil {
746 state.ctx.LogE("llp-process", SdsAdd(sds, SDS{
752 sdsp := SdsAdd(sds, SDS{
754 "hash": ToBase32(file.Hash[:]),
755 "size": strconv.Itoa(len(file.Payload)),
757 filePath := filepath.Join(
759 state.NodeId.String(),
761 ToBase32(file.Hash[:]),
763 state.ctx.LogD("llp-file", sdsp, "opening part")
764 fd, err := os.OpenFile(
766 os.O_RDWR|os.O_CREATE,
770 state.ctx.LogE("llp-file", SdsAdd(sdsp, SDS{"err": err}), "")
775 SdsAdd(sdsp, SDS{"offset": strconv.FormatInt(int64(file.Offset), 10)}),
778 if _, err = fd.Seek(int64(file.Offset), 0); err != nil {
779 state.ctx.LogE("llp-file", SdsAdd(sdsp, SDS{"err": err}), "")
783 state.ctx.LogD("llp-file", sdsp, "writing")
784 _, err = fd.Write(file.Payload)
786 state.ctx.LogE("llp-file", SdsAdd(sdsp, SDS{"err": err}), "")
790 ourSize := uint64(file.Offset) + uint64(len(file.Payload))
791 sdsp["fullsize"] = strconv.FormatInt(int64(state.infosTheir[*file.Hash].Size), 10)
792 sdsp["size"] = strconv.FormatInt(int64(ourSize), 10)
793 state.ctx.LogP("llp-file", sdsp, "")
794 if state.infosTheir[*file.Hash].Size != ourSize {
799 if err := fd.Sync(); err != nil {
800 state.ctx.LogE("llp-file", SdsAdd(sdsp, SDS{"err": err}), "sync")
805 defer state.wg.Done()
807 state.ctx.LogD("llp-file", sdsp, "checking")
808 gut, err := Check(fd, file.Hash[:])
810 if err != nil || !gut {
811 state.ctx.LogE("llp-file", sdsp, "checksum mismatch")
814 state.ctx.LogI("llp-done", SdsAdd(sdsp, SDS{"xx": string(TRx)}), "")
815 os.Rename(filePath+PartSuffix, filePath)
816 state.payloads <- MarshalLLP(LLPTypeDone, LLPDone{file.Hash})
821 SdsAdd(sds, SDS{"type": "done"}),
822 "unmarshaling packet",
825 if _, err = xdr.Unmarshal(r, &done); err != nil {
826 state.ctx.LogE("llp-process", SdsAdd(sds, SDS{
832 sdsp := SdsAdd(sds, SDS{"hash": ToBase32(done.Hash[:])})
833 state.ctx.LogD("llp-done", sdsp, "removing")
834 err := os.Remove(filepath.Join(
836 state.NodeId.String(),
838 ToBase32(done.Hash[:]),
841 state.ctx.LogI("llp-done", SdsAdd(sdsp, SDS{"xx": string(TTx)}), "")
843 state.ctx.LogE("llp-done", SdsAdd(sdsp, SDS{"xx": string(TTx)}), "")
846 sdsp := SdsAdd(sds, SDS{"type": "freq"})
847 state.ctx.LogD("llp-process", sdsp, "unmarshaling packet")
849 if _, err = xdr.Unmarshal(r, &freq); err != nil {
850 state.ctx.LogE("llp-process", SdsAdd(sdsp, SDS{"err": err}), "")
853 state.ctx.LogD("llp-process", SdsAdd(sdsp, SDS{
854 "hash": ToBase32(freq.Hash[:]),
855 "offset": strconv.FormatInt(int64(freq.Offset), 10),
858 state.queueTheir = append(state.queueTheir, &freq)
861 sdsp := SdsAdd(sds, SDS{"type": "halt"})
862 state.ctx.LogD("llp-process", sdsp, "")
864 state.queueTheir = nil
869 SdsAdd(sds, SDS{"type": head.Type}),
872 return nil, BadPktType
878 for _, info := range state.infosTheir {
882 state.ctx.LogI("llp-infos", SDS{
884 "node": state.NodeId,
885 "pkts": strconv.Itoa(pkts),
886 "size": strconv.FormatInt(int64(size), 10),
889 return payloadsSplit(replies), nil