]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/tx.go
Raise copyright years
[nncp.git] / src / cypherpunks.ru / nncp / tx.go
index 03e2df241673b8956bec20f8e4cd2712fd88e5cf..aeb1748e33f3c6592a901f7552260d6514b0d7b9 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2018 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -22,17 +22,21 @@ import (
        "bufio"
        "bytes"
        "compress/zlib"
+       "crypto/rand"
        "errors"
+       "hash"
        "io"
+       "io/ioutil"
        "os"
        "path/filepath"
        "strconv"
        "strings"
 
+       "github.com/davecgh/go-xdr/xdr2"
        "golang.org/x/crypto/blake2b"
 )
 
-func (ctx *Ctx) Tx(node *Node, pkt *Pkt, nice uint8, size int64, src io.Reader) (*Node, error) {
+func (ctx *Ctx) Tx(node *Node, pkt *Pkt, nice uint8, size, minSize int64, src io.Reader) (*Node, error) {
        tmp, err := ctx.NewTmpFileWHash()
        if err != nil {
                return nil, err
@@ -44,6 +48,10 @@ func (ctx *Ctx) Tx(node *Node, pkt *Pkt, nice uint8, size int64, src io.Reader)
                lastNode = ctx.Neigh[*node.Via[i-1]]
                hops = append(hops, lastNode)
        }
+       padSize := minSize - size - int64(len(hops))*(PktOverhead+PktEncOverhead)
+       if padSize < 0 {
+               padSize = 0
+       }
        errs := make(chan error)
        curSize := size
        pipeR, pipeW := io.Pipe()
@@ -53,9 +61,10 @@ func (ctx *Ctx) Tx(node *Node, pkt *Pkt, nice uint8, size int64, src io.Reader)
                        "nice": strconv.Itoa(int(nice)),
                        "size": strconv.FormatInt(size, 10),
                }, "wrote")
-               errs <- PktEncWrite(ctx.Self, hops[0], pkt, nice, size, src, dst)
+               errs <- PktEncWrite(ctx.Self, hops[0], pkt, nice, size, padSize, src, dst)
                dst.Close()
        }(curSize, src, pipeW)
+       curSize += padSize
 
        var pipeRPrev io.Reader
        for i := 1; i < len(hops); i++ {
@@ -75,7 +84,7 @@ func (ctx *Ctx) Tx(node *Node, pkt *Pkt, nice uint8, size int64, src io.Reader)
                                "nice": strconv.Itoa(int(nice)),
                                "size": strconv.FormatInt(size, 10),
                        }, "trns wrote")
-                       errs <- PktEncWrite(ctx.Self, node, pkt, nice, size, src, dst)
+                       errs <- PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst)
                        dst.Close()
                }(hops[i], &pktTrans, curSize, pipeRPrev, pipeW)
        }
@@ -96,8 +105,52 @@ func (ctx *Ctx) Tx(node *Node, pkt *Pkt, nice uint8, size int64, src io.Reader)
        return lastNode, err
 }
 
