db

package
v0.0.0-...-81bc7c3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package db is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLocationMismatch is returned if the record is not matching the location
	ErrLocationMismatch = errors.New("Location mismatch")
	// ErrWildcardMismatch is returned if the record is a wildcard but we are not
	// looking for wildcard
	ErrWildcardMismatch = errors.New("Wildcard mismatch")
)
View Source
var ErrReloadTimeout = errors.New("DB reload timeout")

ErrReloadTimeout - DB reload timeout

View Source
var ErrValidationKeyNotFound = errors.New("validation key not found in DB")

ErrValidationKeyNotFound - Key not found in DB file

View Source
var SeparateBitMap bool

Functions

func AdditionalSectionForRecords

func AdditionalSectionForRecords(r Reader, a *dns.Msg, locID ID, qclass uint16, records []dns.RR) (weighted bool)

AdditionalSectionForRecords given a list of records and a reader, add the required records to the Extra (additional) section. returns wether or not a weighted record was added to the additional section.

func FindECS

func FindECS(m *dns.Msg) *dns.EDNS0_SUBNET

FindECS finds a EDNS0_SUBNET option in a DNS Msg. It returns a pointer to dns.EDNS0_SUBNET or nil when there is not such EDNS0 option.

func FindSOA

func FindSOA(r Reader, zoneCut []byte, zoneCutString string, locID ID, a *dns.Msg)

FindSOA find SOA record and set it into the Authority section of the message.

func GetNs

func GetNs(r Reader, q []byte, zonename string, class uint16, locID ID) (ns []dns.RR, err error)

GetNs will find and return the authoritative NS for a specific domain `q`. `q` is in wire-format. This is not expecting that `q` is the actual qname which has NS records.

func HasRecord

func HasRecord(msg *dns.Msg, record string, qtype uint16) bool

HasRecord goes over existing records in all sections and checks wether or not it exists in the message payload.

func NewRand

func NewRand() *rand.Rand

NewRand returns a new PRNG with its own source.

Types

type ClosestKeyFinder

type ClosestKeyFinder interface {
	// FindClosestKey checks DB if provided key exists or
	// closest smaller key
	FindClosestKey(key []byte, context Context) ([]byte, error)
}

ClosestKeyFinder allows to search for closest smaller or equal key in the database

type Context

type Context interface {
	Reset()
}

Context interface is an ADT representing the state carried across queries to DBI like iterator state or potentially query cache

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB implements a customized db

func Open

func Open(name string, driver string) (*DB, error)

Open opens the named file read-only and returns a new db object. The file should exist and be a compatible (CDB or RDB) database file.

func (*DB) Destroy

func (f *DB) Destroy()

Destroy destroys a DB. It marks it as destroyable. If there is no refCount it will close the db, if there is refcounts left, the last reference to call Reader.Close() will close the db.

func (*DB) GetStats

func (f *DB) GetStats() map[string]int64

GetStats reports DB backend stats

func (*DB) Reload

func (f *DB) Reload(path string, validationKey []byte, reloadTimeout time.Duration) (*DB, error)

Reload reloads a DB. In case of immutable CDB it will return new DB and close the old one. In case of RocksDB, if path is the same as it was it tries to catch up with WAL and returns existing DB. If path is different it will return new DB and close the old one. The validationKey is used to verify that the format of the DB file is valid, by checking for the existence of a key that is known to exist. If the DB file is invalid, the old DB will continue to be used.

func (*DB) ValidateDbKey

func (f *DB) ValidateDbKey(dbKey []byte) error

ValidateDbKey checks whether record of certain key is in db

type DBI

type DBI interface {
	NewContext() Context
	Find(key []byte, context Context) ([]byte, error)
	ForEach(key []byte, f func(value []byte) error, context Context) error
	FreeContext(Context)
	FindMap(domain, mtype []byte, context Context) ([]byte, error)
	// GetLocationByMap returns the location ID, including the 2-byte header for long IDs.
	GetLocationByMap(ipnet *net.IPNet, mapID []byte, context Context) ([]byte, uint8, error)
	Close() error
	Reload(path string) (DBI, error)
	GetStats() map[string]int64

	// Gets ClosestKeyFinder associated with DBI if it is supported, nil otherwise
	ClosestKeyFinder() ClosestKeyFinder
}

