]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-call/main.go
Operations progress
[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-2019 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
28         "go.cypherpunks.ru/nncp/v5"
29 )
30
31 func usage() {
32         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
33         fmt.Fprintf(os.Stderr, "nncp-call -- call TCP daemon\n\n")
34         fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE[:ADDR] [FORCEADDR]\n", os.Args[0])
35         fmt.Fprintln(os.Stderr, "Options:")
36         flag.PrintDefaults()
37 }
38
39 func main() {
40         var (
41                 cfgPath     = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
42                 niceRaw     = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
43                 rxOnly      = flag.Bool("rx", false, "Only receive packets")
44                 txOnly      = flag.Bool("tx", false, "Only transmit packets")
45                 listOnly    = flag.Bool("list", false, "Only list remote packets")
46                 onlyPktsRaw = flag.String("pkts", "", "Recieve only that packets, comma separated")
47                 rxRate      = flag.Int("rxrate", 0, "Maximal receive rate, pkts/sec")
48                 txRate      = flag.Int("txrate", 0, "Maximal transmit rate, pkts/sec")
49                 spoolPath   = flag.String("spool", "", "Override path to spool")
50                 logPath     = flag.String("log", "", "Override path to logfile")
51                 quiet       = flag.Bool("quiet", false, "Print only errors")
52                 showPrgrs   = flag.Bool("progress", false, "Force progress showing")
53                 omitPrgrs   = flag.Bool("noprogress", false, "Omit progress showing")
54                 debug       = flag.Bool("debug", false, "Print debug messages")
55                 version     = flag.Bool("version", false, "Print version information")
56                 warranty    = flag.Bool("warranty", false, "Print warranty information")
57
58                 onlineDeadline = flag.Uint("onlinedeadline", 0, "Override onlinedeadline option")
59                 maxOnlineTime  = flag.Uint("maxonlinetime", 0, "Override maxonlinetime option")
60         )
61         flag.Usage = usage
62         flag.Parse()
63         if *warranty {
64                 fmt.Println(nncp.Warranty)
65                 return
66         }
67         if *version {
68                 fmt.Println(nncp.VersionGet())
69                 return
70         }
71         if flag.NArg() < 1 {
72                 usage()
73                 os.Exit(1)
74         }
75         nice, err := nncp.NicenessParse(*niceRaw)
76         if err != nil {
77                 log.Fatalln(err)
78         }
79         if *rxOnly && *txOnly {
80                 log.Fatalln("-rx and -tx can not be set simultaneously")
81         }
82
83         ctx, err := nncp.CtxFromCmdline(
84                 *cfgPath,
85                 *spoolPath,
86                 *logPath,
87                 *quiet,
88                 *showPrgrs,
89                 *omitPrgrs,
90                 *debug,
91         )
92         if err != nil {
93                 log.Fatalln("Error during initialization:", err)
94         }
95         if ctx.Self == nil {
96                 log.Fatalln("Config lacks private keys")
97         }
98
99         splitted := strings.SplitN(flag.Arg(0), ":", 2)
100         node, err := ctx.FindNode(splitted[0])
101         if err != nil {
102                 log.Fatalln("Invalid NODE specified:", err)
103         }
104         if node.NoisePub == nil {
105                 log.Fatalln("Node does not have online communication capability")
106         }
107
108         if *onlineDeadline == 0 {
109                 onlineDeadline = &node.OnlineDeadline
110         }
111         if *maxOnlineTime == 0 {
112                 maxOnlineTime = &node.MaxOnlineTime
113         }
114
115         var xxOnly nncp.TRxTx
116         if *rxOnly {
117                 xxOnly = nncp.TRx
118         } else if *txOnly {
119                 xxOnly = nncp.TTx
120         }
121
122         var addrs []string
123         if flag.NArg() == 2 {
124                 addrs = append(addrs, flag.Arg(1))
125         } else if len(splitted) == 2 {
126                 addr, known := ctx.Neigh[*node.Id].Addrs[splitted[1]]
127                 if !known {
128                         log.Fatalln("Unknown ADDR specified")
129                 }
130                 addrs = append(addrs, addr)
131         } else {
132                 for _, addr := range ctx.Neigh[*node.Id].Addrs {
133                         addrs = append(addrs, addr)
134                 }
135         }
136
137         var onlyPkts map[[32]byte]bool
138         if len(*onlyPktsRaw) > 0 {
139                 splitted = strings.Split(*onlyPktsRaw, ",")
140                 onlyPkts = make(map[[32]byte]bool, len(splitted))
141                 for _, pktIdRaw := range splitted {
142                         pktId, err := nncp.FromBase32(pktIdRaw)
143                         if err != nil {
144                                 log.Fatalln("Invalid packet specified: ", err)
145                         }
146                         pktIdArr := new([32]byte)
147                         copy(pktIdArr[:], pktId)
148                         onlyPkts[*pktIdArr] = true
149                 }
150         }
151
152         ctx.Umask()
153         if !ctx.CallNode(
154                 node,
155                 addrs,
156                 nice,
157                 xxOnly,
158                 *rxRate,
159                 *txRate,
160                 *onlineDeadline,
161                 *maxOnlineTime,
162                 *listOnly,
163                 onlyPkts,
164         ) {
165                 os.Exit(1)
166         }
167 }