2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2018 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"
39 func (ctx *Ctx) Tx(node *Node, pkt *Pkt, nice uint8, size, minSize int64, src io.Reader) (*Node, error) {
40 tmp, err := ctx.NewTmpFileWHash()
44 hops := make([]*Node, 0, 1+len(node.Via))
45 hops = append(hops, node)
47 for i := len(node.Via); i > 0; i-- {
48 lastNode = ctx.Neigh[*node.Via[i-1]]
49 hops = append(hops, lastNode)
51 padSize := minSize - size - int64(len(hops))*(PktOverhead+PktEncOverhead)
55 errs := make(chan error)
57 pipeR, pipeW := io.Pipe()
58 go func(size int64, src io.Reader, dst io.WriteCloser) {
61 "nice": strconv.Itoa(int(nice)),
62 "size": strconv.FormatInt(size, 10),
64 errs <- PktEncWrite(ctx.Self, hops[0], pkt, nice, size, padSize, src, dst)
66 }(curSize, src, pipeW)
69 var pipeRPrev io.Reader
70 for i := 1; i < len(hops); i++ {
71 pktTrns, _ := NewPkt(PktTypeTrns, 0, hops[i-1].Id[:])
72 curSize += PktOverhead + PktEncOverhead
74 pipeR, pipeW = io.Pipe()
75 go func(node *Node, pkt *Pkt, size int64, src io.Reader, dst io.WriteCloser) {
78 "nice": strconv.Itoa(int(nice)),
79 "size": strconv.FormatInt(size, 10),
81 errs <- PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst)
83 }(hops[i], pktTrns, curSize, pipeRPrev, pipeW)
86 _, err := io.Copy(tmp.W, pipeR)
89 for i := 0; i <= len(hops); i++ {
96 nodePath := filepath.Join(ctx.Spool, lastNode.Id.String())
97 err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
98 os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name))
102 func prepareTxFile(srcPath string) (io.Reader, *os.File, int64, error) {
108 src, err = ioutil.TempFile("", "nncp-file")
110 return nil, nil, 0, err
112 os.Remove(src.Name())
113 tmpW := bufio.NewWriter(src)
114 tmpKey := new([32]byte)
115 if _, err = rand.Read(tmpKey[:]); err != nil {
116 return nil, nil, 0, err
118 written, err := ae(tmpKey, bufio.NewReader(os.Stdin), tmpW)
120 return nil, nil, 0, err
122 fileSize = int64(written)
126 go ae(tmpKey, bufio.NewReader(src), w)
129 src, err = os.Open(srcPath)
131 return nil, nil, 0, err
133 srcStat, err := src.Stat()
135 return nil, nil, 0, err
137 fileSize = srcStat.Size()
138 reader = bufio.NewReader(src)
140 return reader, src, fileSize, nil
143 func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
146 return errors.New("Must provide destination filename")
148 dstPath = filepath.Base(srcPath)
150 dstPath = filepath.Clean(dstPath)
151 if filepath.IsAbs(dstPath) {
152 return errors.New("Relative destination path required")
154 pkt, err := NewPkt(PktTypeFile, nice, []byte(dstPath))
158 reader, src, fileSize, err := prepareTxFile(srcPath)
165 _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
169 "nice": strconv.Itoa(int(nice)),
172 "size": strconv.FormatInt(fileSize, 10),
175 ctx.LogI("tx", sds, "sent")
178 ctx.LogE("tx", sds, "sent")
183 func (ctx *Ctx) TxFileChunked(node *Node, nice uint8, srcPath, dstPath string, minSize int64, chunkSize int64) error {
186 return errors.New("Must provide destination filename")
188 dstPath = filepath.Base(srcPath)
190 dstPath = filepath.Clean(dstPath)
191 if filepath.IsAbs(dstPath) {
192 return errors.New("Relative destination path required")
194 reader, src, fileSize, err := prepareTxFile(srcPath)
202 if fileSize <= chunkSize {
203 pkt, err := NewPkt(PktTypeFile, nice, []byte(dstPath))
207 _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
211 "nice": strconv.Itoa(int(nice)),
214 "size": strconv.FormatInt(fileSize, 10),
217 ctx.LogI("tx", sds, "sent")
220 ctx.LogE("tx", sds, "sent")
226 metaPkt := ChunkedMeta{
228 FileSize: uint64(fileSize),
229 ChunkSize: uint64(chunkSize),
230 Checksums: make([][32]byte, 0, (fileSize/chunkSize)+1),
232 for i := int64(0); i < (fileSize/chunkSize)+1; i++ {
234 metaPkt.Checksums = append(metaPkt.Checksums, *hsh)
242 if leftSize <= chunkSize {
243 sizeToSend = leftSize
245 sizeToSend = chunkSize
247 path = dstPath + ChunkedSuffixPart + strconv.Itoa(chunkNum)
248 pkt, err = NewPkt(PktTypeFile, nice, []byte(path))
252 hsh, err = blake2b.New256(nil)
262 io.TeeReader(reader, hsh),
267 "nice": strconv.Itoa(int(nice)),
270 "size": strconv.FormatInt(sizeToSend, 10),
273 ctx.LogI("tx", sds, "sent")
276 ctx.LogE("tx", sds, "sent")
279 hsh.Sum(metaPkt.Checksums[chunkNum][:0])
280 leftSize -= sizeToSend
286 var metaBuf bytes.Buffer
287 _, err = xdr.Marshal(&metaBuf, metaPkt)
291 path = dstPath + ChunkedSuffixMeta
292 pkt, err = NewPkt(PktTypeFile, nice, []byte(path))
296 metaPktSize := int64(metaBuf.Len())
297 _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf)
301 "nice": strconv.Itoa(int(nice)),
304 "size": strconv.FormatInt(metaPktSize, 10),
307 ctx.LogI("tx", sds, "sent")
310 ctx.LogE("tx", sds, "sent")
315 func (ctx *Ctx) TxFreq(node *Node, nice, replyNice uint8, srcPath, dstPath string, minSize int64) error {
316 dstPath = filepath.Clean(dstPath)
317 if filepath.IsAbs(dstPath) {
318 return errors.New("Relative destination path required")
320 srcPath = filepath.Clean(srcPath)
321 if filepath.IsAbs(srcPath) {
322 return errors.New("Relative source path required")
324 pkt, err := NewPkt(PktTypeFreq, replyNice, []byte(srcPath))
328 src := strings.NewReader(dstPath)
329 size := int64(src.Len())
330 _, err = ctx.Tx(node, pkt, nice, size, minSize, src)
334 "nice": strconv.Itoa(int(nice)),
335 "replynice": strconv.Itoa(int(replyNice)),
340 ctx.LogI("tx", sds, "sent")
343 ctx.LogE("tx", sds, "sent")
348 func (ctx *Ctx) TxExec(node *Node, nice, replyNice uint8, handle string, args []string, body []byte, minSize int64) error {
349 path := make([][]byte, 0, 1+len(args))
350 path = append(path, []byte(handle))
351 for _, arg := range args {
352 path = append(path, []byte(arg))
354 pkt, err := NewPkt(PktTypeExec, replyNice, bytes.Join(path, []byte{0}))
358 var compressed bytes.Buffer
359 compressor, err := zlib.NewWriterLevel(&compressed, zlib.BestCompression)
363 if _, err = io.Copy(compressor, bytes.NewReader(body)); err != nil {
367 size := int64(compressed.Len())
368 _, err = ctx.Tx(node, pkt, nice, size, minSize, &compressed)
372 "nice": strconv.Itoa(int(nice)),
373 "replynice": strconv.Itoa(int(replyNice)),
374 "dst": strings.Join(append([]string{handle}, args...), " "),
375 "size": strconv.FormatInt(size, 10),
378 ctx.LogI("tx", sds, "sent")
381 ctx.LogE("tx", sds, "sent")
386 func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error {
390 "nice": strconv.Itoa(int(nice)),
391 "size": strconv.FormatInt(size, 10),
393 ctx.LogD("tx", sds, "taken")
394 tmp, err := ctx.NewTmpFileWHash()
398 if _, err = io.Copy(tmp.W, src); err != nil {
401 nodePath := filepath.Join(ctx.Spool, node.Id.String())
402 err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
404 ctx.LogI("tx", sds, "sent")
407 ctx.LogI("tx", sds, "sent")
409 os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name))