]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-ack/main.go
Remove huge usage headers, -warranty exists anyway
[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-2023 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         "bufio"
23         "errors"
24         "flag"
25         "fmt"
26         "io"
27         "log"
28         "os"
29         "path/filepath"
30         "strings"
31
32         xdr "github.com/davecgh/go-xdr/xdr2"
33         "go.cypherpunks.ru/nncp/v8"
34 )
35
36 func usage() {
37         fmt.Fprint(os.Stderr, "nncp-ack -- send packet receipt acknowledgement\n\n")
38         fmt.Fprintf(os.Stderr, "Usage: %s [options] -all\n", os.Args[0])
39         fmt.Fprintf(os.Stderr, "Usage: %s [options] -node NODE[,...]\n", os.Args[0])
40         fmt.Fprintf(os.Stderr, "Usage: %s [options] -node NODE -pkt PKT\n", os.Args[0])
41         fmt.Fprintln(os.Stderr, "Options:")
42         flag.PrintDefaults()
43 }
44
45 func main() {
46         var (
47                 cfgPath     = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
48                 niceRaw     = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFreq), "Outbound packet niceness")
49                 minSizeRaw  = flag.Uint64("minsize", 0, "Minimal required resulting packet size, in KiB")
50                 viaOverride = flag.String("via", "", "Override Via path to destination node (ignored with -all)")
51                 spoolPath   = flag.String("spool", "", "Override path to spool")
52                 logPath     = flag.String("log", "", "Override path to logfile")
53                 doAll       = flag.Bool("all", false, "ACK all rx packet for all nodes")
54                 nodesRaw    = flag.String("node", "", "ACK rx packets for that node")
55                 pktRaw      = flag.String("pkt", "", "ACK only that packet")
56                 quiet       = flag.Bool("quiet", false, "Print only errors")
57                 showPrgrs   = flag.Bool("progress", false, "Force progress showing")
58                 omitPrgrs   = flag.Bool("noprogress", false, "Omit progress showing")
59                 debug       = flag.Bool("debug", false, "Print debug messages")
60                 version     = flag.Bool("version", false, "Print version information")
61                 warranty    = flag.Bool("warranty", false, "Print warranty information")
62         )
63         log.SetFlags(log.Lshortfile)
64         flag.Usage = usage
65         flag.Parse()
66         if *warranty {
67                 fmt.Println(nncp.Warranty)
68                 return
69         }
70         if *version {
71                 fmt.Println(nncp.VersionGet())
72                 return
73         }
74         nice, err := nncp.NicenessParse(*niceRaw)
75         if err != nil {
76                 log.Fatalln(err)
77         }
78
79         ctx, err := nncp.CtxFromCmdline(
80                 *cfgPath,
81                 *spoolPath,
82                 *logPath,
83                 *quiet,
84                 *showPrgrs,
85                 *omitPrgrs,
86                 *debug,
87         )
88         if err != nil {
89                 log.Fatalln("Error during initialization:", err)
90         }
91         if ctx.Self == nil {
92                 log.Fatalln("Config lacks private keys")
93         }
94
95         ctx.Umask()
96         minSize := int64(*minSizeRaw) * 1024
97
98         var nodes []*nncp.Node
99         if *nodesRaw != "" {
100                 for _, nodeRaw := range strings.Split(*nodesRaw, ",") {
101                         node, err := ctx.FindNode(nodeRaw)
102                         if err != nil {
103                                 log.Fatalln("Invalid -node specified:", err)
104                         }
105                         nodes = append(nodes, node)
106                 }
107         }
108         if *doAll {
109                 if len(nodes) != 0 {
110                         usage()
111                         os.Exit(1)
112                 }
113                 for _, node := range ctx.Neigh {
114                         nodes = append(nodes, node)
115                 }
116         } else if len(nodes) == 0 {
117                 usage()
118                 os.Exit(1)
119         }
120
121         acksCreated := os.NewFile(uintptr(4), "ACKsCreated")
122         if acksCreated == nil {
123                 log.Fatalln("can not open FD:4")
124         }
125
126         if *pktRaw != "" {
127                 if len(nodes) != 1 {
128                         usage()
129                         os.Exit(1)
130                 }
131                 nncp.ViaOverride(*viaOverride, ctx, nodes[0])
132                 pktName, err := ctx.TxACK(nodes[0], nice, *pktRaw, minSize)
133                 if err != nil {
134                         log.Fatalln(err)
135                 }
136                 acksCreated.WriteString(nodes[0].Id.String() + "/" + pktName + "\n")
137                 return
138         }
139
140         isBad := false
141         for _, node := range nodes {
142                 for job := range ctx.Jobs(node.Id, nncp.TRx) {
143                         pktName := filepath.Base(job.Path)
144                         sender := ctx.Neigh[*job.PktEnc.Sender]
145                         les := nncp.LEs{
146                                 {K: "Node", V: job.PktEnc.Sender},
147                                 {K: "Pkt", V: pktName},
148                         }
149                         logMsg := func(les nncp.LEs) string {
150                                 return fmt.Sprintf(
151                                         "ACKing %s/%s",
152                                         ctx.NodeName(job.PktEnc.Sender), pktName,
153                                 )
154                         }
155                         if sender == nil {
156                                 err := errors.New("unknown node")
157                                 ctx.LogE("ack-read", les, err, logMsg)
158                                 isBad = true
159                                 continue
160                         }
161                         fd, err := os.Open(job.Path)
162                         if err != nil {
163                                 ctx.LogE("ack-read-open", les, err, func(les nncp.LEs) string {
164                                         return logMsg(les) + ": opening" + job.Path
165                                 })
166                                 isBad = true
167                                 continue
168                         }
169                         pktEnc, _, err := ctx.HdrRead(fd)
170                         if err != nil {
171                                 fd.Close()
172                                 ctx.LogE("ack-read-read", les, err, func(les nncp.LEs) string {
173                                         return logMsg(les) + ": reading" + job.Path
174                                 })
175                                 isBad = true
176                                 continue
177                         }
178                         switch pktEnc.Magic {
179                         case nncp.MagicNNCPEv1.B:
180                                 err = nncp.MagicNNCPEv1.TooOld()
181                         case nncp.MagicNNCPEv2.B:
182                                 err = nncp.MagicNNCPEv2.TooOld()
183                         case nncp.MagicNNCPEv3.B:
184                                 err = nncp.MagicNNCPEv3.TooOld()
185                         case nncp.MagicNNCPEv4.B:
186                                 err = nncp.MagicNNCPEv4.TooOld()
187                         case nncp.MagicNNCPEv5.B:
188                                 err = nncp.MagicNNCPEv5.TooOld()
189                         case nncp.MagicNNCPEv6.B:
190                         default:
191                                 err = errors.New("is not an encrypted packet")
192                         }
193                         if err != nil {
194                                 fd.Close()
195                                 ctx.LogE("ack-read-magic", les, err, logMsg)
196                                 isBad = true
197                                 continue
198                         }
199                         if _, err = fd.Seek(0, io.SeekStart); err != nil {
200                                 fd.Close()
201                                 ctx.LogE("ack-read-seek", les, err, func(les nncp.LEs) string {
202                                         return logMsg(les) + ": seeking"
203                                 })
204                                 isBad = true
205                                 continue
206                         }
207                         pipeR, pipeW := io.Pipe()
208                         go nncp.PktEncRead(
209                                 ctx.Self,
210                                 ctx.Neigh,
211                                 bufio.NewReaderSize(fd, nncp.MTHBlockSize),
212                                 pipeW, true, nil,
213                         )
214                         var pkt nncp.Pkt
215                         _, err = xdr.Unmarshal(pipeR, &pkt)
216                         fd.Close()
217                         pipeW.Close()
218                         if err != nil {
219                                 ctx.LogE("ack-read-unmarshal", les, err, func(les nncp.LEs) string {
220                                         return logMsg(les) + ": unmarshal"
221                                 })
222                                 isBad = true
223                                 continue
224                         }
225                         if pkt.Type == nncp.PktTypeACK {
226                                 ctx.LogI("ack-read-if-ack", les, func(les nncp.LEs) string {
227                                         return logMsg(les) + ": it is ACK, skipping"
228                                 })
229                                 continue
230                         }
231                         newPktName, err := ctx.TxACK(node, nice, pktName, minSize)
232                         if err != nil {
233                                 log.Fatalln(err)
234                         }
235                         acksCreated.WriteString(node.Id.String() + "/" + newPktName + "\n")
236                 }
237         }
238         if isBad {
239                 os.Exit(1)
240         }
241 }