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