示例#1
0
func (ls *LevelDBStore) ExpandCIDR(ip string) ([]net.IP, error) {
	var ips []net.IP
	start, end, err := ipset.CIDRToByteStrings(ip)
	if err != nil {
		return ips, err
	}
	bstart := []byte(start)
	bend := []byte(end)
	iter := ls.db.NewIterator(&util.Range{Start: bstart, Limit: nil}, nil)
	for iter.Next() {
		key := iter.Key()
		if ignoreKey(key) {
			continue
		}
		if len(key) != len(start) {
			//Ensure the matched keys are in the right ip family
			continue
		}
		if bytes.Compare(key, bend) > 0 {
			break
		}
		keycopy := make([]byte, len(key))
		copy(keycopy, key)
		ip := net.IP(keycopy)
		ips = append(ips, ip)
	}
	iter.Release()
	err = iter.Error()
	return ips, err
}
示例#2
0
func (ls *LevelDBStore) QueryStringCidr(ip string) ([]string, error) {
	var start, end string
	start, end, err := ipset.CIDRToByteStrings(ip)
	if err != nil {
		return nil, err
	}
	bstart := []byte(start)
	bend := []byte(end)
	bs := bitset.New(8)
	iter := ls.db.NewIterator(&util.Range{Start: bstart, Limit: nil}, nil)

	codec := ls.codecFactory()
	for iter.Next() {
		key := iter.Key()
		if ignoreKey(key) {
			continue
		}
		if len(key) != len(start) {
			//Ensure the matched keys are in the right ip family
			continue
		}
		if bytes.Compare(key, bend) > 0 {
			break
		}
		codec.FromBytes(iter.Value())
		tmpBs := codec.ToBitset()
		bs = bs.Union(tmpBs)
	}
	iter.Release()
	err = iter.Error()
	if err != nil {
		return nil, err
	}

	return ls.bitsetToDocs(bs)
}