wgengine/magicsock: use netaddr more

In prep for deleting from the ever-growing maps.
This commit is contained in:
Brad Fitzpatrick
2020-04-17 15:15:42 -07:00
parent 6fb30ff543
commit 7fc97c5493
3 changed files with 15 additions and 12 deletions

View File

@@ -1513,7 +1513,8 @@ type AddrSet struct {
// [DERP fakeip:node, Global IP:port, LAN ip:port]
//
// But there could be multiple or none of each.
addrs []net.UDPAddr
addrs []net.UDPAddr
ipPorts []netaddr.IPPort // same as addrs, in different form
// clock, if non-nil, is used in tests instead of time.Now.
clock func() time.Time
@@ -1748,12 +1749,17 @@ func (c *Conn) CreateEndpoint(key [32]byte, addrs string) (conn.Endpoint, error)
if addrs != "" {
for _, ep := range strings.Split(addrs, ",") {
addr, err := net.ResolveUDPAddr("udp", ep)
ua, err := net.ResolveUDPAddr("udp", ep)
if err != nil {
return nil, err
}
addr.IP = ip4or6(addr.IP)
a.addrs = append(a.addrs, *addr)
ipp, ok := netaddr.FromStdAddr(ua.IP, ua.Port, ua.Zone)
if !ok {
return nil, fmt.Errorf("bogus address %q", ep)
}
ua.IP = ipp.IP.IPAddr().IP // makes IPv4 addresses 4 bytes long
a.ipPorts = append(a.ipPorts, ipp)
a.addrs = append(a.addrs, *ua)
}
}
@@ -1903,13 +1909,6 @@ func peerShort(k key.Public) string {
return k2.ShortString()
}
func ip4or6(ip net.IP) net.IP {
if ip4 := ip.To4(); ip4 != nil {
return ip4
}
return ip
}
func sbPrintAddr(sb *strings.Builder, a net.UDPAddr) {
is6 := a.IP.To4() == nil
if is6 {