]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-ack/main.go
e97202811230ac269fd321cd6e9d4d0c2c7aa816
[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         "strings"
28
29         "go.cypherpunks.ru/nncp/v8"
30 )
31
32 func usage() {
33         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
34         fmt.Fprintf(os.Stderr, "nncp-ack -- send packet receipt acknowledgement\n\n")
35         fmt.Fprintf(os.Stderr, "Usage: %s [options] -all\n", os.Args[0])
36         fmt.Fprintf(os.Stderr, "Usage: %s           -node NODE[,...]\n", os.Args[0])
37         fmt.Fprintf(os.Stderr, "Usage: %s           -node NODE -pkt PKT\n", os.Args[0])
38         fmt.Fprintln(os.Stderr, "Options:")
39         flag.PrintDefaults()
40 }
41
42 func main() {
43         var (
44                 cfgPath     = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
45                 niceRaw     = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFreq), "Outbound packet niceness")
46                 minSizeRaw  = flag.Uint64("minsize", 0, "Minimal required resulting packet size, in KiB")
47                 viaOverride = flag.String("via", "", "Override Via path to destination node (ignored with -all)")
48                 spoolPath   = flag.String("spool", "", "Override path to spool")
49                 logPath     = flag.String("log", "", "Override path to logfile")
50                 doAll       = flag.Bool("all", false, "ACK all rx packet for all nodes")
51                 nodesRaw    = flag.String("node", "", "ACK rx packets for that node")
52                 pktRaw      = flag.String("pkt", "", "ACK only that packet")
53                 quiet       = flag.Bool("quiet", false, "Print only errors")
54                 showPrgrs   = flag.Bool("progress", false, "Force progress showing")
55                 omitPrgrs   = flag.Bool("noprogress", false, "Omit progress showing")
56                 debug       = flag.Bool("debug", false, "Print debug messages")
57                 version     = flag.Bool("version", false, "Print version information")
58                 warranty    = flag.Bool("warranty", false, "Print warranty information")
59         )
60         log.SetFlags(log.Lshortfile)
61         flag.Usage = usage
62         flag.Parse()
63         if *warranty {
64                 fmt.Println(nncp.Warranty)
65                 return
66         }
67         if *version {
68                 fmt.Println(nncp.VersionGet())
69                 return
70         }
71         nice, err := nncp.NicenessParse(*niceRaw)
72         if err != nil {
73                 log.Fatalln(err)
74         }
75
76         ctx, err := nncp.CtxFromCmdline(
77                 *cfgPath,
78                 *spoolPath,
79                 *logPath,
80                 *quiet,
81                 *showPrgrs,
82                 *omitPrgrs,
83                 *debug,
84         )
85         if err != nil {
86                 log.Fatalln("Error during initialization:", err)
87         }
88         if ctx.Self == nil {
89                 log.Fatalln("Config lacks private keys")
90         }
91
92         ctx.Umask()
93         minSize := int64(*minSizeRaw) * 1024
94
95         var nodes []*nncp.Node
96         if *nodesRaw != "" {
97                 for _, nodeRaw := range strings.Split(*nodesRaw, ",") {
98                         node, err := ctx.FindNode(nodeRaw)
99                         if err != nil {
100                                 log.Fatalln("Invalid -node specified:", err)
101                         }
102                         nodes = append(nodes, node)
103                 }
104         }
105         if *doAll {
106                 if len(nodes) != 0 {
107                         usage()
108                         os.Exit(1)
109                 }
110                 for _, node := range ctx.Neigh {
111                         nodes = append(nodes, node)
112                 }
113         } else if len(nodes) == 0 {
114                 usage()
115                 os.Exit(1)
116         }
117
118         if *pktRaw != "" {
119                 if len(nodes) != 1 {
120                         usage()
121                         os.Exit(1)
122                 }
123                 nncp.ViaOverride(*viaOverride, ctx, nodes[0])
124                 if err = ctx.TxACK(nodes[0], nice, *pktRaw, minSize); err != nil {
125                         log.Fatalln(err)
126                 }
127                 return
128         }
129
130         for _, node := range nodes {
131                 for job := range ctx.Jobs(node.Id, nncp.TRx) {
132                         pktName := filepath.Base(job.Path)
133                         if err = ctx.TxACK(node, nice, pktName, minSize); err != nil {
134                                 log.Fatalln(err)
135                         }
136                 }
137         }
138 }