Skip to content

Commit

Permalink
netlist: revert d3d14ae
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Mar 30, 2021
1 parent 99a11c0 commit a55faf0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
21 changes: 8 additions & 13 deletions dispatcher/pkg/matcher/netlist/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ import (
// List is a list of Nets. All Nets will be in ipv6 format, even it's an
// ipv4 addr. Because we use bin search.
type List struct {
e []*Net
e []Net
sorted bool
}

// NewList returns a *List.
func NewList() *List {
return &List{
e: make([]*Net, 0),
e: make([]Net, 0),
}
}

// Append appends new Nets to the list.
// This modified list. Caller must call List.Sort() before calling List.Contains()
func (list *List) Append(newNet ...*Net) {
func (list *List) Append(newNet ...Net) {
list.e = append(list.e, newNet...)
list.sorted = false
}
Expand All @@ -60,16 +60,11 @@ func (list *List) Sort() {
sort.Sort(list)

result := list.e[:0]
lastValid := 0
for i := range list.e {
if i == 0 { // first elem
result = append(result, list.e[i])
continue
}

if !list.e[lastValid].Contains(list.e[i].ip) {
result = append(result, list.e[i])
lastValid = i
var lastValid Net
for i, n := range list.e {
if i == 0 || !lastValid.Contains(n.ip) {
lastValid = n
result = append(result, lastValid)
}
}

Expand Down
20 changes: 10 additions & 10 deletions dispatcher/pkg/matcher/netlist/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ type Net struct {

// NewNet returns a new IPNet, mask should be an ipv6 mask,
// which means you should +96 if you have an ipv4 mask.
func NewNet(ipv6 IPv6, m int) *Net {
func NewNet(ipv6 IPv6, m int) Net {
um := mask(m)
n := &Net{
n := Net{
ip: ipv6,
mask: um,
}
Expand All @@ -85,7 +85,7 @@ func NewNet(ipv6 IPv6, m int) *Net {
}

// Contains reports whether the net includes the ip.
func (n *Net) Contains(ip IPv6) bool {
func (n Net) Contains(ip IPv6) bool {
for offset := uint8(0); offset < 2; offset++ {
if ip[offset]&getMask(n.mask, offset) == n.ip[offset] {
continue
Expand Down Expand Up @@ -145,18 +145,18 @@ func ParseIP(s string) (IPv6, IPVersion, error) {

// ParseCIDR parses s as a CIDR notation IP address and prefix length.
// As defined in RFC 4632 and RFC 4291.
func ParseCIDR(s string) (*Net, error) {
func ParseCIDR(s string) (Net, error) {
ipStr, maskStr, ok := utils.SplitString2(s, "/")
if ok { // has "/"
// ip
ipv6, version, err := ParseIP(ipStr)
if err != nil {
return nil, err
return Net{}, err
}
// mask
maskLen, err := strconv.ParseUint(maskStr, 10, 0)
if err != nil {
return nil, fmt.Errorf("invalid cidr mask %s", s)
return Net{}, fmt.Errorf("invalid cidr mask %s", s)
}

// if string is a ipv4 addr, add 96
Expand All @@ -165,15 +165,15 @@ func ParseCIDR(s string) (*Net, error) {
}

if maskLen > 128 {
return nil, fmt.Errorf("cidr mask %s overflow", s)
return Net{}, fmt.Errorf("cidr mask %s overflow", s)
}

return NewNet(ipv6, int(maskLen)), nil
}

ipv6, _, err := ParseIP(s)
if err != nil {
return nil, err
return Net{}, err
}
return NewNet(ipv6, 128), nil
}
Expand All @@ -198,13 +198,13 @@ func uint64ToBytes(in [2]uint64, out []byte) {
binary.BigEndian.PutUint64(out[8:], in[1])
}

func (n *Net) ToNetIPNet() *net.IPNet {
func (n Net) ToNetIPNet() *net.IPNet {
nn := new(net.IPNet)
nn.IP = n.ip.ToNetIP()
nn.Mask = n.mask.toNetMask()
return nn
}

func (n *Net) String() string {
func (n Net) String() string {
return n.ToNetIPNet().String()
}

0 comments on commit a55faf0

Please sign in to comment.