-func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string) error {
+func prepareTxFile(srcPath string) (io.Reader, *os.File, int64, error) {
+       var reader io.Reader
+       var src *os.File
+       var fileSize int64
+       var err error
+       if srcPath == "-" {
+               src, err = ioutil.TempFile("", "nncp-file")
+               if err != nil {
+                       return nil, nil, 0, err
+               }
+               os.Remove(src.Name())
+               tmpW := bufio.NewWriter(src)
+               tmpKey := new([32]byte)
+               if _, err = rand.Read(tmpKey[:]); err != nil {
+                       return nil, nil, 0, err
+               }
+               written, err := ae(tmpKey, bufio.NewReader(os.Stdin), tmpW)
+               if err != nil {
+                       return nil, nil, 0, err
+               }
+               fileSize = int64(written)
+               tmpW.Flush()
+               src.Seek(0, 0)
+               r, w := io.Pipe()
+               go ae(tmpKey, bufio.NewReader(src), w)
+               reader = r
+       } else {
+               src, err = os.Open(srcPath)
+               if err != nil {
+                       return nil, nil, 0, err
+               }
+               srcStat, err := src.Stat()
+               if err != nil {
+                       return nil, nil, 0, err
+               }
+               fileSize = srcStat.Size()
+               reader = bufio.NewReader(src)
+       }
+       return reader, src, fileSize, nil
+}
+
+func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
        if dstPath == "" {
+               if srcPath == "-" {
+                       return errors.New("Must provide destination filename")
+               }
                dstPath = filepath.Base(srcPath)
        }
        dstPath = filepath.Clean(dstPath)
@@ -108,16 +161,14 @@ func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string) error {
        if err != nil {
                return err
        }
-       src, err := os.Open(srcPath)
-       if err != nil {
-               return err
+       reader, src, fileSize, err := prepareTxFile(srcPath)
+       if src != nil {
+               defer src.Close()
        }
-       defer src.Close()
-       srcStat, err := src.Stat()
        if err != nil {
                return err
        }
-       _, err = ctx.Tx(node, pkt, nice, srcStat.Size(), bufio.NewReader(src))
+       _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
        if err == nil {
                ctx.LogI("tx", SDS{
                        "type": "file",
@@ -125,7 +176,7 @@ func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string) error {
                        "nice": strconv.Itoa(int(nice)),
                        "src":  srcPath,
                        "dst":  dstPath,
-                       "size": strconv.FormatInt(srcStat.Size(), 10),
+                       "size": strconv.FormatInt(fileSize, 10),
                }, "sent")
        } else {
                ctx.LogE("tx", SDS{
@@ -134,14 +185,135 @@ func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string) error {
                        "nice": strconv.Itoa(int(nice)),
                        "src":  srcPath,
                        "dst":  dstPath,
-                       "size": strconv.FormatInt(srcStat.Size(), 10),
+                       "size": strconv.FormatInt(fileSize, 10),
+                       "err":  err,
+               }, "sent")
+       }
+       return err
+}
+
+func (ctx *Ctx) TxFileChunked(node *Node, nice uint8, srcPath, dstPath string, minSize int64, chunkSize int64) error {
+       if dstPath == "" {
+               if srcPath == "-" {
+                       return errors.New("Must provide destination filename")
+               }
+               dstPath = filepath.Base(srcPath)
+       }
+       dstPath = filepath.Clean(dstPath)
+       if filepath.IsAbs(dstPath) {
+               return errors.New("Relative destination path required")
+       }
+       reader, src, fileSize, err := prepareTxFile(srcPath)
+       if src != nil {
+               defer src.Close()
+       }
+       if err != nil {
+               return err
+       }
+
+       leftSize := fileSize
+       metaPkt := ChunkedMeta{
+               Magic:     MagicNNCPMv1,
+               FileSize:  uint64(fileSize),
+               ChunkSize: uint64(chunkSize),
+               Checksums: make([][32]byte, 0, (fileSize/chunkSize)+1),
+       }
+       for i := int64(0); i < (fileSize/chunkSize)+1; i++ {
+               hsh := new([32]byte)
+               metaPkt.Checksums = append(metaPkt.Checksums, *hsh)
+       }
+       var sizeToSend int64
+       var hsh hash.Hash
+       var pkt *Pkt
+       var chunkNum int
+       var path string
+       for {
+               if leftSize <= chunkSize {
+                       sizeToSend = leftSize
+               } else {
+                       sizeToSend = chunkSize
+               }
+               path = dstPath + ChunkedSuffixPart + strconv.Itoa(chunkNum)
+               pkt, err = NewPkt(PktTypeFile, path)
+               if err != nil {
+                       return err
+               }
+               hsh, err = blake2b.New256(nil)
+               if err != nil {
+                       return err
+               }
+               _, err = ctx.Tx(
+                       node,
+                       pkt,
+                       nice,
+                       sizeToSend,
+                       minSize,
+                       io.TeeReader(reader, hsh),
+               )
+               if err == nil {
+                       ctx.LogI("tx", SDS{
+                               "type": "file",
+                               "node": node.Id,
+                               "nice": strconv.Itoa(int(nice)),
+                               "src":  srcPath,
+                               "dst":  path,
+                               "size": strconv.FormatInt(sizeToSend, 10),
+                       }, "sent")
+               } else {
+                       ctx.LogE("tx", SDS{
+                               "type": "file",
+                               "node": node.Id,
+                               "nice": strconv.Itoa(int(nice)),
+                               "src":  srcPath,
+                               "dst":  path,
+                               "size": strconv.FormatInt(sizeToSend, 10),
+                               "err":  err,
+                       }, "sent")
+                       return err
+               }
+               hsh.Sum(metaPkt.Checksums[chunkNum][:0])
+               leftSize -= sizeToSend
+               chunkNum++
+               if leftSize == 0 {
+                       break
+               }
+       }
+       var metaBuf bytes.Buffer
+       _, err = xdr.Marshal(&metaBuf, metaPkt)
+       if err != nil {
+               return err
+       }
+       path = dstPath + ChunkedSuffixMeta
+       pkt, err = NewPkt(PktTypeFile, path)
+       if err != nil {
+               return err
+       }
+       metaPktSize := int64(metaBuf.Len())
+       _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf)
+       if err == nil {
+               ctx.LogI("tx", SDS{
+                       "type": "file",
+                       "node": node.Id,
+                       "nice": strconv.Itoa(int(nice)),
+                       "src":  srcPath,
+                       "dst":  path,
+                       "size": strconv.FormatInt(metaPktSize, 10),
+               }, "sent")
+       } else {
+               ctx.LogE("tx", SDS{
+                       "type": "file",
+                       "node": node.Id,
+                       "nice": strconv.Itoa(int(nice)),
+                       "src":  srcPath,
+                       "dst":  path,
+                       "size": strconv.FormatInt(metaPktSize, 10),
                        "err":  err,
                }, "sent")
        }
        return err
 }
 
