]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-call/main.go
771582b907eee5e35ad1e0e590ca39661a6b321f
[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-2017 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         "io/ioutil"
26         "log"
27         "net"
28         "os"
29         "strconv"
30         "strings"
31
32         "cypherpunks.ru/nncp"
33 )
34
35 func usage() {
36         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
37         fmt.Fprintln(os.Stderr, "nncp-call -- call TCP daemon\n")
38         fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE[:ADDR] [FORCEADDR]\n", os.Args[0])
39         fmt.Fprintln(os.Stderr, "Options:")
40         flag.PrintDefaults()
41 }
42
43 func main() {
44         var (
45                 cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
46                 niceRaw  = flag.Int("nice", 255, "Minimal required niceness")
47                 rxOnly   = flag.Bool("rx", false, "Only receive packets")
48                 txOnly   = flag.Bool("tx", false, "Only transfer packets")
49                 quiet    = flag.Bool("quiet", false, "Print only errors")
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")
53
54                 onlineDeadline = flag.Int("onlinedeadline", 0, "Override onlinedeadline option")
55         )
56         flag.Usage = usage
57         flag.Parse()
58         if *warranty {
59                 fmt.Println(nncp.Warranty)
60                 return
61         }
62         if *version {
63                 fmt.Println(nncp.VersionGet())
64                 return
65         }
66         if flag.NArg() < 1 {
67                 usage()
68                 os.Exit(1)
69         }
70         if *niceRaw < 1 || *niceRaw > 255 {
71                 log.Fatalln("-nice must be between 1 and 255")
72         }
73         nice := uint8(*niceRaw)
74         if *rxOnly && *txOnly {
75                 log.Fatalln("-rx and -tx can not be set simultaneously")
76         }
77
78         cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
79         if err != nil {
80                 log.Fatalln("Can not read config:", err)
81         }
82         ctx, err := nncp.CfgParse(cfgRaw)
83         if err != nil {
84                 log.Fatalln("Can not parse config:", err)
85         }
86         ctx.Quiet = *quiet
87         ctx.Debug = *debug
88
89         splitted := strings.SplitN(flag.Arg(0), ":", 2)
90         node, err := ctx.FindNode(splitted[0])
91         if err != nil {
92                 log.Fatalln("Invalid NODE specified:", err)
93         }
94         if node.NoisePub == nil {
95                 log.Fatalln("Node does not have online communication capability")
96         }
97
98         if *onlineDeadline > 0 {
99                 node.OnlineDeadline = *onlineDeadline
100         }
101
102         var xxOnly nncp.TRxTx
103         if *rxOnly {
104                 xxOnly = nncp.TRx
105         } else if *txOnly {
106                 xxOnly = nncp.TTx
107         }
108
109         var addrs []string
110         if flag.NArg() == 2 {
111                 addrs = append(addrs, flag.Arg(1))
112         } else if len(splitted) == 2 {
113                 addr, known := ctx.Neigh[*node.Id].Addrs[splitted[1]]
114                 if !known {
115                         log.Fatalln("Unknown ADDR specified")
116                 }
117                 addrs = append(addrs, addr)
118         } else {
119                 for _, addr := range ctx.Neigh[*node.Id].Addrs {
120                         addrs = append(addrs, addr)
121                 }
122         }
123
124         isGood := false
125         for _, addr := range addrs {
126                 ctx.LogD("call", nncp.SDS{"addr": addr}, "dialing")
127                 conn, err := net.Dial("tcp", addr)
128                 if err != nil {
129                         log.Println("Can not connect:", err)
130                         continue
131                 }
132                 ctx.LogD("call", nncp.SDS{"addr": addr}, "connected")
133                 state, err := ctx.StartI(conn, node.Id, nice, &xxOnly)
134                 if err == nil {
135                         ctx.LogI("call-start", nncp.SDS{"node": state.Node.Id}, "connected")
136                         state.Wait()
137                         ctx.LogI("call-finish", nncp.SDS{
138                                 "node":     state.Node.Id,
139                                 "duration": strconv.FormatInt(int64(state.Duration.Seconds()), 10),
140                                 "rxbytes":  strconv.FormatInt(state.RxBytes, 10),
141                                 "txbytes":  strconv.FormatInt(state.TxBytes, 10),
142                                 "rxspeed":  strconv.FormatInt(state.RxSpeed, 10),
143                                 "txspeed":  strconv.FormatInt(state.TxSpeed, 10),
144                         }, "")
145                         isGood = true
146                         conn.Close()
147                         break
148                 } else {
149                         ctx.LogE("call-start", nncp.SDS{"node": state.Node.Id, "err": err}, "")
150                         conn.Close()
151                 }
152         }
153         if !isGood {
154                 os.Exit(1)
155         }
156 }