small pixel drawing of a pufferfish cascade

ipaddr/ipaddr.go

package ipaddr

import (
	"log"
	"net"
	"strconv"
	"strings"
)

// resolveHost will take a single host string and convert it to a list of TCPAddrs
// This will process any port in the input as well as looking up the hostname using
// normal DNS resolution.
func ParseIPPort(IPPort string) *net.TCPAddr {
	var addr net.TCPAddr
	ip, portStr, err := net.SplitHostPort(IPPort)
	if err != nil {
		if strings.Contains(err.Error(), "missing port in address") {
			ip = IPPort
		} else {
			log.Printf("error splitting ip+port '%s' into IP and port: %s\n", IPPort, err)
			return &addr
		}
	} else {
		addr.Port, err = strconv.Atoi(portStr)
		if err != nil {
			log.Printf("Parsed port '%s' is not an integer: %s\n", portStr, err)
			return &addr
		}
	}

	addr.IP = net.ParseIP(ip)

	return &addr
}