DBI interface represents the pluggable API for the backing storage nolint:revive

type DataReader

type DataReader struct {
	// contains filtered or unexported fields
}

DataReader wraps an DB to carry a Context around. This structure will be able to perform DNS queries.

func (*DataReader) Close

func (r *DataReader) Close()

Close close a reader. This puts back a context in the pool

func (*DataReader) Data

func (r *DataReader) Data(key []byte) ([]byte, error)

Data returns the first data value for the given key. If no such record exists, it returns EOF.

func (*DataReader) EcsLocation

func (r *DataReader) EcsLocation(q []byte, ecs *dns.EDNS0_SUBNET) (*Location, error)

EcsLocation find a Location ID that matches this Client Subnet. If we do not find a match, Location will be nil and ECS's SourceScope will be set to 0. If we find a match, Location will contain the matching LocationID and ECS option will have SourceScope set.

func (*DataReader) Find

func (r *DataReader) Find(key []byte) ([]byte, error)

Find returns the first data value for the given key as a byte slice. Find is the same as FindStart followed by FindNext.

func (*DataReader) FindAnswer

func (r *DataReader) FindAnswer(q []byte, packedControlName []byte, qname string, qtype uint16, locID ID, a *dns.Msg, maxAnswer int) (bool, int)

FindAnswer will find answers for a given query q

func (*DataReader) FindLocation

func (r *DataReader) FindLocation(qname []byte, ecs *dns.EDNS0_SUBNET, ip string) (loc *Location, err error)

FindLocation find a Location given a wired format qname, *dns.Msg and a resolver IP First, it tries to find a matching location for a subnet client. Then, it tries to find a matching location for a resolver IP. return nil for Location if no location were found. error will be set on error.

func (*DataReader) ForEach

func (r *DataReader) ForEach(key []byte, f func(value []byte) error) (err error)

ForEach calls a function for each key match. The function takes a byte slice as a value and return an error. if error is not nil, the loop will stop.

func (*DataReader) ForEachResourceRecord

func (r *DataReader) ForEachResourceRecord(domainName []byte, locID ID, parseRecord func(result []byte) error) error

ForEachResourceRecord calls parseRecord for each RR record in DB in provided AND default location

func (*DataReader) IsAuthoritative

func (r *DataReader) IsAuthoritative(q []byte, locID ID) (ns bool, auth bool, zoneCut []byte, err error)

IsAuthoritative find whether or not we are authoritative and have NS records for the given domain. Starting from the original qname, it iterates through every possible parent domain by removing 1 label at a time until it find a match, or there is no more labels. It returns a boolean if we have NS records, and if we are authoritative. And the packed zone cut for which we found NS/Auth.

If `ns` is True and `auth` is False: this is a delegation. If `ns` and `auth` are True, we are authoritative.

func (*DataReader) ResolverLocation

func (r *DataReader) ResolverLocation(q []byte, ip string) (*Location, error)

ResolverLocation find the location associated with a client IP (resolver)

type ID

type ID []byte

ID is an alias for []byte, used for Map IDs and Location IDs. It is always at least 2 bytes long. For long IDs, it contains the header and contents.

var (
	// ZeroID is the value used to represent an empty Map ID or Location ID
	ZeroID ID = []byte{0, 0}

	// EmptyLocation is used to index (prefix) non-location aware qnames for RR's
	EmptyLocation = Location{MapID: ZeroID, LocID: ZeroID}
)

func (ID) Contents

func (id ID) Contents() []byte

Contents are the ID bytes minus any long-ID header.

func (ID) IsZero

func (id ID) IsZero() bool

IsZero is true when its LocID is ZeroID.

type Location

type Location struct {
	MapID ID    // The map in which we found the name.
	Mask  uint8 // The subnet mask we found a match for. Used for ECS.
	LocID ID    // The location ID.
}

