Skip to content

Commit 5ba73ca

Browse files
committed
Update package
1 parent f65ab7f commit 5ba73ca

File tree

15 files changed

+219
-378
lines changed

15 files changed

+219
-378
lines changed

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# go-nfdump
22

3+
[![Go Reference](https://pkg.go.dev/badge/github.com/phaag/go-nfdump.svg)](https://pkg.go.dev/github.com/phaag/go-nfdump)
4+
35
This Go module allows to read and process files created by [nfdump](https://github.com/phaag/nfdump), the netflow/ipfix/sflow collector and processing tools.
46

57
This module is experimental and does not yet decode all available nfdump record extensions. It reads and processes only nfdump v2 files, which are created by nfdump-1.7.x. Files created with nfdump-1.6.x are recogized but skipped for decoding.
@@ -15,9 +17,9 @@ package main
1517
import (
1618
"flag"
1719
"fmt"
18-
"github.com/phaag/go-nfdump/nffile"
19-
"github.com/phaag/go-nfdump/nfrecord"
2020
"os"
21+
22+
nfdump "github.com/phaag/go-nfdump"
2123
)
2224

2325
var (
@@ -30,28 +32,31 @@ func main() {
3032
fmt.Fprintf(os.Stderr, "Usage of %s [flags]\n", os.Args[0])
3133
flag.PrintDefaults()
3234
}
33-
35+
3436
flag.Parse()
35-
37+
3638
if len(*fileName) == 0 {
3739
fmt.Printf("Filename required\n")
3840
flag.PrintDefaults()
3941
os.Exit(255)
4042
}
41-
42-
nffile := nffile.New()
43+
44+
nffile := nfdump.New()
45+
4346
if err := nffile.Open(*fileName); err != nil {
4447
fmt.Printf("Failed to open nf file: %v\n", err)
4548
os.Exit(255)
4649
}
47-
50+
4851
// print nffile stats
4952
fmt.Printf("nffile:\n%v", nffile)
50-
53+
5154
// Dump flow records
52-
recordChannel, _ := nfrecord.AllRecords(nffile)
55+
recordChannel, _ := nfdump.AllRecords(nffile)
56+
cnt := 0
5357
for record := range recordChannel {
54-
fmt.Printf("%v\n", record)
58+
cnt++
59+
fmt.Printf("record: %d\n%v\n", cnt, record)
5560
genericFlow := record.GenericFlow()
5661
if genericFlow != nil {
5762
fmt.Printf("SrcPort: %d\n", genericFlow.SrcPort)
@@ -71,10 +76,9 @@ func main() {
7176
*/
7277
}
7378
}
74-
7579
```
7680

77-
The `nfrecord/defs.go` file includes nfdump's `nfxV3.h` header file to convert individual record extensions into appropriate Golang records. So far the generic, misc, flowCount, vlan and asRouting extensions as well as IPv4/IPv6 addresses are available through the interface. See the nfxV3.go file for its definitions.
81+
The `defs.go` file includes nfdump's `nfxV3.h` header file to convert individual record extensions into appropriate Golang records. So far the generic, misc, flowCount, vlan and asRouting extensions as well as IPv4/IPv6 addresses are available through the interface. See the nfxV3.go file for its definitions.
7882

7983
If you modify the `defs.go` file, generate `nfxV3.go` use the go command
8084

File renamed without changes.

compress.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright © 2023 Peter Haag [email protected]
2+
// All rights reserved.
3+
//
4+
// Use of this source code is governed by the license that can be
5+
// found in the LICENSE file.
6+
7+
package nfdump
8+
9+
import (
10+
"bytes"
11+
"compress/bzip2"
12+
"fmt"
13+
"io"
14+
15+
"github.com/pierrec/lz4/v4"
16+
lzo "github.com/rasky/go-lzo"
17+
)
18+
19+
func (nfFile *NfFile) uncompressBlock(blockHeader *DataBlockHeader) ([]byte, error) {
20+
21+
dataBlock := make([]byte, blockHeader.Size)
22+
if _, err := io.ReadAtLeast(nfFile.file, dataBlock, int(blockHeader.Size)); err != nil {
23+
return nil, fmt.Errorf("nfFile read appendix data block: %v", err)
24+
}
25+
26+
switch nfFile.Header.Compression {
27+
case NOT_COMPRESSED:
28+
case LZO_COMPRESSED:
29+
out, err := lzo.Decompress1X(bytes.NewReader(dataBlock), int(blockHeader.Size), 2*int(blockHeader.Size))
30+
if err != nil {
31+
return nil, fmt.Errorf("nfFile uncompress lzo1x-1 data block: %v", err)
32+
}
33+
dataBlock = out
34+
blockHeader.Size = uint32(len(out))
35+
case BZ2_COMPRESSED:
36+
reader := bzip2.NewReader(bytes.NewReader(dataBlock))
37+
out := make([]byte, 3*len(dataBlock))
38+
n, err := reader.Read(out)
39+
if err != nil {
40+
return nil, fmt.Errorf("nfFile uncompress bzip2 data block: %v", err)
41+
}
42+
out = out[:n]
43+
dataBlock = out
44+
blockHeader.Size = uint32(n)
45+
case LZ4_COMPRESSED:
46+
out := make([]byte, 3*len(dataBlock))
47+
n, err := lz4.UncompressBlock(dataBlock, out)
48+
if err != nil {
49+
return nil, fmt.Errorf("nfFile uncompress lz4 data block: %v", err)
50+
}
51+
out = out[:n]
52+
dataBlock = out
53+
blockHeader.Size = uint32(n)
54+
default:
55+
return nil, fmt.Errorf("unknown data block compression: %d", nfFile.Header.Compression)
56+
}
57+
58+
return dataBlock, nil
59+
}

defs.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//go:build ignore
2+
3+
// Copyright © 2023 Peter Haag [email protected]
4+
// All rights reserved.
5+
//
6+
// Use of this source code is governed by the license that can be
7+
// found in the LICENSE file.
8+
9+
package nfdump
10+
11+
//#define GOLANG 1
12+
//#include <stdint.h>
13+
//#include "nfxV3.h"
14+
//#include "id.h"
15+
//
16+
import "C"
17+
18+
const EXnull = uint(C.EXnull)
19+
const EXgenericFlowID = uint16(C.EXgenericFlowID)
20+
const EXipv4FlowID = uint16(C.EXipv4FlowID)
21+
const EXipv6FlowID = uint16(C.EXipv6FlowID)
22+
const EXflowMiscID = uint16(C.EXflowMiscID)
23+
const EXcntFlowID = uint16(C.EXcntFlowID)
24+
const EXvLanID = uint16(C.EXvLanID)
25+
const EXasRoutingID = uint16(C.EXasRoutingID)
26+
27+
const V3_FLAG_EVENT = uint(C.V3_FLAG_EVENT)
28+
const V3_FLAG_SAMPLED = uint(C.V3_FLAG_SAMPLED)
29+
const V3_FLAG_ANON = uint(C.V3_FLAG_ANON)
30+
31+
const V3Record = uint16(C.V3Record)
32+
33+
const MAXEXTENSIONS = uint16(C.MAXEXTENSIONS)
34+
35+
type recordHeaderV3 C.struct_recordHeaderV3_s
36+
37+
type EXgenericFlow C.struct_EXgenericFlow_s
38+
type EXflowMisc C.struct_EXflowMisc_s
39+
type EXcntFlow C.struct_EXcntFlow_s
40+
type EXvLan C.struct_EXvLan_s
41+
type EXasRouting C.struct_EXasRouting_s

example/reader/main.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright © 2023 Peter Haag [email protected]
2+
// All rights reserved.
3+
//
4+
// Use of this source code is governed by the license that can be
5+
// found in the LICENSE file.
6+
7+
package main
8+
9+
import (
10+
"flag"
11+
"fmt"
12+
"os"
13+
14+
nfdump "github.com/phaag/go-nfdump"
15+
)
16+
17+
var (
18+
fileName = flag.String("r", "", "nfdump file to read")
19+
)
20+
21+
func main() {
22+
23+
flag.CommandLine.Usage = func() {
24+
fmt.Fprintf(os.Stderr, "Usage of %s [flags]\n", os.Args[0])
25+
flag.PrintDefaults()
26+
}
27+
28+
flag.Parse()
29+
30+
if len(*fileName) == 0 {
31+
fmt.Printf("Filename required\n")
32+
flag.PrintDefaults()
33+
os.Exit(255)
34+
}
35+
36+
nffile := nfdump.New()
37+
38+
if err := nffile.Open(*fileName); err != nil {
39+
fmt.Printf("Failed to open nf file: %v\n", err)
40+
os.Exit(255)
41+
}
42+
43+
// print nffile stats
44+
fmt.Printf("nffile:\n%v", nffile)
45+
46+
// Dump flow records
47+
recordChannel, _ := nfdump.AllRecords(nffile)
48+
cnt := 0
49+
for record := range recordChannel {
50+
cnt++
51+
fmt.Printf("record: %d\n%v\n", cnt, record)
52+
genericFlow := record.GenericFlow()
53+
if genericFlow != nil {
54+
fmt.Printf("SrcPort: %d\n", genericFlow.SrcPort)
55+
fmt.Printf("DstPort: %d\n", genericFlow.DstPort)
56+
}
57+
ipAddr := record.IP()
58+
if ipAddr != nil {
59+
fmt.Printf("SrcIP: %v\n", ipAddr.SrcIP)
60+
fmt.Printf("DstIP: %v\n", ipAddr.DstIP)
61+
}
62+
/*
63+
other extension
64+
flowMisc := record.FlowMisc()
65+
cntFlow := record.CntFlow()
66+
vLan := record.VLan()
67+
asRouting := record.AsRouting()
68+
*/
69+
}
70+
}

nfrecord/id.h renamed to id.h

File renamed without changes.

main.go

Lines changed: 0 additions & 94 deletions
This file was deleted.

nffile/nffile.go renamed to nffile.go

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
1-
/*
2-
* Copyright (c) 2023, Peter Haag
3-
* All rights reserved.
4-
*
5-
* Redistribution and use in source and binary forms, with or without
6-
* modification, are permitted provided that the following conditions are met:
7-
*
8-
* * Redistributions of source code must retain the above copyright notice,
9-
* this list of conditions and the following disclaimer.
10-
* * Redistributions in binary form must reproduce the above copyright notice,
11-
* this list of conditions and the following disclaimer in the documentation
12-
* and/or other materials provided with the distribution.
13-
* * Neither the name of the author nor the names of its contributors may be
14-
* used to endorse or promote products derived from this software without
15-
* specific prior written permission.
16-
*
17-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20-
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21-
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22-
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23-
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24-
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25-
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26-
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27-
* POSSIBILITY OF SUCH DAMAGE.
28-
*/
1+
// Copyright © 2023 Peter Haag [email protected]
2+
// All rights reserved.
3+
//
4+
// Use of this source code is governed by the license that can be
5+
// found in the LICENSE file.
296

30-
package nffile
7+
// Package nfdump provides an API for nfdump files
8+
package nfdump
319

3210
import (
3311
"bytes"

0 commit comments

Comments
 (0)