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 // Create/digest stream of NNCP encrypted packets.
35 "github.com/davecgh/go-xdr/xdr2"
36 "go.cypherpunks.ru/nncp/v4"
37 "golang.org/x/crypto/blake2b"
45 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
46 fmt.Fprintf(os.Stderr, "nncp-bundle -- Create/digest stream of NNCP encrypted packets\n\n")
47 fmt.Fprintf(os.Stderr, "Usage: %s [options] -tx [-delete] NODE [NODE ...] > ...\n", os.Args[0])
48 fmt.Fprintf(os.Stderr, " %s [options] -rx -delete [-dryrun] [NODE ...] < ...\n", os.Args[0])
49 fmt.Fprintf(os.Stderr, " %s [options] -rx [-check] [-dryrun] [NODE ...] < ...\n", os.Args[0])
50 fmt.Fprintln(os.Stderr, "Options:")
56 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
57 niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
58 doRx = flag.Bool("rx", false, "Receive packets")
59 doTx = flag.Bool("tx", false, "Transfer packets")
60 doDelete = flag.Bool("delete", false, "Delete transferred packets")
61 doCheck = flag.Bool("check", false, "Check integrity while receiving")
62 dryRun = flag.Bool("dryrun", false, "Do no writes")
63 spoolPath = flag.String("spool", "", "Override path to spool")
64 logPath = flag.String("log", "", "Override path to logfile")
65 quiet = flag.Bool("quiet", false, "Print only errors")
66 debug = flag.Bool("debug", false, "Print debug messages")
67 version = flag.Bool("version", false, "Print version information")
68 warranty = flag.Bool("warranty", false, "Print warranty information")
73 fmt.Println(nncp.Warranty)
77 fmt.Println(nncp.VersionGet())
80 nice, err := nncp.NicenessParse(*niceRaw)
85 log.Fatalln("-rx and -tx can not be set simultaneously")
88 log.Fatalln("At least one of -rx and -tx must be specified")
91 ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
93 log.Fatalln("Error during initialization:", err)
96 nodeIds := make(map[nncp.NodeId]struct{}, flag.NArg())
97 for i := 0; i < flag.NArg(); i++ {
98 node, err := ctx.FindNode(flag.Arg(i))
100 log.Fatalln("Invalid specified:", err)
102 nodeIds[*node.Id] = struct{}{}
107 sds["xx"] = string(nncp.TTx)
109 bufStdout := bufio.NewWriter(os.Stdout)
110 tarWr := tar.NewWriter(bufStdout)
111 for nodeId, _ := range nodeIds {
112 sds["node"] = nodeId.String()
113 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
114 pktName = filepath.Base(job.Fd.Name())
116 if job.PktEnc.Nice > nice {
117 ctx.LogD("nncp-bundle", sds, "too nice")
121 if err = tarWr.WriteHeader(&tar.Header{
122 Format: tar.FormatUSTAR,
123 Name: nncp.NNCPBundlePrefix,
125 Typeflag: tar.TypeDir,
127 log.Fatalln("Error writing tar header:", err)
129 if err = tarWr.WriteHeader(&tar.Header{
130 Format: tar.FormatPAX,
131 Name: strings.Join([]string{
132 nncp.NNCPBundlePrefix,
139 Typeflag: tar.TypeReg,
141 log.Fatalln("Error writing tar header:", err)
143 if _, err = io.Copy(tarWr, job.Fd); err != nil {
144 log.Fatalln("Error during copying to tar:", err)
147 if err = tarWr.Flush(); err != nil {
148 log.Fatalln("Error during tar flushing:", err)
150 if err = bufStdout.Flush(); err != nil {
151 log.Fatalln("Error during stdout flushing:", err)
154 if err = os.Remove(job.Fd.Name()); err != nil {
155 log.Fatalln("Error during deletion:", err)
158 ctx.LogI("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{
159 "size": strconv.FormatInt(job.Size, 10),
163 if err = tarWr.Close(); err != nil {
164 log.Fatalln("Error during tar closing:", err)
167 bufStdin := bufio.NewReaderSize(os.Stdin, CopyBufSize*2)
171 var entry *tar.Header
173 pktEncBuf := make([]byte, nncp.PktEncOverhead)
174 var pktEnc *nncp.PktEnc
179 peeked, err = bufStdin.Peek(CopyBufSize)
180 if err != nil && err != io.EOF {
181 log.Fatalln("Error during reading:", err)
183 prefixIdx = bytes.Index(peeked, []byte(nncp.NNCPBundlePrefix))
188 bufStdin.Discard(bufStdin.Buffered() - (len(nncp.NNCPBundlePrefix) - 1))
191 bufStdin.Discard(prefixIdx)
192 tarR = tar.NewReader(bufStdin)
193 sds["xx"] = string(nncp.TRx)
194 entry, err = tarR.Next()
199 nncp.SdsAdd(sds, nncp.SDS{"err": err}),
205 if entry.Typeflag != tar.TypeDir {
206 ctx.LogD("nncp-bundle", sds, "Expected NNCP/")
209 entry, err = tarR.Next()
214 nncp.SdsAdd(sds, nncp.SDS{"err": err}),
220 sds["pkt"] = entry.Name
221 if entry.Size < nncp.PktEncOverhead {
222 ctx.LogD("nncp-bundle", sds, "Too small packet")
225 pktName = filepath.Base(entry.Name)
226 if _, err = nncp.FromBase32(pktName); err != nil {
227 ctx.LogD("nncp-bundle", sds, "Bad packet name")
230 if _, err = io.ReadFull(tarR, pktEncBuf); err != nil {
231 ctx.LogD("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
234 if _, err = xdr.Unmarshal(bytes.NewReader(pktEncBuf), &pktEnc); err != nil {
235 ctx.LogD("nncp-bundle", sds, "Bad packet structure")
238 if pktEnc.Magic != nncp.MagicNNCPEv4 {
239 ctx.LogD("nncp-bundle", sds, "Bad packet magic number")
242 if pktEnc.Nice > nice {
243 ctx.LogD("nncp-bundle", sds, "too nice")
246 if *pktEnc.Sender == *ctx.SelfId && *doDelete {
247 if len(nodeIds) > 0 {
248 if _, exists = nodeIds[*pktEnc.Recipient]; !exists {
249 ctx.LogD("nncp-bundle", sds, "Recipient is not requested")
253 nodeId32 := nncp.ToBase32(pktEnc.Recipient[:])
254 sds["xx"] = string(nncp.TTx)
255 sds["node"] = nodeId32
257 dstPath = filepath.Join(
263 if _, err = os.Stat(dstPath); err != nil {
264 ctx.LogD("nncp-bundle", sds, "Packet is already missing")
267 hsh, err := blake2b.New256(nil)
269 log.Fatalln("Error during hasher creation:", err)
271 if _, err = hsh.Write(pktEncBuf); err != nil {
272 log.Fatalln("Error during writing:", err)
274 if _, err = io.Copy(hsh, tarR); err != nil {
275 log.Fatalln("Error during copying:", err)
277 if nncp.ToBase32(hsh.Sum(nil)) == pktName {
278 ctx.LogI("nncp-bundle", sds, "removed")
283 ctx.LogE("nncp-bundle", sds, "bad checksum")
287 if *pktEnc.Recipient != *ctx.SelfId {
288 ctx.LogD("nncp-bundle", sds, "Unknown recipient")
291 if len(nodeIds) > 0 {
292 if _, exists = nodeIds[*pktEnc.Sender]; !exists {
293 ctx.LogD("nncp-bundle", sds, "Sender is not requested")
297 sds["node"] = nncp.ToBase32(pktEnc.Recipient[:])
299 selfPath = filepath.Join(ctx.Spool, ctx.SelfId.String(), string(nncp.TRx))
300 dstPath = filepath.Join(selfPath, pktName)
301 if _, err = os.Stat(dstPath); err == nil || !os.IsNotExist(err) {
302 ctx.LogD("nncp-bundle", sds, "Packet already exists")
305 if _, err = os.Stat(dstPath + nncp.SeenSuffix); err == nil || !os.IsNotExist(err) {
306 ctx.LogD("nncp-bundle", sds, "Packet already exists")
311 hsh, err := blake2b.New256(nil)
313 log.Fatalln("Error during hasher creation:", err)
315 if _, err = hsh.Write(pktEncBuf); err != nil {
316 log.Fatalln("Error during writing:", err)
318 if _, err = io.Copy(hsh, tarR); err != nil {
319 log.Fatalln("Error during copying:", err)
321 if nncp.ToBase32(hsh.Sum(nil)) != pktName {
322 ctx.LogE("nncp-bundle", sds, "bad checksum")
326 tmp, err := ctx.NewTmpFileWHash()
328 log.Fatalln("Error during temporary file creation:", err)
330 if _, err = tmp.W.Write(pktEncBuf); err != nil {
331 log.Fatalln("Error during writing:", err)
333 if _, err = io.Copy(tmp.W, tarR); err != nil {
334 log.Fatalln("Error during copying:", err)
336 if err = tmp.W.Flush(); err != nil {
337 log.Fatalln("Error during flusing:", err)
339 if nncp.ToBase32(tmp.Hsh.Sum(nil)) == pktName {
340 if err = tmp.Commit(selfPath); err != nil {
341 log.Fatalln("Error during commiting:", err)
344 ctx.LogE("nncp-bundle", sds, "bad checksum")
351 if _, err = io.Copy(ioutil.Discard, tarR); err != nil {
352 log.Fatalln("Error during copying:", err)
355 tmp, err := ctx.NewTmpFile()
357 log.Fatalln("Error during temporary file creation:", err)
359 bufTmp := bufio.NewWriterSize(tmp, CopyBufSize)
360 if _, err = bufTmp.Write(pktEncBuf); err != nil {
361 log.Fatalln("Error during writing:", err)
363 if _, err = io.Copy(bufTmp, tarR); err != nil {
364 log.Fatalln("Error during copying:", err)
366 if err = bufTmp.Flush(); err != nil {
367 log.Fatalln("Error during flushing:", err)
369 if err = tmp.Sync(); err != nil {
370 log.Fatalln("Error during syncing:", err)
373 if err = os.MkdirAll(selfPath, os.FileMode(0777)); err != nil {
374 log.Fatalln("Error during mkdir:", err)
376 if err = os.Rename(tmp.Name(), dstPath); err != nil {
377 log.Fatalln("Error during renaming:", err)
381 ctx.LogI("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{
382 "size": strconv.FormatInt(entry.Size, 10),