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:", 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 force = flag.Bool("force", false, "Force outbound directories creation")
52 keep = flag.Bool("keep", false, "Do not delete transferred packets")
53 quiet = flag.Bool("quiet", false, "Print only errors")
54 debug = flag.Bool("debug", false, "Enable debugging information")
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 if *niceRaw < 1 || *niceRaw > 255 {
73 log.Fatalln("-nice must be between 1 and 255")
75 nice := uint8(*niceRaw)
76 if *rxOnly && *txOnly {
77 log.Fatalln("-rx and -tx can not be set simultaneously")
80 cfgRaw, err := ioutil.ReadFile(*cfgPath)
82 log.Fatalln("Can not read config:", err)
84 ctx, err := nncp.CfgParse(cfgRaw)
86 log.Fatalln("Can not parse config:", err)
91 var nodeOnly *nncp.Node
93 nodeOnly, err = ctx.FindNode(*nodeRaw)
95 log.Fatalln("Invalid -node specified:", err)
99 selfPath := filepath.Join(flag.Arg(0), ctx.Self.Id.String())
102 var fis []os.FileInfo
107 sds["xx"] = string(nncp.TRx)
108 sds["dir"] = selfPath
109 ctx.LogD("nncp-xfer", sds, "self")
110 if _, err = os.Stat(selfPath); err != nil {
111 if os.IsNotExist(err) {
112 ctx.LogD("nncp-xfer", sds, "no dir")
115 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
119 dir, err = os.Open(selfPath)
121 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
125 fis, err = dir.Readdir(0)
128 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
132 for _, fi := range fis {
136 nodeId, err := nncp.NodeIdFromString(fi.Name())
137 sds["node"] = fi.Name()
139 ctx.LogD("nncp-xfer", sds, "is not NodeId")
142 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
143 ctx.LogD("nncp-xfer", sds, "skip")
146 if _, known := ctx.Neigh[*nodeId]; !known {
147 ctx.LogD("nncp-xfer", sds, "unknown")
150 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
152 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
156 fisInt, err := dir.Readdir(0)
159 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
163 for _, fiInt := range fisInt {
167 filename := filepath.Join(dir.Name(), fiInt.Name())
168 sds["file"] = filename
169 fd, err := os.Open(filename)
171 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
175 var pktEnc nncp.PktEnc
176 _, err = xdr.Unmarshal(fd, &pktEnc)
177 if err != nil || pktEnc.Magic != nncp.MagicNNCPEv1 {
178 ctx.LogD("nncp-xfer", sds, "is not a packet")
182 if pktEnc.Nice > nice {
183 ctx.LogD("nncp-xfer", sds, "too nice")
188 tmp, err := ctx.NewTmpFileWHash()
192 copied, err := io.Copy(tmp.W, bufio.NewReader(fd))
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", nncp.SdsAdd(sds, nncp.SDS{
209 "size": strconv.FormatInt(copied, 10),
212 if err = os.Remove(filename); err != nil {
213 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
227 sds["xx"] = string(nncp.TTx)
228 for nodeId, _ := range ctx.Neigh {
230 if nodeOnly != nil && nodeId != *nodeOnly.Id {
231 ctx.LogD("nncp-xfer", sds, "skip")
234 dirLock, err := ctx.LockDir(&nodeId, nncp.TTx)
238 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
239 sds["dir"] = nodePath
240 _, err = os.Stat(nodePath)
242 if os.IsNotExist(err) {
243 ctx.LogD("nncp-xfer", sds, "does not exist")
245 ctx.UnlockDir(dirLock)
248 if err = os.Mkdir(nodePath, os.FileMode(0700)); err != nil {
249 ctx.UnlockDir(dirLock)
250 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
255 ctx.UnlockDir(dirLock)
256 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
261 dstPath := filepath.Join(nodePath, ctx.Self.Id.String())
263 _, err = os.Stat(dstPath)
265 if os.IsNotExist(err) {
266 if err = os.Mkdir(dstPath, os.FileMode(0700)); err != nil {
267 ctx.UnlockDir(dirLock)
268 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
273 ctx.UnlockDir(dirLock)
274 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
280 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
281 pktName := filepath.Base(job.Fd.Name())
283 if job.PktEnc.Nice > nice {
284 ctx.LogD("nncp-xfer", sds, "too nice")
288 tmp, err := ioutil.TempFile(dstPath, "nncp-xfer")
290 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mktemp")
295 sds["tmp"] = tmp.Name()
296 ctx.LogD("nncp-xfer", sds, "created")
297 bufW := bufio.NewWriter(tmp)
298 copied, err := io.Copy(bufW, bufio.NewReader(job.Fd))
301 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
310 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
314 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
315 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename")
320 ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
321 "size": strconv.FormatInt(copied, 10),
324 if err = os.Remove(job.Fd.Name()); err != nil {
325 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
330 ctx.UnlockDir(dirLock)