]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/toss.go
MTH
[nncp.git] / src / toss.go
index eaf74019fcc0acc0a9519d31ccf53f942da6f1fc..7537d695d08f5edcb874b72a747cf624fea22ad7 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 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
@@ -20,6 +20,8 @@ package nncp
 import (
        "bufio"
        "bytes"
+       "encoding/base64"
+       "errors"
        "fmt"
        "io"
        "io/ioutil"
@@ -31,11 +33,11 @@ import (
        "path/filepath"
        "strconv"
        "strings"
+       "time"
 
        xdr "github.com/davecgh/go-xdr/xdr2"
        "github.com/dustin/go-humanize"
        "github.com/klauspost/compress/zstd"
-       "golang.org/x/crypto/blake2b"
        "golang.org/x/crypto/poly1305"
 )
 
@@ -43,13 +45,23 @@ const (
        SeenSuffix = ".seen"
 )
 
-func newNotification(fromTo *FromToJSON, subject string) io.Reader {
-       return strings.NewReader(fmt.Sprintf(
-               "From: %s\nTo: %s\nSubject: %s\n",
-               fromTo.From,
-               fromTo.To,
-               mime.BEncoding.Encode("UTF-8", subject),
-       ))
+func newNotification(fromTo *FromToJSON, subject string, body []byte) io.Reader {
+       lines := []string{
+               "From: " + fromTo.From,
+               "To: " + fromTo.To,
+               "Subject: " + mime.BEncoding.Encode("UTF-8", subject),
+       }
+       if len(body) > 0 {
+               lines = append(
+                       lines,
+                       "MIME-Version: 1.0",
+                       "Content-Type: text/plain; charset=utf-8",
+                       "Content-Transfer-Encoding: base64",
+                       "",
+                       base64.StdEncoding.EncodeToString(body),
+               )
+       }
+       return strings.NewReader(strings.Join(lines, "\n"))
 }
 
 func (ctx *Ctx) Toss(
@@ -57,45 +69,70 @@ func (ctx *Ctx) Toss(
        nice uint8,
        dryRun, doSeen, noFile, noFreq, noExec, noTrns bool,
 ) bool {
+       dirLock, err := ctx.LockDir(nodeId, "toss")
+       if err != nil {
+               return false
+       }
+       defer ctx.UnlockDir(dirLock)
        isBad := false
+       sendmail := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
        decompressor, err := zstd.NewReader(nil)
        if err != nil {
                panic(err)
        }
        defer decompressor.Close()
        for job := range ctx.Jobs(nodeId, TRx) {
-               pktName := filepath.Base(job.Fd.Name())
-               sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName}
+               pktName := filepath.Base(job.Path)
+               les := LEs{
+                       {"Node", job.PktEnc.Sender},
+                       {"Pkt", pktName},
+                       {"Nice", int(job.PktEnc.Nice)},
+               }
                if job.PktEnc.Nice > nice {
-                       ctx.LogD("rx", SdsAdd(sds, SDS{
-                               "nice": strconv.Itoa(int(job.PktEnc.Nice)),
-                       }), "too nice")
+                       ctx.LogD("rx-too-nice", les, func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Tossing %s/%s: too nice: %s",
+                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                       NicenessFmt(job.PktEnc.Nice),
+                               )
+                       })
                        continue
                }
+               fd, err := os.Open(job.Path)
+               if err != nil {
+                       ctx.LogE("rx-open", les, err, func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Tossing %s/%s: opening %s",
+                                       ctx.NodeName(job.PktEnc.Sender), pktName, job.Path,
+                               )
+                       })
+                       isBad = true
+                       continue
+               }
+
                pipeR, pipeW := io.Pipe()
-               errs := make(chan error, 1)
-               go func(job Job) {
+               go func(job Job) error {
                        pipeWB := bufio.NewWriter(pipeW)
-                       _, _, err := PktEncRead(
-                               ctx.Self,
-                               ctx.Neigh,
-                               bufio.NewReader(job.Fd),
-                               pipeWB,
-                       )
-                       errs <- err
-                       pipeWB.Flush()
-                       pipeW.Close()
-                       job.Fd.Close()
+                       _, _, err := PktEncRead(ctx.Self, ctx.Neigh, bufio.NewReader(fd), pipeWB)
+                       fd.Close() // #nosec G104
                        if err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "decryption")
+                               return pipeW.CloseWithError(err)
+                       }
+                       if err = pipeWB.Flush(); err != nil {
+                               return pipeW.CloseWithError(err)
                        }
