]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-call/main.go
Generate ACKs during tossing
[nncp.git] / src / cmd / nncp-call / main.go
index 077a907fdcd45761de28ac2bcf1629415bcb954f..23b6fb9d5a91378190b86597b7b3ebc474a87d3c 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2023 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
@@ -22,7 +22,9 @@ import (
        "flag"
        "fmt"
        "log"
+       "net"
        "os"
+       "regexp"
        "strings"
        "time"
 
@@ -30,8 +32,7 @@ import (
 )
 
 func usage() {
-       fmt.Fprintf(os.Stderr, nncp.UsageHeader())
-       fmt.Fprintf(os.Stderr, "nncp-call -- call TCP daemon\n\n")
+       fmt.Fprint(os.Stderr, "nncp-call -- call TCP daemon\n\n")
        fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE[:ADDR] [FORCEADDR]\n", os.Args[0])
        fmt.Fprintln(os.Stderr, "Options:")
        flag.PrintDefaults()
@@ -47,6 +48,7 @@ func main() {
                listOnly    = flag.Bool("list", false, "Only list remote packets")
                noCK        = flag.Bool("nock", false, "Do no checksum checking")
                onlyPktsRaw = flag.String("pkts", "", "Recieve only that packets, comma separated")
+               mcdWait     = flag.Uint("mcd-wait", 0, "Wait for MCD for specified number of seconds")
                rxRate      = flag.Int("rxrate", 0, "Maximal receive rate, pkts/sec")
                txRate      = flag.Int("txrate", 0, "Maximal transmit rate, pkts/sec")
                spoolPath   = flag.String("spool", "", "Override path to spool")
@@ -58,16 +60,29 @@ func main() {
                version     = flag.Bool("version", false, "Print version information")
                warranty    = flag.Bool("warranty", false, "Print warranty information")
 
-               onlineDeadlineSec = flag.Uint("onlinedeadline", 0, "Override onlinedeadline option")
-               maxOnlineTimeSec  = flag.Uint("maxonlinetime", 0, "Override maxonlinetime option")
+               onlineDeadlineSec = flag.Uint("onlinedeadline", 0,
+                       "Override onlinedeadline option")
+               maxOnlineTimeSec = flag.Uint("maxonlinetime", 0,
+                       "Override maxonlinetime option")
 
-               autoToss       = flag.Bool("autotoss", false, "Toss after call is finished")
-               autoTossDoSeen = flag.Bool("autotoss-seen", false, "Create seen/ files during tossing")
-               autoTossNoFile = flag.Bool("autotoss-nofile", false, "Do not process \"file\" packets during tossing")
-               autoTossNoFreq = flag.Bool("autotoss-nofreq", false, "Do not process \"freq\" packets during tossing")
-               autoTossNoExec = flag.Bool("autotoss-noexec", false, "Do not process \"exec\" packets during tossing")
-               autoTossNoTrns = flag.Bool("autotoss-notrns", false, "Do not process \"trns\" packets during tossing")
-               autoTossNoArea = flag.Bool("autotoss-noarea", false, "Do not process \"area\" packets during tossing")
+               autoToss = flag.Bool("autotoss", false,
+                       "Toss after call is finished")
+               autoTossDoSeen = flag.Bool("autotoss-seen", false,
+                       "Create seen/ files during tossing")
+               autoTossNoFile = flag.Bool("autotoss-nofile", false,
+                       "Do not process \"file\" packets during tossing")
+               autoTossNoFreq = flag.Bool("autotoss-nofreq", false,
+                       "Do not process \"freq\" packets during tossing")
+               autoTossNoExec = flag.Bool("autotoss-noexec", false,
+                       "Do not process \"exec\" packets during tossing")
+               autoTossNoTrns = flag.Bool("autotoss-notrns", false,
+                       "Do not process \"trns\" packets during tossing")
+               autoTossNoArea = flag.Bool("autotoss-noarea", false,
+                       "Do not process \"area\" packets during tossing")
+               autoTossNoACK = flag.Bool("autotoss-noack", false,
+                       "Do not process \"ack\" packets during tossing")
+               autoTossGenACK = flag.Bool("autotoss-gen-ack", false,
+                       "Generate ACK packets")
        )
        log.SetFlags(log.Lshortfile)
        flag.Usage = usage
@@ -150,6 +165,41 @@ func main() {
                }
        }
 
+       if *mcdWait > 0 {
+               ifis, err := net.Interfaces()
+               if err != nil {
+                       log.Fatalln("Can not get network interfaces list:", err)
+               }
+               for _, ifiReString := range ctx.MCDRxIfis {
+                       ifiRe, err := regexp.CompilePOSIX(ifiReString)
+                       if err != nil {
+                               log.Fatalf("Can not compile POSIX regexp \"%s\": %s", ifiReString, err)
+                       }
+                       for _, ifi := range ifis {
+                               if ifiRe.MatchString(ifi.Name) {
+                                       if err = ctx.MCDRx(ifi.Name); err != nil {
+                                               log.Printf("Can not run MCD reception on %s: %s", ifi.Name, err)
+                                       }
+                               }
+                       }
+               }
+               addrs = nil
+               for i := int(*mcdWait); i > 0; i-- {
+                       nncp.MCDAddrsM.RLock()
+                       for _, mcdAddr := range nncp.MCDAddrs[*node.Id] {
+                               addrs = append(addrs, mcdAddr.Addr.String())
+                       }
+                       if len(addrs) > 0 {
+                               break
+                       }
+                       nncp.MCDAddrsM.RUnlock()
+                       time.Sleep(time.Second)
+               }
+               if len(addrs) == 0 {
+                       log.Fatalf("No MCD packets from the node during %d seconds", *mcdWait)
+               }
+       }
+
        var onlyPkts map[[32]byte]bool
        if len(*onlyPktsRaw) > 0 {
                splitted = strings.Split(*onlyPktsRaw, ",")
@@ -172,13 +222,17 @@ func main() {
        if *autoToss {
                autoTossFinish, autoTossBadCode = ctx.AutoToss(
                        node.Id,
-                       nice,
-                       *autoTossDoSeen,
-                       *autoTossNoFile,
-                       *autoTossNoFreq,
-                       *autoTossNoExec,
-                       *autoTossNoTrns,
-                       *autoTossNoArea,
+                       &nncp.TossOpts{
+                               Nice:   nice,
+                               DoSeen: *autoTossDoSeen,
+                               NoFile: *autoTossNoFile,
+                               NoFreq: *autoTossNoFreq,
+                               NoExec: *autoTossNoExec,
+                               NoTrns: *autoTossNoTrns,
+                               NoArea: *autoTossNoArea,
+                               NoACK:  *autoTossNoACK,
+                               GenACK: *autoTossGenACK,
+                       },
                )
        }