Skip to content

Commit

Permalink
IDs Remove 2byte limit
Browse files Browse the repository at this point in the history
Summary: fbdns-data (data producer) part of D55709600

Reviewed By: abulimov

Differential Revision: D55745224

fbshipit-source-id: 1a1e0c6b6751fbcc8123499cad8f2d08383b47bf
  • Loading branch information
deathowl authored and facebook-github-bot committed Apr 24, 2024
1 parent b9b5ef9 commit 197a8fe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
44 changes: 35 additions & 9 deletions dnsrocks/dnsdata/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ type Rtype string
type Loc []byte

// Lmap is map ID
type Lmap [2]byte
type Lmap []byte

// WireType represent DNS wire types
type WireType uint16
Expand Down Expand Up @@ -577,8 +577,8 @@ func (r *Rrangepoint) UnmarshalText(text []byte) error {
if err != nil {
return err
}
r.pt.location.locID = make([]byte, len(locID))
copy(r.pt.location.locID[:], locID)

r.pt.location.locIDIsNull = (locID == nil)
if !r.pt.location.locIDIsNull && ip.To4() != nil {
r.pt.location.maskLen += (net.IPv6len - net.IPv4len) * 8
Expand Down Expand Up @@ -1478,16 +1478,16 @@ func getloc(b []byte) (Loc, error) {
if err != nil {
return Loc(nil), err
}
if len(q) != 2 {
if len(q) < 2 {
return Loc(nil), nil
}
return Loc(q), nil
}

func getlmap(b []byte) Lmap {
q, _ := quote.Bunquote(b) // BUG: handle error
var a [2]byte
copy(a[:], q)
var a = make([]byte, len(q))
copy(a, q)
return Lmap(a)
}

Expand Down Expand Up @@ -1568,7 +1568,12 @@ func putloc(w io.Writer, lo Loc) {
if len(lo) == 2 {
_, err = w.Write(lo)
} else {
_, err = w.Write([]byte{0, 0})
if len(lo) < 2 {
_, err = w.Write([]byte{0, 0})
} else {
_, _ = w.Write([]byte{0xff, byte(len(lo))})
_, err = w.Write(lo)
}
}
if err != nil {
glog.Errorf("%v", err)
Expand All @@ -1583,13 +1588,29 @@ func putloctext(w io.Writer, lo Loc) {

// write a two-byte location map ID
func putlmap(w io.Writer, m Lmap) {
_, err := w.Write(m[:])
var err error
if len(m) == 2 {
_, err = w.Write(m)
} else {
if len(m) < 2 {
_, err = w.Write([]byte{0, 0})
} else {
_, err = w.Write([]byte{0xff, byte(len(m))})
if err != nil {
glog.Errorf("%v", err)
}
_, err = w.Write(m)
}
}
if err != nil {
glog.Errorf("%v", err)
}
}

func putlmaptext(w io.Writer, m Lmap) {
if len(m) == 0 {
m = []byte{0, 0}
}
for _, b := range m {
fmt.Fprintf(w, "\\%03o", b)
}
Expand All @@ -1607,7 +1628,7 @@ func putrrhead(w io.Writer, t WireType, ttl uint32, loc Loc, iswildcard bool) {
if err != nil {
glog.Errorf("%v", err)
}
if len(loc) != 2 || (loc[0] == 0 && loc[1] == 0) {
if len(loc) < 2 || (len(loc) == 2 && (loc[0] == 0 && loc[1] == 0)) {
if iswildcard {
_, err = w.Write([]byte("*"))
} else {
Expand All @@ -1625,7 +1646,12 @@ func putrrhead(w io.Writer, t WireType, ttl uint32, loc Loc, iswildcard bool) {
if err != nil {
glog.Errorf("%v", err)
}
_, err = w.Write(loc)
if len(loc) > 2 {
_, _ = w.Write([]byte{0xff, byte(len(loc))})
_, err = w.Write(loc)
} else {
_, err = w.Write(loc)
}
if err != nil {
glog.Errorf("%v", err)
}
Expand Down
14 changes: 7 additions & 7 deletions dnsrocks/dnsdata/rearranger.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type rangeLocation struct {
// this mask is the length of the matched prefix
maskLen uint8
locIDIsNull bool
locID [2]byte
locID []byte
}

// RangePoint stores range point along with associated location ID
Expand All @@ -59,6 +59,7 @@ type Rearranger struct {
hasDefaultIPv4Range bool
hasDefaultIPv6Range bool
points RangePoints
lmap Lmap
}

// String returns a string representation of these RangePoints, useful for debugging
Expand Down Expand Up @@ -122,7 +123,7 @@ func (p *RangePoint) LocIsNull() bool {

// LocID returns the location ID for this range
func (p *RangePoint) LocID() []byte {
return p.location.locID[:]
return p.location.locID
}

// String returns a string representation of this location, used for debugging
Expand Down Expand Up @@ -157,14 +158,13 @@ func NewRearranger(locationCount int) *Rearranger {
// ErrInvalidLocation is used when location doesn't match expectations (nil or exactly 2 bytes)
var ErrInvalidLocation = errors.New("location should be either nil or exactly 2 bytes long as FBDNS depends on it")

func copyLocID(locID []byte) ([2]byte, error) {
var x [2]byte
func copyLocID(locID []byte) ([]byte, error) {
var x = make([]byte, len(locID))

if locID == nil || len(locID) != 2 {
if locID == nil || len(locID) < 2 {
return x, fmt.Errorf("%w. location value '%v'", ErrInvalidLocation, locID)
}
copy(x[:], locID)

copy(x, locID)
return x, nil
}

Expand Down
12 changes: 6 additions & 6 deletions dnsrocks/dnsdata/rearranger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,10 +849,10 @@ func TestInvalidLocation(t *testing.T) {
}

testCases := []testCase{
{
network: "31.185.13.0/24",
locID: []byte{0, 1, 2, 3},
},
// {
// network: "31.185.13.0/24",
// locID: []byte{0, 1, 2, 3},
// }, We can be longer than 2 bytes now, so this is a bad testcase.
{
network: "31.185.13.0/24",
locID: []byte{1},
Expand Down Expand Up @@ -1035,7 +1035,7 @@ func TestRangePointMarshalTextForLmap(t *testing.T) {
rangeStart: ParseIP("1.1.1.1"),
location: rangeLocation{
maskLen: 120,
locID: [2]byte{100, 101},
locID: []byte{100, 101},
locIDIsNull: false,
},
},
Expand All @@ -1047,7 +1047,7 @@ func TestRangePointMarshalTextForLmap(t *testing.T) {
rangeStart: ParseIP("2a00:1fa0:42d8::"),
location: rangeLocation{
maskLen: 64,
locID: [2]byte{97, 1},
locID: []byte{97, 1},
locIDIsNull: false,
},
},
Expand Down
17 changes: 9 additions & 8 deletions dnsrocks/dnsdata/subnetranger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ const (
// the auxiliary structures for the subnet-based location lookup
type SubnetRanger struct {
enabled bool
arng map[Lmap]*Rearranger
arng map[string]*Rearranger
}

// Enable enables the functionality
func (r *SubnetRanger) Enable() {
r.arng = make(map[Lmap]*Rearranger)
r.arng = make(map[string]*Rearranger)
r.enabled = true
}

func (r *SubnetRanger) getRearranger(lmap Lmap) *Rearranger {
a := r.arng[lmap]
a := r.arng[lmap.String()]
if a == nil {
a = NewRearranger(InitLocationCount)
r.arng[lmap] = a
a.lmap = lmap
r.arng[lmap.String()] = a
}
return a
}
Expand All @@ -65,9 +66,9 @@ func (r *SubnetRanger) MarshalMap() (result []MapRecord, err error) {

group := &errgroup.Group{}

for lmap, a := range r.arng {
for _, a := range r.arng {
// closures
localLmap := lmap
localLmap := a.lmap
rearranger := a

group.Go(
Expand Down Expand Up @@ -114,9 +115,9 @@ func (r *SubnetRanger) OpenScanner() (s *SubnetRangerScanner) {

group := &errgroup.Group{}

for lmap, a := range r.arng {
for _, a := range r.arng {
// closures
localLmap := lmap
localLmap := a.lmap
rearranger := a

group.Go(
Expand Down

0 comments on commit 197a8fe

Please sign in to comment.