mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-25 16:41:12 +02:00
When talking to an IPv6 address for a gRPC server, enclose the IPv6 address in brackets. Also fixes backwards implementation of IPv4/IPv6 test. Fixes #983 Signed-off-by: Seán C McCord <ulexus@gmail.com>
44 lines
956 B
Go
44 lines
956 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.To4() == nil {
|
|
return "[" + ip.String() + "]"
|
|
}
|
|
return ip.String()
|
|
}
|
|
return addr
|
|
}
|