This repository was archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathread.go
More file actions
112 lines (102 loc) · 2.82 KB
/
Copy pathread.go
File metadata and controls
112 lines (102 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bufio"
"encoding/hex"
"fmt"
"io"
"log"
"net"
)
const headerLen int = 20
// Read handles the incoming packets
func Read(ic *net.TCPConn) {
var (
version int
dataLen int
setID int
dataSet []byte
)
log.Printf("New IPFIX connection from %s at %v\n", *name, ic.RemoteAddr())
conn := NewExtConns()
// Close connections when this function ends
defer func() {
CloseExtConns(conn)
log.Printf("Close IPFIX connection to %s at %v\n", *name, ic.RemoteAddr())
err := ic.Close()
checkErr(err)
}()
// Create a buffer for incoming packets
r := bufio.NewReader(ic)
header := make([]byte, headerLen)
for {
// Read the next 20 bytes which represent
// the ipfix header + setheader at once
if _, err := io.ReadFull(r, header); err == nil {
// Extract the ipfix version
version = int(uint16(header[0])<<8 + uint16(header[1]))
// Extract the length of the whole packet
dataLen = int(uint16(header[2])<<8 + uint16(header[3]))
// Extract the setID
setID = int(uint16(header[16])<<8 + uint16(header[17]))
// Check if we have a valid IPFIX packet
if version != 10 || dataLen < headerLen {
log.Printf("[WARN] Malformed IPFIX header: %v\n", header)
break
}
// Create a buffer which holds exactly one
// dataSet of the length of (dataLen-headerLen)
// or in words the packet length minus the header length
dataSet = make([]byte, dataLen-headerLen)
} else if err != nil {
if err != io.EOF {
log.Printf("[WARN] Couldn't read header: %s\n", err)
}
break
}
// Read the next (dataLen-headerLen) bytes into
// the dataSet buffer at once
if _, err := io.ReadFull(r, dataSet); err == nil {
if *verbose {
fmt.Println("########################################################################")
fmt.Printf("headerversion: %d, headerlength: %d, setID: %d\n", version, dataLen, setID)
fmt.Println("raw header:", header)
}
if *debug {
fmt.Println("hexdump output:")
fmt.Printf("%s\n", hex.Dump(header))
fmt.Printf("%s\n", hex.Dump(dataSet))
}
if *paddr != "" {
prom.CounterVecMetrics["horaclifix_packets_total"].WithLabelValues(*name).Inc()
}
switch setID {
case 256:
// Append the dataSet to the header.
data := append(header, dataSet...)
SendHandshake(ic, data)
case 258:
msg := ParseRecSipUDP(dataSet)
conn.Send(msg)
case 259:
msg := ParseSendSipUDP(dataSet)
conn.Send(msg)
case 260:
msg := ParseRecSipTCP(dataSet)
conn.Send(msg)
case 261:
msg := ParseSendSipTCP(dataSet)
conn.Send(msg)
case 268:
msg := ParseQosStats(dataSet)
conn.Send(msg)
default:
log.Printf("[WARN] Unhandled setID: %v\nfor dataset: %v\n", setID, dataSet)
}
} else if err != nil {
if err != io.EOF {
log.Printf("[WARN] Couldn't read dataset: %s\n", err)
}
break
}
}
}