2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2017-10-05 15:47:43 +02:00
|
|
|
package utils
|
2012-04-07 20:44:59 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2012-12-09 07:54:58 +01:00
|
|
|
"strings"
|
2012-04-07 20:44:59 +02:00
|
|
|
)
|
|
|
|
|
2019-01-22 11:01:01 +01:00
|
|
|
var (
|
|
|
|
// subnet mask for an ipv6 /128:
|
|
|
|
mask128 = net.CIDRMask(128, 128)
|
|
|
|
)
|
|
|
|
|
2017-04-16 03:31:33 +02:00
|
|
|
// IPString returns a simple IP string from the given net.Addr.
|
2016-10-11 15:51:46 +02:00
|
|
|
func IPString(addr net.Addr) string {
|
2012-12-09 07:54:58 +01:00
|
|
|
addrStr := addr.String()
|
|
|
|
ipaddr, _, err := net.SplitHostPort(addrStr)
|
2016-10-13 09:36:44 +02:00
|
|
|
//TODO(dan): Why is this needed, does this happen?
|
2012-12-09 07:54:58 +01:00
|
|
|
if err != nil {
|
2016-10-11 15:51:46 +02:00
|
|
|
return addrStr
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2016-10-11 15:51:46 +02:00
|
|
|
return ipaddr
|
2014-02-13 22:19:26 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 09:36:44 +02:00
|
|
|
// AddrLookupHostname returns the hostname (if possible) or address for the given `net.Addr`.
|
2016-10-11 15:51:46 +02:00
|
|
|
func AddrLookupHostname(addr net.Addr) string {
|
2018-02-01 21:53:49 +01:00
|
|
|
if AddrIsUnix(addr) {
|
|
|
|
return "localhost"
|
|
|
|
}
|
2014-02-13 22:19:26 +01:00
|
|
|
return LookupHostname(IPString(addr))
|
2014-02-11 04:39:53 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:07:57 +01:00
|
|
|
// AddrIsLocal returns whether the address is from a trusted local connection (loopback or unix).
|
|
|
|
func AddrIsLocal(addr net.Addr) bool {
|
|
|
|
if tcpaddr, ok := addr.(*net.TCPAddr); ok {
|
|
|
|
return tcpaddr.IP.IsLoopback()
|
|
|
|
}
|
2018-02-01 21:53:49 +01:00
|
|
|
_, ok := addr.(*net.UnixAddr)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddrToIP returns the IP address for a net.Addr, or nil if it's a unix domain socket.
|
|
|
|
func AddrToIP(addr net.Addr) net.IP {
|
|
|
|
if tcpaddr, ok := addr.(*net.TCPAddr); ok {
|
|
|
|
return tcpaddr.IP
|
2018-02-01 03:07:57 +01:00
|
|
|
}
|
2018-02-01 21:53:49 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddrIsUnix returns whether the address is a unix domain socket.
|
|
|
|
func AddrIsUnix(addr net.Addr) bool {
|
|
|
|
_, ok := addr.(*net.UnixAddr)
|
|
|
|
return ok
|
2018-02-01 03:07:57 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 09:36:44 +02:00
|
|
|
// LookupHostname returns the hostname for `addr` if it has one. Otherwise, just returns `addr`.
|
2016-10-11 15:51:46 +02:00
|
|
|
func LookupHostname(addr string) string {
|
|
|
|
names, err := net.LookupAddr(addr)
|
2017-09-05 17:43:50 +02:00
|
|
|
if err == nil && len(names) > 0 {
|
|
|
|
candidate := strings.TrimSuffix(names[0], ".")
|
|
|
|
if IsHostname(candidate) {
|
|
|
|
return candidate
|
2016-10-16 06:17:29 +02:00
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2014-02-14 03:59:45 +01:00
|
|
|
|
2017-09-05 17:43:50 +02:00
|
|
|
// return original address if no hostname found
|
|
|
|
if len(addr) > 0 && addr[0] == ':' {
|
|
|
|
// fix for IPv6 hostnames (so they don't start with a colon), same as all other IRCds
|
|
|
|
addr = "0" + addr
|
|
|
|
}
|
|
|
|
return addr
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2016-04-21 02:48:15 +02:00
|
|
|
|
|
|
|
var allowedHostnameChars = "abcdefghijklmnopqrstuvwxyz1234567890-."
|
|
|
|
|
2016-10-13 09:36:44 +02:00
|
|
|
// IsHostname returns whether we consider `name` a valid hostname.
|
2016-04-21 02:48:15 +02:00
|
|
|
func IsHostname(name string) bool {
|
|
|
|
// IRC hostnames specifically require a period
|
|
|
|
if !strings.Contains(name, ".") || len(name) < 1 || len(name) > 253 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure each part of hostname is valid
|
|
|
|
for _, part := range strings.Split(name, ".") {
|
|
|
|
if len(part) < 1 || len(part) > 63 || strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure all chars of hostname are valid
|
|
|
|
for _, char := range strings.Split(strings.ToLower(name), "") {
|
|
|
|
if !strings.Contains(allowedHostnameChars, char) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2019-01-22 11:01:01 +01:00
|
|
|
|
|
|
|
// NormalizeIPToNet represents an address (v4 or v6) as the v6 /128 CIDR
|
|
|
|
// containing only it.
|
|
|
|
func NormalizeIPToNet(addr net.IP) (network net.IPNet) {
|
|
|
|
// represent ipv4 addresses as ipv6 addresses, using the 4-in-6 prefix
|
|
|
|
// (actually this should be a no-op for any address returned by ParseIP)
|
|
|
|
addr = addr.To16()
|
|
|
|
// the network corresponding to this address is now an ipv6 /128:
|
|
|
|
return net.IPNet{
|
|
|
|
IP: addr,
|
|
|
|
Mask: mask128,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NormalizeNet normalizes an IPNet to a v6 CIDR, using the 4-in-6 prefix.
|
|
|
|
// (this is like IP.To16(), but for IPNet instead of IP)
|
|
|
|
func NormalizeNet(network net.IPNet) (result net.IPNet) {
|
|
|
|
if len(network.IP) == 16 {
|
|
|
|
return network
|
|
|
|
}
|
|
|
|
ones, _ := network.Mask.Size()
|
|
|
|
return net.IPNet{
|
|
|
|
IP: network.IP.To16(),
|
|
|
|
// include the 96 bits of the 4-in-6 prefix
|
|
|
|
Mask: net.CIDRMask(96+ones, 128),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a network, produce a human-readable string
|
|
|
|
// (i.e., CIDR if it's actually a network, IPv6 address if it's a v6 /128,
|
|
|
|
// dotted quad if it's a v4 /32).
|
|
|
|
func NetToNormalizedString(network net.IPNet) string {
|
|
|
|
ones, bits := network.Mask.Size()
|
|
|
|
if ones == bits && ones == len(network.IP)*8 {
|
|
|
|
// either a /32 or a /128, output the address:
|
|
|
|
return network.IP.String()
|
|
|
|
}
|
|
|
|
return network.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a human-readable description (an address or CIDR, either v4 or v6)
|
|
|
|
// into a normalized v6 net.IPNet.
|
|
|
|
func NormalizedNetFromString(str string) (result net.IPNet, err error) {
|
|
|
|
_, network, err := net.ParseCIDR(str)
|
|
|
|
if err == nil {
|
|
|
|
return NormalizeNet(*network), nil
|
|
|
|
}
|
|
|
|
ip := net.ParseIP(str)
|
|
|
|
if ip == nil {
|
|
|
|
err = &net.AddrError{
|
|
|
|
Err: "Couldn't interpret as either CIDR or address",
|
|
|
|
Addr: str,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return NormalizeIPToNet(ip), nil
|
|
|
|
}
|