]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go
AEAD encryption mode and new encrypted packet format
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-xfer / main.go
index f500cc919ffe965caac6cf215867eaffcf5c9e47..102292f2747c8fcda59e28e5508dd29e9f39c155 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -16,7 +16,7 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-// Copy NNCP inbound and outbounds packets
+// Exchange NNCP inbound and outbounds packets with external directory.
 package main
 
 import (
@@ -36,7 +36,7 @@ import (
 
 func usage() {
        fmt.Fprintf(os.Stderr, nncp.UsageHeader())
-       fmt.Fprintln(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n")
+       fmt.Fprintf(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
        fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
        flag.PrintDefaults()
 }
@@ -45,7 +45,7 @@ func main() {
        var (
                cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
                nodeRaw   = flag.String("node", "", "Process only that node")
-               niceRaw   = flag.Int("nice", 255, "Minimal required niceness")
+               niceRaw   = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
                rxOnly    = flag.Bool("rx", false, "Only receive packets")
                txOnly    = flag.Bool("tx", false, "Only transfer packets")
                mkdir     = flag.Bool("mkdir", false, "Create necessary outbound directories")
@@ -71,10 +71,10 @@ func main() {
                usage()
                os.Exit(1)
        }
-       if *niceRaw < 1 || *niceRaw > 255 {
-               log.Fatalln("-nice must be between 1 and 255")
+       nice, err := nncp.NicenessParse(*niceRaw)
+       if err != nil {
+               log.Fatalln(err)
        }
-       nice := uint8(*niceRaw)
        if *rxOnly && *txOnly {
                log.Fatalln("-rx and -tx can not be set simultaneously")
        }
@@ -170,7 +170,7 @@ func main() {
                        }
                        var pktEnc nncp.PktEnc
                        _, err = xdr.Unmarshal(fd, &pktEnc)
-                       if err != nil || pktEnc.Magic != nncp.MagicNNCPEv2 {
+                       if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
                                ctx.LogD("nncp-xfer", sds, "is not a packet")
                                fd.Close()
                                continue
@@ -286,7 +286,7 @@ Tx:
                                job.Fd.Close()
                                continue
                        }
-                       if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenPostfix)); err == nil || !os.IsNotExist(err) {
+                       if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
                                ctx.LogD("nncp-xfer", sds, "already exists")
                                job.Fd.Close()
                                continue
@@ -309,14 +309,19 @@ Tx:
                                isBad = true
                                continue
                        }
-                       err = bufW.Flush()
-                       tmp.Sync()
-                       tmp.Close()
-                       if err != nil {
-                               ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
+                       if err = bufW.Flush(); err != nil {
+                               tmp.Close()
+                               ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "flush")
                                isBad = true
                                continue
                        }
+                       if err = tmp.Sync(); err != nil {
+                               tmp.Close()
+                               ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "sync")
+                               isBad = true
+                               continue
+                       }
+                       tmp.Close()
                        if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
                                ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename")
                                isBad = true