Documentation
¶
Overview ¶
Package nf9packet provides structures and functions to decode and analyze NetFlow v9 packets.
This package does only packet decoding in a single packet context. It keeps no state when decoding multiple packets. As a result Data FlowSets can not be decoded during initial packet decoding. To decode Data FlowSets user must keep track of Template Records and Options Template Records manually.
Examples of NetFlow v9 packets:
+--------+--------------------------------------------------------+ | | +----------+ +---------+ +-----------+ +---------+ | | Packet | | Template | | Data | | Options | | Data | | | Header | | FlowSet | | FlowSet | ... | Template | | FlowSet | | | | | | | | | FlowSet | | | | | | +----------+ +---------+ +-----------+ +---------+ | +--------+--------------------------------------------------------+ +--------+----------------------------------------------+ | | +---------+ +---------+ +---------+ | | Packet | | Data | ... | Data | ... | Data | | | Header | | FlowSet | ... | FlowSet | ... | FlowSet | | | | +---------+ +---------+ +---------+ | +--------+----------------------------------------------+ +--------+-------------------------------------------------+ | | +----------+ +----------+ +----------+ | | Packet | | Template | | Template | | Options | | | Header | | FlowSet | ... | FlowSet | ... | Template | | | | | | | | | FlowSet | | | | +----------+ +----------+ +----------+ | +--------+-------------------------------------------------+
Example of struct hierarchy after packet decoding:
Package | +--TemplateFlowSet | | | +--TemplateRecord | | | | | +--Field | | +--... | | +--Field | | | +--... | | | +--TemplateRecord | | | +--Field | +--... | +--Field | +--DataFlowSet | +--... | +--OptionsTemplateFlowSet | | | +--OptionsTemplateRecord | | | | | +--Field (scope) | | +--... (scope) | | +--Field (scope) | | | | | +--Field (option) | | +--... (option) | | +--Field (option) | | | +--... | | | +--OptionsTemplateRecord | | | +--Field (scope) | +--... (scope) | +--Field (scope) | | | +--Field (option) | +--... (option) | +--Field (option) | +--DataFlowSet
When matched with appropriate template Data FlowSet can be decoded to list of Flow Data Records or list of Options Data Records. Struct hierarchy example:
[]FlowDataRecord
|
+--FlowDataRecord
| |
| +--[]byte
| +--...
| +--[]byte
|
+--...
|
+--FlowDataRecord
|
+--[]byte
+--...
+--[]byte
[]OptionsDataRecord
|
+--OptionsDataRecord
| |
| +--[]byte (scope)
| +--... (scope)
| +--[]byte (scope)
| |
| +--[]byte (option)
| +--... (option)
| +--[]byte (option)
|
+--...
|
+--OptionsDataRecord
|
+--[]byte
+--...
+--[]byte
|
+--[]byte (option)
+--... (option)
+--[]byte (option)
Most of structure names and comments are taken directly from RFC 3954. Reading the NetFlow v9 protocol specification is highly recommended before using this package.
Index ¶
- func Dump(packet *Packet, w io.Writer)
- type DataFlowSet
- type Field
- func (f *Field) DataToString(data []byte) string
- func (f *Field) DataToUint64(data []byte) uint64
- func (f *Field) DefaultLength() int
- func (f *Field) Description() string
- func (f *Field) Name() string
- func (f *Field) ScopeDefaultLength() int
- func (f *Field) ScopeDescription() string
- func (f *Field) ScopeName() string
- func (f Field) String() string
- type FlowDataRecord
- type FlowSetHeader
- type OptionsDataRecord
- type OptionsTemplateFlowSet
- type OptionsTemplateRecord
- type Packet
- type TemplateFlowSet
- type TemplateRecord
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DataFlowSet ¶
type DataFlowSet struct {
FlowSetHeader
// Raw data bytes
Data []byte
}
DataFlowSet is a collection of Data Records (actual NetFlow data) and Options Data Rcords (meta data).
type Field ¶
type Field struct {
// A numeric value that represents the type of field.
Type uint16
// The length (in bytes) of the field.
Length uint16
}
Field describes type and length of a single value in a Flow Data Record. Field does not contain the record value itself it is just a description of what record value will look like.
func (*Field) DataToString ¶
DataToString converts field value to string representation based on field type. If used with unknow field type string "n/a" will be returned.
func (*Field) DataToUint64 ¶
DataToUint64 converts field value to uint64. This function will not generate errors if used with incompatible field types. Field value length can be up to 8 bytes, for longer values only first 8 bytes are used.
func (*Field) DefaultLength ¶
DefaultLength returns length of field type as specified in RFC 3954 and Cisco documentation. For variable length fields and unknown fields -1 is returned.
func (*Field) Description ¶
Description returns field type description based on RFC 3954 and Cisco documentation. For unkown field types string "Unknown type" will be returned.
func (*Field) Name ¶
Name returns a short field type identifier based on RFC 3954 and Cisco documentation. For unkown field types string "UNKNOWN_TYPE" will be returned.
func (*Field) ScopeDefaultLength ¶
ScopeDefaultLength is the same as DefaultLength() but should be used only for Scope Fields
func (*Field) ScopeDescription ¶
ScopeDescription is the same as Description but should be used only for Scope Fields
type FlowDataRecord ¶
type FlowDataRecord struct {
// List of Flow Data Record values stored in raw format as []byte
Values [][]byte
}
FlowDataRecord is actual NetFlow data. This structure does not contain any information about the actual data meanind. It must be combined with corresponding TemplateRecord to be decoded to a single NetFlow data row.
type FlowSetHeader ¶
type FlowSetHeader struct {
// FlowSet ID:
// 0 for TemplateFlowSet
// 1 for OptionsTemplateFlowSet
// 256-65535 for DataFlowSet (used as TemplateId)
Id uint16
// The total length of this FlowSet in bytes (including padding).
Length uint16
}
FlowSetHeader contains fields shared by all Flow Sets (DataFlowSet, TemplateFlowSet, OptionsTemplateFlowSet).
type OptionsDataRecord ¶
type OptionsDataRecord struct {
// List of Scope values stored in raw format as []byte
ScopeValues [][]byte
// List of Optons values stored in raw format as []byte
OptionValues [][]byte
}
OptionsDataRecord is meta data sent alongide actual NetFlow data. Combined with OptionsTemplateRecord it can be decoded to a single data row.
type OptionsTemplateFlowSet ¶
type OptionsTemplateFlowSet struct {
FlowSetHeader
// List of Options Template Records
Records []OptionsTemplateRecord
}
OptionsTemplateFlowSet is a collection of templates that describe structure of Options Data Records.
type OptionsTemplateRecord ¶
type OptionsTemplateRecord struct {
// Template ID of this Options Template. This value is greater than 255.
TemplateId uint16
// The length in bytes of all Scope field definitions contained in this
// Options Template Record.
ScopeLength uint16
// The length (in bytes) of all options field definitions contained in
// this Options Template Record.
OptionLength uint16
// List of Scope fields in this Options Template Record.
Scopes []Field
// List of Option fields in this Options Template Record.
Options []Field
}
OptionsTemplateRecord is a template that describes structure of an Options Data Record (meta data).
func (*OptionsTemplateRecord) DecodeFlowSet ¶
func (otpl *OptionsTemplateRecord) DecodeFlowSet(set *DataFlowSet) (list []OptionsDataRecord)
DecodeFlowSet uses current OptionsTemplateRecord to decode data in Data FlowSet to a list of Options Data Records.
type Packet ¶
type Packet struct {
// Version of Flow Record format exported in this packet. The value of
//this field is 9 for the current version.
Version uint16
// The total number of records in the Export Packet, which is the sum
// of Options FlowSet records, Template FlowSet records, and Data
// FlowSet records.
Count uint16
// Time in milliseconds since this device was first booted.
SysUpTime uint32
// Time in seconds since 0000 UTC 1970, at which the Export Packet
// leaves the Exporter.
UnixSecs uint32
// Incremental sequence counter of all Export Packets sent from the
// current Observation Domain by the Exporter.
SequenceNumber uint32
// A 32-bit value that identifies the Exporter Observation Domain.
SourceId uint32
// A slice of structs. Each element is instance of DataFlowSet or
// TemplateFlowSet or OptionsTemplateFlowSet.
FlowSets []interface{}
}
Packet is a decoded representation of a single NetFlow v9 UDP packet.
func Decode ¶
Decode is the main function of this package. It converts raw packet bytes to Packet struct.
func (*Packet) DataFlowSets ¶
func (p *Packet) DataFlowSets() (list []DataFlowSet)
DataFlowSets generate a list of all Data FlowSets in the packet. If matched with appropriate templates Data FlowSets can be decoded to Data Records or Options Data Records.
func (*Packet) OptionsTemplateRecords ¶
func (p *Packet) OptionsTemplateRecords() (list []*OptionsTemplateRecord)
OptionsTemplateRecords generate a list of all Options Template Records in the packet. Options Template Records can be used to decode Data FlowSets to Options Data Records.
func (*Packet) TemplateRecords ¶
func (p *Packet) TemplateRecords() (list []*TemplateRecord)
TemplateRecords generate a list of all Template Records in the packet. Template Records can be used to decode Data FlowSets to Data Records.
type TemplateFlowSet ¶
type TemplateFlowSet struct {
FlowSetHeader
// List of Template Records
Records []TemplateRecord
}
TemplateFlowSet is a collection of templates that describe structure of Data Records (actual NetFlow data).
type TemplateRecord ¶
type TemplateRecord struct {
// Each of the newly generated Template Records is given a unique
// Template ID. This uniqueness is local to the Observation Domain that
// generated the Template ID. Template IDs of Data FlowSets are numbered
// from 256 to 65535.
TemplateId uint16
// Number of fields in this Template Record. Because a Template FlowSet
// usually contains multiple Template Records, this field allows the
// Collector to determine the end of the current Template Record and
// the start of the next.
FieldCount uint16
// List of fields in this Template Record.
Fields []Field
}
TemplateRecord is a single template that describes structure of a Flow Record (actual Netflow data).
func (*TemplateRecord) DecodeFlowSet ¶
func (dtpl *TemplateRecord) DecodeFlowSet(set *DataFlowSet) (list []FlowDataRecord)
DecodeFlowSet uses current TemplateRecord to decode data in Data FlowSet to a list of Flow Data Records.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
nf9-data-dump
command
|
|
|
nf9-packet-dump
command
|
|
|
nf9-template-dump
command
|