Location is a native representation of a DNS location representation. It holds: the MapID it belongs to For ECS MapID, the associated mask. The LocID, the location ID used to find matching records.

type MockClosestKeyFinder

type MockClosestKeyFinder struct {
	// contains filtered or unexported fields
}

MockClosestKeyFinder is a mock of ClosestKeyFinder interface

func NewMockClosestKeyFinder

func NewMockClosestKeyFinder(ctrl *gomock.Controller) *MockClosestKeyFinder

NewMockClosestKeyFinder creates a new mock instance

func (*MockClosestKeyFinder) EXPECT

EXPECT returns an object that allows the caller to indicate expected use

func (*MockClosestKeyFinder) FindClosestKey

func (m *MockClosestKeyFinder) FindClosestKey(key []byte, context Context) ([]byte, error)

FindClosestKey mocks base method

type MockClosestKeyFinderMockRecorder

type MockClosestKeyFinderMockRecorder struct {
	// contains filtered or unexported fields
}

MockClosestKeyFinderMockRecorder is the mock recorder for MockClosestKeyFinder

func (*MockClosestKeyFinderMockRecorder) FindClosestKey

func (mr *MockClosestKeyFinderMockRecorder) FindClosestKey(key, context interface{}) *gomock.Call

FindClosestKey indicates an expected call of FindClosestKey

type MockContext

type MockContext struct {
	// contains filtered or unexported fields
}

MockContext is a mock of Context interface

func NewMockContext

func NewMockContext(ctrl *gomock.Controller) *MockContext

NewMockContext creates a new mock instance

func (*MockContext) EXPECT

func (m *MockContext) EXPECT() *MockContextMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockContext) Reset

func (m *MockContext) Reset()

Reset mocks base method

type MockContextMockRecorder

type MockContextMockRecorder struct {
	// contains filtered or unexported fields
}

MockContextMockRecorder is the mock recorder for MockContext

func (*MockContextMockRecorder) Reset

func (mr *MockContextMockRecorder) Reset() *gomock.Call

Reset indicates an expected call of Reset

type MockDBI

type MockDBI struct {
	// contains filtered or unexported fields
}

MockDBI is a mock of DBI interface

func NewMockDBI

func NewMockDBI(ctrl *gomock.Controller) *MockDBI

NewMockDBI creates a new mock instance

func (*MockDBI) Close

func (m *MockDBI) Close() error

Close mocks base method

func (*MockDBI) ClosestKeyFinder

func (m *MockDBI) ClosestKeyFinder() ClosestKeyFinder

ClosestKeyFinder mocks base method

func (*MockDBI) EXPECT

func (m *MockDBI) EXPECT() *MockDBIMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockDBI) Find

func (m *MockDBI) Find(key []byte, context Context) ([]byte, error)

Find mocks base method

func (*MockDBI) FindMap

func (m *MockDBI) FindMap(domain, mtype []byte, context Context) ([]byte, error)

FindMap mocks base method

func (*MockDBI) ForEach

func (m *MockDBI) ForEach(key []byte, f func([]byte) error, context Context) error

ForEach mocks base method

func (*MockDBI) FreeContext

func (m *MockDBI) FreeContext(arg0 Context)

FreeContext mocks base method

func (*MockDBI) GetLocationByMap

func (m *MockDBI) GetLocationByMap(ipnet *net.IPNet, mapID []byte, context Context) ([]byte, uint8, error)

GetLocationByMap mocks base method

func (*MockDBI) GetStats

func (m *MockDBI) GetStats() map[string]int64

GetStats mocks base method

func (*MockDBI) NewContext

func (m *MockDBI) NewContext() Context

NewContext mocks base method

func (*MockDBI) Reload

func (m *MockDBI) Reload(path string) (DBI, error)

Reload mocks base method

type MockDBIMockRecorder

type MockDBIMockRecorder struct {
	// contains filtered or unexported fields
}

MockDBIMockRecorder is the mock recorder for MockDBI

func (*MockDBIMockRecorder) Close

