From d18e052e01ec6fa6df4165a409058fb81adf0555 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 17 Apr 2018 14:07:44 -0400 Subject: don't make tinc.go worry about net.Dial conventions --- main.go | 18 ++++++++++++++++-- tinc.go | 16 +++------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 2c32c41..9717040 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,19 @@ import ( "sync" ) +func fmtAddress(node, port string) string { + if isIPv6(node) { + return fmt.Sprintf("[%s]:%s", node, port) + } else { + return fmt.Sprintf("%s:%s", node, port) + } +} + +func isIPv6(node string) bool { + ip := net.ParseIP(node) + return ip != nil && ip.To4() == nil +} + var wg sync.WaitGroup func Emit(pt Point) { @@ -28,9 +41,10 @@ func DoHostfile(fname string) { return } for _, address := range getAddresses(cfg) { + addressStr := fmtAddress(address.Node, address.Port) wg.Add(2) - go Emit(DoAddress(hostname, "tcp4", address)) - go Emit(DoAddress(hostname, "tcp6", address)) + go Emit(DoAddress(hostname, "tcp4", addressStr)) + go Emit(DoAddress(hostname, "tcp6", addressStr)) } } diff --git a/tinc.go b/tinc.go index 76857cb..1bfefc6 100644 --- a/tinc.go +++ b/tinc.go @@ -3,7 +3,6 @@ package main import ( "bufio" "fmt" - "net" "os" "strings" ) @@ -92,8 +91,8 @@ func readConfigFile(fname string) (map[string][]string, error) { // Returns a list of public addresses for a host-config in Go // "net.Dial" format. -func getAddresses(cfg map[string][]string) []string { - var result []string +func getAddresses(cfg map[string][]string) []struct{ Node, Port string } { + var result []struct{ Node, Port string } for _, node := range cfg["address"] { var port string @@ -109,17 +108,8 @@ func getAddresses(cfg map[string][]string) []string { node = node[:space] } - if isIPv6(node) { - result = append(result, fmt.Sprintf("[%s]:%s", node, port)) - } else { - result = append(result, fmt.Sprintf("%s:%s", node, port)) - } + result = append(result, struct{ Node, Port string }{node, port}) } return result } - -func isIPv6(node string) bool { - ip := net.ParseIP(node) - return ip != nil && ip.To4() == nil -} -- cgit v1.2.3