Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for inserting ranges. #25

Merged
merged 3 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.14
require (
github.com/oschwald/maxminddb-golang v1.7.1-0.20200819192241-1f1e288ee3f9
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.6.0
github.com/stretchr/testify v1.7.0
inet.af/netaddr v0.0.0-20210729200904-31d5ee66059c
)
44 changes: 41 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415 h1:q1oJaUPdmpDm/VyXosjgPgr6wS7c5iV2p0PwJD73bUI=
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/oschwald/maxminddb-golang v1.7.1-0.20200819192241-1f1e288ee3f9 h1:VA/ssbFy4lxVq8zTwwNlw2n0d9QTvKiq8uxhMVM2nl4=
github.com/oschwald/maxminddb-golang v1.7.1-0.20200819192241-1f1e288ee3f9/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.6.0 h1:hUDfIISABYI59DyeB3OTay/HxSRwTQ8rB/H83k6r5dM=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 h1:Dho5nD6R3PcW2SH1or8vS0dszDaXRxIw55lBX7XiE5g=
github.com/yuin/goldmark v1.2.1 h1:ruQGxdhGHe7FWOJPT0mKs5+pD2Xs1Bm/kdGlHO04FmM=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go4.org/intern v0.0.0-20210108033219-3eb7198706b2 h1:VFTf+jjIgsldaz/Mr00VaCSswHJrI2hIjQygE/W4IMg=
go4.org/intern v0.0.0-20210108033219-3eb7198706b2/go.mod h1:vLqJ+12kCw61iCWsPto0EOHhBS+o4rO5VIucbc9g2Cc=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222175341-b30ae309168e/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222180813-1025295fd063 h1:1tk03FUNpulq2cuWpXZWj649rwJpk0d20rxWiopKRmc=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222180813-1025295fd063/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
inet.af/netaddr v0.0.0-20210729200904-31d5ee66059c h1:AmHBNAHZCfyMGFk5kd060kpnQ6GJLHmGwdJGGQgr1Y8=
inet.af/netaddr v0.0.0-20210729200904-31d5ee66059c/go.mod h1:z0nx+Dh+7N7CC8V5ayHtHGpZpxLQZZxkIaaz6HN65Ls=
14 changes: 7 additions & 7 deletions inserter/inserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/pkg/errors"
)

// InserterFunc is a function that returns the data type to be inserted into an
// Func is a function that returns the data type to be inserted into an
// mmdbwriter.Tree using some conflict resolution strategy.
type InserterFunc func(mmdbtype.DataType) (mmdbtype.DataType, error)
type Func func(mmdbtype.DataType) (mmdbtype.DataType, error)

// InserterFuncGenerator is a function that generates an InserterFunc given a
// FuncGenerator is a function that generates an Func given a
// value.
type InserterFuncGenerator func(value mmdbtype.DataType) InserterFunc
type FuncGenerator func(value mmdbtype.DataType) Func

// Remove any records for the network being inserted.
func Remove(value mmdbtype.DataType) (mmdbtype.DataType, error) {
Expand All @@ -22,7 +22,7 @@ func Remove(value mmdbtype.DataType) (mmdbtype.DataType, error) {

// ReplaceWith generates an inserter function that replaces the existing
// value with the new value.
func ReplaceWith(value mmdbtype.DataType) InserterFunc {
func ReplaceWith(value mmdbtype.DataType) Func {
return func(_ mmdbtype.DataType) (mmdbtype.DataType, error) {
return value, nil
}
Expand All @@ -34,7 +34,7 @@ func ReplaceWith(value mmdbtype.DataType) InserterFunc {
//
// Both the new and existing value must be a Map. An error will be returned
// otherwise.
func TopLevelMergeWith(newValue mmdbtype.DataType) InserterFunc {
func TopLevelMergeWith(newValue mmdbtype.DataType) Func {
return func(existingValue mmdbtype.DataType) (mmdbtype.DataType, error) {
newMap, ok := newValue.(mmdbtype.Map)
if !ok {
Expand Down Expand Up @@ -71,7 +71,7 @@ func TopLevelMergeWith(newValue mmdbtype.DataType) InserterFunc {
// DeepMergeWith creates an inserter that will recursively update an existing
// value. Map and Slice values will be merged recursively. Other values will
// be replaced by the new value.
func DeepMergeWith(newValue mmdbtype.DataType) InserterFunc {
func DeepMergeWith(newValue mmdbtype.DataType) Func {
return func(existingValue mmdbtype.DataType) (mmdbtype.DataType, error) {
return deepMerge(existingValue, newValue)
}
Expand Down
59 changes: 55 additions & 4 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/maxmind/mmdbwriter/mmdbtype"
"github.com/oschwald/maxminddb-golang"
"github.com/pkg/errors"
"inet.af/netaddr"
)

var (
Expand Down Expand Up @@ -76,7 +77,7 @@ type Options struct {
// Inserter is the insert function used when calling `Insert`. It defaults
// to `inserter.ReplaceWith`, which replaces any conflicting old value
// entirely with the new.
Inserter inserter.InserterFuncGenerator
Inserter inserter.FuncGenerator
}

// Tree represents an MaxMind DB search tree.
Expand All @@ -93,7 +94,7 @@ type Tree struct {
treeDepth int
// This is set when the tree is finalized
nodeCount int
inserterFuncGen inserter.InserterFuncGenerator
inserterFuncGen inserter.FuncGenerator
}

// New creates a new Tree.
Expand Down Expand Up @@ -246,15 +247,15 @@ func (t *Tree) Insert(network *net.IPNet, value mmdbtype.DataType) error {
// This is not safe to call from multiple threads.
func (t *Tree) InsertFunc(
network *net.IPNet,
inserter inserter.InserterFunc,
inserter inserter.Func,
) error {
return t.insert(network, recordTypeData, inserter, nil)
}

func (t *Tree) insert(
network *net.IPNet,
recordType recordType,
inserter inserter.InserterFunc,
inserter inserter.Func,
node *node,
) error {
// We set this to 0 so that the tree must be finalized again.
Expand Down Expand Up @@ -282,6 +283,56 @@ func (t *Tree) insert(
)
}

// InsertRange is the same as Insert, except it will insert all subnets within
// the range of IPs specified by `[start,end]`.
func (t *Tree) InsertRange(
start net.IP,
end net.IP,
value mmdbtype.DataType,
) error {
return t.InsertRangeFunc(start, end, t.inserterFuncGen(value))
}

// InsertRangeFunc is the same as InsertFunc, except it will insert all subnets
// within the range of IPs specified by `[start,end]`.
func (t *Tree) InsertRangeFunc(
start net.IP,
end net.IP,
inserter inserter.Func,
) error {
return t.insertRange(start, end, recordTypeData, inserter, nil)
}

func (t *Tree) insertRange(
start net.IP,
end net.IP,
recordType recordType,
inserter inserter.Func,
node *node,
) error {
_start, ok := netaddr.FromStdIP(start)
if !ok {
return errors.New("start IP is invalid")
}
_end, ok := netaddr.FromStdIP(end)
if !ok {
return errors.New("end IP is invalid")
}

r := netaddr.IPRangeFrom(_start, _end)
if !r.IsValid() {
return errors.New("start & end IPs did not give valid range")
}
subnets := r.Prefixes()
for _, subnet := range subnets {
if err := t.insert(subnet.IPNet(), recordType, inserter, node); err != nil {
return err
}
}

return nil
}

func (t *Tree) insertStringNetwork(
network string,
recordType recordType,
Expand Down
Loading