+                       return pipeW.Close()
                }(job)
                var pkt Pkt
-               var err error
                var pktSize int64
                var pktSizeBlocks int64
                if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
-                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "unmarshal")
+                       ctx.LogE("rx-unmarshal", les, err, func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Tossing %s/%s: unmarshal",
+                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                               )
+                       })
                        isBad = true
                        goto Closing
                }
@@ -105,10 +142,17 @@ func (ctx *Ctx) Toss(
                        pktSize -= poly1305.TagSize
                }
                pktSize -= pktSizeBlocks * poly1305.TagSize
-               sds["size"] = strconv.FormatInt(pktSize, 10)
-               ctx.LogD("rx", sds, "taken")
+               les = append(les, LE{"Size", pktSize})
+               ctx.LogD("rx", les, func(les LEs) string {
+                       return fmt.Sprintf(
+                               "Tossing %s/%s (%s)",
+                               ctx.NodeName(job.PktEnc.Sender), pktName,
+                               humanize.IBytes(uint64(pktSize)),
+                       )
+               })
+
                switch pkt.Type {
-               case PktTypeExec:
+               case PktTypeExec, PktTypeExecFat:
                        if noExec {
                                goto Closing
                        }
@@ -118,101 +162,225 @@ func (ctx *Ctx) Toss(
                        for _, p := range path[1:] {
                                args = append(args, string(p))
                        }
-                       sds := SdsAdd(sds, SDS{
-                               "type": "exec",
-                               "dst":  strings.Join(append([]string{handle}, args...), " "),
-                       })
+                       argsStr := strings.Join(append([]string{handle}, args...), " ")
+                       les = append(les, LE{"Type", "exec"}, LE{"Dst", argsStr})
                        sender := ctx.Neigh[*job.PktEnc.Sender]
                        cmdline, exists := sender.Exec[handle]
                        if !exists || len(cmdline) == 0 {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": "No handle found"}), "")
+                               ctx.LogE(
+                                       "rx-no-handle", les, errors.New("No handle found"),
+                                       func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing exec %s/%s (%s): %s",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), argsStr,
+                                               )
+                                       },
+                               )
                                isBad = true
                                goto Closing
                        }
-                       if err = decompressor.Reset(pipeR); err != nil {
-                               log.Fatalln(err)
+                       if pkt.Type == PktTypeExec {
+                               if err = decompressor.Reset(pipeR); err != nil {
+                                       log.Fatalln(err)
+                               }
                        }
                        if !dryRun {
-                               cmd := exec.Command(
-                                       cmdline[0],
-                                       append(cmdline[1:len(cmdline)], args...)...,
-                               )
+                               cmd := exec.Command(cmdline[0], append(cmdline[1:], args...)...)
                                cmd.Env = append(
                                        cmd.Env,
                                        "NNCP_SELF="+ctx.Self.Id.String(),
                                        "NNCP_SENDER="+sender.Id.String(),
                                        "NNCP_NICE="+strconv.Itoa(int(pkt.Nice)),
                                )
-                               cmd.Stdin = decompressor
-                               if err = cmd.Run(); err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "handle")
+                               if pkt.Type == PktTypeExec {
+                                       cmd.Stdin = decompressor
+                               } else {
+                                       cmd.Stdin = pipeR
+                               }
+                               output, err := cmd.Output()
+                               if err != nil {
+                                       ctx.LogE("rx-hande", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing exec %s/%s (%s): %s: handling",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), argsStr,
+                                               )
+                                       })
                                        isBad = true
                                        goto Closing
                                }
