util

package
v0.0.0-...-891f3ce Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2019 License: BSD-2-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package util defines miscellaneous functions used in multiple parts of bgpmon or other projects

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoIP is returned when a IPWrapper can't be parsed.
	ErrNoIP = errors.New("could not decode IP from protobuf IP wrapper")

	// ErrNilCap is returned when an invalid capture is passed.
	ErrNilCap = errors.New("nil BGP capture provided")

	// ErrNilPrefWrap is returned when an IPWrapper is nil
	ErrNilPrefWrap = errors.New("nil Prefix wrapper provided")

	// ErrNoASPath is returned when a capture has no AS path
	ErrNoASPath = errors.New("could not extract an Autonomous System Path")

	// ErrNoAdvertisedPrefixes is returned when a capture has no advertized prefixes
	ErrNoAdvertisedPrefixes = errors.New("could not extract any Advertised Prefixes")

	// ErrNoWithdrawnPrefixes is returned when a capture has no withdrawn prefixes
	ErrNoWithdrawnPrefixes = errors.New("could not extract any Withdrawn Prefixes")
)
View Source
var (
	// ErrOpt is returned by StringToOptMap on failure
	ErrOpt = errors.New("error parsing options")
)

Functions

func CheckForKeys

func CheckForKeys(in map[string]string, keys ...string) bool

CheckForKeys checks a map[string]string for the existence of all string keys provided.

func DisableLogging

func DisableLogging()

DisableLogging reroutes all created loggers to a nil output.

func GetASPath

func GetASPath(cap *pb.BGPCapture) ([]int, error)

GetASPath returns an Autonomous System path as an array of integers from a protobuf capture.

func GetAdvertisedPrefixes

func GetAdvertisedPrefixes(cap *pb.BGPCapture) ([]*net.IPNet, error)

GetAdvertisedPrefixes returns the advertised routes as a slice of IPNet and possibly an error from a protobuf capture.

func GetIPNetsAsPrefixList

func GetIPNetsAsPrefixList(nets []*net.IPNet) []*pbcomm.PrefixWrapper

GetIPNetsAsPrefixList returns []*PrefixWrapper from a []*net.IPNet

func GetIPWrapper

func GetIPWrapper(pIP *pbcomm.IPAddressWrapper) (net.IP, error)

GetIPWrapper returns a net.IP and possibly an error from the protobuf IP address wrapper.

func GetNextHop

func GetNextHop(cap *pb.BGPCapture) (net.IP, error)

GetNextHop returns the IP and possibly an error of the next hop from a protobuf capture.

func GetOriginAS

func GetOriginAS(cap *pb.BGPCapture) (int, error)

GetOriginAS returns the origin AS as an integer (the AS at the last index of the AS-Path) of the ASPath from a capture or possibly an error.

func GetPeerIP

func GetPeerIP(cap *pb.BGPCapture) (net.IP, error)

GetPeerIP returns a PeerIP and possibly and error from a protobuf capture.

func GetPrefixListAsIPNet

func GetPrefixListAsIPNet(prefs []*pbcomm.PrefixWrapper) ([]*net.IPNet, error)

GetPrefixListAsIPNet returns the slice of IPNet and possibly an error from a slice of protobuf PrefixWrapper. In case an error is found during the decoding of any part of the prefix list, this function will return that error and an empty slice.

func GetProtoMsg

func GetProtoMsg(cap *pb.BGPCapture) []byte

GetProtoMsg returns a byte array representing the capture from a protobuf capture.

func GetTimeColIP

func GetTimeColIP(cap *pb.BGPCapture) (time.Time, net.IP, error)

GetTimeColIP returns the time of the capture, the collector IP and possibly an error from a protobuf BGP capture.

func GetWithdrawnPrefixes

func GetWithdrawnPrefixes(cap *pb.BGPCapture) ([]*net.IPNet, error)

GetWithdrawnPrefixes returns the withdrawn routes as a slice of IPNet and possibly an error from a protobuf capture.

func IsClosed

func IsClosed(ctx context.Context) bool

IsClosed returns true if a context has been closed, false otherwise. This function is non-blocking.

func OptMapToString

func OptMapToString(in map[string]string) string

OptMapToString is a function that turns a map[string]string to a string like "-key1 val1 -key2 val2". It should be the reverse operation of StringToOptMap. This function only guarantees the correct pairings, not the original order of the pairings when the map was created.

func PrefixesToPQArray

func PrefixesToPQArray(n []*net.IPNet) interface {
	driver.Valuer
	sql.Scanner
}

PrefixesToPQArray handles a strange case where protobuf deserialize an array element of nil as "<nil>" and that kills the db insert statement cause it can't make it into a cidr. The return value is an inline interface which matches the return value of pq.Arrary.

func SanitizeDBString

func SanitizeDBString(s string) string

SanitizeDBString removes any characters from s that might be intended for a SQL injection attack.

func StringToOptMap

func StringToOptMap(in string) (map[string]string, error)

StringToOptMap is a function that will turn a string in the form "-opt1 val1 -opt2 val2" to a map[string]string with key:values like opt1:val1, opt2:val2. It will return an error in the case of a malformed string. For now only options with values are supported, so the input string must be split in an even number of parts.

Types

type AtomicSQLExecutor

type AtomicSQLExecutor interface {
	SQLExecutor

	// Since this interface is meant for use with transactions and other structs
	// that wrap transactions, Commit and Rollback are within the context of that
	// underlying transaction.
	Commit() error
	Rollback() error
}

