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 // Exchange NNCP inbound and outbounds packets with external directory.
32 "github.com/dustin/go-humanize"
33 "go.cypherpunks.ru/nncp/v8"
37 fmt.Fprint(os.Stderr, nncp.UsageHeader())
38 fmt.Fprint(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
39 fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
45 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
46 nodeRaw = flag.String("node", "", "Process only that node")
47 niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
48 rxOnly = flag.Bool("rx", false, "Only receive packets")
49 txOnly = flag.Bool("tx", false, "Only transfer packets")
50 mkdir = flag.Bool("mkdir", false, "Create necessary outbound directories")
51 keep = flag.Bool("keep", false, "Do not delete transferred packets")
52 spoolPath = flag.String("spool", "", "Override path to spool")
53 logPath = flag.String("log", "", "Override path to logfile")
54 quiet = flag.Bool("quiet", false, "Print only errors")
55 showPrgrs = flag.Bool("progress", false, "Force progress showing")
56 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
57 debug = flag.Bool("debug", false, "Print debug messages")
58 version = flag.Bool("version", false, "Print version information")
59 warranty = flag.Bool("warranty", false, "Print warranty information")
61 log.SetFlags(log.Lshortfile)
65 fmt.Println(nncp.Warranty)
69 fmt.Println(nncp.VersionGet())
76 nice, err := nncp.NicenessParse(*niceRaw)
80 if *rxOnly && *txOnly {
81 log.Fatalln("-rx and -tx can not be set simultaneously")
84 ctx, err := nncp.CtxFromCmdline(
94 log.Fatalln("Error during initialization:", err)
97 var nodeOnly *nncp.Node
99 nodeOnly, err = ctx.FindNode(*nodeRaw)
101 log.Fatalln("Invalid -node specified:", err)
106 selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
109 var entries []os.DirEntry
111 var logMsg func(les nncp.LEs) string
116 {K: "XX", V: string(nncp.TRx)},
117 {K: "Dir", V: selfPath},
119 logMsg = func(les nncp.LEs) string {
120 return "Packet transfer, received from self"
122 ctx.LogD("xfer-self", les, logMsg)
123 if _, err = os.Stat(selfPath); err != nil {
124 if errors.Is(err, fs.ErrNotExist) {
125 ctx.LogD("xfer-self-no-dir", les, func(les nncp.LEs) string {
126 return logMsg(les) + ": no directory"
130 ctx.LogE("xfer-self-stat", les, err, func(les nncp.LEs) string {
131 return logMsg(les) + ": stating"
136 dir, err = os.Open(selfPath)
138 ctx.LogE("xfer-self-open", les, err, func(les nncp.LEs) string {
139 return logMsg(les) + ": opening"
144 entries, err = dir.ReadDir(0)
147 ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
148 return logMsg(les) + ": reading"
153 for _, entry := range entries {
157 nodeId, err := nncp.NodeIdFromString(entry.Name())
158 les := append(les, nncp.LE{K: "Node", V: entry.Name()})
159 logMsg := func(les nncp.LEs) string {
160 return "Packet transfer, received from " + ctx.NodeName(nodeId)
163 ctx.LogD("xfer-rx-not-node", les, func(les nncp.LEs) string {
164 return logMsg(les) + ": is not NodeId"
168 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
169 ctx.LogD("xfer-rx-skip", les, func(les nncp.LEs) string {
170 return logMsg(les) + ": skipping"
174 if _, known := ctx.Neigh[*nodeId]; !known {
175 ctx.LogD("xfer-rx-unknown", les, func(les nncp.LEs) string {
176 return logMsg(les) + ": unknown"
180 dir, err = os.Open(filepath.Join(selfPath, entry.Name()))
182 ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
183 return logMsg(les) + ": opening"
188 fisInt, err := dir.Readdir(0)
191 ctx.LogE("xfer-rx-read", les, err, func(les nncp.LEs) string {
192 return logMsg(les) + ": reading"
197 for _, fiInt := range fisInt {
201 // Check that it is valid Base32 encoding
202 if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
205 filename := filepath.Join(dir.Name(), fiInt.Name())
206 les := append(les, nncp.LE{K: "File", V: filename})
207 logMsg := func(les nncp.LEs) string {
209 "Packet transfer, received from %s: %s",
210 ctx.NodeName(nodeId), filename,
213 if _, err = os.Stat(filepath.Join(
219 )); err == nil || !errors.Is(err, fs.ErrNotExist) {
220 ctx.LogI("xfer-rx-seen", les, func(les nncp.LEs) string {
221 return logMsg(les) + ": packet already seen"
224 if err = os.Remove(filename); err != nil {
225 ctx.LogE("xfer-rx-remove", les, err, logMsg)
231 fd, err := os.Open(filename)
233 ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
234 return logMsg(les) + ": opening"
239 pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
241 switch pktEnc.Magic {
242 case nncp.MagicNNCPEv1.B:
243 err = nncp.MagicNNCPEv1.TooOld()
244 case nncp.MagicNNCPEv2.B:
245 err = nncp.MagicNNCPEv2.TooOld()
246 case nncp.MagicNNCPEv3.B:
247 err = nncp.MagicNNCPEv3.TooOld()
248 case nncp.MagicNNCPEv4.B:
249 err = nncp.MagicNNCPEv4.TooOld()
250 case nncp.MagicNNCPEv5.B:
251 err = nncp.MagicNNCPEv5.TooOld()
252 case nncp.MagicNNCPEv6.B:
254 err = errors.New("is not an encrypted packet")
259 "xfer-rx-not-packet",
260 append(les, nncp.LE{K: "Err", V: err}),
261 func(les nncp.LEs) string {
262 return logMsg(les) + ": not valid packet: " + err.Error()
268 if pktEnc.Nice > nice {
269 ctx.LogD("xfer-rx-too-nice", les, func(les nncp.LEs) string {
270 return logMsg(les) + ": too nice"
275 les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
276 logMsg = func(les nncp.LEs) string {
278 "Packet transfer, received from %s: %s (%s)",
279 ctx.NodeName(nodeId), filename,
280 humanize.IBytes(uint64(fiInt.Size())),
283 if !ctx.IsEnoughSpace(fiInt.Size()) {
284 ctx.LogE("xfer-rx", les, errors.New("is not enough space"), logMsg)
288 if _, err = fd.Seek(0, io.SeekStart); err != nil {
291 tmp, err := ctx.NewTmpFileWHash()
298 w, bufio.NewReaderSize(fd, nncp.MTHBlockSize), fiInt.Size(),
304 ctx.LogE("xfer-rx", les, err, logMsg)
305 w.CloseWithError(err)
308 _, err = nncp.CopyProgressed(
312 nncp.LE{K: "Pkt", V: filename},
313 nncp.LE{K: "FullSize", V: fiInt.Size()},
319 ctx.LogE("xfer-rx", les, err, logMsg)
324 if err = tmp.W.Flush(); err != nil {
325 ctx.LogE("xfer-rx", les, err, logMsg)
330 if tmp.Checksum() != fiInt.Name() {
331 ctx.LogE("xfer-rx", les, errors.New("checksum mismatch"), logMsg)
336 if err = tmp.Commit(filepath.Join(
343 ctx.LogI("xfer-rx", les, logMsg)
345 if err = os.Remove(filename); err != nil {
346 ctx.LogE("xfer-rx-remove", les, err, logMsg)
351 ctx.HdrWrite(pktEncRaw, filepath.Join(
368 for nodeId := range ctx.Neigh {
369 les := nncp.LEs{{K: "XX", V: string(nncp.TTx)}, {K: "Node", V: nodeId}}
370 logMsg := func(les nncp.LEs) string {
371 return "Packet transfer, sent to " + ctx.NodeName(&nodeId)
373 if nodeOnly != nil && nodeId != *nodeOnly.Id {
374 ctx.LogD("xfer-tx-skip", les, func(les nncp.LEs) string {
375 return logMsg(les) + ": skipping"
379 dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
383 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
384 les = append(les, nncp.LE{K: "Dir", V: nodePath})
385 logMsg = func(les nncp.LEs) string {
387 "Packet transfer, sent to %s: directory %s",
388 ctx.NodeName(&nodeId), nodePath,
391 _, err = os.Stat(nodePath)
393 if errors.Is(err, fs.ErrNotExist) {
394 ctx.LogD("xfer-tx-not-exist", les, func(les nncp.LEs) string {
395 return logMsg(les) + ": does not exist"
398 ctx.UnlockDir(dirLock)
401 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
402 ctx.UnlockDir(dirLock)
403 ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
408 ctx.UnlockDir(dirLock)
409 ctx.LogE("xfer-tx", les, err, logMsg)
414 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
415 les[len(les)-1].V = dstPath
416 logMsg = func(les nncp.LEs) string {
418 "Packet transfer, sent to %s: directory %s",
419 ctx.NodeName(&nodeId), dstPath,
422 _, err = os.Stat(dstPath)
424 if errors.Is(err, fs.ErrNotExist) {
425 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
426 ctx.UnlockDir(dirLock)
427 ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
432 ctx.UnlockDir(dirLock)
433 ctx.LogE("xfer-tx", les, err, logMsg)
438 les = les[:len(les)-1]
439 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
440 pktName := filepath.Base(job.Path)
441 les := append(les, nncp.LE{K: "Pkt", V: pktName})
442 logMsg = func(les nncp.LEs) string {
444 "Packet transfer, sent to %s: %s",
445 ctx.NodeName(&nodeId), pktName,
448 if job.PktEnc.Nice > nice {
449 ctx.LogD("xfer-tx-too-nice", les, func(les nncp.LEs) string {
450 return logMsg(les) + ": too nice"
454 if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !errors.Is(err, fs.ErrNotExist) {
455 ctx.LogD("xfer-tx-exists", les, func(les nncp.LEs) string {
456 return logMsg(les) + ": already exists"
460 tmp, err := nncp.TempFile(dstPath, "xfer")
462 ctx.LogE("xfer-tx-mktemp", les, err, func(les nncp.LEs) string {
463 return logMsg(les) + ": mktemp"
468 les = append(les, nncp.LE{K: "Tmp", V: tmp.Name()})
469 ctx.LogD("xfer-tx-tmp-create", les, func(les nncp.LEs) string {
470 return fmt.Sprintf("%s: temporary %s created", logMsg(les), tmp.Name())
472 fd, err := os.Open(job.Path)
474 ctx.LogE("xfer-tx-open", les, err, func(les nncp.LEs) string {
475 return logMsg(les) + ": opening"
481 bufW := bufio.NewWriter(tmp)
482 copied, err := nncp.CopyProgressed(
483 bufW, bufio.NewReaderSize(fd, nncp.MTHBlockSize), "Tx",
484 append(les, nncp.LE{K: "FullSize", V: job.Size}),
489 ctx.LogE("xfer-tx-copy", les, err, func(les nncp.LEs) string {
490 return logMsg(les) + ": copying"
496 if err = bufW.Flush(); err != nil {
498 ctx.LogE("xfer-tx-flush", les, err, func(les nncp.LEs) string {
499 return logMsg(les) + ": flushing"
505 if err = tmp.Sync(); err != nil {
507 ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string {
508 return logMsg(les) + ": syncing"
514 if err = tmp.Close(); err != nil {
515 ctx.LogE("xfer-tx-close", les, err, func(les nncp.LEs) string {
516 return logMsg(les) + ": closing"
519 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
520 ctx.LogE("xfer-tx-rename", les, err, func(les nncp.LEs) string {
521 return logMsg(les) + ": renaming"
526 if err = nncp.DirSync(dstPath); err != nil {
527 ctx.LogE("xfer-tx-dirsync", les, err, func(les nncp.LEs) string {
528 return logMsg(les) + ": dirsyncing"
533 os.Remove(filepath.Join(dstPath, pktName+".part"))
534 les = les[:len(les)-1]
537 append(les, nncp.LE{K: "Size", V: copied}),
538 func(les nncp.LEs) string {
540 "%s (%s)", logMsg(les), humanize.IBytes(uint64(copied)),
545 if err = os.Remove(job.Path); err != nil {
546 ctx.LogE("xfer-tx-remove", les, err, func(les nncp.LEs) string {
547 return logMsg(les) + ": removing"
550 } else if ctx.HdrUsage {
551 os.Remove(nncp.JobPath2Hdr(job.Path))
555 ctx.UnlockDir(dirLock)