2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 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/davecgh/go-xdr/xdr2"
32 "go.cypherpunks.ru/nncp/v4"
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 debug = flag.Bool("debug", false, "Print debug messages")
55 version = flag.Bool("version", false, "Print version information")
56 warranty = flag.Bool("warranty", false, "Print warranty information")
61 fmt.Println(nncp.Warranty)
65 fmt.Println(nncp.VersionGet())
72 nice, err := nncp.NicenessParse(*niceRaw)
76 if *rxOnly && *txOnly {
77 log.Fatalln("-rx and -tx can not be set simultaneously")
80 ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
82 log.Fatalln("Error during initialization:", err)
85 var nodeOnly *nncp.Node
87 nodeOnly, err = ctx.FindNode(*nodeRaw)
89 log.Fatalln("Invalid -node specified:", err)
93 selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
101 sds["xx"] = string(nncp.TRx)
102 sds["dir"] = selfPath
103 ctx.LogD("nncp-xfer", sds, "self")
104 if _, err = os.Stat(selfPath); err != nil {
105 if os.IsNotExist(err) {
106 ctx.LogD("nncp-xfer", sds, "no dir")
109 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
113 dir, err = os.Open(selfPath)
115 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
119 fis, err = dir.Readdir(0)
122 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
126 for _, fi := range fis {
130 nodeId, err := nncp.NodeIdFromString(fi.Name())
131 sds["node"] = fi.Name()
133 ctx.LogD("nncp-xfer", sds, "is not NodeId")
136 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
137 ctx.LogD("nncp-xfer", sds, "skip")
140 if _, known := ctx.Neigh[*nodeId]; !known {
141 ctx.LogD("nncp-xfer", sds, "unknown")
144 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
146 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
150 fisInt, err := dir.Readdir(0)
153 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
157 for _, fiInt := range fisInt {
161 filename := filepath.Join(dir.Name(), fiInt.Name())
162 sds["file"] = filename
164 fd, err := os.Open(filename)
166 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
170 var pktEnc nncp.PktEnc
171 _, err = xdr.Unmarshal(fd, &pktEnc)
172 if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
173 ctx.LogD("nncp-xfer", sds, "is not a packet")
177 if pktEnc.Nice > nice {
178 ctx.LogD("nncp-xfer", sds, "too nice")
182 sds["size"] = strconv.FormatInt(fiInt.Size(), 10)
183 if !ctx.IsEnoughSpace(fiInt.Size()) {
184 ctx.LogE("nncp-xfer", sds, "is not enough space")
189 tmp, err := ctx.NewTmpFileWHash()
193 if _, err = io.CopyN(tmp.W, bufio.NewReader(fd), fiInt.Size()); err != nil {
194 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
201 if err = tmp.Commit(filepath.Join(
208 ctx.LogI("nncp-xfer", sds, "")
210 if err = os.Remove(filename); err != nil {
211 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
225 sds["xx"] = string(nncp.TTx)
226 for nodeId, _ := range ctx.Neigh {
228 if nodeOnly != nil && nodeId != *nodeOnly.Id {
229 ctx.LogD("nncp-xfer", sds, "skip")
232 dirLock, err := ctx.LockDir(&nodeId, nncp.TTx)
236 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
237 sds["dir"] = nodePath
238 _, err = os.Stat(nodePath)
240 if os.IsNotExist(err) {
241 ctx.LogD("nncp-xfer", sds, "does not exist")
243 ctx.UnlockDir(dirLock)
246 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
247 ctx.UnlockDir(dirLock)
248 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
253 ctx.UnlockDir(dirLock)
254 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
259 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
261 _, err = os.Stat(dstPath)
263 if os.IsNotExist(err) {
264 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
265 ctx.UnlockDir(dirLock)
266 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
271 ctx.UnlockDir(dirLock)
272 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
278 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
279 pktName := filepath.Base(job.Fd.Name())
281 if job.PktEnc.Nice > nice {
282 ctx.LogD("nncp-xfer", sds, "too nice")
286 if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
287 ctx.LogD("nncp-xfer", sds, "already exists")
291 if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
292 ctx.LogD("nncp-xfer", sds, "already exists")
296 tmp, err := nncp.TempFile(dstPath, "xfer")
298 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mktemp")
303 sds["tmp"] = tmp.Name()
304 ctx.LogD("nncp-xfer", sds, "created")
305 bufW := bufio.NewWriter(tmp)
306 copied, err := io.Copy(bufW, bufio.NewReader(job.Fd))
309 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
314 if err = bufW.Flush(); err != nil {
316 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "flush")
320 if err = tmp.Sync(); err != nil {
322 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "sync")
327 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
328 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename")
332 os.Remove(filepath.Join(dstPath, pktName+".part"))
334 ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
335 "size": strconv.FormatInt(copied, 10),
338 if err = os.Remove(job.Fd.Name()); err != nil {
339 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
344 ctx.UnlockDir(dirLock)