From: Sergey Matveev Date: Sat, 7 Jan 2017 15:36:30 +0000 (+0300) Subject: Specify call addresses through the config X-Git-Tag: 0.1~35 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b3ab2f627d53bc1b74c4c54091e08fbcfcc58dd4;p=nncp.git Specify call addresses through the config --- diff --git a/src/cypherpunks.ru/nncp/cfg.go b/src/cypherpunks.ru/nncp/cfg.go index bb6c520..4e6268d 100644 --- a/src/cypherpunks.ru/nncp/cfg.go +++ b/src/cypherpunks.ru/nncp/cfg.go @@ -39,6 +39,8 @@ type NodeYAML struct { Incoming *string `incoming,omitempty` Freq *string `freq,omitempty` Via []string `via,omitempty` + + Addrs map[string]string `addrs,omitempty` } type NodeOurYAML struct { @@ -127,6 +129,7 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { NoisePub: new([32]byte), Incoming: incoming, Freq: freq, + Addrs: yml.Addrs, } copy(node.ExchPub[:], exchPub) copy(node.NoisePub[:], noisePub) diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go index 4a25e21..93c1a52 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go @@ -27,6 +27,7 @@ import ( "net" "os" "strconv" + "strings" "cypherpunks.ru/nncp" ) @@ -34,7 +35,9 @@ import ( func usage() { fmt.Fprintf(os.Stderr, nncp.UsageHeader()) fmt.Fprintln(os.Stderr, "nncp-call -- call TCP daemon\n") - fmt.Fprintln(os.Stderr, "Usage: %s [options] NODE ADDR\nOptions:", os.Args[0]) + fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE[:ADDR] [FORCEADDR]\n", os.Args[0]) + fmt.Fprintln(os.Stderr, "You must specify either [NODE:ADDR] or [NODE FORCEADDR]\n") + fmt.Fprintln(os.Stderr, "Options:") flag.PrintDefaults() } @@ -58,7 +61,7 @@ func main() { fmt.Println(nncp.VersionGet()) return } - if flag.NArg() != 2 { + if flag.NArg() < 1 { usage() os.Exit(1) } @@ -80,16 +83,31 @@ func main() { } ctx.Debug = *debug - node, err := ctx.FindNode(flag.Arg(0)) + splitted := strings.SplitN(flag.Arg(0), ":", 2) + node, err := ctx.FindNode(splitted[0]) if err != nil { log.Fatalln("Invalid NODE specified:", err) } + if len(splitted) == 1 && flag.NArg() != 2 { + usage() + os.Exit(1) + } + var dst string + if len(splitted) == 2 { + var known bool + dst, known = ctx.Neigh[*node.Id].Addrs[splitted[1]] + if !known { + log.Fatalln("Unknown ADDR specified") + } + } else { + dst = flag.Arg(1) + } - conn, err := net.Dial("tcp", flag.Arg(1)) + conn, err := net.Dial("tcp", dst) if err != nil { log.Fatalln("Can not connect:", err) } - ctx.LogD("call", nncp.SDS{"addr": flag.Arg(1)}, "connected") + ctx.LogD("call", nncp.SDS{"addr": dst}, "connected") var xxOnly nncp.TRxTx if *rxOnly { xxOnly = nncp.TRx diff --git a/src/cypherpunks.ru/nncp/node.go b/src/cypherpunks.ru/nncp/node.go index a86f37e..acf01da 100644 --- a/src/cypherpunks.ru/nncp/node.go +++ b/src/cypherpunks.ru/nncp/node.go @@ -43,6 +43,7 @@ type Node struct { Incoming *string Freq *string Via []*NodeId + Addrs map[string]string } type NodeOur struct {