3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-29 15:40:02 +01:00
ergo/irc/net.go
2014-02-13 18:59:45 -08:00

37 lines
621 B
Go

package irc
import (
"log"
"net"
"strings"
)
func IPString(addr net.Addr) string {
addrStr := addr.String()
ipaddr, _, err := net.SplitHostPort(addrStr)
if err != nil {
return addrStr
}
return ipaddr
}
func AddrLookupHostname(addr net.Addr) string {
return LookupHostname(IPString(addr))
}
func LookupHostname(addr string) string {
if DEBUG_NET {
log.Printf("LookupHostname(%s)", addr)
}
names, err := net.LookupAddr(addr)
if err != nil {
return addr
}
hostname := strings.TrimSuffix(names[0], ".")
if DEBUG_NET {
log.Printf("LookupHostname(%s) → %s", addr, hostname)
}
return hostname
}