2016-02-16 21:46:40 +01:00
|
|
|
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mrd0ll4r/netmatch"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/server/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
store.RegisterIPStoreDriver("memory", &ipStoreDriver{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type ipStoreDriver struct{}
|
|
|
|
|
2016-03-11 21:09:49 +01:00
|
|
|
func (d *ipStoreDriver) New(_ *store.DriverConfig) (store.IPStore, error) {
|
2016-02-16 21:46:40 +01:00
|
|
|
return &ipStore{
|
|
|
|
ips: make(map[[16]byte]struct{}),
|
|
|
|
networks: netmatch.New(),
|
2016-06-19 18:49:43 +02:00
|
|
|
closed: make(chan struct{}),
|
2016-02-16 21:46:40 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ipStore implements store.IPStore using an in-memory map of byte arrays and
|
|
|
|
// a trie-like structure.
|
|
|
|
type ipStore struct {
|
|
|
|
ips map[[16]byte]struct{}
|
|
|
|
networks *netmatch.Trie
|
2016-06-19 18:49:43 +02:00
|
|
|
closed chan struct{}
|
2016-02-16 21:46:40 +01:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ store.IPStore = &ipStore{}
|
|
|
|
v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
|
|
|
|
)
|
|
|
|
|
|
|
|
// key converts an IP address to a [16]byte.
|
|
|
|
// The byte array can then be used as a key for a map, unlike net.IP, which is a
|
|
|
|
// []byte.
|
|
|
|
// If an IPv4 address is specified, it will be prefixed with
|
|
|
|
// the net.v4InV6Prefix and thus becomes a valid IPv6 address.
|
|
|
|
func key(ip net.IP) [16]byte {
|
|
|
|
var array [16]byte
|
|
|
|
|
|
|
|
if len(ip) == net.IPv4len {
|
|
|
|
copy(array[:], v4InV6Prefix)
|
|
|
|
copy(array[12:], ip)
|
|
|
|
} else {
|
|
|
|
copy(array[:], ip)
|
|
|
|
}
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ipStore) AddNetwork(network string) error {
|
|
|
|
key, length, err := netmatch.ParseNetwork(network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:56:07 +02:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2016-06-19 18:49:43 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
panic("attempted to interact with stopped store")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:46:40 +01:00
|
|
|
return s.networks.Add(key, length)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ipStore) AddIP(ip net.IP) error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2016-06-19 18:49:43 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
panic("attempted to interact with stopped store")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:46:40 +01:00
|
|
|
s.ips[key(ip)] = struct{}{}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ipStore) HasIP(ip net.IP) (bool, error) {
|
2016-05-01 23:56:07 +02:00
|
|
|
key := key(ip)
|
2016-02-16 21:46:40 +01:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2016-06-19 18:49:43 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
panic("attempted to interact with stopped store")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:46:40 +01:00
|
|
|
_, ok := s.ips[key]
|
|
|
|
if ok {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
match, err := s.networks.Match(key)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return match, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ipStore) HasAnyIP(ips []net.IP) (bool, error) {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2016-06-19 18:49:43 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
panic("attempted to interact with stopped store")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:46:40 +01:00
|
|
|
for _, ip := range ips {
|
|
|
|
key := key(ip)
|
|
|
|
if _, ok := s.ips[key]; ok {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
match, err := s.networks.Match(key)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if match {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ipStore) HasAllIPs(ips []net.IP) (bool, error) {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2016-06-19 18:49:43 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
panic("attempted to interact with stopped store")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:46:40 +01:00
|
|
|
for _, ip := range ips {
|
|
|
|
key := key(ip)
|
|
|
|
if _, ok := s.ips[key]; !ok {
|
|
|
|
match, err := s.networks.Match(key)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ipStore) RemoveIP(ip net.IP) error {
|
2016-05-01 23:56:07 +02:00
|
|
|
key := key(ip)
|
2016-02-16 21:46:40 +01:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2016-06-19 18:49:43 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
panic("attempted to interact with stopped store")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:56:07 +02:00
|
|
|
if _, ok := s.ips[key]; !ok {
|
|
|
|
return store.ErrResourceDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(s.ips, key)
|
2016-02-16 21:46:40 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ipStore) RemoveNetwork(network string) error {
|
|
|
|
key, length, err := netmatch.ParseNetwork(network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:56:07 +02:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2016-06-19 18:49:43 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
panic("attempted to interact with stopped store")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:56:07 +02:00
|
|
|
err = s.networks.Remove(key, length)
|
|
|
|
if err != nil && err == netmatch.ErrNotContained {
|
|
|
|
return store.ErrResourceDoesNotExist
|
|
|
|
}
|
|
|
|
return err
|
2016-02-16 21:46:40 +01:00
|
|
|
}
|
2016-06-19 18:49:43 +02:00
|
|
|
|
|
|
|
func (s *ipStore) Stop() <-chan error {
|
|
|
|
toReturn := make(chan error)
|
|
|
|
go func() {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.ips = make(map[[16]byte]struct{})
|
|
|
|
s.networks = netmatch.New()
|
|
|
|
close(s.closed)
|
|
|
|
close(toReturn)
|
|
|
|
}()
|
|
|
|
return toReturn
|
|
|
|
}
|