2 NNCP -- Node-to-Node CoPy
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 debug = flag.Bool("debug", false, "Enable debugging information")
54 version = flag.Bool("version", false, "Print version information")
55 warranty = flag.Bool("warranty", false, "Print warranty information")
60 fmt.Println(nncp.Warranty)
64 fmt.Println(nncp.VersionGet())
71 if *niceRaw < 1 || *niceRaw > 255 {
72 log.Fatalln("-nice must be between 1 and 255")
74 nice := uint8(*niceRaw)
75 if *rxOnly && *txOnly {
76 log.Fatalln("-rx and -tx can not be set simultaneously")
79 cfgRaw, err := ioutil.ReadFile(*cfgPath)
81 log.Fatalln("Can not read config:", err)
83 ctx, err := nncp.CfgParse(cfgRaw)
85 log.Fatalln("Can not parse config:", err)
89 var nodeOnly *nncp.Node
91 nodeOnly, err = ctx.FindNode(*nodeRaw)
93 log.Fatalln("Invalid -node specified:", err)
97 selfPath := filepath.Join(flag.Arg(0), ctx.Self.Id.String())
100 var fis []os.FileInfo
105 sds["xx"] = string(nncp.TRx)
106 sds["dir"] = selfPath
107 ctx.LogD("nncp-xfer", sds, "self")
108 if _, err = os.Stat(selfPath); err != nil {
109 if os.IsNotExist(err) {
110 ctx.LogD("nncp-xfer", sds, "no dir")
113 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
117 dir, err = os.Open(selfPath)
119 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
123 fis, err = dir.Readdir(0)
126 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
130 for _, fi := range fis {
134 nodeId, err := nncp.NodeIdFromString(fi.Name())
135 sds["node"] = fi.Name()
137 ctx.LogD("nncp-xfer", sds, "is not NodeId")
140 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
141 ctx.LogD("nncp-xfer", sds, "skip")
144 if _, known := ctx.Neigh[*nodeId]; !known {
145 ctx.LogD("nncp-xfer", sds, "unknown")
148 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
150 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
154 fisInt, err := dir.Readdir(0)
157 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
161 for _, fiInt := range fisInt {
165 filename := filepath.Join(dir.Name(), fiInt.Name())
166 sds["file"] = filename
167 fd, err := os.Open(filename)
169 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
173 var pktEnc nncp.PktEnc
174 _, err = xdr.Unmarshal(fd, &pktEnc)
175 if err != nil || pktEnc.Magic != nncp.MagicNNCPEv1 {
176 ctx.LogD("nncp-xfer", sds, "is not a packet")
180 if pktEnc.Nice > nice {
181 ctx.LogD("nncp-xfer", sds, "too nice")
186 tmp, err := ctx.NewTmpFileWHash()
190 copied, err := io.Copy(tmp.W, bufio.NewReader(fd))
192 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
199 if err = tmp.Commit(filepath.Join(
206 ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
207 "size": strconv.FormatInt(copied, 10),
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(0700)); 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.Self.Id.String())
261 _, err = os.Stat(dstPath)
263 if os.IsNotExist(err) {
264 if err = os.Mkdir(dstPath, os.FileMode(0700)); 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 tmp, err := ioutil.TempFile(dstPath, "nncp-xfer")
288 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mktemp")
293 sds["tmp"] = tmp.Name()
294 ctx.LogD("nncp-xfer", sds, "created")
295 bufW := bufio.NewWriter(tmp)
296 copied, err := io.Copy(bufW, bufio.NewReader(job.Fd))
299 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
308 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
312 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
313 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename")
318 ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
319 "size": strconv.FormatInt(copied, 10),
322 if err = os.Remove(job.Fd.Name()); err != nil {
323 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
328 ctx.UnlockDir(dirLock)