talos/pkg/net/net.go
Seán C McCord d0ff28a8c7 fix: enclose server address is bracks if IPv6
Fixes #980

Signed-off-by: Seán C McCord <ulexus@gmail.com>
2019-08-10 17:42:17 -07:00

44 lines
957 B
Go

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package net
import (
"net"
)
// IPAddrs finds and returns a list of non-loopback IPv4 addresses of the
// current machine.
func IPAddrs() (ips []net.IP, err error) {
ips = []net.IP{}
addrs, err := net.InterfaceAddrs()
if err != nil {
return
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP)
}
}
}
return ips, nil
}
// FormatAddress checks that the address has a consistent format.
func FormatAddress(addr string) string {
if ip := net.ParseIP(addr); ip != nil {
// If this is an IPv6 address, encapsulate it in brackets
if ip.To16() != nil {
return "[" + ip.String() + "]"
}
return ip.String()
}
return addr
}