summaryrefslogtreecommitdiff
path: root/code/perpacket/parser.py
blob: 37e7a8a1c4fada9b4b8dd9622a81d407fc9bc8b4 (plain)
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
import csv
import numpy as np
import matplotlib.pyplot as plt

with open('packetdata.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    line_count = 0
    flowsTCP = []
    flowsUDP = []
    for row in csv_reader:
        if row["Protocol"] == 'TCP':
            flowExists = False
            src = row["Source"]
            dest = row["Destination"]
            srcPort = row["Info"].split(' > ')[0].split(' ')[0]
            destPort = row["Info"].split(' > ')[1].split(' ')[1]
            timeElapsed = float(row["Time delta from previous captured frame"])
            for flow in flowsTCP:
                if src in flow and dest in flow and srcPort in flow and destPort in flow:
                    flowExists = True
                    flow[4] += timeElapsed
                    flow[5] += 1
                    break
            if not flowExists:
                flowsTCP.append([src, dest, srcPort, destPort, timeElapsed, 1])
        # if row["Protocol"] == 'UDP':
        #     flowExists = False
        #     src = row["Source"]
        #     dest = row["Destination"]
        #     srcPort = row["Info"].split(' > ')[0].split(' ')[0]
        #     destPort = row["Info"].split(' > ')[1].split(' ')[1]
        #     for flow in flowsUDP:
        #         if src in flow and dest in flow and srcPort in flow and destPort in flow:
        #             flowExists = True
        #             break
        #     if not flowExists:
        #         flowsUDP.append([src, dest, srcPort, destPort])
    flowsTCPTimeAverages  = []    
    for flow in flowsTCP:
        flowsTCPTimeAverages.append(flow[4] / flow[5])
    with open('tcp_arrivel_time.csv', mode='w', newline='') as tcpArrival:
        fieldnames = ['time']
        writer = csv.DictWriter(tcpArrival, fieldnames=fieldnames)
        writer.writeheader()
        for flow in flowsTCPTimeAverages:
            writer.writerow({'time': flow})
    # num_bins = 20
    # counts, bin_edges = np.histogram (flowsTCPTimeAverages, bins=num_bins, normed=True)
    # cdf = np.cumsum (counts)
    # plt.plot (bin_edges[1:], cdf/cdf[-1])
    # plt.show()
    # print(flowsTCPTimeAverages)
    # print('UDP Length ' + str(len(flowsUDP)))