+                               if len(sendmail) > 0 && ctx.NotifyExec != nil {
+                                       notify, exists := ctx.NotifyExec[sender.Name+"."+handle]
+                                       if !exists {
+                                               notify, exists = ctx.NotifyExec["*."+handle]
+                                       }
+                                       if exists {
+                                               cmd := exec.Command(
+                                                       sendmail[0],
+                                                       append(sendmail[1:], notify.To)...,
+                                               )
+                                               cmd.Stdin = newNotification(notify, fmt.Sprintf(
+                                                       "Exec from %s: %s", sender.Name, argsStr,
+                                               ), output)
+                                               if err = cmd.Run(); err != nil {
+                                                       ctx.LogE("rx-notify", les, err, func(les LEs) string {
+                                                               return fmt.Sprintf(
+                                                                       "Tossing exec %s/%s (%s): %s: notifying",
+                                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                                       humanize.IBytes(uint64(pktSize)), argsStr,
+                                                               )
+                                                       })
+                                               }
+                                       }
+                               }
                        }
-                       ctx.LogI("rx", sds, "")
+                       ctx.LogI("rx", les, func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Got exec from %s to %s (%s)",
+                                       ctx.NodeName(job.PktEnc.Sender), argsStr,
+                                       humanize.IBytes(uint64(pktSize)),
+                               )
+                       })
                        if !dryRun {
                                if doSeen {
-                                       if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
-                                               fd.Close()
+                                       if fd, err := os.Create(job.Path + SeenSuffix); err == nil {
+                                               fd.Close() // #nosec G104
                                        }
                                }
-                               if err = os.Remove(job.Fd.Name()); err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                               if err = os.Remove(job.Path); err != nil {
+                                       ctx.LogE("rx-notify", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing exec %s/%s (%s): %s: notifying",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), argsStr,
+                                               )
+                                       })
                                        isBad = true
+                               } else if ctx.HdrUsage {
+                                       os.Remove(job.Path + HdrSuffix)
                                }
                        }
+
                case PktTypeFile:
                        if noFile {
                                goto Closing
                        }
                        dst := string(pkt.Path[:int(pkt.PathLen)])
-                       sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
+                       les = append(les, LE{"Type", "file"}, LE{"Dst", dst})
                        if filepath.IsAbs(dst) {
-                               ctx.LogE("rx", sds, "non-relative destination path")
+                               ctx.LogE(
+                                       "rx-non-rel", les, errors.New("non-relative destination path"),
+                                       func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       },
+                               )
                                isBad = true
                                goto Closing
                        }
                        incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
                        if incoming == nil {
-                               ctx.LogE("rx", sds, "incoming is not allowed")
+                               ctx.LogE(
+                                       "rx-no-incoming", les, errors.New("incoming is not allowed"),
+                                       func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       },
+                               )
                                isBad = true
                                goto Closing
                        }
                        dir := filepath.Join(*incoming, path.Dir(dst))
                        if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
