]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-caller/main.go
08c4afafa7ea9115626dfdcbf3fca65dca90a00d
[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         )
71         log.SetFlags(log.Lshortfile)
72         flag.Usage = usage
73         flag.Parse()
74         if *warranty {
75                 fmt.Println(nncp.Warranty)
76                 return
77         }
78         if *version {
79                 fmt.Println(nncp.VersionGet())
80                 return
81         }
82
83         ctx, err := nncp.CtxFromCmdline(
84                 *cfgPath,
85                 *spoolPath,
86                 *logPath,
87                 *quiet,
88                 *showPrgrs,
89                 *omitPrgrs,
90                 *debug,
91         )
92         if err != nil {
93                 log.Fatalln("Error during initialization:", err)
94         }
95         if ctx.Self == nil {
96                 log.Fatalln("Config lacks private keys")
97         }
98         ctx.Umask()
99
100         var nodes []*nncp.Node
101         if flag.NArg() > 0 {
102                 for _, nodeId := range flag.Args() {
103                         node, err := ctx.FindNode(nodeId)
104                         if err != nil {
105                                 log.Fatalln("Invalid NODE specified:", err)
106                         }
107                         if node.NoisePub == nil {
108                                 log.Fatalln("Node", nodeId, "does not have online communication capability")
109                         }
110                         if len(node.Calls) == 0 {
111                                 ctx.LogD(
112                                         "caller-no-calls",
113                                         nncp.LEs{{K: "Node", V: node.Id}},
114                                         func(les nncp.LEs) string {
115                                                 return fmt.Sprintf("%s node has no calls, skipping", node.Name)
116                                         },
117                                 )
118                                 continue
119                         }
120                         nodes = append(nodes, node)
121                 }
122         } else {
123                 for _, node := range ctx.Neigh {
124                         if len(node.Calls) == 0 {
125                                 ctx.LogD(
126                                         "caller-no-calls",
127                                         nncp.LEs{{K: "Node", V: node.Id}},
128                                         func(les nncp.LEs) string {
129                                                 return fmt.Sprintf("%s node has no calls, skipping", node.Name)
130                                         },
131                                 )
132                                 continue
133                         }
134                         nodes = append(nodes, node)
135                 }
136         }
137
138         ifis, err := net.Interfaces()
139         if err != nil {
140                 log.Fatalln("Can not get network interfaces list:", err)
141         }
142         for _, ifiReString := range ctx.MCDRxIfis {
143                 ifiRe, err := regexp.CompilePOSIX(ifiReString)
144                 if err != nil {
145                         log.Fatalf("Can not compile POSIX regexp \"%s\": %s", ifiReString, err)
146                 }
147                 for _, ifi := range ifis {
148                         if ifiRe.MatchString(ifi.Name) {
149                                 if err = ctx.MCDRx(ifi.Name); err != nil {
150                                         log.Printf("Can not run MCD reception on %s: %s", ifi.Name, err)
151                                 }
152                         }
153                 }
154         }
155
156         var wg sync.WaitGroup
157         for _, node := range nodes {
158                 for i, call := range node.Calls {
159                         wg.Add(1)
160                         go func(node *nncp.Node, i int, call *nncp.Call) {
161                                 defer wg.Done()
162                                 var addrsFromCfg []string
163                                 if call.Addr == nil {
164                                         for _, addr := range node.Addrs {
165                                                 addrsFromCfg = append(addrsFromCfg, addr)
166                                         }
167                                 } else {
168                                         addrsFromCfg = append(addrsFromCfg, *call.Addr)
169                                 }
170                                 les := nncp.LEs{{K: "Node", V: node.Id}, {K: "CallIndex", V: i}}
171                                 logMsg := func(les nncp.LEs) string {
172                                         return fmt.Sprintf("%s node, call %d", node.Name, i)
173                                 }
174                                 for {
175                                         n := time.Now()
176                                         t := call.Cron.Next(n)
177                                         ctx.LogD("caller-time", les, func(les nncp.LEs) string {
178                                                 return logMsg(les) + ": " + t.String()
179                                         })
180                                         if t.IsZero() {
181                                                 ctx.LogE("caller", les, errors.New("got zero time"), logMsg)
182                                                 return
183                                         }
184                                         time.Sleep(t.Sub(n))
185                                         node.Lock()
186                                         if node.Busy {
187                                                 node.Unlock()
188                                                 ctx.LogD("caller-busy", les, func(les nncp.LEs) string {
189                                                         return logMsg(les) + ": busy"
190                                                 })
191                                                 continue
192                                         } else {
193                                                 node.Busy = true
194                                                 node.Unlock()
195
196                                                 if call.WhenTxExists && call.Xx != "TRx" {
197                                                         ctx.LogD("caller", les, func(les nncp.LEs) string {
198                                                                 return logMsg(les) + ": checking tx existence"
199                                                         })
200                                                         txExists := false
201                                                         for job := range ctx.Jobs(node.Id, nncp.TTx) {
202                                                                 if job.PktEnc.Nice > call.Nice {
203                                                                         continue
204                                                                 }
205                                                                 txExists = true
206                                                         }
207                                                         if !txExists {
208                                                                 ctx.LogD("caller-no-tx", les, func(les nncp.LEs) string {
209                                                                         return logMsg(les) + ": no tx"
210                                                                 })
211                                                                 node.Lock()
212                                                                 node.Busy = false
213                                                                 node.Unlock()
214                                                                 continue
215                                                         }
216                                                 }
217
218                                                 var autoTossFinish chan struct{}
219                                                 var autoTossBadCode chan bool
220                                                 if call.AutoToss || *autoToss {
221                                                         autoTossFinish, autoTossBadCode = ctx.AutoToss(
222                                                                 node.Id,
223                                                                 &nncp.TossOpts{
224                                                                         Nice:   call.Nice,
225                                                                         DoSeen: call.AutoTossDoSeen || *autoTossDoSeen,
226                                                                         NoFile: call.AutoTossNoFile || *autoTossNoFile,
227                                                                         NoFreq: call.AutoTossNoFreq || *autoTossNoFreq,
228                                                                         NoExec: call.AutoTossNoExec || *autoTossNoExec,
229                                                                         NoTrns: call.AutoTossNoTrns || *autoTossNoTrns,
230                                                                         NoArea: call.AutoTossNoArea || *autoTossNoArea,
231                                                                         NoACK:  call.AutoTossNoACK || *autoTossNoACK,
232                                                                 },
233                                                         )
234                                                 }
235
236                                                 var addrs []string
237                                                 if !call.MCDIgnore {
238                                                         nncp.MCDAddrsM.RLock()
239                                                         for _, mcdAddr := range nncp.MCDAddrs[*node.Id] {
240                                                                 ctx.LogD("caller", les, func(les nncp.LEs) string {
241                                                                         return logMsg(les) + ": adding MCD address: " +
242                                                                                 mcdAddr.Addr.String()
243                                                                 })
244                                                                 addrs = append(addrs, mcdAddr.Addr.String())
245                                                         }
246                                                         nncp.MCDAddrsM.RUnlock()
247                                                 }
248
249                                                 ctx.CallNode(
250                                                         node,
251                                                         append(addrs, addrsFromCfg...),
252                                                         call.Nice,
253                                                         call.Xx,
254                                                         call.RxRate,
255                                                         call.TxRate,
256                                                         call.OnlineDeadline,
257                                                         call.MaxOnlineTime,
258                                                         false,
259                                                         call.NoCK,
260                                                         nil,
261                                                 )
262
263                                                 if call.AutoToss || *autoToss {
264                                                         close(autoTossFinish)
265                                                         <-autoTossBadCode
266                                                 }
267
268                                                 node.Lock()
269                                                 node.Busy = false
270                                                 node.Unlock()
271                                         }
272                                 }
273                         }(node, i, call)
274                 }
275         }
276         wg.Wait()
277         nncp.SPCheckerWg.Wait()
278 }