Skip to content

Commit 5ee38d1

Browse files
committed
Merge branch 'addr-checks'
2 parents 027a108 + 3b4f0b7 commit 5ee38d1

4 files changed

Lines changed: 53 additions & 13 deletions

File tree

client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,18 @@ func (c *Client) authenticate(ctx context.Context, conn *quic.Conn, retoken []by
351351
return nil, fmt.Errorf("authentication failed: %w", resp.Error)
352352
}
353353

354+
publicAddr, err := resp.Public.AsNetip()
355+
if err != nil {
356+
return nil, fmt.Errorf("authentication convert public address failed: %w", err)
357+
}
358+
354359
c.addrs.Update(func(t advertiseAddrs) advertiseAddrs {
355-
c.logger.Debug("updating nat stun", "addr", resp.Public.AsNetip())
356-
t.STUN = []netip.AddrPort{resp.Public.AsNetip()}
360+
c.logger.Debug("updating nat stun", "addr", publicAddr)
361+
t.STUN = []netip.AddrPort{publicAddr}
357362
return t
358363
})
359364

360-
c.logger.Info("authenticated to server", "addr", c.controlAddr, "direct", resp.Public.AsNetip())
365+
c.logger.Info("authenticated to server", "addr", c.controlAddr, "direct", publicAddr)
361366
return &session{conn, resp.ReconnectToken}, nil
362367
}
363368

peer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,11 @@ func (p *peer) status() (StatusPeer, error) {
441441
ID: peer.Id,
442442
Metadata: peer.Metadata,
443443
DirectAddrs: iterc.MapSlice(peer.Peer.Directs, func(addr *pbmodel.AddrPort) string {
444-
return addr.AsNetip().String()
444+
naddr, err := addr.AsNetip()
445+
if err != nil {
446+
return fmt.Sprintf("invalid address: %v", err)
447+
}
448+
return naddr.String()
445449
}),
446450
}
447451
}

peer_remote.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ func (p *remotePeer) runErr(ctx context.Context) error {
9090

9191
addrs := map[netip.AddrPort]struct{}{}
9292
for _, addr := range remote.Peer.Directs {
93-
addrs[addr.AsNetip()] = struct{}{}
93+
if a, err := addr.AsNetip(); err != nil {
94+
p.logger.Warn("skipping invalid direct address", "err", err)
95+
} else {
96+
addrs[a] = struct{}{}
97+
}
9498
}
9599
if p.outgoing == nil {
96100
remoteServerConf, err := newServerTLSConfig(remote.Peer.ServerCertificate)

proto/pbmodel/addr.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pbmodel
22

33
import (
4+
"fmt"
45
"net"
56
"net/netip"
67

@@ -16,11 +17,23 @@ func AddrFromNetip(addr netip.Addr) *Addr {
1617
return &Addr{V4: v4[:]}
1718
}
1819

19-
func (a *Addr) AsNetip() netip.Addr {
20+
func (a *Addr) AsNetip() (netip.Addr, error) {
21+
if a == nil {
22+
return netip.Addr{}, fmt.Errorf("parse addr: nil")
23+
}
2024
if len(a.V6) > 0 {
21-
return netip.AddrFrom16([16]byte(a.V6))
25+
if len(a.V6) != 16 {
26+
return netip.Addr{}, fmt.Errorf("parse addr: v6 length is %d, want 16", len(a.V6))
27+
}
28+
return netip.AddrFrom16([16]byte(a.V6)), nil
29+
}
30+
if len(a.V4) == 0 {
31+
return netip.Addr{}, fmt.Errorf("parse addr: v4 and v6 both empty")
2232
}
23-
return netip.AddrFrom4([4]byte(a.V4))
33+
if len(a.V4) != 4 {
34+
return netip.Addr{}, fmt.Errorf("parse addr: v4 length is %d, want 4", len(a.V4))
35+
}
36+
return netip.AddrFrom4([4]byte(a.V4)), nil
2437
}
2538

2639
func AddrPortFromNet(addr net.Addr) (*AddrPort, error) {
@@ -38,16 +51,30 @@ func AddrPortFromNetip(addr netip.AddrPort) *AddrPort {
3851
}
3952
}
4053

41-
func (a *AddrPort) AsNetip() netip.AddrPort {
42-
return netip.AddrPortFrom(a.Addr.AsNetip(), uint16(a.Port))
54+
func (a *AddrPort) AsNetip() (netip.AddrPort, error) {
55+
if a == nil {
56+
return netip.AddrPort{}, fmt.Errorf("parse addrport: nil")
57+
}
58+
if a.Addr == nil {
59+
return netip.AddrPort{}, fmt.Errorf("parse addrport: missing addr")
60+
}
61+
addr, err := a.Addr.AsNetip()
62+
if err != nil {
63+
return netip.AddrPort{}, err
64+
}
65+
return netip.AddrPortFrom(addr, uint16(a.Port)), nil
4366
}
4467

45-
func AsNetips(pb []*AddrPort) []netip.AddrPort {
68+
func AsNetips(pb []*AddrPort) ([]netip.AddrPort, error) {
69+
var err error
4670
s := make([]netip.AddrPort, len(pb))
4771
for i, pbi := range pb {
48-
s[i] = pbi.AsNetip()
72+
s[i], err = pbi.AsNetip()
73+
if err != nil {
74+
return nil, err
75+
}
4976
}
50-
return s
77+
return s, nil
5178
}
5279

5380
func AsAddrPorts(addrs []netip.AddrPort) []*AddrPort {

0 commit comments

Comments
 (0)