+                               ctx.LogE("rx-mkdir", les, err, func(les LEs) string {
+                                       return fmt.Sprintf(
+                                               "Tossing file %s/%s (%s): %s: mkdir",
+                                               ctx.NodeName(job.PktEnc.Sender), pktName,
+                                               humanize.IBytes(uint64(pktSize)), dst,
+                                       )
+                               })
                                isBad = true
                                goto Closing
                        }
                        if !dryRun {
                                tmp, err := TempFile(dir, "file")
                                if err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
+                                       ctx.LogE("rx-mktemp", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s: mktemp",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       })
                                        isBad = true
                                        goto Closing
                                }
-                               sds["tmp"] = tmp.Name()
-                               ctx.LogD("rx", sds, "created")
+                               les = append(les, LE{"Tmp", tmp.Name()})
+                               ctx.LogD("rx-tmp-created", les, func(les LEs) string {
+                                       return fmt.Sprintf(
+                                               "Tossing file %s/%s (%s): %s: created: %s",
+                                               ctx.NodeName(job.PktEnc.Sender), pktName,
+                                               humanize.IBytes(uint64(pktSize)), dst, tmp.Name(),
+                                       )
+                               })
                                bufW := bufio.NewWriter(tmp)
-                               if _, err = io.Copy(bufW, pipeR); err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
+                               if _, err = CopyProgressed(
+                                       bufW, pipeR, "Rx file",
+                                       append(les, LE{"FullSize", pktSize}),
+                                       ctx.ShowPrgrs,
+                               ); err != nil {
+                                       ctx.LogE("rx-copy", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s: copying",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       })
                                        isBad = true
                                        goto Closing
                                }
                                if err = bufW.Flush(); err != nil {
-                                       tmp.Close()
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
+                                       tmp.Close() // #nosec G104
+                                       ctx.LogE("rx-flush", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s: flushing",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       })
                                        isBad = true
                                        goto Closing
                                }
                                if err = tmp.Sync(); err != nil {
-                                       tmp.Close()
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
+                                       tmp.Close() // #nosec G104
+                                       ctx.LogE("rx-sync", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s: syncing",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       })
+                                       isBad = true
+                                       goto Closing
+                               }
+                               if err = tmp.Close(); err != nil {
+                                       ctx.LogE("rx-close", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s: closing",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       })
                                        isBad = true
                                        goto Closing
                                }
-                               tmp.Close()
                                dstPathOrig := filepath.Join(*incoming, dst)
                                dstPath := dstPathOrig
                                dstPathCtr := 0
@@ -221,7 +389,13 @@ func (ctx *Ctx) Toss(
                                                if os.IsNotExist(err) {
                                                        break
                                                }
-                                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
+                                               ctx.LogE("rx-stat", les, err, func(les LEs) string {
+                                                       return fmt.Sprintf(
+                                                               "Tossing file %s/%s (%s): %s: stating: %s",
+                                                               ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                               humanize.IBytes(uint64(pktSize)), dst, dstPath,
+                                                       )
+                                               })
                                                isBad = true
                                                goto Closing
                                        }
@@ -229,60 +403,122 @@ func (ctx *Ctx) Toss(
                                        dstPathCtr++
                                }
                                if err = os.Rename(tmp.Name(), dstPath); err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
+                                       ctx.LogE("rx-rename", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s: renaming",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       })
                                        isBad = true
                                }
-                               delete(sds, "tmp")
+                               if err = DirSync(*incoming); err != nil {
+                                       ctx.LogE("rx-dirsync", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s: dirsyncing",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       })
+                                       isBad = true
+                               }
+                               les = les[:len(les)-1] // delete Tmp
                        }
-                       ctx.LogI("rx", sds, "")
+                       ctx.LogI("rx", les, func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Got file %s (%s) from %s",
+                                       dst, humanize.IBytes(uint64(pktSize)),
+                                       ctx.NodeName(job.PktEnc.Sender),
+                               )
+                       })
                        if !dryRun {
                                if doSeen {
-                                       if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
-                                               fd.Close()
+                                       if fd, err := os.Create(job.Path + SeenSuffix); err == nil {
+                                               fd.Close() // #nosec G104
                                        }
                                }
-                               if err = os.Remove(job.Fd.Name()); err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                               if err = os.Remove(job.Path); err != nil {
+                                       ctx.LogE("rx-remove", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing file %s/%s (%s): %s: removing",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), dst,
+                                               )
+                                       })
                                        isBad = true
+                               } else if ctx.HdrUsage {
+                                       os.Remove(job.Path + HdrSuffix)
                                }
-                               sendmail, exists := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
-                               if exists && len(sendmail) > 0 && ctx.NotifyFile != nil {
+                               if len(sendmail) > 0 && ctx.NotifyFile != nil {
                                        cmd := exec.Command(
                                                sendmail[0],
-                                               append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
+                                               append(sendmail[1:], ctx.NotifyFile.To)...,
                                        )
                                        cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
                                                "File from %s: %s (%s)",
                                                ctx.Neigh[*job.PktEnc.Sender].Name,
                                                dst,
                                                humanize.IBytes(uint64(pktSize)),
-                                       ))
-                                       cmd.Run()
+                                       ), nil)
+                                       if err = cmd.Run(); err != nil {
+                                               ctx.LogE("rx-notify", les, err, func(les LEs) string {
+                                                       return fmt.Sprintf(
+                                                               "Tossing file %s/%s (%s): %s: notifying",
+                                                               ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                               humanize.IBytes(uint64(pktSize)), dst,
+                                                       )
+                                               })
+                                       }
                                }
                        }
