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