echoip/iputil/iputil.go

39 lines
671 B
Go
Raw Permalink Normal View History

2018-02-10 14:35:12 +01:00
package iputil
import (
"fmt"
"math/big"
"net"
"strings"
"time"
)
2018-03-19 19:54:24 +01:00
func LookupAddr(ip net.IP) (string, error) {
2018-02-10 14:35:12 +01:00
names, err := net.LookupAddr(ip.String())
2018-03-19 19:54:24 +01:00
if err != nil || len(names) == 0 {
return "", err
2018-02-10 14:35:12 +01:00
}
2018-03-19 19:54:24 +01:00
// Always return unrooted name
return strings.TrimRight(names[0], "."), nil
2018-02-10 14:35:12 +01:00
}
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
}
func ToDecimal(ip net.IP) *big.Int {
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)
}
return i
2018-02-10 14:35:12 +01:00
}