2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // Copy NNCP inbound and outbounds packets
34 "github.com/davecgh/go-xdr/xdr2"
38 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
39 fmt.Fprintln(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n")
40 fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
46 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
47 nodeRaw = flag.String("node", "", "Process only that node")
48 niceRaw = flag.Int("nice", 255, "Minimal required niceness")
49 rxOnly = flag.Bool("rx", false, "Only receive packets")
50 txOnly = flag.Bool("tx", false, "Only transfer packets")
51 mkdir = flag.Bool("mkdir", false, "Create necessary outbound directories")
52 keep = flag.Bool("keep", false, "Do not delete transferred packets")
53 spoolPath = flag.String("spool", "", "Override path to spool")
54 logPath = flag.String("log", "", "Override path to logfile")
55 quiet = flag.Bool("quiet", false, "Print only errors")
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 if *niceRaw < 1 || *niceRaw > 255 {
75 log.Fatalln("-nice must be between 1 and 255")
77 nice := uint8(*niceRaw)
78 if *rxOnly && *txOnly {
79 log.Fatalln("-rx and -tx can not be set simultaneously")
82 ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
84 log.Fatalln("Error during initialization:", err)
87 var nodeOnly *nncp.Node
89 nodeOnly, err = ctx.FindNode(*nodeRaw)
91 log.Fatalln("Invalid -node specified:", err)
95 selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
103 sds["xx"] = string(nncp.TRx)
104 sds["dir"] = selfPath
105 ctx.LogD("nncp-xfer", sds, "self")
106 if _, err = os.Stat(selfPath); err != nil {
107 if os.IsNotExist(err) {
108 ctx.LogD("nncp-xfer", sds, "no dir")
111 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
115 dir, err = os.Open(selfPath)
117 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
121 fis, err = dir.Readdir(0)
124 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
128 for _, fi := range fis {
132 nodeId, err := nncp.NodeIdFromString(fi.Name())
133 sds["node"] = fi.Name()
135 ctx.LogD("nncp-xfer", sds, "is not NodeId")
138 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
139 ctx.LogD("nncp-xfer", sds, "skip")
142 if _, known := ctx.Neigh[*nodeId]; !known {
143 ctx.LogD("nncp-xfer", sds, "unknown")
146 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
148 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
152 fisInt, err := dir.Readdir(0)
155 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
159 for _, fiInt := range fisInt {
163 filename := filepath.Join(dir.Name(), fiInt.Name())
164 sds["file"] = filename
165 fd, err := os.Open(filename)
167 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
171 var pktEnc nncp.PktEnc
172 _, err = xdr.Unmarshal(fd, &pktEnc)
173 if err != nil || pktEnc.Magic != nncp.MagicNNCPEv2 {
174 ctx.LogD("nncp-xfer", sds, "is not a packet")
178 if pktEnc.Nice > nice {
179 ctx.LogD("nncp-xfer", sds, "too nice")
184 tmp, err := ctx.NewTmpFileWHash()
188 copied, err := io.Copy(tmp.W, bufio.NewReader(fd))
190 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
197 if err = tmp.Commit(filepath.Join(
204 ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
205 "size": strconv.FormatInt(copied, 10),
208 if err = os.Remove(filename); err != nil {
209 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
223 sds["xx"] = string(nncp.TTx)
224 for nodeId, _ := range ctx.Neigh {
226 if nodeOnly != nil && nodeId != *nodeOnly.Id {
227 ctx.LogD("nncp-xfer", sds, "skip")
230 dirLock, err := ctx.LockDir(&nodeId, nncp.TTx)
234 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
235 sds["dir"] = nodePath
236 _, err = os.Stat(nodePath)
238 if os.IsNotExist(err) {
239 ctx.LogD("nncp-xfer", sds, "does not exist")
241 ctx.UnlockDir(dirLock)
244 if err = os.Mkdir(nodePath, os.FileMode(0700)); err != nil {
245 ctx.UnlockDir(dirLock)
246 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
251 ctx.UnlockDir(dirLock)
252 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
257 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
259 _, err = os.Stat(dstPath)
261 if os.IsNotExist(err) {
262 if err = os.Mkdir(dstPath, os.FileMode(0700)); err != nil {
263 ctx.UnlockDir(dirLock)
264 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
269 ctx.UnlockDir(dirLock)
270 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
276 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
277 pktName := filepath.Base(job.Fd.Name())
279 if job.PktEnc.Nice > nice {
280 ctx.LogD("nncp-xfer", sds, "too nice")
284 if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
285 ctx.LogD("nncp-xfer", sds, "already exists")
289 if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
290 ctx.LogD("nncp-xfer", sds, "already exists")
294 tmp, err := ioutil.TempFile(dstPath, "nncp-xfer")
296 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mktemp")
301 sds["tmp"] = tmp.Name()
302 ctx.LogD("nncp-xfer", sds, "created")
303 bufW := bufio.NewWriter(tmp)
304 copied, err := io.Copy(bufW, bufio.NewReader(job.Fd))
307 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
316 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
320 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
321 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename")
325 os.Remove(filepath.Join(dstPath, pktName+".part"))
327 ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
328 "size": strconv.FormatInt(copied, 10),
331 if err = os.Remove(job.Fd.Name()); err != nil {
332 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
337 ctx.UnlockDir(dirLock)