2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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.
31 "github.com/dustin/go-humanize"
32 "go.cypherpunks.ru/nncp/v6"
36 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
37 fmt.Fprintf(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
38 fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
44 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
45 nodeRaw = flag.String("node", "", "Process only that node")
46 niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
47 rxOnly = flag.Bool("rx", false, "Only receive packets")
48 txOnly = flag.Bool("tx", false, "Only transfer packets")
49 mkdir = flag.Bool("mkdir", false, "Create necessary outbound directories")
50 keep = flag.Bool("keep", false, "Do not delete transferred packets")
51 spoolPath = flag.String("spool", "", "Override path to spool")
52 logPath = flag.String("log", "", "Override path to logfile")
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")
63 fmt.Println(nncp.Warranty)
67 fmt.Println(nncp.VersionGet())
74 nice, err := nncp.NicenessParse(*niceRaw)
78 if *rxOnly && *txOnly {
79 log.Fatalln("-rx and -tx can not be set simultaneously")
82 ctx, err := nncp.CtxFromCmdline(
92 log.Fatalln("Error during initialization:", err)
95 var nodeOnly *nncp.Node
97 nodeOnly, err = ctx.FindNode(*nodeRaw)
99 log.Fatalln("Invalid -node specified:", err)
104 selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
107 var fis []os.FileInfo
109 var logMsg func(les nncp.LEs) string
114 {K: "XX", V: string(nncp.TRx)},
115 {K: "Dir", V: selfPath},
117 logMsg = func(les nncp.LEs) string {
118 return "Packet transfer, received from self"
120 ctx.LogD("xfer-self", les, logMsg)
121 if _, err = os.Stat(selfPath); err != nil {
122 if os.IsNotExist(err) {
123 ctx.LogD("xfer-self-no-dir", les, func(les nncp.LEs) string {
124 return logMsg(les) + ": no directory"
128 ctx.LogE("xfer-self-stat", les, err, func(les nncp.LEs) string {
129 return logMsg(les) + ": stating"
134 dir, err = os.Open(selfPath)
136 ctx.LogE("xfer-self-open", les, err, func(les nncp.LEs) string {
137 return logMsg(les) + ": opening"
142 fis, err = dir.Readdir(0)
143 dir.Close() // #nosec G104
145 ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
146 return logMsg(les) + ": reading"
151 for _, fi := range fis {
155 nodeId, err := nncp.NodeIdFromString(fi.Name())
156 les := append(les, nncp.LE{K: "Node", V: fi.Name()})
157 logMsg := func(les nncp.LEs) string {
158 return "Packet transfer, received from " + ctx.NodeName(nodeId)
161 ctx.LogD("xfer-rx-not-node", les, func(les nncp.LEs) string {
162 return logMsg(les) + ": is not NodeId"
166 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
167 ctx.LogD("xfer-rx-skip", les, func(les nncp.LEs) string {
168 return logMsg(les) + ": skipping"
172 if _, known := ctx.Neigh[*nodeId]; !known {
173 ctx.LogD("xfer-rx-unknown", les, func(les nncp.LEs) string {
174 return logMsg(les) + ": unknown"
178 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
180 ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
181 return logMsg(les) + ": opening"
186 fisInt, err := dir.Readdir(0)
187 dir.Close() // #nosec G104
189 ctx.LogE("xfer-rx-read", les, err, func(les nncp.LEs) string {
190 return logMsg(les) + ": reading"
195 for _, fiInt := range fisInt {
199 // Check that it is valid Base32 encoding
200 if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
203 filename := filepath.Join(dir.Name(), fiInt.Name())
204 les := append(les, nncp.LE{K: "File", V: filename})
205 logMsg := func(les nncp.LEs) string {
207 "Packet transfer, received from %s: %s",
208 ctx.NodeName(nodeId), filename,
211 fd, err := os.Open(filename)
213 ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
214 return logMsg(les) + ": opening"
219 pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
220 if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
221 ctx.LogD("xfer-rx-not-packet", les, func(les nncp.LEs) string {
222 return logMsg(les) + ": is not a packet"
224 fd.Close() // #nosec G104
227 if pktEnc.Nice > nice {
228 ctx.LogD("xfer-rx-too-nice", les, func(les nncp.LEs) string {
229 return logMsg(les) + ": too nice"
231 fd.Close() // #nosec G104
234 les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
235 logMsg = func(les nncp.LEs) string {
237 "Packet transfer, received from %s: %s (%s)",
238 ctx.NodeName(nodeId), filename,
239 humanize.IBytes(uint64(fiInt.Size())),
242 if !ctx.IsEnoughSpace(fiInt.Size()) {
243 ctx.LogE("xfer-rx", les, errors.New("is not enough space"), logMsg)
244 fd.Close() // #nosec G104
247 if _, err = fd.Seek(0, 0); err != nil {
250 tmp, err := ctx.NewTmpFileWHash()
256 _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
261 ctx.LogE("xfer-rx", les, err, logMsg)
262 w.CloseWithError(err) // #nosec G104
265 if _, err = nncp.CopyProgressed(
269 nncp.LE{K: "Pkt", V: filename},
270 nncp.LE{K: "FullSize", V: fiInt.Size()},
274 ctx.LogE("xfer-rx", les, err, logMsg)
277 fd.Close() // #nosec G104
282 if err = tmp.Commit(filepath.Join(
289 ctx.LogI("xfer-rx", les, logMsg)
291 if err = os.Remove(filename); err != nil {
292 ctx.LogE("xfer-rx-remove", les, err, logMsg)
297 ctx.HdrWrite(pktEncRaw, filepath.Join(
314 for nodeId := range ctx.Neigh {
315 les := nncp.LEs{{K: "XX", V: string(nncp.TTx)}, {K: "Node", V: nodeId}}
316 logMsg := func(les nncp.LEs) string {
317 return "Packet transfer, sent to " + ctx.NodeName(&nodeId)
319 if nodeOnly != nil && nodeId != *nodeOnly.Id {
320 ctx.LogD("xfer-tx-skip", les, func(les nncp.LEs) string {
321 return logMsg(les) + ": skipping"
325 dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
329 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
330 les = append(les, nncp.LE{K: "Dir", V: nodePath})
331 logMsg = func(les nncp.LEs) string {
333 "Packet transfer, sent to %s: directory %s",
334 ctx.NodeName(&nodeId), nodePath,
337 _, err = os.Stat(nodePath)
339 if os.IsNotExist(err) {
340 ctx.LogD("xfer-tx-not-exist", les, func(les nncp.LEs) string {
341 return logMsg(les) + ": does not exist"
344 ctx.UnlockDir(dirLock)
347 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
348 ctx.UnlockDir(dirLock)
349 ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
354 ctx.UnlockDir(dirLock)
355 ctx.LogE("xfer-tx", les, err, logMsg)
360 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
361 les[len(les)-1].V = dstPath
362 logMsg = func(les nncp.LEs) string {
364 "Packet transfer, sent to %s: directory %s",
365 ctx.NodeName(&nodeId), dstPath,
368 _, err = os.Stat(dstPath)
370 if os.IsNotExist(err) {
371 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
372 ctx.UnlockDir(dirLock)
373 ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
378 ctx.UnlockDir(dirLock)
379 ctx.LogE("xfer-tx", les, err, logMsg)
384 les = les[:len(les)-1]
385 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
386 pktName := filepath.Base(job.Path)
387 les := append(les, nncp.LE{K: "Pkt", V: pktName})
388 logMsg = func(les nncp.LEs) string {
390 "Packet transfer, sent to %s: %s",
391 ctx.NodeName(&nodeId), pktName,
394 if job.PktEnc.Nice > nice {
395 ctx.LogD("xfer-tx-too-nice", les, func(les nncp.LEs) string {
396 return logMsg(les) + ": too nice"
400 if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
401 ctx.LogD("xfer-tx-exists", les, func(les nncp.LEs) string {
402 return logMsg(les) + ": already exists"
406 if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
407 ctx.LogD("xfer-tx-seen", les, func(les nncp.LEs) string {
408 return logMsg(les) + ": already seen"
412 tmp, err := nncp.TempFile(dstPath, "xfer")
414 ctx.LogE("xfer-tx-mktemp", les, err, func(les nncp.LEs) string {
415 return logMsg(les) + ": mktemp"
420 les = append(les, nncp.LE{K: "Tmp", V: tmp.Name()})
421 ctx.LogD("xfer-tx-tmp-create", les, func(les nncp.LEs) string {
422 return fmt.Sprintf("%s: temporary %s created", logMsg(les), tmp.Name())
424 fd, err := os.Open(job.Path)
426 ctx.LogE("xfer-tx-open", les, err, func(les nncp.LEs) string {
427 return logMsg(les) + ": opening"
429 tmp.Close() // #nosec G104
433 bufW := bufio.NewWriter(tmp)
434 copied, err := nncp.CopyProgressed(
435 bufW, bufio.NewReader(fd), "Tx",
436 append(les, nncp.LE{K: "FullSize", V: job.Size}),
439 fd.Close() // #nosec G104
441 ctx.LogE("xfer-tx-copy", les, err, func(les nncp.LEs) string {
442 return logMsg(les) + ": copying"
444 tmp.Close() // #nosec G104
448 if err = bufW.Flush(); err != nil {
449 tmp.Close() // #nosec G104
450 ctx.LogE("xfer-tx-flush", les, err, func(les nncp.LEs) string {
451 return logMsg(les) + ": flushing"
456 if err = tmp.Sync(); err != nil {
457 tmp.Close() // #nosec G104
458 ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string {
459 return logMsg(les) + ": syncing"
464 if err = tmp.Close(); err != nil {
465 ctx.LogE("xfer-tx-close", les, err, func(les nncp.LEs) string {
466 return logMsg(les) + ": closing"
469 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
470 ctx.LogE("xfer-tx-rename", les, err, func(les nncp.LEs) string {
471 return logMsg(les) + ": renaming"
476 if err = nncp.DirSync(dstPath); err != nil {
477 ctx.LogE("xfer-tx-dirsync", les, err, func(les nncp.LEs) string {
478 return logMsg(les) + ": dirsyncing"
483 os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
484 les = les[:len(les)-1]
487 append(les, nncp.LE{K: "Size", V: copied}),
488 func(les nncp.LEs) string {
490 "%s (%s)", logMsg(les), humanize.IBytes(uint64(copied)),
495 if err = os.Remove(job.Path); err != nil {
496 ctx.LogE("xfer-tx-remove", les, err, func(les nncp.LEs) string {
497 return logMsg(les) + ": removing"
500 } else if ctx.HdrUsage {
501 os.Remove(job.Path + nncp.HdrSuffix)
505 ctx.UnlockDir(dirLock)