AtomicSQLExecutor is a SQLExecutor that can be committed or rolled back. This wraps sql.Tx and others we implement

type GetTimeouter

type GetTimeouter interface {
	GetTimeout() time.Duration
}

GetTimeouter is an interface to describe anything that expires after a certain amount of time

type InsertBuffer

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

InsertBuffer Represents the VALUES() array for an insert statement. Helps to optimize the amount of inserted values on a single query.

func NewInsertBuffer

func NewInsertBuffer(ex SQLExecutor, max int, usePositional bool) *InsertBuffer

NewInsertBuffer returns a SQLBuffer which buffers values for an insert statement. stmt is the original SQL statement which will be appended with VALUES() clauses. ex is the SQLExecutor that the query will be run on when the buffer is full. max is the number of VALUES clauses that can be added to this buffer. Each VALUES() clause must contain exactly batchSize values. If usePositional is true, the resulting insert statement will use this format: ($1, $2, $3). If it is false, it will use: (?,?,?).

func (*InsertBuffer) Clear

func (ib *InsertBuffer) Clear()

Clear Satisfies the SQLBuffer interface. It will remove all added values, leaving the buffer in a clean state.

func (*InsertBuffer) Commit

func (ib *InsertBuffer) Commit() error

Commit allows the insert buffer to adhere to the AtomicSQLExecutor interface.

func (*InsertBuffer) Exec

func (ib *InsertBuffer) Exec(query string, arg ...interface{}) (sql.Result, error)

Exec allows the insert buffer to adhere to the SQLExecutor interface.

func (*InsertBuffer) Flush

func (ib *InsertBuffer) Flush() error

Flush Satisfies the SQLBuffer interface. It will execute the INSERT statement on the provided executor.

func (*InsertBuffer) Query

func (ib *InsertBuffer) Query(query string, args ...interface{}) (*sql.Rows, error)

Query allows the insert buffer to adhere to the SQLExecutor interface.

func (*InsertBuffer) QueryRow

func (ib *InsertBuffer) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow allows the insert buffer to adhere to the SQLExecutor interface.

func (*InsertBuffer) Rollback

func (ib *InsertBuffer) Rollback() error

Rollback allows the insert buffer to adhere to the AtomicSQLExecutor interface.

type Logger

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

Logger is a simple wrapper around logrus.FieldLogger which eases use and adds some functionality.

func NewLogger

func NewLogger(fields ...string) Logger

NewLogger returns a util.Logger, with pairs of strings matched for fields.

func (Logger) Errorf

func (l Logger) Errorf(tmpl string, args ...interface{}) error

Errorf prints to the screen with ERROR priority, and returns an error to be handled. It is a combination of log.Errorf and fmt.Errorf.

func (Logger) Fatalf

func (l Logger) Fatalf(tmpl string, args ...interface{})

Fatalf prints to the screen with FATAL priority, and exits the application.

func (Logger) Infof

func (l Logger) Infof(tmpl string, args ...interface{})

Infof prints to the screen with INFO priority. This isn't meant to be used for error messages.

type SQLBuffer

type SQLBuffer interface {
	SQLExecutor
	// Flush will execute the statement provided to the buffer with
	// the provided values. If it does not return an error, it should
	// leave the buffer in a cleared state.
	Flush() error

	// Clear removes all of the previously added values without flushing
	// them.
	Clear()
}

SQLBuffer is an interface used to describe anything that can buffer SQL values to be flushed later.

type SQLExecutor

type SQLExecutor interface {
	// Exec is meant for queries that perform an action on a database. They may return
	// a success code, but shouldn't return any rows. This includes INSERT, CREATE, DROP,
	// etc.
	Exec(query string, args ...interface{}) (sql.Result, error)

	// Query is meant for queries that return multiple rows. The *sql.Rows returned
	// can be used to iterate over the results, and must be closed when it is done
	// being used.
	Query(query string, args ...interface{}) (*sql.Rows, error)

	// QueryRow is meant for queries that only expect one row, such as specifically
	// crafted SELECT statements.
	QueryRow(query string, args ...interface{}) *sql.Row
}

SQLExecutor is a wrapper around sql.Tx, sql.Db, and others we implement. It represents something that can execute queries on a database.

type TimedBuffer

type TimedBuffer struct {
	SQLBuffer
	// contains filtered or unexported fields
}

TimedBuffer is a SQLBuffer that wraps another SQLBuffer, flushing it after a certain amount of time.

func NewTimedBuffer

func NewTimedBuffer(parent SQLBuffer, d time.Duration) *TimedBuffer

NewTimedBuffer returns a TimedBuffer that expires after duration d.

func (*TimedBuffer) Exec

func (t *TimedBuffer) Exec(query string, args ...interface{}) (sql.Result, error)

Exec Satisfies the SQLBuffer interface. For a TimedBuffer, the args are added to the underlying buffer and the timer is reset.

func (*TimedBuffer) Stop

func (t *TimedBuffer) Stop()

Stop is unique to the TimedBuffer, it prevents the buffer from expiring after the provided duration.

type Timespan

type Timespan struct {
	Start time.Time
	End   time.Time
}

Timespan represents a start and end time.

func (Timespan) Contains

func (ts Timespan) Contains(t time.Time) bool

Contains returns true if t is within [start, end)

Jump to

Keyboard shortcuts

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