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")
71 log.SetFlags(log.Lshortfile)
75 fmt.Println(nncp.Warranty)
79 fmt.Println(nncp.VersionGet())
83 ctx, err := nncp.CtxFromCmdline(
93 log.Fatalln("Error during initialization:", err)
96 log.Fatalln("Config lacks private keys")
100 var nodes []*nncp.Node
102 for _, nodeId := range flag.Args() {
103 node, err := ctx.FindNode(nodeId)
105 log.Fatalln("Invalid NODE specified:", err)
107 if node.NoisePub == nil {
108 log.Fatalln("Node", nodeId, "does not have online communication capability")
110 if len(node.Calls) == 0 {
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)
120 nodes = append(nodes, node)
123 for _, node := range ctx.Neigh {
124 if len(node.Calls) == 0 {
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)
134 nodes = append(nodes, node)
138 ifis, err := net.Interfaces()
140 log.Fatalln("Can not get network interfaces list:", err)
142 for _, ifiReString := range ctx.MCDRxIfis {
143 ifiRe, err := regexp.CompilePOSIX(ifiReString)
145 log.Fatalf("Can not compile POSIX regexp \"%s\": %s", ifiReString, err)
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)
156 var wg sync.WaitGroup
157 for _, node := range nodes {
158 for i, call := range node.Calls {
160 go func(node *nncp.Node, i int, call *nncp.Call) {
162 var addrsFromCfg []string
163 if call.Addr == nil {
164 for _, addr := range node.Addrs {
165 addrsFromCfg = append(addrsFromCfg, addr)
168 addrsFromCfg = append(addrsFromCfg, *call.Addr)
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)
176 t := call.Cron.Next(n)
177 ctx.LogD("caller-time", les, func(les nncp.LEs) string {
178 return logMsg(les) + ": " + t.String()
181 ctx.LogE("caller", les, errors.New("got zero time"), logMsg)
188 ctx.LogD("caller-busy", les, func(les nncp.LEs) string {
189 return logMsg(les) + ": busy"
196 if call.WhenTxExists && call.Xx != "TRx" {
197 ctx.LogD("caller", les, func(les nncp.LEs) string {
198 return logMsg(les) + ": checking tx existence"
201 for job := range ctx.Jobs(node.Id, nncp.TTx) {
202 if job.PktEnc.Nice > call.Nice {
208 ctx.LogD("caller-no-tx", les, func(les nncp.LEs) string {
209 return logMsg(les) + ": no tx"
218 var autoTossFinish chan struct{}
219 var autoTossBadCode chan bool
220 if call.AutoToss || *autoToss {
221 autoTossFinish, autoTossBadCode = ctx.AutoToss(
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,
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()
244 addrs = append(addrs, mcdAddr.Addr.String())
246 nncp.MCDAddrsM.RUnlock()
251 append(addrs, addrsFromCfg...),
263 if call.AutoToss || *autoToss {
264 close(autoTossFinish)
277 nncp.SPCheckerWg.Wait()