2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
18 // Croned NNCP TCP daemon caller.
32 "go.cypherpunks.ru/nncp/v8"
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:")
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")
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")
63 log.SetFlags(log.Lshortfile)
67 fmt.Println(nncp.Warranty)
71 fmt.Println(nncp.VersionGet())
75 ctx, err := nncp.CtxFromCmdline(
85 log.Fatalln("Error during initialization:", err)
88 log.Fatalln("Config lacks private keys")
92 var nodes []*nncp.Node
94 for _, nodeId := range flag.Args() {
95 node, err := ctx.FindNode(nodeId)
97 log.Fatalln("Invalid NODE specified:", err)
99 if node.NoisePub == nil {
100 log.Fatalln("Node", nodeId, "does not have online communication capability")
102 if len(node.Calls) == 0 {
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)
112 nodes = append(nodes, node)
115 for _, node := range ctx.Neigh {
116 if len(node.Calls) == 0 {
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)
126 nodes = append(nodes, node)
130 ifis, err := net.Interfaces()
132 log.Fatalln("Can not get network interfaces list:", err)
134 for _, ifiReString := range ctx.MCDRxIfis {
135 ifiRe, err := regexp.CompilePOSIX(ifiReString)
137 log.Fatalf("Can not compile POSIX regexp \"%s\": %s", ifiReString, err)
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)
148 var wg sync.WaitGroup
149 for _, node := range nodes {
150 for i, call := range node.Calls {
152 go func(node *nncp.Node, i int, call *nncp.Call) {
154 var addrsFromCfg []string
155 if call.Addr == nil {
156 for _, addr := range node.Addrs {
157 addrsFromCfg = append(addrsFromCfg, addr)
160 addrsFromCfg = append(addrsFromCfg, *call.Addr)
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)
168 t := call.Cron.Next(n)
169 ctx.LogD("caller-time", les, func(les nncp.LEs) string {
170 return logMsg(les) + ": " + t.String()
173 ctx.LogE("caller", les, errors.New("got zero time"), logMsg)
180 ctx.LogD("caller-busy", les, func(les nncp.LEs) string {
181 return logMsg(les) + ": busy"
188 if call.WhenTxExists && call.Xx != "TRx" {
189 ctx.LogD("caller", les, func(les nncp.LEs) string {
190 return logMsg(les) + ": checking tx existence"
193 for job := range ctx.Jobs(node.Id, nncp.TTx) {
194 if job.PktEnc.Nice > call.Nice {
200 ctx.LogD("caller-no-tx", les, func(les nncp.LEs) string {
201 return logMsg(les) + ": no tx"
210 var autoTossFinish chan struct{}
211 var autoTossBadCode chan bool
212 if call.AutoToss || *autoToss {
213 autoTossFinish, autoTossBadCode = ctx.AutoToss(
216 call.AutoTossDoSeen || *autoTossDoSeen,
217 call.AutoTossNoFile || *autoTossNoFile,
218 call.AutoTossNoFreq || *autoTossNoFreq,
219 call.AutoTossNoExec || *autoTossNoExec,
220 call.AutoTossNoTrns || *autoTossNoTrns,
221 call.AutoTossNoArea || *autoTossNoArea,
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()
233 addrs = append(addrs, mcdAddr.Addr.String())
235 nncp.MCDAddrsM.RUnlock()
240 append(addrs, addrsFromCfg...),
252 if call.AutoToss || *autoToss {
253 close(autoTossFinish)
266 nncp.SPCheckerWg.Wait()