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 store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-06-19 18:49:43 +02:00
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/pkg/stopper"
|
2016-02-16 21:46:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var ipStoreDrivers = make(map[string]IPStoreDriver)
|
|
|
|
|
|
|
|
// IPStore represents an interface for manipulating IPs and IP ranges.
|
|
|
|
type IPStore interface {
|
|
|
|
// AddIP adds a single IP address to the IPStore.
|
|
|
|
AddIP(ip net.IP) error
|
|
|
|
|
|
|
|
// AddNetwork adds a range of IP addresses, denoted by a network in CIDR
|
|
|
|
// notation, to the IPStore.
|
|
|
|
AddNetwork(network string) error
|
|
|
|
|
|
|
|
// HasIP returns whether the given IP address is contained in the IPStore
|
2016-05-01 23:56:07 +02:00
|
|
|
// or belongs to any of the stored networks.
|
2016-02-16 21:46:40 +01:00
|
|
|
HasIP(ip net.IP) (bool, error)
|
|
|
|
|
2016-05-01 23:56:07 +02:00
|
|
|
// HasAnyIP returns whether any of the given IP addresses are contained
|
|
|
|
// in the IPStore or belongs to any of the stored networks.
|
2016-02-16 21:46:40 +01:00
|
|
|
HasAnyIP(ips []net.IP) (bool, error)
|
|
|
|
|
2016-05-01 23:56:07 +02:00
|
|
|
// HassAllIPs returns whether all of the given IP addresses are
|
|
|
|
// contained in the IPStore or belongs to any of the stored networks.
|
2016-02-16 21:46:40 +01:00
|
|
|
HasAllIPs(ips []net.IP) (bool, error)
|
|
|
|
|
|
|
|
// RemoveIP removes a single IP address from the IPStore.
|
|
|
|
//
|
|
|
|
// This wil not remove the given address from any networks it belongs to
|
|
|
|
// that are stored in the IPStore.
|
2016-05-01 23:56:07 +02:00
|
|
|
//
|
|
|
|
// Returns ErrResourceDoesNotExist if the given IP address is not
|
|
|
|
// contained in the store.
|
2016-02-16 21:46:40 +01:00
|
|
|
RemoveIP(ip net.IP) error
|
|
|
|
|
2016-05-01 23:56:07 +02:00
|
|
|
// RemoveNetwork removes a range of IP addresses that was previously
|
|
|
|
// added through AddNetwork.
|
2016-02-16 21:46:40 +01:00
|
|
|
//
|
|
|
|
// The given network must not, as a string, match the previously added
|
|
|
|
// network, but rather denote the same network, e.g. if the network
|
|
|
|
// 192.168.22.255/24 was added, removing the network 192.168.22.123/24
|
|
|
|
// will succeed.
|
2016-05-01 23:56:07 +02:00
|
|
|
//
|
|
|
|
// Returns ErrResourceDoesNotExist if the given network is not
|
|
|
|
// contained in the store.
|
2016-02-16 21:46:40 +01:00
|
|
|
RemoveNetwork(network string) error
|
2016-06-19 18:49:43 +02:00
|
|
|
|
|
|
|
// Stopper provides the Stop method that stops the IPStore.
|
|
|
|
// Stop should shut down the IPStore in a separate goroutine and send
|
|
|
|
// an error to the channel if the shutdown failed. If the shutdown
|
|
|
|
// was successful, the channel is to be closed.
|
|
|
|
stopper.Stopper
|
2016-02-16 21:46:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IPStoreDriver represents an interface for creating a handle to the
|
|
|
|
// storage of IPs.
|
|
|
|
type IPStoreDriver interface {
|
2016-03-11 21:09:49 +01:00
|
|
|
New(*DriverConfig) (IPStore, error)
|
2016-02-16 21:46:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterIPStoreDriver makes a driver available by the provided name.
|
|
|
|
//
|
|
|
|
// If this function is called twice with the same name or if the driver is nil,
|
|
|
|
// it panics.
|
|
|
|
func RegisterIPStoreDriver(name string, driver IPStoreDriver) {
|
|
|
|
if driver == nil {
|
2016-03-11 21:12:43 +01:00
|
|
|
panic("store: could not register nil IPStoreDriver")
|
2016-02-16 21:46:40 +01:00
|
|
|
}
|
|
|
|
if _, dup := ipStoreDrivers[name]; dup {
|
2016-03-11 21:12:43 +01:00
|
|
|
panic("store: could not register duplicate IPStoreDriver: " + name)
|
2016-02-16 21:46:40 +01:00
|
|
|
}
|
|
|
|
ipStoreDrivers[name] = driver
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenIPStore returns an IPStore specified by a configuration.
|
2016-03-11 21:09:49 +01:00
|
|
|
func OpenIPStore(cfg *DriverConfig) (IPStore, error) {
|
|
|
|
driver, ok := ipStoreDrivers[cfg.Name]
|
2016-02-16 21:46:40 +01:00
|
|
|
if !ok {
|
2016-03-11 21:12:43 +01:00
|
|
|
return nil, fmt.Errorf("store: unknown IPStoreDriver %q (forgotten import?)", cfg)
|
2016-02-16 21:46:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return driver.New(cfg)
|
|
|
|
}
|