2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
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/>.
36 "github.com/davecgh/go-xdr/xdr2"
37 "golang.org/x/crypto/blake2b"
38 "golang.org/x/crypto/twofish"
41 func (ctx *Ctx) Tx(node *Node, pkt *Pkt, nice uint8, size, minSize int64, src io.Reader) (*Node, error) {
42 tmp, err := ctx.NewTmpFileWHash()
46 hops := make([]*Node, 0, 1+len(node.Via))
47 hops = append(hops, node)
49 for i := len(node.Via); i > 0; i-- {
50 lastNode = ctx.Neigh[*node.Via[i-1]]
51 hops = append(hops, lastNode)
53 padSize := minSize - size - int64(len(hops))*(PktOverhead+PktEncOverhead)
57 errs := make(chan error)
59 pipeR, pipeW := io.Pipe()
60 go func(size int64, src io.Reader, dst io.WriteCloser) {
63 "nice": strconv.Itoa(int(nice)),
64 "size": strconv.FormatInt(size, 10),
66 errs <- PktEncWrite(ctx.Self, hops[0], pkt, nice, size, padSize, src, dst)
68 }(curSize, src, pipeW)
71 var pipeRPrev io.Reader
72 for i := 1; i < len(hops); i++ {
76 PathLen: blake2b.Size256,
77 Path: new([MaxPathSize]byte),
79 copy(pktTrans.Path[:], hops[i-1].Id[:])
80 curSize += PktOverhead + PktEncOverhead
82 pipeR, pipeW = io.Pipe()
83 go func(node *Node, pkt *Pkt, size int64, src io.Reader, dst io.WriteCloser) {
86 "nice": strconv.Itoa(int(nice)),
87 "size": strconv.FormatInt(size, 10),
89 errs <- PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst)
91 }(hops[i], &pktTrans, curSize, pipeRPrev, pipeW)
94 _, err := io.Copy(tmp.W, pipeR)
97 for i := 0; i <= len(hops); i++ {
104 nodePath := filepath.Join(ctx.Spool, lastNode.Id.String())
105 err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
106 os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name))
110 func prepareTxFile(srcPath string) (io.Reader, *os.File, int64, error) {
116 src, err = ioutil.TempFile("", "nncp-file")
118 return nil, nil, 0, err
120 os.Remove(src.Name())
121 tmpW := bufio.NewWriter(src)
123 tmpKey := make([]byte, 32)
124 if _, err = rand.Read(tmpKey); err != nil {
125 return nil, nil, 0, err
127 ciph, err := twofish.NewCipher(tmpKey)
129 return nil, nil, 0, err
131 ctr := cipher.NewCTR(ciph, make([]byte, twofish.BlockSize))
132 encrypter := &cipher.StreamWriter{S: ctr, W: tmpW}
133 fileSize, err = io.Copy(encrypter, bufio.NewReader(os.Stdin))
135 return nil, nil, 0, err
139 ctr = cipher.NewCTR(ciph, make([]byte, twofish.BlockSize))
140 reader = &cipher.StreamReader{S: ctr, R: bufio.NewReader(src)}
142 src, err = os.Open(srcPath)
144 return nil, nil, 0, err
146 srcStat, err := src.Stat()
148 return nil, nil, 0, err
150 fileSize = srcStat.Size()
151 reader = bufio.NewReader(src)
153 return reader, src, fileSize, nil
156 func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
159 return errors.New("Must provide destination filename")
161 dstPath = filepath.Base(srcPath)
163 dstPath = filepath.Clean(dstPath)
164 if filepath.IsAbs(dstPath) {
165 return errors.New("Relative destination path required")
167 pkt, err := NewPkt(PktTypeFile, dstPath)
171 reader, src, fileSize, err := prepareTxFile(srcPath)
178 _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
183 "nice": strconv.Itoa(int(nice)),
186 "size": strconv.FormatInt(fileSize, 10),
192 "nice": strconv.Itoa(int(nice)),
195 "size": strconv.FormatInt(fileSize, 10),
202 func (ctx *Ctx) TxFileChunked(node *Node, nice uint8, srcPath, dstPath string, minSize int64, chunkSize int64) error {
205 return errors.New("Must provide destination filename")
207 dstPath = filepath.Base(srcPath)
209 dstPath = filepath.Clean(dstPath)
210 if filepath.IsAbs(dstPath) {
211 return errors.New("Relative destination path required")
213 reader, src, fileSize, err := prepareTxFile(srcPath)
222 metaPkt := ChunkedMeta{
224 FileSize: uint64(fileSize),
225 ChunkSize: uint64(chunkSize),
226 Checksums: make([][32]byte, 0, (fileSize/chunkSize)+1),
228 for i := int64(0); i < (fileSize/chunkSize)+1; i++ {
230 metaPkt.Checksums = append(metaPkt.Checksums, *hsh)
238 if leftSize <= chunkSize {
239 sizeToSend = leftSize
241 sizeToSend = chunkSize
243 path = dstPath + ChunkedSuffixPart + strconv.Itoa(chunkNum)
244 pkt, err = NewPkt(PktTypeFile, path)
248 hsh, err = blake2b.New256(nil)
258 io.TeeReader(reader, hsh),
264 "nice": strconv.Itoa(int(nice)),
267 "size": strconv.FormatInt(sizeToSend, 10),
273 "nice": strconv.Itoa(int(nice)),
276 "size": strconv.FormatInt(sizeToSend, 10),
281 hsh.Sum(metaPkt.Checksums[chunkNum][:0])
282 leftSize -= sizeToSend
288 var metaBuf bytes.Buffer
289 _, err = xdr.Marshal(&metaBuf, metaPkt)
293 path = dstPath + ChunkedSuffixMeta
294 pkt, err = NewPkt(PktTypeFile, path)
298 metaPktSize := int64(metaBuf.Len())
299 _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf)
304 "nice": strconv.Itoa(int(nice)),
307 "size": strconv.FormatInt(metaPktSize, 10),
312 "nice": strconv.Itoa(int(nice)),
315 "size": strconv.FormatInt(fileSize, 10),
321 "nice": strconv.Itoa(int(nice)),
324 "size": strconv.FormatInt(metaPktSize, 10),
331 func (ctx *Ctx) TxFreq(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
332 dstPath = filepath.Clean(dstPath)
333 if filepath.IsAbs(dstPath) {
334 return errors.New("Relative destination path required")
336 srcPath = filepath.Clean(srcPath)
337 if filepath.IsAbs(srcPath) {
338 return errors.New("Relative source path required")
340 pkt, err := NewPkt(PktTypeFreq, srcPath)
344 src := strings.NewReader(dstPath)
345 size := int64(src.Len())
346 _, err = ctx.Tx(node, pkt, nice, size, minSize, src)
351 "nice": strconv.Itoa(int(nice)),
359 "nice": strconv.Itoa(int(nice)),
368 func (ctx *Ctx) TxMail(node *Node, nice uint8, recipient string, body []byte, minSize int64) error {
369 pkt, err := NewPkt(PktTypeMail, recipient)
373 var compressed bytes.Buffer
374 compressor, err := zlib.NewWriterLevel(&compressed, zlib.BestCompression)
378 if _, err = io.Copy(compressor, bytes.NewReader(body)); err != nil {
382 size := int64(compressed.Len())
383 _, err = ctx.Tx(node, pkt, nice, size, minSize, &compressed)
388 "nice": strconv.Itoa(int(nice)),
390 "size": strconv.FormatInt(size, 10),
396 "nice": strconv.Itoa(int(nice)),
398 "size": strconv.FormatInt(size, 10),
405 func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error {
409 "nice": strconv.Itoa(int(nice)),
410 "size": strconv.FormatInt(size, 10),
412 tmp, err := ctx.NewTmpFileWHash()
416 if _, err = io.Copy(tmp.W, src); err != nil {
419 nodePath := filepath.Join(ctx.Spool, node.Id.String())
420 err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
425 "nice": strconv.Itoa(int(nice)),
426 "size": strconv.FormatInt(size, 10),
432 "nice": strconv.Itoa(int(nice)),
433 "size": strconv.FormatInt(size, 10),
437 os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name))