-func (ctx *Ctx) TxFreq(node *Node, nice uint8, srcPath, dstPath string) error {
+func (ctx *Ctx) TxFreq(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
        dstPath = filepath.Clean(dstPath)
        if filepath.IsAbs(dstPath) {
                return errors.New("Relative destination path required")
@@ -156,7 +328,7 @@ func (ctx *Ctx) TxFreq(node *Node, nice uint8, srcPath, dstPath string) error {
        }
        src := strings.NewReader(dstPath)
        size := int64(src.Len())
-       _, err = ctx.Tx(node, pkt, nice, size, src)
+       _, err = ctx.Tx(node, pkt, nice, size, minSize, src)
        if err == nil {
                ctx.LogI("tx", SDS{
                        "type": "freq",
@@ -178,19 +350,22 @@ func (ctx *Ctx) TxFreq(node *Node, nice uint8, srcPath, dstPath string) error {
        return err
 }
 
-func (ctx *Ctx) TxMail(node *Node, nice uint8, recipient string, body []byte) error {
+func (ctx *Ctx) TxMail(node *Node, nice uint8, recipient string, body []byte, minSize int64) error {
        pkt, err := NewPkt(PktTypeMail, recipient)
        if err != nil {
                return err
        }
        var compressed bytes.Buffer
-       compressor := zlib.NewWriter(&compressed)
+       compressor, err := zlib.NewWriterLevel(&compressed, zlib.BestCompression)
+       if err != nil {
+               return err
+       }
        if _, err = io.Copy(compressor, bytes.NewReader(body)); err != nil {
                return err
        }
        compressor.Close()
        size := int64(compressed.Len())
-       _, err = ctx.Tx(node, pkt, nice, size, &compressed)
+       _, err = ctx.Tx(node, pkt, nice, size, minSize, &compressed)
        if err == nil {
                ctx.LogI("tx", SDS{
                        "type": "mail",