]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-call/main.go
nncp-call -mcd-wait
[nncp.git] / src / cmd / nncp-call / 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 // Call NNCP TCP daemon.
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "log"
25         "net"
26         "os"
27         "regexp"
28         "strings"
29         "time"
30
31         "go.cypherpunks.ru/nncp/v8"
32 )
33
34 func usage() {
35         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
36         fmt.Fprintf(os.Stderr, "nncp-call -- call TCP daemon\n\n")
37         fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE[:ADDR] [FORCEADDR]\n", os.Args[0])
38         fmt.Fprintln(os.Stderr, "Options:")
39         flag.PrintDefaults()
40 }
41
42 func main() {
43         var (
44                 cfgPath     = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
45                 ucspi       = flag.Bool("ucspi", false, "Is it started as UCSPI-TCP client")
46                 niceRaw     = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
47                 rxOnly      = flag.Bool("rx", false, "Only receive packets")
48                 txOnly      = flag.Bool("tx", false, "Only transmit packets")
49                 listOnly    = flag.Bool("list", false, "Only list remote packets")
50                 noCK        = flag.Bool("nock", false, "Do no checksum checking")
51                 onlyPktsRaw = flag.String("pkts", "", "Recieve only that packets, comma separated")
52                 mcdWait     = flag.Uint("mcd-wait", 60, "Wait for MCD for specified number of seconds")
53                 rxRate      = flag.Int("rxrate", 0, "Maximal receive rate, pkts/sec")
54                 txRate      = flag.Int("txrate", 0, "Maximal transmit rate, pkts/sec")
55                 spoolPath   = flag.String("spool", "", "Override path to spool")
56                 logPath     = flag.String("log", "", "Override path to logfile")
57                 quiet       = flag.Bool("quiet", false, "Print only errors")
58                 showPrgrs   = flag.Bool("progress", false, "Force progress showing")
59                 omitPrgrs   = flag.Bool("noprogress", false, "Omit progress showing")
60                 debug       = flag.Bool("debug", false, "Print debug messages")
61                 version     = flag.Bool("version", false, "Print version information")
62                 warranty    = flag.Bool("warranty", false, "Print warranty information")
63
64                 onlineDeadlineSec = flag.Uint("onlinedeadline", 0, "Override onlinedeadline option")
65                 maxOnlineTimeSec  = flag.Uint("maxonlinetime", 0, "Override maxonlinetime option")
66
67                 autoToss       = flag.Bool("autotoss", false, "Toss after call is finished")
68                 autoTossDoSeen = flag.Bool("autotoss-seen", false, "Create seen/ files during tossing")
69                 autoTossNoFile = flag.Bool("autotoss-nofile", false, "Do not process \"file\" packets during tossing")
70                 autoTossNoFreq = flag.Bool("autotoss-nofreq", false, "Do not process \"freq\" packets during tossing")
71                 autoTossNoExec = flag.Bool("autotoss-noexec", false, "Do not process \"exec\" packets during tossing")
72                 autoTossNoTrns = flag.Bool("autotoss-notrns", false, "Do not process \"trns\" packets during tossing")
73                 autoTossNoArea = flag.Bool("autotoss-noarea", false, "Do not process \"area\" packets during tossing")
74         )
75         log.SetFlags(log.Lshortfile)
76         flag.Usage = usage
77         flag.Parse()
78         if *warranty {
79                 fmt.Println(nncp.Warranty)
80                 return
81         }
82         if *version {
83                 fmt.Println(nncp.VersionGet())
84                 return
85         }
86         if flag.NArg() < 1 {
87                 usage()
88                 os.Exit(1)
89         }
90         nice, err := nncp.NicenessParse(*niceRaw)
91         if err != nil {
92                 log.Fatalln(err)
93         }
94         if *rxOnly && *txOnly {
95                 log.Fatalln("-rx and -tx can not be set simultaneously")
96         }
97
98         ctx, err := nncp.CtxFromCmdline(
99                 *cfgPath,
100                 *spoolPath,
101                 *logPath,
102                 *quiet,
103                 *showPrgrs,
104                 *omitPrgrs,
105                 *debug,
106         )
107         if err != nil {
108                 log.Fatalln("Error during initialization:", err)
109         }
110         if ctx.Self == nil {
111                 log.Fatalln("Config lacks private keys")
112         }
113
114         splitted := strings.SplitN(flag.Arg(0), ":", 2)
115         node, err := ctx.FindNode(splitted[0])
116         if err != nil {
117                 log.Fatalln("Invalid NODE specified:", err)
118         }
119         if node.NoisePub == nil {
120                 log.Fatalln("Node does not have online communication capability")
121         }
122
123         onlineDeadline := node.OnlineDeadline
124         if *onlineDeadlineSec != 0 {
125                 onlineDeadline = time.Duration(*onlineDeadlineSec) * time.Second
126         }
127         maxOnlineTime := node.MaxOnlineTime
128         if *maxOnlineTimeSec != 0 {
129                 maxOnlineTime = time.Duration(*maxOnlineTimeSec) * time.Second
130         }
131
132         var xxOnly nncp.TRxTx
133         if *rxOnly {
134                 xxOnly = nncp.TRx
135         } else if *txOnly {
136                 xxOnly = nncp.TTx
137         }
138
139         var addrs []string
140         if *ucspi {
141                 addrs = append(addrs, nncp.UCSPITCPClient)
142         } else if flag.NArg() == 2 {
143                 addrs = append(addrs, flag.Arg(1))
144         } else if len(splitted) == 2 {
145                 addr, known := ctx.Neigh[*node.Id].Addrs[splitted[1]]
146                 if !known {
147                         log.Fatalln("Unknown ADDR specified")
148                 }
149                 addrs = append(addrs, addr)
150         } else {
151                 for _, addr := range ctx.Neigh[*node.Id].Addrs {
152                         addrs = append(addrs, addr)
153                 }
154         }
155
156         if *mcdWait > 0 {
157                 ifis, err := net.Interfaces()
158                 if err != nil {
159                         log.Fatalln("Can not get network interfaces list:", err)
160                 }
161                 for _, ifiReString := range ctx.MCDRxIfis {
162                         ifiRe, err := regexp.CompilePOSIX(ifiReString)
163                         if err != nil {
164                                 log.Fatalf("Can not compile POSIX regexp \"%s\": %s", ifiReString, err)
165                         }
166                         for _, ifi := range ifis {
167                                 if ifiRe.MatchString(ifi.Name) {
168                                         if err = ctx.MCDRx(ifi.Name); err != nil {
169                                                 log.Printf("Can not run MCD reception on %s: %s", ifi.Name, err)
170                                         }
171                                 }
172                         }
173                 }
174                 addrs = nil
175                 for i := int(*mcdWait); i > 0; i-- {
176                         nncp.MCDAddrsM.RLock()
177                         for _, mcdAddr := range nncp.MCDAddrs[*node.Id] {
178                                 addrs = append(addrs, mcdAddr.Addr.String())
179                         }
180                         if len(addrs) > 0 {
181                                 break
182                         }
183                         nncp.MCDAddrsM.RUnlock()
184                         time.Sleep(time.Second)
185                 }
186                 if len(addrs) == 0 {
187                         log.Fatalf("No MCD packets from the node during %d seconds", *mcdWait)
188                 }
189         }
190
191         var onlyPkts map[[32]byte]bool
192         if len(*onlyPktsRaw) > 0 {
193                 splitted = strings.Split(*onlyPktsRaw, ",")
194                 onlyPkts = make(map[[32]byte]bool, len(splitted))
195                 for _, pktIdRaw := range splitted {
196                         pktId, err := nncp.Base32Codec.DecodeString(pktIdRaw)
197                         if err != nil {
198                                 log.Fatalln("Invalid packet specified: ", err)
199                         }
200                         pktIdArr := new([32]byte)
201                         copy(pktIdArr[:], pktId)
202                         onlyPkts[*pktIdArr] = true
203                 }
204         }
205
206         ctx.Umask()
207
208         var autoTossFinish chan struct{}
209         var autoTossBadCode chan bool
210         if *autoToss {
211                 autoTossFinish, autoTossBadCode = ctx.AutoToss(
212                         node.Id,
213                         nice,
214                         *autoTossDoSeen,
215                         *autoTossNoFile,
216                         *autoTossNoFreq,
217                         *autoTossNoExec,
218                         *autoTossNoTrns,
219                         *autoTossNoArea,
220                 )
221         }
222
223         badCode := !ctx.CallNode(
224                 node,
225                 addrs,
226                 nice,
227                 xxOnly,
228                 *rxRate,
229                 *txRate,
230                 onlineDeadline,
231                 maxOnlineTime,
232                 *listOnly,
233                 *noCK,
234                 onlyPkts,
235         )
236
237         if *autoToss {
238                 close(autoTossFinish)
239                 badCode = (<-autoTossBadCode) || badCode
240         }
241         nncp.SPCheckerWg.Wait()
242         if badCode {
243                 os.Exit(1)
244         }
245 }