echoip/iputil/iputil.go

38 lines
672 B
Go
Raw Normal View History

2018-02-10 14:35:12 +01:00
package iputil
import (
"fmt"
"math/big"
"net"
"strings"
"time"
)
func LookupAddr(ip net.IP) ([]string, error) {
names, err := net.LookupAddr(ip.String())
for i, _ := range names {
names[i] = strings.TrimRight(names[i], ".") // Always return unrooted name
}
return names, err
}
func LookupPort(ip net.IP, port uint64) error {
address := fmt.Sprintf("[%s]:%d", ip, port)
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
if err != nil {
return err
}
defer conn.Close()
return nil
}
2018-02-10 18:10:16 +01:00
func ToDecimal(ip net.IP) uint64 {
2018-02-10 14:35:12 +01:00
i := big.NewInt(0)
if to4 := ip.To4(); to4 != nil {
i.SetBytes(to4)
} else {
i.SetBytes(ip)
}
2018-02-10 18:10:16 +01:00
return i.Uint64()
2018-02-10 14:35:12 +01:00
}