]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-caller/main.go
MTH
[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/v7"
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         log.SetFlags(log.Lshortfile)
61         flag.Usage = usage
62         flag.Parse()
63         if *warranty {
64                 fmt.Println(nncp.Warranty)
65                 return
66         }
67         if *version {
68                 fmt.Println(nncp.VersionGet())
69                 return
70         }
71
72         ctx, err := nncp.CtxFromCmdline(
73                 *cfgPath,
74                 *spoolPath,
75                 *logPath,
76                 *quiet,
77                 *showPrgrs,
78                 *omitPrgrs,
79                 *debug,
80         )
81         if err != nil {
82                 log.Fatalln("Error during initialization:", err)
83         }
84         if ctx.Self == nil {
85                 log.Fatalln("Config lacks private keys")
86         }
87         ctx.Umask()
88
89         var nodes []*nncp.Node
90         if flag.NArg() > 0 {
91                 for _, nodeId := range flag.Args() {
92                         node, err := ctx.FindNode(nodeId)
93                         if err != nil {
94                                 log.Fatalln("Invalid NODE specified:", err)
95                         }
96                         if len(node.Calls) == 0 {
97                                 ctx.LogD(
98                                         "caller-no-calls",
99                                         nncp.LEs{{K: "Node", V: node.Id}},
100                                         func(les nncp.LEs) string {
101                                                 return fmt.Sprintf("%s node has no calls, skipping", node.Name)
102                                         },
103                                 )
104                                 continue
105                         }
106                         nodes = append(nodes, node)
107                 }
108         } else {
109                 for _, node := range ctx.Neigh {
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         }
123
124         for _, ifiName := range ctx.MCDRxIfis {
125                 if err = ctx.MCDRx(ifiName); err != nil {
126                         log.Fatalln("Can not run MCD reception:", err)
127                 }
128         }
129
130         var wg sync.WaitGroup
131         for _, node := range nodes {
132                 for i, call := range node.Calls {
133                         wg.Add(1)
134                         go func(node *nncp.Node, i int, call *nncp.Call) {
135                                 defer wg.Done()
136                                 var addrsFromCfg []string
137                                 if call.Addr == nil {
138                                         for _, addr := range node.Addrs {
139                                                 addrsFromCfg = append(addrsFromCfg, addr)
140                                         }
141                                 } else {
142                                         addrsFromCfg = append(addrsFromCfg, *call.Addr)
143                                 }
144                                 les := nncp.LEs{{K: "Node", V: node.Id}, {K: "CallIndex", V: i}}
145                                 logMsg := func(les nncp.LEs) string {
146                                         return fmt.Sprintf("%s node, call %d", node.Name, i)
147                                 }
148                                 for {
149                                         n := time.Now()
150                                         t := call.Cron.Next(n)
151                                         ctx.LogD("caller-time", les, func(les nncp.LEs) string {
152                                                 return logMsg(les) + ": " + t.String()
153                                         })
154                                         if t.IsZero() {
155                                                 ctx.LogE("caller", les, errors.New("got zero time"), logMsg)
156                                                 return
157                                         }
158                                         time.Sleep(t.Sub(n))
159                                         node.Lock()
160                                         if node.Busy {
161                                                 node.Unlock()
162                                                 ctx.LogD("caller-busy", les, func(les nncp.LEs) string {
163                                                         return logMsg(les) + ": busy"
164                                                 })
165                                                 continue
166                                         } else {
167                                                 node.Busy = true
168                                                 node.Unlock()
169
170                                                 if call.WhenTxExists && call.Xx != "TRx" {
171                                                         ctx.LogD("caller", les, func(les nncp.LEs) string {
172                                                                 return logMsg(les) + ": checking tx existence"
173                                                         })
174                                                         txExists := false
175                                                         for job := range ctx.Jobs(node.Id, nncp.TTx) {
176                                                                 if job.PktEnc.Nice > call.Nice {
177                                                                         continue
178                                                                 }
179                                                                 txExists = true
180                                                         }
181                                                         if !txExists {
182                                                                 ctx.LogD("caller-no-tx", les, func(les nncp.LEs) string {
183                                                                         return logMsg(les) + ": no tx"
184                                                                 })
185                                                                 node.Lock()
186                                                                 node.Busy = false
187                                                                 node.Unlock()
188                                                                 continue
189                                                         }
190                                                 }
191
192                                                 var autoTossFinish chan struct{}
193                                                 var autoTossBadCode chan bool
194                                                 if call.AutoToss || *autoToss {
195                                                         autoTossFinish, autoTossBadCode = ctx.AutoToss(
196                                                                 node.Id,
197                                                                 call.Nice,
198                                                                 call.AutoTossDoSeen || *autoTossDoSeen,
199                                                                 call.AutoTossNoFile || *autoTossNoFile,
200                                                                 call.AutoTossNoFreq || *autoTossNoFreq,
201                                                                 call.AutoTossNoExec || *autoTossNoExec,
202                                                                 call.AutoTossNoTrns || *autoTossNoTrns,
203                                                         )
204                                                 }
205
206                                                 var addrs []string
207                                                 if !call.MCDIgnore {
208                                                         nncp.MCDAddrsM.RLock()
209                                                         for _, mcdAddr := range nncp.MCDAddrs[*node.Id] {
210                                                                 ctx.LogD("caller", les, func(les nncp.LEs) string {
211                                                                         return logMsg(les) + ": adding MCD address: " +
212                                                                                 mcdAddr.Addr.String()
213                                                                 })
214                                                                 addrs = append(addrs, mcdAddr.Addr.String())
215                                                         }
216                                                         nncp.MCDAddrsM.RUnlock()
217                                                 }
218
219                                                 ctx.CallNode(
220                                                         node,
221                                                         append(addrs, addrsFromCfg...),
222                                                         call.Nice,
223                                                         call.Xx,
224                                                         call.RxRate,
225                                                         call.TxRate,
226                                                         call.OnlineDeadline,
227                                                         call.MaxOnlineTime,
228                                                         false,
229                                                         call.NoCK,
230                                                         nil,
231                                                 )
232
233                                                 if call.AutoToss || *autoToss {
234                                                         close(autoTossFinish)
235                                                         <-autoTossBadCode
236                                                 }
237
238                                                 node.Lock()
239                                                 node.Busy = false
240                                                 node.Unlock()
241                                         }
242                                 }
243                         }(node, i, call)
244                 }
245         }
246         wg.Wait()
247         nncp.SPCheckerWg.Wait()
248 }