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)
128 if err = tmpW.Flush(); err != nil {
129 return nil, nil, 0, err
131 src.Seek(0, io.SeekStart)
133 go ae(tmpKey, bufio.NewReader(src), w)
136 src, err = os.Open(srcPath)
138 return nil, nil, 0, err
140 srcStat, err := src.Stat()
142 return nil, nil, 0, err
144 fileSize = srcStat.Size()
145 reader = bufio.NewReader(src)
147 return reader, src, fileSize, nil
150 func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
153 return errors.New("Must provide destination filename")
155 dstPath = filepath.Base(srcPath)
157 dstPath = filepath.Clean(dstPath)
158 if filepath.IsAbs(dstPath) {
159 return errors.New("Relative destination path required")
161 pkt, err := NewPkt(PktTypeFile, nice, []byte(dstPath))
165 reader, src, fileSize, err := prepareTxFile(srcPath)
172 _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
176 "nice": strconv.Itoa(int(nice)),
179 "size": strconv.FormatInt(fileSize, 10),
182 ctx.LogI("tx", sds, "sent")
185 ctx.LogE("tx", sds, "sent")
190 func (ctx *Ctx) TxFileChunked(
193 srcPath, dstPath string,
195 chunkSize int64) error {
198 return errors.New("Must provide destination filename")
200 dstPath = filepath.Base(srcPath)
202 dstPath = filepath.Clean(dstPath)
203 if filepath.IsAbs(dstPath) {
204 return errors.New("Relative destination path required")
206 reader, src, fileSize, err := prepareTxFile(srcPath)
214 if fileSize <= chunkSize {
215 pkt, err := NewPkt(PktTypeFile, nice, []byte(dstPath))
219 _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
223 "nice": strconv.Itoa(int(nice)),
226 "size": strconv.FormatInt(fileSize, 10),
229 ctx.LogI("tx", sds, "sent")
232 ctx.LogE("tx", sds, "sent")
238 metaPkt := ChunkedMeta{
240 FileSize: uint64(fileSize),
241 ChunkSize: uint64(chunkSize),
242 Checksums: make([][32]byte, 0, (fileSize/chunkSize)+1),
244 for i := int64(0); i < (fileSize/chunkSize)+1; i++ {
246 metaPkt.Checksums = append(metaPkt.Checksums, *hsh)
254 if leftSize <= chunkSize {
255 sizeToSend = leftSize
257 sizeToSend = chunkSize
259 path = dstPath + ChunkedSuffixPart + strconv.Itoa(chunkNum)
260 pkt, err = NewPkt(PktTypeFile, nice, []byte(path))
264 hsh, err = blake2b.New256(nil)
274 io.TeeReader(reader, hsh),
279 "nice": strconv.Itoa(int(nice)),
282 "size": strconv.FormatInt(sizeToSend, 10),
285 ctx.LogI("tx", sds, "sent")
288 ctx.LogE("tx", sds, "sent")
291 hsh.Sum(metaPkt.Checksums[chunkNum][:0])
292 leftSize -= sizeToSend
298 var metaBuf bytes.Buffer
299 _, err = xdr.Marshal(&metaBuf, metaPkt)
303 path = dstPath + ChunkedSuffixMeta
304 pkt, err = NewPkt(PktTypeFile, nice, []byte(path))
308 metaPktSize := int64(metaBuf.Len())
309 _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf)
313 "nice": strconv.Itoa(int(nice)),
316 "size": strconv.FormatInt(metaPktSize, 10),
319 ctx.LogI("tx", sds, "sent")
322 ctx.LogE("tx", sds, "sent")
327 func (ctx *Ctx) TxFreq(
329 nice, replyNice uint8,
330 srcPath, dstPath string,
331 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, replyNice, []byte(srcPath))
344 src := strings.NewReader(dstPath)
345 size := int64(src.Len())
346 _, err = ctx.Tx(node, pkt, nice, size, minSize, src)
350 "nice": strconv.Itoa(int(nice)),
351 "replynice": strconv.Itoa(int(replyNice)),
356 ctx.LogI("tx", sds, "sent")
359 ctx.LogE("tx", sds, "sent")
364 func (ctx *Ctx) TxExec(
366 nice, replyNice uint8,
370 minSize int64) error {
371 path := make([][]byte, 0, 1+len(args))
372 path = append(path, []byte(handle))
373 for _, arg := range args {
374 path = append(path, []byte(arg))
376 pkt, err := NewPkt(PktTypeExec, replyNice, bytes.Join(path, []byte{0}))
380 var compressed bytes.Buffer
381 compressor, err := zlib.NewWriterLevel(&compressed, zlib.BestCompression)
385 if _, err = io.Copy(compressor, bytes.NewReader(body)); err != nil {
389 size := int64(compressed.Len())
390 _, err = ctx.Tx(node, pkt, nice, size, minSize, &compressed)
394 "nice": strconv.Itoa(int(nice)),
395 "replynice": strconv.Itoa(int(replyNice)),
396 "dst": strings.Join(append([]string{handle}, args...), " "),
397 "size": strconv.FormatInt(size, 10),
400 ctx.LogI("tx", sds, "sent")
403 ctx.LogE("tx", sds, "sent")
408 func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error {
412 "nice": strconv.Itoa(int(nice)),
413 "size": strconv.FormatInt(size, 10),
415 ctx.LogD("tx", sds, "taken")
416 tmp, err := ctx.NewTmpFileWHash()
420 if _, err = io.Copy(tmp.W, src); err != nil {
423 nodePath := filepath.Join(ctx.Spool, node.Id.String())
424 err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
426 ctx.LogI("tx", sds, "sent")
429 ctx.LogI("tx", sds, "sent")
431 os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name))