]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-caller/main.go
Generate ACKs during tossing
[nncp.git] / src / cmd / nncp-caller / main.go
index dd93bfffe044f04abcb12751e58848267b33939f..3eff4d2a99a2fff62367d43b2b3d4abd25795c5a 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2021 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
@@ -23,16 +23,17 @@ import (
        "flag"
        "fmt"
        "log"
+       "net"
        "os"
+       "regexp"
        "sync"
        "time"
 
-       "go.cypherpunks.ru/nncp/v6"
+       "go.cypherpunks.ru/nncp/v8"
 )
 
 func usage() {
-       fmt.Fprintf(os.Stderr, nncp.UsageHeader())
-       fmt.Fprintf(os.Stderr, "nncp-caller -- croned NNCP TCP daemon caller\n\n")
+       fmt.Fprint(os.Stderr, "nncp-caller -- croned NNCP TCP daemon caller\n\n")
        fmt.Fprintf(os.Stderr, "Usage: %s [options] [NODE ...]\n", os.Args[0])
        fmt.Fprintln(os.Stderr, "Options:")
        flag.PrintDefaults()
@@ -49,7 +50,27 @@ func main() {
                debug     = flag.Bool("debug", false, "Print debug messages")
                version   = flag.Bool("version", false, "Print version information")
                warranty  = flag.Bool("warranty", false, "Print warranty information")
+
+               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
        flag.Parse()
        if *warranty {
@@ -85,6 +106,9 @@ func main() {
                        if err != nil {
                                log.Fatalln("Invalid NODE specified:", err)
                        }
+                       if node.NoisePub == nil {
+                               log.Fatalln("Node", nodeId, "does not have online communication capability")
+                       }
                        if len(node.Calls) == 0 {
                                ctx.LogD(
                                        "caller-no-calls",
@@ -113,19 +137,37 @@ func main() {
                }
        }
 
+       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)
+                               }
+                       }
+               }
+       }
+
        var wg sync.WaitGroup
        for _, node := range nodes {
                for i, call := range node.Calls {
                        wg.Add(1)
                        go func(node *nncp.Node, i int, call *nncp.Call) {
                                defer wg.Done()
-                               var addrs []string
+                               var addrsFromCfg []string
                                if call.Addr == nil {
                                        for _, addr := range node.Addrs {
-                                               addrs = append(addrs, addr)
+                                               addrsFromCfg = append(addrsFromCfg, addr)
                                        }
                                } else {
-                                       addrs = append(addrs, *call.Addr)
+                                       addrsFromCfg = append(addrsFromCfg, *call.Addr)
                                }
                                les := nncp.LEs{{K: "Node", V: node.Id}, {K: "CallIndex", V: i}}
                                logMsg := func(les nncp.LEs) string {
@@ -177,21 +219,39 @@ func main() {
 
                                                var autoTossFinish chan struct{}
                                                var autoTossBadCode chan bool
-                                               if call.AutoToss {
+                                               if call.AutoToss || *autoToss {
                                                        autoTossFinish, autoTossBadCode = ctx.AutoToss(
                                                                node.Id,
-                                                               call.Nice,
-                                                               call.AutoTossDoSeen,
-                                                               call.AutoTossNoFile,
-                                                               call.AutoTossNoFreq,
-                                                               call.AutoTossNoExec,
-                                                               call.AutoTossNoTrns,
+                                                               &nncp.TossOpts{
+                                                                       Nice:   call.Nice,
+                                                                       DoSeen: call.AutoTossDoSeen || *autoTossDoSeen,
+                                                                       NoFile: call.AutoTossNoFile || *autoTossNoFile,
+                                                                       NoFreq: call.AutoTossNoFreq || *autoTossNoFreq,
+                                                                       NoExec: call.AutoTossNoExec || *autoTossNoExec,
+                                                                       NoTrns: call.AutoTossNoTrns || *autoTossNoTrns,
+                                                                       NoArea: call.AutoTossNoArea || *autoTossNoArea,
+                                                                       NoACK:  call.AutoTossNoACK || *autoTossNoACK,
+                                                                       GenACK: call.AutoTossGenACK || *autoTossGenACK,
+                                                               },
                                                        )
                                                }
 
+                                               var addrs []string
+                                               if !call.MCDIgnore {
+                                                       nncp.MCDAddrsM.RLock()
+                                                       for _, mcdAddr := range nncp.MCDAddrs[*node.Id] {
+                                                               ctx.LogD("caller", les, func(les nncp.LEs) string {
+                                                                       return logMsg(les) + ": adding MCD address: " +
+                                                                               mcdAddr.Addr.String()
+                                                               })
+                                                               addrs = append(addrs, mcdAddr.Addr.String())
+                                                       }
+                                                       nncp.MCDAddrsM.RUnlock()
+                                               }
+
                                                ctx.CallNode(
                                                        node,
-                                                       addrs,
+                                                       append(addrs, addrsFromCfg...),
                                                        call.Nice,
                                                        call.Xx,
                                                        call.RxRate,
@@ -203,7 +263,7 @@ func main() {
                                                        nil,
                                                )
 
-                                               if call.AutoToss {
+                                               if call.AutoToss || *autoToss {
                                                        close(autoTossFinish)
                                                        <-autoTossBadCode
                                                }
@@ -217,4 +277,5 @@ func main() {
                }
        }
        wg.Wait()
+       nncp.SPCheckerWg.Wait()
 }