]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-call/main.go
Leading dots in packages description
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-call / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2018 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, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // Call NNCP TCP daemon.
20 package main
21
22 import (
23         "flag"
24         "fmt"
25         "log"
26         "os"
27         "strings"
28
29         "cypherpunks.ru/nncp"
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                 rxRate    = flag.Int("rxrate", 0, "Maximal receive rate, pkts/sec")
47                 txRate    = flag.Int("txrate", 0, "Maximal transmit rate, pkts/sec")
48                 spoolPath = flag.String("spool", "", "Override path to spool")
49                 logPath   = flag.String("log", "", "Override path to logfile")
50                 quiet     = flag.Bool("quiet", false, "Print only errors")
51                 debug     = flag.Bool("debug", false, "Print debug messages")
52                 version   = flag.Bool("version", false, "Print version information")
53                 warranty  = flag.Bool("warranty", false, "Print warranty information")
54
55                 onlineDeadline = flag.Uint("onlinedeadline", 0, "Override onlinedeadline option")
56                 maxOnlineTime  = flag.Uint("maxonlinetime", 0, "Override maxonlinetime option")
57         )
58         flag.Usage = usage
59         flag.Parse()
60         if *warranty {
61                 fmt.Println(nncp.Warranty)
62                 return
63         }
64         if *version {
65                 fmt.Println(nncp.VersionGet())
66                 return
67         }
68         if flag.NArg() < 1 {
69                 usage()
70                 os.Exit(1)
71         }
72         nice, err := nncp.NicenessParse(*niceRaw)
73         if err != nil {
74                 log.Fatalln(err)
75         }
76         if *rxOnly && *txOnly {
77                 log.Fatalln("-rx and -tx can not be set simultaneously")
78         }
79
80         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
81         if err != nil {
82                 log.Fatalln("Error during initialization:", err)
83         }
84         if ctx.Self == nil {
85                 log.Fatalln("Config lacks private keys")
86         }
87
88         splitted := strings.SplitN(flag.Arg(0), ":", 2)
89         node, err := ctx.FindNode(splitted[0])
90         if err != nil {
91                 log.Fatalln("Invalid NODE specified:", err)
92         }
93         if node.NoisePub == nil {
94                 log.Fatalln("Node does not have online communication capability")
95         }
96
97         if *onlineDeadline == 0 {
98                 onlineDeadline = &node.OnlineDeadline
99         }
100         if *maxOnlineTime == 0 {
101                 maxOnlineTime = &node.MaxOnlineTime
102         }
103
104         var xxOnly nncp.TRxTx
105         if *rxOnly {
106                 xxOnly = nncp.TRx
107         } else if *txOnly {
108                 xxOnly = nncp.TTx
109         }
110
111         var addrs []string
112         if flag.NArg() == 2 {
113                 addrs = append(addrs, flag.Arg(1))
114         } else if len(splitted) == 2 {
115                 addr, known := ctx.Neigh[*node.Id].Addrs[splitted[1]]
116                 if !known {
117                         log.Fatalln("Unknown ADDR specified")
118                 }
119                 addrs = append(addrs, addr)
120         } else {
121                 for _, addr := range ctx.Neigh[*node.Id].Addrs {
122                         addrs = append(addrs, addr)
123                 }
124         }
125
126         if !ctx.CallNode(
127                 node,
128                 addrs,
129                 nice,
130                 xxOnly,
131                 *rxRate,
132                 *txRate,
133                 *onlineDeadline,
134                 *maxOnlineTime,
135         ) {
136                 os.Exit(1)
137         }
138 }