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

37 lines
621 B
Go
Raw Normal View History

2012-04-07 20:44:59 +02:00
package irc
import (
2014-02-14 03:59:45 +01:00
"log"
2012-04-07 20:44:59 +02:00
"net"
"strings"
2012-04-07 20:44:59 +02:00
)
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))
2014-02-11 04:39:53 +01:00
}
func LookupHostname(addr string) string {
2014-02-14 03:59:45 +01:00
if DEBUG_NET {
log.Printf("LookupHostname(%s)", addr)
}
2014-02-11 04:39:53 +01:00
names, err := net.LookupAddr(addr)
if err != nil {
2014-02-11 04:39:53 +01:00
return addr
}
2014-02-14 03:59:45 +01:00
hostname := strings.TrimSuffix(names[0], ".")
if DEBUG_NET {
log.Printf("LookupHostname(%s) → %s", addr, hostname)
}
return hostname
}