func (mr *MockDBIMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close

func (*MockDBIMockRecorder) ClosestKeyFinder

func (mr *MockDBIMockRecorder) ClosestKeyFinder() *gomock.Call

ClosestKeyFinder indicates an expected call of ClosestKeyFinder

func (*MockDBIMockRecorder) Find

func (mr *MockDBIMockRecorder) Find(key, context interface{}) *gomock.Call

Find indicates an expected call of Find

func (*MockDBIMockRecorder) FindMap

func (mr *MockDBIMockRecorder) FindMap(domain, mtype, context interface{}) *gomock.Call

FindMap indicates an expected call of FindMap

func (*MockDBIMockRecorder) ForEach

func (mr *MockDBIMockRecorder) ForEach(key, f, context interface{}) *gomock.Call

ForEach indicates an expected call of ForEach

func (*MockDBIMockRecorder) FreeContext

func (mr *MockDBIMockRecorder) FreeContext(arg0 interface{}) *gomock.Call

FreeContext indicates an expected call of FreeContext

func (*MockDBIMockRecorder) GetLocationByMap

func (mr *MockDBIMockRecorder) GetLocationByMap(ipnet, mapID, context interface{}) *gomock.Call

GetLocationByMap indicates an expected call of GetLocationByMap

func (*MockDBIMockRecorder) GetStats

func (mr *MockDBIMockRecorder) GetStats() *gomock.Call

GetStats indicates an expected call of GetStats

func (*MockDBIMockRecorder) NewContext

func (mr *MockDBIMockRecorder) NewContext() *gomock.Call

NewContext indicates an expected call of NewContext

func (*MockDBIMockRecorder) Reload

func (mr *MockDBIMockRecorder) Reload(path interface{}) *gomock.Call

Reload indicates an expected call of Reload

type MockReader

type MockReader struct {
	// contains filtered or unexported fields
}

MockReader is a mock of Reader interface

func NewMockReader

func NewMockReader(ctrl *gomock.Controller) *MockReader

NewMockReader creates a new mock instance

func (*MockReader) Close

func (m *MockReader) Close()

Close mocks base method

func (*MockReader) EXPECT

func (m *MockReader) EXPECT() *MockReaderMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockReader) EcsLocation

func (m *MockReader) EcsLocation(q []byte, ecs *dns.EDNS0_SUBNET) (*Location, error)

EcsLocation mocks base method

func (*MockReader) FindAnswer

func (m *MockReader) FindAnswer(q, packedControlName []byte, qname string, qtype uint16, loc Location, a *dns.Msg, maxAnswer int) (bool, bool)

FindAnswer mocks base method

func (*MockReader) FindLocation

func (m_2 *MockReader) FindLocation(qname []byte, m *dns.Msg, ip string) (*dns.EDNS0_SUBNET, *Location, error)

FindLocation mocks base method

func (*MockReader) ForEach

func (m *MockReader) ForEach(key []byte, f func([]byte) error) error

ForEach mocks base method

func (*MockReader) ForEachResourceRecord

func (m *MockReader) ForEachResourceRecord(domainName []byte, loc *Location, parseRecord func([]byte) error) error

ForEachResourceRecord mocks base method

func (*MockReader) IsAuthoritative

func (m *MockReader) IsAuthoritative(q []byte, loc *Location) (bool, bool, []byte, error)

IsAuthoritative mocks base method

func (*MockReader) ResolverLocation

func (m *MockReader) ResolverLocation(q []byte, ip string) (*Location, error)

ResolverLocation mocks base method

type MockReaderMockRecorder

type MockReaderMockRecorder struct {
	// contains filtered or unexported fields
}

MockReaderMockRecorder is the mock recorder for MockReader

func (*MockReaderMockRecorder) Close

