tlv: small cleanups

This commit is contained in:
Johannes Kimmel 2023-03-09 10:43:21 +01:00
parent a3905135ff
commit 69e2890ea0
1 changed files with 32 additions and 25 deletions

View File

@ -84,7 +84,7 @@ func AEFromPrefix(p netip.Prefix) AEType {
// AEFromIP returns the address encoding of an address.
func AEFromIP(p netip.Addr) AEType {
switch {
case p.IsUnspecified():
case p.IsUnspecified(), p == netip.Addr{}:
return AEWildcard
case p.Is4():
return AEIPv4
@ -541,14 +541,12 @@ func routerIDFromBytes(b []byte) (RouterID, []byte, error) {
}
// skip 2 reserved bytes
var rid RouterID
copy(rid[:], b[2:])
b = b[10:]
rid, b := RouterID(b[2:10]), b[10:]
if rid == (RouterID{}) {
switch rid {
case RouterID{}:
return RouterID{}, b, ErrRouterIDZeros
}
if rid == (RouterID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}) {
case RouterID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}:
return RouterID{}, b, ErrRouterIDOnes
}
@ -655,7 +653,7 @@ func (s *PacketDecoder) updateFromBytes(b []byte) (Update, []byte, error) {
var u Update
var err error
ae := b[0]
ae := AEType(b[0])
u.Flags = b[1]
plen := b[2]
u.Omitted = b[3]
@ -665,22 +663,26 @@ func (s *PacketDecoder) updateFromBytes(b []byte) (Update, []byte, error) {
b = b[10:]
switch ae {
case 0:
case 1:
case AEWildcard:
case AEIPv4:
u.Prefix, b, err = prefixV4Default(s.v4, plen, u.Omitted, b)
if u.Flags&0x80 > 0 {
s.v4 = u.Prefix.Addr().As4()
}
u.NextHop = s.nexthopv4
case 2:
case AEIPv6:
u.Prefix, b, err = prefixV6Default(s.v6, plen, u.Omitted, b)
if u.Flags&0x80 > 0 {
s.v6 = u.Prefix.Addr().As16()
}
u.NextHop = s.nexthopv6
case 3:
case AEIPv6LL:
u.Prefix, b, err = prefixV6LL(b)
u.NextHop = s.nexthopv6
case AEIPv4oIPv6:
return Update{}, b, fmt.Errorf("Not implemented AE Type %s", ae)
default:
return Update{}, b, fmt.Errorf("Unknown AE Type %d", ae)
}
if u.Flags&0x40 > 0 {
@ -688,7 +690,7 @@ func (s *PacketDecoder) updateFromBytes(b []byte) (Update, []byte, error) {
switch {
case addr.Is6():
v6 := addr.As16()
copy(s.routerID[:], v6[8:])
s.routerID = RouterID(v6[8:])
case addr.Is4():
s.routerID = RouterID{}
v4 := addr.As4()
@ -769,7 +771,7 @@ func seqnoRequestFromBytes(b []byte) (SeqnoRequest, []byte, error) {
plen := b[1]
sr.Seqno = uint16(b[2])<<8 | uint16(b[3])
sr.HopCount = b[4]
copy(sr.RouterID[:], b[6:])
sr.RouterID = RouterID(b[6:])
sr.Prefix, b, err = prefixUncompressed(ae, plen, b[14:])
return sr, b, err
@ -806,7 +808,7 @@ func SourcePrefixFromBytes(b []byte) (SourcePrefix, []byte, error) {
}
func psizeFromPlen(plen uint8) uint8 {
return uint8((uint16(plen) + 7) / 8)
return (plen + 7) / 8
}
func prefixUncompressed(ae uint8, plen uint8, b []byte) (netip.Prefix, []byte, error) {
if len(b) < int(psizeFromPlen(plen)) {
@ -858,28 +860,33 @@ func prefixV6LL(b []byte) (netip.Prefix, []byte, error) {
copy(ip6ll[8:], b[:8])
return netip.PrefixFrom(netip.AddrFrom16(ip6ll), 8), b[8:], nil
}
type ErrorIpFromBytesLength struct {
ae AEType
length int
}
func (e ErrorIpFromBytesLength) Error() string {
return fmt.Sprintf("Not enough bytes for address encoding %s: %d", e.ae, e.length)
}
func ipFromBytes(ae AEType, b []byte) (netip.Addr, []byte, error) {
switch ae {
case AEWildcard:
return netip.Addr{}, b, nil
case AEIPv4:
if len(b) < 4 {
return netip.Addr{}, b, fmt.Errorf("Not enough bytes for v4 address: %d", len(b))
return netip.Addr{}, b, ErrorIpFromBytesLength{ae: AEIPv4, length: len(b)}
}
//var ip4 [4]byte
//copy(ip4[:], b[:4])
ip4 := [4]byte(b[:4])
return netip.AddrFrom4(ip4), b[4:], nil
return netip.AddrFrom4([4]byte(b)), b[4:], nil
case AEIPv6:
if len(b) < 16 {
return netip.Addr{}, b, fmt.Errorf("Not enough bytes for v6 address: %d", len(b))
return netip.Addr{}, b, ErrorIpFromBytesLength{ae: AEIPv6, length: len(b)}
}
var ip6 [16]byte
copy(ip6[:], b[:16])
return netip.AddrFrom16(ip6), b[16:], nil
return netip.AddrFrom16([16]byte(b)), b[16:], nil
case AEIPv6LL:
if len(b) < 8 {
return netip.Addr{}, b, fmt.Errorf("Not enough bytes for v6ll address: %d", len(b))
return netip.Addr{}, b, ErrorIpFromBytesLength{ae: AEIPv6LL, length: len(b)}
}
var ip6ll [16]byte
ip6ll[0] = 0xfe