2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
18 // Send packet receipt acknowledgement via NNCP.
32 xdr "github.com/davecgh/go-xdr/xdr2"
33 "go.cypherpunks.ru/nncp/v8"
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:")
47 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
48 niceRaw = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFreq),
49 "Outbound packet niceness")
50 minSizeRaw = flag.Int64("minsize", -1, "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")
64 log.SetFlags(log.Lshortfile)
68 fmt.Println(nncp.Warranty)
72 fmt.Println(nncp.VersionGet())
75 nice, err := nncp.NicenessParse(*niceRaw)
80 ctx, err := nncp.CtxFromCmdline(
90 log.Fatalln("Error during initialization:", err)
93 log.Fatalln("Config lacks private keys")
98 var nodes []*nncp.Node
100 for _, nodeRaw := range strings.Split(*nodesRaw, ",") {
101 node, err := ctx.FindNode(nodeRaw)
103 log.Fatalln("Invalid -node specified:", err)
105 nodes = append(nodes, node)
113 for _, node := range ctx.Neigh {
114 nodes = append(nodes, node)
116 } else if len(nodes) == 0 {
121 acksCreated := os.NewFile(uintptr(4), "ACKsCreated")
122 if acksCreated == nil {
123 log.Fatalln("can not open FD:4")
131 nncp.ViaOverride(*viaOverride, ctx, nodes[0])
135 minSize = nodes[0].ACKMinSize
136 } else if *minSizeRaw > 0 {
137 minSize = *minSizeRaw * 1024
140 pktName, err := ctx.TxACK(nodes[0], nice, *pktRaw, minSize)
144 acksCreated.WriteString(nodes[0].Id.String() + "/" + pktName + "\n")
149 for _, node := range nodes {
152 minSize = node.ACKMinSize
153 } else if *minSizeRaw > 0 {
154 minSize = *minSizeRaw * 1024
156 for job := range ctx.Jobs(node.Id, nncp.TRx) {
157 pktName := filepath.Base(job.Path)
158 sender := ctx.Neigh[*job.PktEnc.Sender]
160 {K: "Node", V: job.PktEnc.Sender},
161 {K: "Pkt", V: pktName},
163 logMsg := func(les nncp.LEs) string {
166 ctx.NodeName(job.PktEnc.Sender), pktName,
170 err := errors.New("unknown node")
171 ctx.LogE("ack-read", les, err, logMsg)
175 fd, err := os.Open(job.Path)
177 ctx.LogE("ack-read-open", les, err, func(les nncp.LEs) string {
178 return logMsg(les) + ": opening" + job.Path
183 pktEnc, _, err := ctx.HdrRead(fd)
186 ctx.LogE("ack-read-read", les, err, func(les nncp.LEs) string {
187 return logMsg(les) + ": reading" + job.Path
192 switch pktEnc.Magic {
193 case nncp.MagicNNCPEv1.B:
194 err = nncp.MagicNNCPEv1.TooOld()
195 case nncp.MagicNNCPEv2.B:
196 err = nncp.MagicNNCPEv2.TooOld()
197 case nncp.MagicNNCPEv3.B:
198 err = nncp.MagicNNCPEv3.TooOld()
199 case nncp.MagicNNCPEv4.B:
200 err = nncp.MagicNNCPEv4.TooOld()
201 case nncp.MagicNNCPEv5.B:
202 err = nncp.MagicNNCPEv5.TooOld()
203 case nncp.MagicNNCPEv6.B:
205 err = errors.New("is not an encrypted packet")
209 ctx.LogE("ack-read-magic", les, err, logMsg)
213 if _, err = fd.Seek(0, io.SeekStart); err != nil {
215 ctx.LogE("ack-read-seek", les, err, func(les nncp.LEs) string {
216 return logMsg(les) + ": seeking"
221 pipeR, pipeW := io.Pipe()
225 bufio.NewReaderSize(fd, nncp.MTHBlockSize),
229 _, err = xdr.Unmarshal(pipeR, &pkt)
233 ctx.LogE("ack-read-unmarshal", les, err, func(les nncp.LEs) string {
234 return logMsg(les) + ": unmarshal"
239 if pkt.Type == nncp.PktTypeACK {
240 ctx.LogI("ack-read-if-ack", les, func(les nncp.LEs) string {
241 return logMsg(les) + ": it is ACK, skipping"
245 newPktName, err := ctx.TxACK(node, nice, pktName, minSize)
249 acksCreated.WriteString(node.Id.String() + "/" + newPktName + "\n")