func (mr *MockReaderMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close

func (*MockReaderMockRecorder) EcsLocation

func (mr *MockReaderMockRecorder) EcsLocation(q, ecs interface{}) *gomock.Call

EcsLocation indicates an expected call of EcsLocation

func (*MockReaderMockRecorder) FindAnswer

func (mr *MockReaderMockRecorder) FindAnswer(q, packedControlName, qname, qtype, loc, a, maxAnswer interface{}) *gomock.Call

FindAnswer indicates an expected call of FindAnswer

func (*MockReaderMockRecorder) FindLocation

func (mr *MockReaderMockRecorder) FindLocation(qname, m, ip interface{}) *gomock.Call

FindLocation indicates an expected call of FindLocation

func (*MockReaderMockRecorder) ForEach

func (mr *MockReaderMockRecorder) ForEach(key, f interface{}) *gomock.Call

ForEach indicates an expected call of ForEach

func (*MockReaderMockRecorder) ForEachResourceRecord

func (mr *MockReaderMockRecorder) ForEachResourceRecord(domainName, loc, parseRecord interface{}) *gomock.Call

ForEachResourceRecord indicates an expected call of ForEachResourceRecord

func (*MockReaderMockRecorder) IsAuthoritative

func (mr *MockReaderMockRecorder) IsAuthoritative(q, loc interface{}) *gomock.Call

IsAuthoritative indicates an expected call of IsAuthoritative

func (*MockReaderMockRecorder) ResolverLocation

func (mr *MockReaderMockRecorder) ResolverLocation(q, ip interface{}) *gomock.Call

ResolverLocation indicates an expected call of ResolverLocation

type Reader

type Reader interface {
	FindLocation(qname []byte, ecs *dns.EDNS0_SUBNET, ip string) (loc *Location, err error)
	IsAuthoritative(q []byte, locID ID) (ns bool, auth bool, zoneCut []byte, err error)
	FindAnswer(q []byte, packedControlName []byte, qname string, qtype uint16, locID ID, a *dns.Msg, maxAnswer int) (bool, int)

	EcsLocation(q []byte, ecs *dns.EDNS0_SUBNET) (*Location, error)
	ResolverLocation(q []byte, ip string) (*Location, error)

	ForEach(key []byte, f func(value []byte) error) (err error)
	ForEachResourceRecord(domainName []byte, locID ID, parseRecord func(result []byte) error) error

	Close()
	// contains filtered or unexported methods
}

Reader will be able to perform DNS queries. It wraps DB to carry query context and properly count reference count to DB

func NewReader

func NewReader(db *DB) (Reader, error)

NewReader returns a new DB reader to be used to perform DNS record search in DB.

type ResourceRecord

type ResourceRecord struct {
	Weight uint32
	Qtype  uint16
	TTL    uint32
	Offset int
}

ResourceRecord holds the representation of a row from DB

func ExtractRRFromRow

func ExtractRRFromRow(row []byte, wildcard bool) (rr ResourceRecord, err error)

ExtractRRFromRow extracts a ResourceRecord from a DB row. A DB row contains data ResourceRecord information: qtype (2) ch (1) recordloc (2+)? ttl (4) ttd (8) weight (4)? rdata (vlen) recordloc is present only if ch == '+' + 1 or ch == '*' + 1 weight is only present if qtype is A or AAAA It returns a pointer to a ResourceRecord, nil when not a proper match. nil is returned when the row is not matching the specific filters (e.g Location or wildcard).

type Wrs

type Wrs struct {
	MaxAnswers int
	V4         []WrsItem
	V4Count    uint32
	V6         []WrsItem
	V6Count    uint32
}

Wrs Weighted Random Sample

func (*Wrs) AAAARecord

func (w *Wrs) AAAARecord(name string, class uint16) (rrs []dns.RR, err error)

AAAARecord returns the weighted random sample for AAAA qtype if there is any.

func (*Wrs) ARecord

func (w *Wrs) ARecord(name string, class uint16) (rrs []dns.RR, err error)

ARecord returns the weighted random sample for A qtype if there is any.

func (*Wrs) Add

func (w *Wrs) Add(rec ResourceRecord, data []byte) error

Add adds a ResourceRecord to Wrs if its randomly computed weight is greater then the existing record.

func (*Wrs) WeightedAnswer

func (w *Wrs) WeightedAnswer() bool

WeightedAnswer returns true if the answer selected a subset of possible results.

type WrsItem

type WrsItem struct {
	Key  float64
	TTL  uint32
	Addr net.IP
}

WrsItem Weighted Random Sample Item

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL