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