2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2020 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 xdr "github.com/davecgh/go-xdr/xdr2"
32 "go.cypherpunks.ru/nncp/v5"
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
112 sds["xx"] = string(nncp.TRx)
113 sds["dir"] = selfPath
114 ctx.LogD("nncp-xfer", sds, "self")
115 if _, err = os.Stat(selfPath); err != nil {
116 if os.IsNotExist(err) {
117 ctx.LogD("nncp-xfer", sds, "no dir")
120 ctx.LogE("nncp-xfer", sds, err, "stat")
124 dir, err = os.Open(selfPath)
126 ctx.LogE("nncp-xfer", sds, err, "open")
130 fis, err = dir.Readdir(0)
131 dir.Close() // #nosec G104
133 ctx.LogE("nncp-xfer", sds, err, "read")
137 for _, fi := range fis {
141 nodeId, err := nncp.NodeIdFromString(fi.Name())
142 sds["node"] = fi.Name()
144 ctx.LogD("nncp-xfer", sds, "is not NodeId")
147 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
148 ctx.LogD("nncp-xfer", sds, "skip")
151 if _, known := ctx.Neigh[*nodeId]; !known {
152 ctx.LogD("nncp-xfer", sds, "unknown")
155 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
157 ctx.LogE("nncp-xfer", sds, err, "open")
161 fisInt, err := dir.Readdir(0)
162 dir.Close() // #nosec G104
164 ctx.LogE("nncp-xfer", sds, err, "read")
168 for _, fiInt := range fisInt {
172 // Check that it is valid Base32 encoding
173 if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
176 filename := filepath.Join(dir.Name(), fiInt.Name())
177 sds["file"] = filename
179 fd, err := os.Open(filename)
181 ctx.LogE("nncp-xfer", sds, err, "open")
185 var pktEnc nncp.PktEnc
186 _, err = xdr.Unmarshal(fd, &pktEnc)
187 if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
188 ctx.LogD("nncp-xfer", sds, "is not a packet")
189 fd.Close() // #nosec G104
192 if pktEnc.Nice > nice {
193 ctx.LogD("nncp-xfer", sds, "too nice")
194 fd.Close() // #nosec G104
197 sds["size"] = fiInt.Size()
198 if !ctx.IsEnoughSpace(fiInt.Size()) {
199 ctx.LogE("nncp-xfer", sds, errors.New("is not enough space"), "")
200 fd.Close() // #nosec G104
203 if _, err = fd.Seek(0, 0); err != nil {
206 tmp, err := ctx.NewTmpFileWHash()
212 _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
217 ctx.LogE("nncp-xfer", sds, err, "copy")
218 w.CloseWithError(err) // #nosec G104
221 if _, err = nncp.CopyProgressed(
223 nncp.SdsAdd(sds, nncp.SDS{
225 "fullsize": sds["size"],
229 ctx.LogE("nncp-xfer", sds, err, "copy")
232 fd.Close() // #nosec G104
237 if err = tmp.Commit(filepath.Join(
244 ctx.LogI("nncp-xfer", sds, "")
246 if err = os.Remove(filename); err != nil {
247 ctx.LogE("nncp-xfer", sds, err, "remove")
261 sds["xx"] = string(nncp.TTx)
262 for nodeId, _ := range ctx.Neigh {
264 if nodeOnly != nil && nodeId != *nodeOnly.Id {
265 ctx.LogD("nncp-xfer", sds, "skip")
268 dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
272 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
273 sds["dir"] = nodePath
274 _, err = os.Stat(nodePath)
276 if os.IsNotExist(err) {
277 ctx.LogD("nncp-xfer", sds, "does not exist")
279 ctx.UnlockDir(dirLock)
282 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
283 ctx.UnlockDir(dirLock)
284 ctx.LogE("nncp-xfer", sds, err, "mkdir")
289 ctx.UnlockDir(dirLock)
290 ctx.LogE("nncp-xfer", sds, err, "stat")
295 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
297 _, err = os.Stat(dstPath)
299 if os.IsNotExist(err) {
300 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
301 ctx.UnlockDir(dirLock)
302 ctx.LogE("nncp-xfer", sds, err, "mkdir")
307 ctx.UnlockDir(dirLock)
308 ctx.LogE("nncp-xfer", sds, err, "stat")
314 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
315 pktName := filepath.Base(job.Fd.Name())
317 if job.PktEnc.Nice > nice {
318 ctx.LogD("nncp-xfer", sds, "too nice")
319 job.Fd.Close() // #nosec G104
322 if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
323 ctx.LogD("nncp-xfer", sds, "already exists")
324 job.Fd.Close() // #nosec G104
327 if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
328 ctx.LogD("nncp-xfer", sds, "already exists")
329 job.Fd.Close() // #nosec G104
332 tmp, err := nncp.TempFile(dstPath, "xfer")
334 ctx.LogE("nncp-xfer", sds, err, "mktemp")
335 job.Fd.Close() // #nosec G104
339 sds["tmp"] = tmp.Name()
340 ctx.LogD("nncp-xfer", sds, "created")
341 bufW := bufio.NewWriter(tmp)
342 copied, err := nncp.CopyProgressed(
343 bufW, bufio.NewReader(job.Fd), "Tx",
344 nncp.SdsAdd(sds, nncp.SDS{"fullsize": job.Size}),
347 job.Fd.Close() // #nosec G104
349 ctx.LogE("nncp-xfer", sds, err, "copy")
350 tmp.Close() // #nosec G104
354 if err = bufW.Flush(); err != nil {
355 tmp.Close() // #nosec G104
356 ctx.LogE("nncp-xfer", sds, err, "flush")
360 if err = tmp.Sync(); err != nil {
361 tmp.Close() // #nosec G104
362 ctx.LogE("nncp-xfer", sds, err, "sync")
366 if err = tmp.Close(); err != nil {
367 ctx.LogE("nncp-xfer", sds, err, "sync")
369 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
370 ctx.LogE("nncp-xfer", sds, err, "rename")
374 if err = nncp.DirSync(dstPath); err != nil {
375 ctx.LogE("nncp-xfer", sds, err, "sync")
379 os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
381 ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"size": copied}), "")
383 if err = os.Remove(job.Fd.Name()); err != nil {
384 ctx.LogE("nncp-xfer", sds, err, "remove")
389 ctx.UnlockDir(dirLock)