-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_commented.go
403 lines (350 loc) · 10.8 KB
/
project_commented.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package main
import (
"encoding/binary"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"log"
"net"
"os"
"strconv"
"time"
)
var (
device string = ""
snapshotLen int32 = 1024
promiscuous bool = false
err error
timeout time.Duration = 30 * time.Second
handle *pcap.Handle
)
type status uint8
const (
CLOSED status = iota
SYN_RECEIVED
SYN_SENT
ESTABLISHED
FIN_WAIT_1
FIN_WAIT_2
CLOSING
CLOSE_WAIT
LAST_ACK
USELESS
)
type flow_key struct {
addr1 uint32
addr2 uint32
port1 uint16
port2 uint16
}
type flow_value struct {
pkts uint32
bytes uint32
starting_time time.Time
tot_duration time.Duration
incarnations uint32
s1 status
s2 status
}
func main() {
if(len(os.Args) != 1+2){
fmt.Println("Usage is:",os.Args[0]," <device> <#tcp_pkts>")
return
}
//args
device = os.Args[1]
limit,err := strconv.Atoi(os.Args[2])
if err!=nil {
fmt.Println("<#tcp_pkts> must be an integer")
return
}
//map maintaining sensed/processed data
m := make(map[uint32]map[flow_key]flow_value)
// Open device
handle, err = pcap.OpenLive(device, snapshotLen, promiscuous, timeout)
if err != nil {log.Fatal(err) }
defer handle.Close()
count := 0
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
if processPacketInfo(packet,&m) {
count ++
}
if count >= limit {
break
}
fmt.Println("______________________________")
}
print_statistics(&m)
}
func print_statistics(m *map[uint32]map[flow_key]flow_value) {
for k,v := range *m {
fmt.Println("hash:",k)
for k2,v2 := range v {
fmt.Println("-",int2ipv4(k2.addr1),"(port",k2.port1,") <---->",int2ipv4(k2.addr2), "(port",k2.port2,")")
fmt.Println("\tpkts:",v2.pkts)
fmt.Println("\tbytes:",v2.bytes)
if v2.incarnations>0 {
fmt.Println("\tavg duration:",time.Duration(uint64(v2.tot_duration)/uint64(v2.incarnations)))
} else {
fmt.Println("\tavg duration: NO DATA")
}
fmt.Println("\t# TCP sessions:",v2.incarnations)
fmt.Println("\tstatus 1:",v2.s1)
fmt.Println("\tstatus 2:",v2.s2)
}
fmt.Println()
}
}
func compute_flow_key(saddr uint32, daddr uint32, sport uint16, dport uint16) (flow_key,bool) {
var fk flow_key
is1to2 := saddr < daddr
if is1to2 {
fk = flow_key{
saddr,
daddr,
sport,
dport,
}
}else{
fk = flow_key{
daddr,
saddr,
dport,
sport,
}
}
return fk, is1to2
}
func processPacketInfo(packet gopacket.Packet, m *map[uint32]map[flow_key]flow_value) bool {
isTCP := false
// L3 - Let's see if the packet is IP
ipLayer := packet.Layer(layers.LayerTypeIPv4)
if ipLayer != nil {
fmt.Println("IP layer detected.")
ip, _ := ipLayer.(*layers.IPv4)
// IP layer variables:
// Version (Either 4 or 6)
// IHL (IP Header Length in 32-bit words)
// TOS, Length, Id, Flags, FragOffset, TTL, Protocol (TCP?),
// Checksum, SrcIP, DstIP
fmt.Printf("From %s to %s\n", ip.SrcIP, ip.DstIP)
// L4 - Let's see if the packet is TCP
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
isTCP = true
fmt.Println("TCP layer detected.")
tcp,_ := tcpLayer.(*layers.TCP)
// TCP layer variables:
// SrcPort, DstPort, Seq, Ack, DataOffset, Window, Checksum, Urgent
// Bool flags: FIN, SYN, RST, PSH, ACK, URG, ECE, CWR, NS
fmt.Printf("From port %d to %d\n", tcp.SrcPort, tcp.DstPort)
syn := tcp.SYN
fin := tcp.FIN
ack := tcp.ACK
// L5 - applicationLayer contains the payload
var bytes uint32
bytes = 0
applicationLayer := packet.ApplicationLayer()
if applicationLayer != nil {
fmt.Println("Application layer/Payload found.")
bytes = uint32(len(applicationLayer.Payload()))
fmt.Printf("payload length: %d\n", bytes)
}//end L5
//processing: from here
//check previous TCP status
fk,is1to2 := compute_flow_key(
to_uint32(ip.SrcIP),
to_uint32(ip.DstIP),
uint16(tcp.SrcPort),
uint16(tcp.DstPort),
)
hash := hashing(fk)
prev_s1,prev_s2 := CLOSED,CLOSED
_,present := (*m)[hash]
if(!present){
(*m)[hash] = make(map[flow_key]flow_value)
}
fv,present2 := (*m)[hash][fk]
if present2 {
prev_s1 = fv.s1
prev_s2 = fv.s2
}
var new_status_sender, new_status_receiver status
var isStatusChanged bool
if is1to2 {
//addr1 sent to addr2, so new status for s1
new_status_sender = compute_new_status_sender(prev_s2,syn,fin)
new_status_receiver,isStatusChanged = compute_new_status_receiver(prev_s1,syn,fin,ack)
}else{
//addr2 sent to addr1, so new status for s2
new_status_sender = compute_new_status_sender(prev_s1,syn,fin)
new_status_receiver,isStatusChanged = compute_new_status_receiver(prev_s2,syn,fin,ack)
}
//updating (or creating) flow_value
if present2 {
fv.bytes += bytes
fv.pkts += 1
}else{
fv = flow_value{
1, //pkts
bytes, //bytes
time.Now(), //starting_time
0, //tot_duration
0, //incarnations
CLOSED, //status 1
CLOSED, //status 2
}
}
if is1to2 {
fv.s1 = new_status_receiver
fv.s2 = new_status_sender
} else {
fv.s2 = new_status_receiver
fv.s1 = new_status_sender
}
//print new status
var str_is1to2, str_ack, str_fin, str_syn string
if ack {str_ack = "A"} else {str_ack = "_"}
if syn {str_syn = "S"} else {str_syn = "_"}
if fin {str_fin = "F"} else {str_fin = "_"}
if is1to2 {str_is1to2 = "1-->2"} else {str_is1to2 = "2-->1"}
fmt.Println("ZZZ",int2ipv4(fk.addr1),"(port",fk.port1,") <---->",int2ipv4(fk.addr2), "(port",fk.port2,")",fv.s1,fv.s2,str_syn,str_fin,str_ack,str_is1to2,new_status_sender,new_status_receiver,prev_s1,prev_s2,time.Since(fv.starting_time))
//update when opening or closing connections
if isStatusChanged {
if fv.s1 == ESTABLISHED && fv.s2 == ESTABLISHED {
fv.starting_time = time.Now()
}else if fv.s1 == CLOSED && fv.s2 == CLOSED {
elapsed := time.Since(fv.starting_time)
fv.tot_duration += elapsed
fv.incarnations ++
}
}
(*m)[hash][fk] = fv
//end processing
}//end L4
}//end L3
// Iterate over all layers, printing out each layer type
/*
fmt.Println("All packet layers:")
for _,layer := range packet.Layers() {
fmt.Println("- ", layer.LayerType())
}
*/
// Check for errors
if err := packet.ErrorLayer(); err != nil {
fmt.Println("Error decoding some part of the packet:", err)
}
return isTCP
}
func compute_new_status_sender(
prev_status status,
syn bool,
fin bool,
) status {
var new_status status = prev_status
switch prev_status {
case CLOSED :
if syn {
new_status = SYN_SENT
}
case ESTABLISHED :
if fin {
new_status = FIN_WAIT_1
}
case CLOSE_WAIT :
if fin {
new_status = LAST_ACK
}
}
fmt.Print("prev_status_send",prev_status,",new stat_send", new_status)
return new_status
}
func compute_new_status_receiver(
prev_status status,
syn bool,
fin bool,
ack bool,
) (status,bool) {
var new_status status = prev_status
switch prev_status {
case CLOSED :
if syn {
new_status = SYN_RECEIVED
}
case SYN_RECEIVED :
if ack {
new_status = ESTABLISHED
}
case SYN_SENT :
if syn {
if ack {
new_status = ESTABLISHED
} else {
new_status = SYN_RECEIVED
}
}
case ESTABLISHED :
if fin {
new_status = CLOSE_WAIT
}
case FIN_WAIT_1 :
if fin {
new_status = CLOSING
} else if ack {
new_status = FIN_WAIT_2
}
case FIN_WAIT_2 :
if fin {
new_status = CLOSED
}
case CLOSING :
if ack {
new_status = CLOSED
}
//case CLOSE_WAIT, nothing to do here...
case LAST_ACK :
if ack {
new_status = CLOSED
}
}
return new_status, new_status!=prev_status
}
func hashing(fk flow_key) uint32 {
var u1,u2,u3 uint32
//u1
//simply the source address
u1 = fk.addr1
//u2
//bits at odd poistions in destination address now occupy even positions, and vice versa
u2 = ((fk.addr2 & 0x55555555) << 1) | ((fk.addr2 & 0xaaaaaaaa) >> 1)
//u3
//source port bits are shifted to most significant and least significant positions
//destination port bits occupy central positions
var port1_32,port2_32 uint32 = uint32(fk.port1),uint32(fk.port2)
u3 = ((port1_32 >> (16-3)) << (32-3)) | (port2_32 << (32-3-16)) | (port1_32 & ((1 << (16-3)) - 1))
//exclusive or operation is performed
return u1 ^ u2 ^ u3
}
//byte array must have dimension 4
func to_uint32(b net.IP) uint32 {
var tmp uint32
tmp = 0
for i:=0; i<4; i++ {
tmp <<= 8
tmp |= uint32(b[i])
}
return tmp
}
//byte array must have dimension 2
func to_uint16(b []byte) uint16 {
var tmp uint16
tmp = uint16(b[0]) << 8 | uint16(b[1])
return tmp
}
func int2ipv4(nn uint32) net.IP {
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, nn)
return ip
}