]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-caller/main.go
Generate ACKs during tossing
[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-2023 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         "net"
27         "os"
28         "regexp"
29         "sync"
30         "time"
31
32         "go.cypherpunks.ru/nncp/v8"
33 )
34
35 func usage() {
36         fmt.Fprint(os.Stderr, "nncp-caller -- croned NNCP TCP daemon caller\n\n")
37         fmt.Fprintf(os.Stderr, "Usage: %s [options] [NODE ...]\n", os.Args[0])
38         fmt.Fprintln(os.Stderr, "Options:")
39         flag.PrintDefaults()
40 }
41
42 func main() {
43         var (
44                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
45                 spoolPath = flag.String("spool", "", "Override path to spool")
46                 logPath   = flag.String("log", "", "Override path to logfile")
47                 quiet     = flag.Bool("quiet", false, "Print only errors")
48                 showPrgrs = flag.Bool("progress", false, "Force progress showing")
49                 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
50                 debug     = flag.Bool("debug", false, "Print debug messages")
51                 version   = flag.Bool("version", false, "Print version information")
52                 warranty  = flag.Bool("warranty", false, "Print warranty information")
53
54                 autoToss = flag.Bool("autotoss", false,
55                         "Toss after call is finished")
56                 autoTossDoSeen = flag.Bool("autotoss-seen", false,
57                         "Create seen/ files during tossing")
58                 autoTossNoFile = flag.Bool("autotoss-nofile", false,
59                         "Do not process \"file\" packets during tossing")
60                 autoTossNoFreq = flag.Bool("autotoss-nofreq", false,
61                         "Do not process \"freq\" packets during tossing")
62                 autoTossNoExec = flag.Bool("autotoss-noexec", false,
63                         "Do not process \"exec\" packets during tossing")
64                 autoTossNoTrns = flag.Bool("autotoss-notrns", false,
65                         "Do not process \"trns\" packets during tossing")
66                 autoTossNoArea = flag.Bool("autotoss-noarea", false,
67                         "Do not process \"area\" packets during tossing")
68                 autoTossNoACK = flag.Bool("autotoss-noack", false,
69                         "Do not process \"ack\" packets during tossing")
70                 autoTossGenACK = flag.Bool("autotoss-gen-ack", false,
71                         "Generate ACK packets")
72         )
73         log.SetFlags(log.Lshortfile)
74         flag.Usage = usage
75         flag.Parse()
76         if *warranty {
77                 fmt.Println(nncp.Warranty)
78                 return
79         }
80         if *version {
81                 fmt.Println(nncp.VersionGet())
82                 return
83         }
84
85         ctx, err := nncp.CtxFromCmdline(
86                 *cfgPath,
87                 *spoolPath,
88                 *logPath,
89                 *quiet,
90                 *showPrgrs,
91                 *omitPrgrs,
92                 *debug,
93         )
94         if err != nil {
95                 log.Fatalln("Error during initialization:", err)
96         }
97         if ctx.Self == nil {
98                 log.Fatalln("Config lacks private keys")
99         }
100         ctx.Umask()
101
102         var nodes []*nncp.Node
103         if flag.NArg() > 0 {
104                 for _, nodeId := range flag.Args() {
105                         node, err := ctx.FindNode(nodeId)
106                         if err != nil {
107                                 log.Fatalln("Invalid NODE specified:", err)
108                         }
109                         if node.NoisePub == nil {
110                                 log.Fatalln("Node", nodeId, "does not have online communication capability")
111                         }
112                         if len(node.Calls) == 0 {
113                                 ctx.LogD(
114                                         "caller-no-calls",
115                                         nncp.LEs{{K: "Node", V: node.Id}},
116                                         func(les nncp.LEs) string {
117                                                 return fmt.Sprintf("%s node has no calls, skipping", node.Name)
118                                         },
119                                 )
120                                 continue
121                         }
122                         nodes = append(nodes, node)
123                 }
124         } else {
125                 for _, node := range ctx.Neigh {
126                         if len(node.Calls) == 0 {
127                                 ctx.LogD(
128                                         "caller-no-calls",
129                                         nncp.LEs{{K: "Node", V: node.Id}},
130                                         func(les nncp.LEs) string {
131                                                 return fmt.Sprintf("%s node has no calls, skipping", node.Name)
132                                         },
133                                 )
134                                 continue
135                         }
136                         nodes = append(nodes, node)
137                 }
138         }
139
140         ifis, err := net.Interfaces()
141         if err != nil {
142                 log.Fatalln("Can not get network interfaces list:", err)
143         }
144         for _, ifiReString := range ctx.MCDRxIfis {
145                 ifiRe, err := regexp.CompilePOSIX(ifiReString)
146                 if err != nil {
147                         log.Fatalf("Can not compile POSIX regexp \"%s\": %s", ifiReString, err)
148                 }
149                 for _, ifi := range ifis {
150                         if ifiRe.MatchString(ifi.Name) {
151                                 if err = ctx.MCDRx(ifi.Name); err != nil {
152                                         log.Printf("Can not run MCD reception on %s: %s", ifi.Name, err)
153                                 }
154                         }
155                 }
156         }
157
158         var wg sync.WaitGroup
159         for _, node := range nodes {
160                 for i, call := range node.Calls {
161                         wg.Add(1)
162                         go func(node *nncp.Node, i int, call *nncp.Call) {
163                                 defer wg.Done()
164                                 var addrsFromCfg []string
165                                 if call.Addr == nil {
166                                         for _, addr := range node.Addrs {
167                                                 addrsFromCfg = append(addrsFromCfg, addr)
168                                         }
169                                 } else {
170                                         addrsFromCfg = append(addrsFromCfg, *call.Addr)
171                                 }
172                                 les := nncp.LEs{{K: "Node", V: node.Id}, {K: "CallIndex", V: i}}
173                                 logMsg := func(les nncp.LEs) string {
174                                         return fmt.Sprintf("%s node, call %d", node.Name, i)
175                                 }
176                                 for {
177                                         n := time.Now()
178                                         t := call.Cron.Next(n)
179                                         ctx.LogD("caller-time", les, func(les nncp.LEs) string {
180                                                 return logMsg(les) + ": " + t.String()
181                                         })
182                                         if t.IsZero() {
183                                                 ctx.LogE("caller", les, errors.New("got zero time"), logMsg)
184                                                 return
185                                         }
186                                         time.Sleep(t.Sub(n))
187                                         node.Lock()
188                                         if node.Busy {
189                                                 node.Unlock()
190                                                 ctx.LogD("caller-busy", les, func(les nncp.LEs) string {
191                                                         return logMsg(les) + ": busy"
192                                                 })
193                                                 continue
194                                         } else {
195                                                 node.Busy = true
196                                                 node.Unlock()
197
198                                                 if call.WhenTxExists && call.Xx != "TRx" {
199                                                         ctx.LogD("caller", les, func(les nncp.LEs) string {
200                                                                 return logMsg(les) + ": checking tx existence"
201                                                         })
202                                                         txExists := false
203                                                         for job := range ctx.Jobs(node.Id, nncp.TTx) {
204                                                                 if job.PktEnc.Nice > call.Nice {
205                                                                         continue
206                                                                 }
207                                                                 txExists = true
208                                                         }
209                                                         if !txExists {
210                                                                 ctx.LogD("caller-no-tx", les, func(les nncp.LEs) string {
211                                                                         return logMsg(les) + ": no tx"
212                                                                 })
213                                                                 node.Lock()
214                                                                 node.Busy = false
215                                                                 node.Unlock()
216                                                                 continue
217                                                         }
218                                                 }
219
220                                                 var autoTossFinish chan struct{}
221                                                 var autoTossBadCode chan bool
222                                                 if call.AutoToss || *autoToss {
223                                                         autoTossFinish, autoTossBadCode = ctx.AutoToss(
224                                                                 node.Id,
225                                                                 &nncp.TossOpts{
226                                                                         Nice:   call.Nice,
227                                                                         DoSeen: call.AutoTossDoSeen || *autoTossDoSeen,
228                                                                         NoFile: call.AutoTossNoFile || *autoTossNoFile,
229                                                                         NoFreq: call.AutoTossNoFreq || *autoTossNoFreq,
230                                                                         NoExec: call.AutoTossNoExec || *autoTossNoExec,
231                                                                         NoTrns: call.AutoTossNoTrns || *autoTossNoTrns,
232                                                                         NoArea: call.AutoTossNoArea || *autoTossNoArea,
233                                                                         NoACK:  call.AutoTossNoACK || *autoTossNoACK,
234                                                                         GenACK: call.AutoTossGenACK || *autoTossGenACK,
235                                                                 },
236                                                         )
237                                                 }
238
239                                                 var addrs []string
240                                                 if !call.MCDIgnore {
241                                                         nncp.MCDAddrsM.RLock()
242                                                         for _, mcdAddr := range nncp.MCDAddrs[*node.Id] {
243                                                                 ctx.LogD("caller", les, func(les nncp.LEs) string {
244                                                                         return logMsg(les) + ": adding MCD address: " +
245                                                                                 mcdAddr.Addr.String()
246                                                                 })
247                                                                 addrs = append(addrs, mcdAddr.Addr.String())
248                                                         }
249                                                         nncp.MCDAddrsM.RUnlock()
250                                                 }
251
252                                                 ctx.CallNode(
253                                                         node,
254                                                         append(addrs, addrsFromCfg...),
255                                                         call.Nice,
256                                                         call.Xx,
257                                                         call.RxRate,
258                                                         call.TxRate,
259                                                         call.OnlineDeadline,
260                                                         call.MaxOnlineTime,
261                                                         false,
262                                                         call.NoCK,
263                                                         nil,
264                                                 )
265
266                                                 if call.AutoToss || *autoToss {
267                                                         close(autoTossFinish)
268                                                         <-autoTossBadCode
269                                                 }
270
271                                                 node.Lock()
272                                                 node.Busy = false
273                                                 node.Unlock()
274                                         }
275                                 }
276                         }(node, i, call)
277                 }
278         }
279         wg.Wait()
280         nncp.SPCheckerWg.Wait()
281 }