]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-caller/main.go
Autotoss ability
[nncp.git] / src / cmd / nncp-caller / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 // Croned NNCP TCP daemon caller.
19 package main
20
21 import (
22         "errors"
23         "flag"
24         "fmt"
25         "log"
26         "os"
27         "sync"
28         "time"
29
30         "go.cypherpunks.ru/nncp/v5"
31 )
32
33 func usage() {
34         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
35         fmt.Fprintf(os.Stderr, "nncp-caller -- croned NNCP TCP daemon caller\n\n")
36         fmt.Fprintf(os.Stderr, "Usage: %s [options] [NODE ...]\n", os.Args[0])
37         fmt.Fprintln(os.Stderr, "Options:")
38         flag.PrintDefaults()
39 }
40
41 func main() {
42         var (
43                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
44                 spoolPath = flag.String("spool", "", "Override path to spool")
45                 logPath   = flag.String("log", "", "Override path to logfile")
46                 quiet     = flag.Bool("quiet", false, "Print only errors")
47                 showPrgrs = flag.Bool("progress", false, "Force progress showing")
48                 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
49                 debug     = flag.Bool("debug", false, "Print debug messages")
50                 version   = flag.Bool("version", false, "Print version information")
51                 warranty  = flag.Bool("warranty", false, "Print warranty information")
52
53                 autotoss       = flag.Bool("autotoss", false, "Toss after call is finished")
54                 autotossDoSeen = flag.Bool("autotoss-seen", false, "Create .seen files during tossing")
55                 autotossNoFile = flag.Bool("autotoss-nofile", false, "Do not process \"file\" packets during tossing")
56                 autotossNoFreq = flag.Bool("autotoss-nofreq", false, "Do not process \"freq\" packets during tossing")
57                 autotossNoExec = flag.Bool("autotoss-noexec", false, "Do not process \"exec\" packets during tossing")
58                 autotossNoTrns = flag.Bool("autotoss-notrns", false, "Do not process \"trns\" packets during tossing")
59         )
60         flag.Usage = usage
61         flag.Parse()
62         if *warranty {
63                 fmt.Println(nncp.Warranty)
64                 return
65         }
66         if *version {
67                 fmt.Println(nncp.VersionGet())
68                 return
69         }
70
71         ctx, err := nncp.CtxFromCmdline(
72                 *cfgPath,
73                 *spoolPath,
74                 *logPath,
75                 *quiet,
76                 *showPrgrs,
77                 *omitPrgrs,
78                 *debug,
79         )
80         if err != nil {
81                 log.Fatalln("Error during initialization:", err)
82         }
83         if ctx.Self == nil {
84                 log.Fatalln("Config lacks private keys")
85         }
86         ctx.Umask()
87
88         var nodes []*nncp.Node
89         if flag.NArg() > 0 {
90                 for _, nodeId := range flag.Args() {
91                         node, err := ctx.FindNode(nodeId)
92                         if err != nil {
93                                 log.Fatalln("Invalid NODE specified:", err)
94                         }
95                         if len(node.Calls) == 0 {
96                                 ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
97                                 continue
98                         }
99                         nodes = append(nodes, node)
100                 }
101         } else {
102                 for _, node := range ctx.Neigh {
103                         if len(node.Calls) == 0 {
104                                 ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
105                                 continue
106                         }
107                         nodes = append(nodes, node)
108                 }
109         }
110
111         var wg sync.WaitGroup
112         for _, node := range nodes {
113                 for i, call := range node.Calls {
114                         wg.Add(1)
115                         go func(node *nncp.Node, i int, call *nncp.Call) {
116                                 defer wg.Done()
117                                 var addrs []string
118                                 if call.Addr == nil {
119                                         for _, addr := range node.Addrs {
120                                                 addrs = append(addrs, addr)
121                                         }
122                                 } else {
123                                         addrs = append(addrs, *call.Addr)
124                                 }
125                                 sds := nncp.SDS{"node": node.Id, "callindex": i}
126                                 for {
127                                         n := time.Now()
128                                         t := call.Cron.Next(n)
129                                         ctx.LogD("caller", sds, t.String())
130                                         if t.IsZero() {
131                                                 ctx.LogE("caller", sds, errors.New("got zero time"), "")
132                                                 return
133                                         }
134                                         time.Sleep(t.Sub(n))
135                                         node.Lock()
136                                         if node.Busy {
137                                                 node.Unlock()
138                                                 ctx.LogD("caller", sds, "busy")
139                                                 continue
140                                         } else {
141                                                 node.Busy = true
142                                                 node.Unlock()
143                                                 ctx.CallNode(
144                                                         node,
145                                                         addrs,
146                                                         call.Nice,
147                                                         call.Xx,
148                                                         call.RxRate,
149                                                         call.TxRate,
150                                                         call.OnlineDeadline,
151                                                         call.MaxOnlineTime,
152                                                         false,
153                                                         nil,
154                                                 )
155                                                 if *autotoss {
156                                                         ctx.Toss(
157                                                                 node.Id,
158                                                                 call.Nice,
159                                                                 false,
160                                                                 *autotossDoSeen,
161                                                                 *autotossNoFile,
162                                                                 *autotossNoFreq,
163                                                                 *autotossNoExec,
164                                                                 *autotossNoTrns,
165                                                         )
166                                                 }
167
168                                                 node.Lock()
169                                                 node.Busy = false
170                                                 node.Unlock()
171                                         }
172                                 }
173                         }(node, i, call)
174                 }
175         }
176         wg.Wait()
177 }