Skip to content

Commit

Permalink
Only listen on each interface once
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Sep 4, 2012
1 parent a311f70 commit 75e3227
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
30 changes: 3 additions & 27 deletions geodns.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,9 @@ func main() {
setupPgeodnsZone(Zones)

go configReader(dirName, Zones)
for _, host := range strings.Split(*flaginter, ",") {
ip, port, err := net.SplitHostPort(host)
if err != nil {
switch {
case strings.Contains(err.Error(), "missing port in address"):
// 127.0.0.1
ip = host
case strings.Contains(err.Error(), "too many colons in address") &&
// [a:b::c]
strings.LastIndex(host, "]") == len(host)-1:
ip = host[1 : len(host)-1]
port = ""
case strings.Contains(err.Error(), "too many colons in address"):
// a:b::c
ip = host
port = ""
default:
log.Fatalf("Could not parse %s: %s\n", host, err)
}
}
if len(port) == 0 {
port = *flagport
}
host = net.JoinHostPort(ip, port)
if len(serverId) == 0 {
serverId = ip
}

inter := getInterfaces()
for _, host := range inter {
go listenAndServe(host, &Zones)
}

Expand Down
51 changes: 51 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"log"
"net"
"strings"
)

func getInterfaces() []string {

var inter []string
uniq := make(map[string]bool)

for _, host := range strings.Split(*flaginter, ",") {
ip, port, err := net.SplitHostPort(host)
if err != nil {
switch {
case strings.Contains(err.Error(), "missing port in address"):
// 127.0.0.1
ip = host
case strings.Contains(err.Error(), "too many colons in address") &&
// [a:b::c]
strings.LastIndex(host, "]") == len(host)-1:
ip = host[1 : len(host)-1]
port = ""
case strings.Contains(err.Error(), "too many colons in address"):
// a:b::c
ip = host
port = ""
default:
log.Fatalf("Could not parse %s: %s\n", host, err)
}
}
if len(port) == 0 {
port = *flagport
}
host = net.JoinHostPort(ip, port)
if uniq[host] {
continue
}
uniq[host] = true

if len(serverId) == 0 {
serverId = ip
}
inter = append(inter, host)

}

return inter
}

0 comments on commit 75e3227

Please sign in to comment.