]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-ack/main.go
ACK
[nncp.git] / src / cmd / nncp-ack / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
4
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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Send packet receipt acknowledgement via NNCP.
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "log"
25         "os"
26         "path/filepath"
27
28         "go.cypherpunks.ru/nncp/v8"
29 )
30
31 func usage() {
32         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
33         fmt.Fprintf(os.Stderr, "nncp-ack -- send packet receipt acknowledgement\n\n")
34         fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE [PKT|rx]\nOptions:\n", os.Args[0])
35         flag.PrintDefaults()
36 }
37
38 func main() {
39         var (
40                 cfgPath     = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
41                 niceRaw     = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFreq), "Outbound packet niceness")
42                 minSizeRaw  = flag.Uint64("minsize", 0, "Minimal required resulting packet size, in KiB")
43                 viaOverride = flag.String("via", "", "Override Via path to destination node")
44                 spoolPath   = flag.String("spool", "", "Override path to spool")
45                 logPath     = flag.String("log", "", "Override path to logfile")
46                 quiet       = flag.Bool("quiet", false, "Print only errors")
47                 showPrgrs   = flag.Bool("progress", false, "Force progress showing")
48                 omitPrgrs   = flag.Bool("noprogress", false, "Omit progress showing")
49                 debug       = flag.Bool("debug", false, "Print debug messages")
50                 version     = flag.Bool("version", false, "Print version information")
51                 warranty    = flag.Bool("warranty", false, "Print warranty information")
52         )
53         log.SetFlags(log.Lshortfile)
54         flag.Usage = usage
55         flag.Parse()
56         if *warranty {
57                 fmt.Println(nncp.Warranty)
58                 return
59         }
60         if *version {
61                 fmt.Println(nncp.VersionGet())
62                 return
63         }
64         if flag.NArg() != 2 {
65                 usage()
66                 os.Exit(1)
67         }
68         nice, err := nncp.NicenessParse(*niceRaw)
69         if err != nil {
70                 log.Fatalln(err)
71         }
72
73         ctx, err := nncp.CtxFromCmdline(
74                 *cfgPath,
75                 *spoolPath,
76                 *logPath,
77                 *quiet,
78                 *showPrgrs,
79                 *omitPrgrs,
80                 *debug,
81         )
82         if err != nil {
83                 log.Fatalln("Error during initialization:", err)
84         }
85         if ctx.Self == nil {
86                 log.Fatalln("Config lacks private keys")
87         }
88
89         node, err := ctx.FindNode(flag.Arg(0))
90         if err != nil {
91                 log.Fatalln("Invalid NODE specified:", err)
92         }
93
94         nncp.ViaOverride(*viaOverride, ctx, node)
95         ctx.Umask()
96         minSize := int64(*minSizeRaw) * 1024
97
98         if flag.Arg(1) == string(nncp.TRx) {
99                 for job := range ctx.Jobs(node.Id, nncp.TRx) {
100                         pktName := filepath.Base(job.Path)
101                         if err = ctx.TxACK(node, nice, pktName, minSize); err != nil {
102                                 log.Fatalln(err)
103                         }
104                 }
105         } else {
106                 if err = ctx.TxACK(node, nice, flag.Arg(1), minSize); err != nil {
107                         log.Fatalln(err)
108                 }
109         }
110 }