+
                case PktTypeFreq:
                        if noFreq {
                                goto Closing
                        }
                        src := string(pkt.Path[:int(pkt.PathLen)])
+                       les := append(les, LE{"Type", "freq"}, LE{"Src", src})
                        if filepath.IsAbs(src) {
-                               ctx.LogE("rx", sds, "non-relative source path")
+                               ctx.LogE(
+                                       "rx-non-rel", les, errors.New("non-relative source path"),
+                                       func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing freq %s/%s (%s): %s: notifying",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), src,
+                                               )
+                                       },
+                               )
                                isBad = true
                                goto Closing
                        }
-                       sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
                        dstRaw, err := ioutil.ReadAll(pipeR)
                        if err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read")
+                               ctx.LogE("rx-read", les, err, func(les LEs) string {
+                                       return fmt.Sprintf(
+                                               "Tossing freq %s/%s (%s): %s: reading",
+                                               ctx.NodeName(job.PktEnc.Sender), pktName,
+                                               humanize.IBytes(uint64(pktSize)), src,
+                                       )
+                               })
                                isBad = true
                                goto Closing
                        }
                        dst := string(dstRaw)
-                       sds["dst"] = dst
+                       les = append(les, LE{"Dst", dst})
                        sender := ctx.Neigh[*job.PktEnc.Sender]
                        freqPath := sender.FreqPath
                        if freqPath == nil {
-                               ctx.LogE("rx", sds, "freqing is not allowed")
+                               ctx.LogE(
+                                       "rx-no-freq", les, errors.New("freqing is not allowed"),
+                                       func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing freq %s/%s (%s): %s -> %s",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), src, dst,
+                                               )
+                                       },
+                               )
                                isBad = true
                                goto Closing
                        }
@@ -294,78 +530,166 @@ func (ctx *Ctx) Toss(
                                        dst,
                                        sender.FreqChunked,
                                        sender.FreqMinSize,
+                                       sender.FreqMaxSize,
                                )
                                if err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
+                                       ctx.LogE("rx-tx", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing freq %s/%s (%s): %s -> %s: txing",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), src, dst,
+                                               )
+                                       })
                                        isBad = true
                                        goto Closing
                                }
                        }
-                       ctx.LogI("rx", sds, "")
+                       ctx.LogI("rx", les, func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Got file request %s to %s",
+                                       src, ctx.NodeName(job.PktEnc.Sender),
+                               )
+                       })
                        if !dryRun {
                                if doSeen {
-                                       if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
-                                               fd.Close()
+                                       if fd, err := os.Create(job.Path + SeenSuffix); err == nil {
+                                               fd.Close() // #nosec G104
                                        }
                                }
-                               if err = os.Remove(job.Fd.Name()); err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                               if err = os.Remove(job.Path); err != nil {
+                                       ctx.LogE("rx-remove", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing freq %s/%s (%s): %s -> %s: removing",
+                                                       ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                       humanize.IBytes(uint64(pktSize)), src, dst,
+                                               )
+                                       })
                                        isBad = true
+                               } else if ctx.HdrUsage {
+                                       os.Remove(job.Path + HdrSuffix)
                                }
-                               sendmail, exists := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
-                               if exists && len(sendmail) > 0 && ctx.NotifyFreq != nil {
+                               if len(sendmail) > 0 && ctx.NotifyFreq != nil {
                                        cmd := exec.Command(
                                                sendmail[0],
-                                               append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
+                                               append(sendmail[1:], ctx.NotifyFreq.To)...,
                                        )
                                        cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
-                                               "Freq from %s: %s",
-                                               ctx.Neigh[*job.PktEnc.Sender].Name,
-                                               src,
-                                       ))
-                                       cmd.Run()
+                                               "Freq from %s: %s", sender.Name, src,
+                                       ), nil)
+                                       if err = cmd.Run(); err != nil {
+                                               ctx.LogE("rx-notify", les, err, func(les LEs) string {
+                                                       return fmt.Sprintf(
+                                                               "Tossing freq %s/%s (%s): %s -> %s: notifying",
+                                                               ctx.NodeName(job.PktEnc.Sender), pktName,
+                                                               humanize.IBytes(uint64(pktSize)), src, dst,
+                                                       )
+                                               })
+                                       }
                                }
                        }
