2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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 xdr "github.com/davecgh/go-xdr/xdr2"
36 "go.cypherpunks.ru/nncp/v5"
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 showPrgrs = flag.Bool("progress", false, "Force progress showing")
67 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
68 debug = flag.Bool("debug", false, "Print debug messages")
69 version = flag.Bool("version", false, "Print version information")
70 warranty = flag.Bool("warranty", false, "Print warranty information")
75 fmt.Println(nncp.Warranty)
79 fmt.Println(nncp.VersionGet())
82 nice, err := nncp.NicenessParse(*niceRaw)
87 log.Fatalln("-rx and -tx can not be set simultaneously")
90 log.Fatalln("At least one of -rx and -tx must be specified")
93 ctx, err := nncp.CtxFromCmdline(
103 log.Fatalln("Error during initialization:", err)
106 nodeIds := make(map[nncp.NodeId]struct{}, flag.NArg())
107 for i := 0; i < flag.NArg(); i++ {
108 node, err := ctx.FindNode(flag.Arg(i))
110 log.Fatalln("Invalid specified:", err)
112 nodeIds[*node.Id] = struct{}{}
119 bufStdout := bufio.NewWriter(os.Stdout)
120 tarWr := tar.NewWriter(bufStdout)
121 for nodeId := range nodeIds {
123 {K: "XX", V: string(nncp.TTx)},
124 {K: "Node", V: nodeId.String()},
125 {K: "Pkt", V: "dummy"},
127 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
128 pktName = filepath.Base(job.Path)
129 les[len(les)-1].V = pktName
130 if job.PktEnc.Nice > nice {
131 ctx.LogD("nncp-bundle", les, "too nice")
134 fd, err := os.Open(job.Path)
136 log.Fatalln("Error during opening:", err)
138 if err = tarWr.WriteHeader(&tar.Header{
139 Format: tar.FormatUSTAR,
140 Name: nncp.NNCPBundlePrefix,
142 Typeflag: tar.TypeDir,
144 log.Fatalln("Error writing tar header:", err)
146 if err = tarWr.WriteHeader(&tar.Header{
147 Format: tar.FormatPAX,
148 Name: strings.Join([]string{
149 nncp.NNCPBundlePrefix,
156 Typeflag: tar.TypeReg,
158 log.Fatalln("Error writing tar header:", err)
160 if _, err = nncp.CopyProgressed(
161 tarWr, bufio.NewReader(fd), "Tx",
162 append(les, nncp.LEs{
163 {K: "Pkt", V: nncp.Base32Codec.EncodeToString(job.HshValue[:])},
164 {K: "FullSize", V: job.Size},
168 log.Fatalln("Error during copying to tar:", err)
170 if err = fd.Close(); err != nil {
171 log.Fatalln("Error during closing:", err)
173 if err = tarWr.Flush(); err != nil {
174 log.Fatalln("Error during tar flushing:", err)
176 if err = bufStdout.Flush(); err != nil {
177 log.Fatalln("Error during stdout flushing:", err)
180 if err = os.Remove(job.Path); err != nil {
181 log.Fatalln("Error during deletion:", err)
182 } else if ctx.HdrUsage {
183 os.Remove(job.Path + nncp.HdrSuffix)
186 ctx.LogI("nncp-bundle", append(les, nncp.LE{K: "Size", V: job.Size}), "")
189 if err = tarWr.Close(); err != nil {
190 log.Fatalln("Error during tar closing:", err)
193 bufStdin := bufio.NewReaderSize(os.Stdin, CopyBufSize*2)
194 pktEncBuf := make([]byte, nncp.PktEncOverhead)
195 var pktEnc *nncp.PktEnc
197 peeked, err := bufStdin.Peek(CopyBufSize)
198 if err != nil && err != io.EOF {
199 log.Fatalln("Error during reading:", err)
201 prefixIdx := bytes.Index(peeked, []byte(nncp.NNCPBundlePrefix))
206 bufStdin.Discard(bufStdin.Buffered() - (len(nncp.NNCPBundlePrefix) - 1)) // #nosec G104
209 if _, err = bufStdin.Discard(prefixIdx); err != nil {
212 tarR := tar.NewReader(bufStdin)
213 entry, err := tarR.Next()
218 nncp.LEs{{K: "XX", V: string(nncp.TRx)}, {K: "Err", V: err}},
224 if entry.Typeflag != tar.TypeDir {
227 nncp.LEs{{K: "XX", V: string(nncp.TRx)}},
232 entry, err = tarR.Next()
237 nncp.LEs{{K: "XX", V: string(nncp.TRx)}, {K: "Err", V: err}},
243 les := nncp.LEs{{K: "XX", V: string(nncp.TRx)}, {K: "Pkt", V: entry.Name}}
244 if entry.Size < nncp.PktEncOverhead {
245 ctx.LogD("nncp-bundle", les, "Too small packet")
248 if !ctx.IsEnoughSpace(entry.Size) {
249 ctx.LogE("nncp-bundle", les, errors.New("not enough spool space"), "")
252 pktName := filepath.Base(entry.Name)
253 if _, err = nncp.Base32Codec.DecodeString(pktName); err != nil {
254 ctx.LogD("nncp-bundle", append(les, nncp.LE{K: "Err", V: "bad packet name"}), "")
257 if _, err = io.ReadFull(tarR, pktEncBuf); err != nil {
258 ctx.LogD("nncp-bundle", append(les, nncp.LE{K: "Err", V: err}), "read")
261 if _, err = xdr.Unmarshal(bytes.NewReader(pktEncBuf), &pktEnc); err != nil {
262 ctx.LogD("nncp-bundle", les, "Bad packet structure")
265 if pktEnc.Magic != nncp.MagicNNCPEv4 {
266 ctx.LogD("nncp-bundle", les, "Bad packet magic number")
269 if pktEnc.Nice > nice {
270 ctx.LogD("nncp-bundle", les, "too nice")
273 if *pktEnc.Sender == *ctx.SelfId && *doDelete {
274 if len(nodeIds) > 0 {
275 if _, exists := nodeIds[*pktEnc.Recipient]; !exists {
276 ctx.LogD("nncp-bundle", les, "Recipient is not requested")
280 nodeId32 := nncp.Base32Codec.EncodeToString(pktEnc.Recipient[:])
282 {K: "XX", V: string(nncp.TTx)},
283 {K: "Node", V: nodeId32},
284 {K: "Pkt", V: pktName},
286 dstPath := filepath.Join(ctx.Spool, nodeId32, string(nncp.TTx), pktName)
287 if _, err = os.Stat(dstPath); err != nil {
288 ctx.LogD("nncp-bundle", les, "Packet is already missing")
291 hsh, err := blake2b.New256(nil)
293 log.Fatalln("Error during hasher creation:", err)
295 if _, err = hsh.Write(pktEncBuf); err != nil {
296 log.Fatalln("Error during writing:", err)
298 if _, err = nncp.CopyProgressed(
300 append(les, nncp.LE{K: "FullSize", V: entry.Size}),
303 log.Fatalln("Error during copying:", err)
305 if nncp.Base32Codec.EncodeToString(hsh.Sum(nil)) == pktName {
306 ctx.LogI("nncp-bundle", les, "removed")
310 os.Remove(dstPath + nncp.HdrSuffix)
314 ctx.LogE("nncp-bundle", les, errors.New("bad checksum"), "")
318 if *pktEnc.Recipient != *ctx.SelfId {
319 ctx.LogD("nncp-bundle", les, "Unknown recipient")
322 if len(nodeIds) > 0 {
323 if _, exists := nodeIds[*pktEnc.Sender]; !exists {
324 ctx.LogD("nncp-bundle", les, "Sender is not requested")
328 sender := nncp.Base32Codec.EncodeToString(pktEnc.Sender[:])
330 {K: "XX", V: string(nncp.TRx)},
331 {K: "Node", V: sender},
332 {K: "Pkt", V: pktName},
333 {K: "FullSize", V: entry.Size},
335 dstDirPath := filepath.Join(ctx.Spool, sender, string(nncp.TRx))
336 dstPath := filepath.Join(dstDirPath, pktName)
337 if _, err = os.Stat(dstPath); err == nil || !os.IsNotExist(err) {
338 ctx.LogD("nncp-bundle", les, "Packet already exists")
341 if _, err = os.Stat(dstPath + nncp.SeenSuffix); err == nil || !os.IsNotExist(err) {
342 ctx.LogD("nncp-bundle", les, "Packet already exists")
347 hsh, err := blake2b.New256(nil)
349 log.Fatalln("Error during hasher creation:", err)
351 if _, err = hsh.Write(pktEncBuf); err != nil {
352 log.Fatalln("Error during writing:", err)
354 if _, err = nncp.CopyProgressed(hsh, tarR, "check", les, ctx.ShowPrgrs); err != nil {
355 log.Fatalln("Error during copying:", err)
357 if nncp.Base32Codec.EncodeToString(hsh.Sum(nil)) != pktName {
358 ctx.LogE("nncp-bundle", les, errors.New("bad checksum"), "")
362 tmp, err := ctx.NewTmpFileWHash()
364 log.Fatalln("Error during temporary file creation:", err)
366 if _, err = tmp.W.Write(pktEncBuf); err != nil {
367 log.Fatalln("Error during writing:", err)
369 if _, err = nncp.CopyProgressed(tmp.W, tarR, "check", les, ctx.ShowPrgrs); err != nil {
370 log.Fatalln("Error during copying:", err)
372 if err = tmp.W.Flush(); err != nil {
373 log.Fatalln("Error during flusing:", err)
375 if nncp.Base32Codec.EncodeToString(tmp.Hsh.Sum(nil)) == pktName {
376 if err = tmp.Commit(dstDirPath); err != nil {
377 log.Fatalln("Error during commiting:", err)
380 ctx.LogE("nncp-bundle", les, errors.New("bad checksum"), "")
387 if _, err = nncp.CopyProgressed(ioutil.Discard, tarR, "Rx", les, ctx.ShowPrgrs); err != nil {
388 log.Fatalln("Error during copying:", err)
391 tmp, err := ctx.NewTmpFile()
393 log.Fatalln("Error during temporary file creation:", err)
395 bufTmp := bufio.NewWriterSize(tmp, CopyBufSize)
396 if _, err = bufTmp.Write(pktEncBuf); err != nil {
397 log.Fatalln("Error during writing:", err)
399 if _, err = nncp.CopyProgressed(bufTmp, tarR, "Rx", les, ctx.ShowPrgrs); err != nil {
400 log.Fatalln("Error during copying:", err)
402 if err = bufTmp.Flush(); err != nil {
403 log.Fatalln("Error during flushing:", err)
405 if err = tmp.Sync(); err != nil {
406 log.Fatalln("Error during syncing:", err)
408 if err = tmp.Close(); err != nil {
409 log.Fatalln("Error during closing:", err)
411 if err = os.MkdirAll(dstDirPath, os.FileMode(0777)); err != nil {
412 log.Fatalln("Error during mkdir:", err)
414 if err = os.Rename(tmp.Name(), dstPath); err != nil {
415 log.Fatalln("Error during renaming:", err)
417 if err = nncp.DirSync(dstDirPath); err != nil {
418 log.Fatalln("Error during syncing:", err)
421 ctx.HdrWrite(pktEncBuf, dstPath)
425 for _, le := range les {
426 if le.K == "FullSize" {
427 les = append(les, nncp.LE{K: "Size", V: le.V})
431 ctx.LogI("nncp-bundle", les, "")