2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 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.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:")
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")
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")
73 log.SetFlags(log.Lshortfile)
77 fmt.Println(nncp.Warranty)
81 fmt.Println(nncp.VersionGet())
85 ctx, err := nncp.CtxFromCmdline(
95 log.Fatalln("Error during initialization:", err)
98 log.Fatalln("Config lacks private keys")
102 var nodes []*nncp.Node
104 for _, nodeId := range flag.Args() {
105 node, err := ctx.FindNode(nodeId)
107 log.Fatalln("Invalid NODE specified:", err)
109 if node.NoisePub == nil {
110 log.Fatalln("Node", nodeId, "does not have online communication capability")
112 if len(node.Calls) == 0 {
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)
122 nodes = append(nodes, node)
125 for _, node := range ctx.Neigh {
126 if len(node.Calls) == 0 {
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)
136 nodes = append(nodes, node)
140 ifis, err := net.Interfaces()
142 log.Fatalln("Can not get network interfaces list:", err)
144 for _, ifiReString := range ctx.MCDRxIfis {
145 ifiRe, err := regexp.CompilePOSIX(ifiReString)
147 log.Fatalf("Can not compile POSIX regexp \"%s\": %s", ifiReString, err)
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)
158 var wg sync.WaitGroup
159 for _, node := range nodes {
160 for i, call := range node.Calls {
162 go func(node *nncp.Node, i int, call *nncp.Call) {
164 var addrsFromCfg []string
165 if call.Addr == nil {
166 for _, addr := range node.Addrs {
167 addrsFromCfg = append(addrsFromCfg, addr)
170 addrsFromCfg = append(addrsFromCfg, *call.Addr)
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)
178 t := call.Cron.Next(n)
179 ctx.LogD("caller-time", les, func(les nncp.LEs) string {
180 return logMsg(les) + ": " + t.String()
183 ctx.LogE("caller", les, errors.New("got zero time"), logMsg)
190 ctx.LogD("caller-busy", les, func(les nncp.LEs) string {
191 return logMsg(les) + ": busy"
198 if call.WhenTxExists && call.Xx != "TRx" {
199 ctx.LogD("caller", les, func(les nncp.LEs) string {
200 return logMsg(les) + ": checking tx existence"
203 for job := range ctx.Jobs(node.Id, nncp.TTx) {
204 if job.PktEnc.Nice > call.Nice {
210 ctx.LogD("caller-no-tx", les, func(les nncp.LEs) string {
211 return logMsg(les) + ": no tx"
220 var autoTossFinish chan struct{}
221 var autoTossBadCode chan bool
222 if call.AutoToss || *autoToss {
223 autoTossFinish, autoTossBadCode = ctx.AutoToss(
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,
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()
247 addrs = append(addrs, mcdAddr.Addr.String())
249 nncp.MCDAddrsM.RUnlock()
254 append(addrs, addrsFromCfg...),
266 if call.AutoToss || *autoToss {
267 close(autoTossFinish)
280 nncp.SPCheckerWg.Wait()