+
                case PktTypeTrns:
                        if noTrns {
                                goto Closing
                        }
-                       dst := new([blake2b.Size256]byte)
+                       dst := new([MTHSize]byte)
                        copy(dst[:], pkt.Path[:int(pkt.PathLen)])
                        nodeId := NodeId(*dst)
                        node, known := ctx.Neigh[nodeId]
-                       sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
+                       les := append(les, LE{"Type", "trns"}, LE{"Dst", nodeId})
+                       logMsg := func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Tossing trns %s/%s (%s): %s",
+                                       ctx.NodeName(job.PktEnc.Sender),
+                                       pktName,
+                                       humanize.IBytes(uint64(pktSize)),
+                                       nodeId.String(),
+                               )
+                       }
                        if !known {
-                               ctx.LogE("rx", sds, "unknown node")
+                               ctx.LogE("rx-unknown", les, errors.New("unknown node"), logMsg)
                                isBad = true
                                goto Closing
                        }
-                       ctx.LogD("rx", sds, "taken")
+                       ctx.LogD("rx-tx", les, logMsg)
                        if !dryRun {
                                if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
+                                       ctx.LogE("rx", les, err, func(les LEs) string {
+                                               return logMsg(les) + ": txing"
+                                       })
                                        isBad = true
                                        goto Closing
                                }
                        }
-                       ctx.LogI("rx", sds, "")
+                       ctx.LogI("rx", les, func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Got transitional packet from %s to %s (%s)",
+                                       ctx.NodeName(job.PktEnc.Sender),
+                                       ctx.NodeName(&nodeId),
+                                       humanize.IBytes(uint64(pktSize)),
+                               )
+                       })
                        if !dryRun {
                                if doSeen {
-                                       if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
-                                               fd.Close()
+                                       if fd, err := os.Create(job.Path + SeenSuffix); err == nil {
+                                               fd.Close() // #nosec G104
                                        }
                                }
-                               if err = os.Remove(job.Fd.Name()); err != nil {
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                               if err = os.Remove(job.Path); err != nil {
+                                       ctx.LogE("rx", les, err, func(les LEs) string {
+                                               return fmt.Sprintf(
+                                                       "Tossing trns %s/%s (%s): %s: removing",
+                                                       ctx.NodeName(job.PktEnc.Sender),
+                                                       pktName,
+                                                       humanize.IBytes(uint64(pktSize)),
+                                                       ctx.NodeName(&nodeId),
+                                               )
+                                       })
                                        isBad = true
+                               } else if ctx.HdrUsage {
+                                       os.Remove(job.Path + HdrSuffix)
                                }
                        }
+
                default:
-                       ctx.LogE("rx", sds, "unknown type")
+                       ctx.LogE(
+                               "rx-type-unknown", les, errors.New("unknown type"),
+                               func(les LEs) string {
+                                       return fmt.Sprintf(
+                                               "Tossing %s/%s (%s)",
+                                               ctx.NodeName(job.PktEnc.Sender),
+                                               pktName,
+                                               humanize.IBytes(uint64(pktSize)),
+                                       )
+                               },
+                       )
                        isBad = true
                }
        Closing:
-               pipeR.Close()
+               pipeR.Close() // #nosec G104
        }
        return isBad
 }
+
+func (ctx *Ctx) AutoToss(
+       nodeId *NodeId,
+       nice uint8,
+       doSeen, noFile, noFreq, noExec, noTrns bool,
+) (chan struct{}, chan bool) {
+       finish := make(chan struct{})
+       badCode := make(chan bool)
+       go func() {
+               bad := false
+               for {
+                       select {
+                       case <-finish:
+                               badCode <- bad
+                               break
+                       default:
+                       }
+                       time.Sleep(time.Second)
+                       bad = !ctx.Toss(nodeId, nice, false, doSeen, noFile, noFreq, noExec, noTrns) || bad
+               }
+       }()
+       return finish, badCode
+}