2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 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/>.
35 "github.com/davecgh/go-xdr/xdr2"
36 "golang.org/x/crypto/blake2b"
44 src io.Reader) (*Node, error) {
45 tmp, err := ctx.NewTmpFileWHash()
49 hops := make([]*Node, 0, 1+len(node.Via))
50 hops = append(hops, node)
52 for i := len(node.Via); i > 0; i-- {
53 lastNode = ctx.Neigh[*node.Via[i-1]]
54 hops = append(hops, lastNode)
56 padSize := minSize - size - int64(len(hops))*(PktOverhead+PktEncOverhead)
60 errs := make(chan error)
62 pipeR, pipeW := io.Pipe()
63 go func(size int64, src io.Reader, dst io.WriteCloser) {
66 "nice": strconv.Itoa(int(nice)),
67 "size": strconv.FormatInt(size, 10),
69 errs <- PktEncWrite(ctx.Self, hops[0], pkt, nice, size, padSize, src, dst)
71 }(curSize, src, pipeW)
74 var pipeRPrev io.Reader
75 for i := 1; i < len(hops); i++ {
76 pktTrns, _ := NewPkt(PktTypeTrns, 0, hops[i-1].Id[:])
77 curSize += PktOverhead + PktEncOverhead
79 pipeR, pipeW = io.Pipe()
80 go func(node *Node, pkt *Pkt, size int64, src io.Reader, dst io.WriteCloser) {
83 "nice": strconv.Itoa(int(nice)),
84 "size": strconv.FormatInt(size, 10),
86 errs <- PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst)
88 }(hops[i], pktTrns, curSize, pipeRPrev, pipeW)
91 _, err := io.Copy(tmp.W, pipeR)
94 for i := 0; i <= len(hops); i++ {
101 nodePath := filepath.Join(ctx.Spool, lastNode.Id.String())
102 err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
103 os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name))
107 func prepareTxFile(srcPath string) (io.Reader, *os.File, int64, error) {
113 src, err = ioutil.TempFile("", "nncp-file")
115 return nil, nil, 0, err
117 os.Remove(src.Name())
118 tmpW := bufio.NewWriter(src)
119 tmpKey := new([32]byte)
120 if _, err = rand.Read(tmpKey[:]); err != nil {
121 return nil, nil, 0, err
123 written, err := ae(tmpKey, bufio.NewReader(os.Stdin), tmpW)
125 return nil, nil, 0, err
127 fileSize = int64(written)
131 go ae(tmpKey, bufio.NewReader(src), w)
134 src, err = os.Open(srcPath)
136 return nil, nil, 0, err
138 srcStat, err := src.Stat()
140 return nil, nil, 0, err
142 fileSize = srcStat.Size()
143 reader = bufio.NewReader(src)
145 return reader, src, fileSize, nil
148 func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
151 return errors.New("Must provide destination filename")
153 dstPath = filepath.Base(srcPath)
155 dstPath = filepath.Clean(dstPath)
156 if filepath.IsAbs(dstPath) {
157 return errors.New("Relative destination path required")
159 pkt, err := NewPkt(PktTypeFile, nice, []byte(dstPath))
163 reader, src, fileSize, err := prepareTxFile(srcPath)
170 _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
174 "nice": strconv.Itoa(int(nice)),
177 "size": strconv.FormatInt(fileSize, 10),
180 ctx.LogI("tx", sds, "sent")
183 ctx.LogE("tx", sds, "sent")
188 func (ctx *Ctx) TxFileChunked(
191 srcPath, dstPath string,
193 chunkSize int64) error {
196 return errors.New("Must provide destination filename")
198 dstPath = filepath.Base(srcPath)
200 dstPath = filepath.Clean(dstPath)
201 if filepath.IsAbs(dstPath) {
202 return errors.New("Relative destination path required")
204 reader, src, fileSize, err := prepareTxFile(srcPath)
212 if fileSize <= chunkSize {
213 pkt, err := NewPkt(PktTypeFile, nice, []byte(dstPath))
217 _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
221 "nice": strconv.Itoa(int(nice)),
224 "size": strconv.FormatInt(fileSize, 10),
227 ctx.LogI("tx", sds, "sent")
230 ctx.LogE("tx", sds, "sent")
236 metaPkt := ChunkedMeta{
238 FileSize: uint64(fileSize),
239 ChunkSize: uint64(chunkSize),
240 Checksums: make([][32]byte, 0, (fileSize/chunkSize)+1),
242 for i := int64(0); i < (fileSize/chunkSize)+1; i++ {
244 metaPkt.Checksums = append(metaPkt.Checksums, *hsh)
252 if leftSize <= chunkSize {
253 sizeToSend = leftSize
255 sizeToSend = chunkSize
257 path = dstPath + ChunkedSuffixPart + strconv.Itoa(chunkNum)
258 pkt, err = NewPkt(PktTypeFile, nice, []byte(path))
262 hsh, err = blake2b.New256(nil)
272 io.TeeReader(reader, hsh),
277 "nice": strconv.Itoa(int(nice)),
280 "size": strconv.FormatInt(sizeToSend, 10),
283 ctx.LogI("tx", sds, "sent")
286 ctx.LogE("tx", sds, "sent")
289 hsh.Sum(metaPkt.Checksums[chunkNum][:0])
290 leftSize -= sizeToSend
296 var metaBuf bytes.Buffer
297 _, err = xdr.Marshal(&metaBuf, metaPkt)
301 path = dstPath + ChunkedSuffixMeta
302 pkt, err = NewPkt(PktTypeFile, nice, []byte(path))
306 metaPktSize := int64(metaBuf.Len())
307 _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf)
311 "nice": strconv.Itoa(int(nice)),
314 "size": strconv.FormatInt(metaPktSize, 10),
317 ctx.LogI("tx", sds, "sent")
320 ctx.LogE("tx", sds, "sent")
325 func (ctx *Ctx) TxFreq(
327 nice, replyNice uint8,
328 srcPath, dstPath string,
329 minSize int64) error {
330 dstPath = filepath.Clean(dstPath)
331 if filepath.IsAbs(dstPath) {
332 return errors.New("Relative destination path required")
334 srcPath = filepath.Clean(srcPath)
335 if filepath.IsAbs(srcPath) {
336 return errors.New("Relative source path required")
338 pkt, err := NewPkt(PktTypeFreq, replyNice, []byte(srcPath))
342 src := strings.NewReader(dstPath)
343 size := int64(src.Len())
344 _, err = ctx.Tx(node, pkt, nice, size, minSize, src)
348 "nice": strconv.Itoa(int(nice)),
349 "replynice": strconv.Itoa(int(replyNice)),
354 ctx.LogI("tx", sds, "sent")
357 ctx.LogE("tx", sds, "sent")
362 func (ctx *Ctx) TxExec(
364 nice, replyNice uint8,
368 minSize int64) error {
369 path := make([][]byte, 0, 1+len(args))
370 path = append(path, []byte(handle))
371 for _, arg := range args {
372 path = append(path, []byte(arg))
374 pkt, err := NewPkt(PktTypeExec, replyNice, bytes.Join(path, []byte{0}))
378 var compressed bytes.Buffer
379 compressor, err := zlib.NewWriterLevel(&compressed, zlib.BestCompression)
383 if _, err = io.Copy(compressor, bytes.NewReader(body)); err != nil {
387 size := int64(compressed.Len())
388 _, err = ctx.Tx(node, pkt, nice, size, minSize, &compressed)
392 "nice": strconv.Itoa(int(nice)),
393 "replynice": strconv.Itoa(int(replyNice)),
394 "dst": strings.Join(append([]string{handle}, args...), " "),
395 "size": strconv.FormatInt(size, 10),
398 ctx.LogI("tx", sds, "sent")
401 ctx.LogE("tx", sds, "sent")
406 func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error {
410 "nice": strconv.Itoa(int(nice)),
411 "size": strconv.FormatInt(size, 10),
413 ctx.LogD("tx", sds, "taken")
414 tmp, err := ctx.NewTmpFileWHash()
418 if _, err = io.Copy(tmp.W, src); err != nil {
421 nodePath := filepath.Join(ctx.Spool, node.Id.String())
422 err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
424 ctx.LogI("tx", sds, "sent")
427 ctx.LogI("tx", sds, "sent")
429 os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name))