summaryrefslogtreecommitdiffstats
path: root/extras/packetforge/ParseGraph.py
blob: 31fc3039fed2c46012355f47876bf5ac7f14c251 (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
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
# Copyright (c) 2022 Intel and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ProtocolHeader import *
from ForgeResult import *
from Node import *
from Edge import *
import os


class ParseGraph:
    def __init__(self):
        self.nodeDict = {}
        self.edgeDict = {}

    def Create(folder):
        try:
            pg = ParseGraph()
            if not os.path.exists(folder):
                print("folder not exisit")
                return None

            if os.path.exists(folder + "/nodes"):
                pg.LoadNodesFromDirectory(folder + "/nodes")
            if os.path.exists(folder + "/edges"):
                pg.LoadEdgesFromDirectory(folder + "/edges")
        except:
            print("Failed to create Parse Graph")
            return None
        else:
            return pg

    def Nodes(self):
        nodes = []
        nodes.extend(self.nodeDict.values)
        return nodes

    def Edges(self):
        edges = []
        edges.extend(self.edgeDict.values)
        return edges

    def LoadNodesFromDirectory(self, folder):
        for root, dirs, files in os.walk(folder):
            for f in files:
                self.LoadNodeFromFile(os.path.join(root, f))

    def LoadEdgesFromDirectory(self, folder):
        for root, dirs, files in os.walk(folder):
            for f in files:
                self.LoadEdgeFromFile(os.path.join(root, f))

    def LoadNodeFromFile(self, file):
        try:
            node = Node.Create(file)

            if node == None:
                print("No node created")
                return None

            self.AddNode(node)
        except:
            print("Failed to create node from " + file)

    def LoadEdgeFromFile(self, file):
        try:
            edges = Edge.Create(file)

            if edges == None:
                print("No edge created")
                return None

            for edge in edges:
                self.AddEdge(edge)
        except:
            print("Failed to create edge from " + file)

    def createProtocolHeader(self, name):
        if name in self.nodeDict:
            return ProtocolHeader(self.nodeDict[name])
        return None

    def GetNode(self, name):
        if name in self.nodeDict:
            return self.nodeDict[name]
        return None

    def GetEdge(self, start, end):
        key = start + "-" + end
        if key in self.edgeDict:
            return self.edgeDict[key]
        return None

    def AddNode(self, node):
        if node.Name in self.nodeDict:
            print("Warning: node {0} already exist", node.Name)

        self.nodeDict[node.Name] = node

    def AddEdge(self, edge):
        key = edge.Start + "-" + edge.End
        if key in self.edgeDict:
            print("Warning: edge {0} already exist", key)
        self.edgeDict[key] = edge

    def Forge(self, path):
        headerList = []

        # set field value/mask
        for headerConfig in path.stack:
            header = self.createProtocolHeader(headerConfig.Header)

            if header == None:
                return None

            for hcf in headerConfig.fields:
                attr = False
                if not header.SetField(hcf.Name, hcf.Value):
                    if not header.SetAttribute(hcf.Name, hcf.Value):
                        print("failed to set value of " + hcf.Name)
                        return None
                    else:
                        attr = True

                if not attr and not header.SetMask(hcf.Name, hcf.Mask):
                    print("failed to set mask of " + hcf.Name)
                    return None

            header.Adjust()

            headerList.append(header)

        # apply edge actions and length autoincrease
        for i in range(1, len(headerList)):
            start = headerList[i - 1]
            end = headerList[i]

            edge = self.GetEdge(start.Name(), end.Name())

            if edge == None:
                print("no edge exist for {0}, {1}", start.Name, end.Name)
                return None

            edge.Apply(start, end)

            increase = end.GetSize()
            for j in range(i):
                headerList[j].AppendAuto(increase)

        # resolve buffer
        pktLen = 0
        for header in headerList:
            header.Resolve()
            pktLen += len(header.Buffer)

        # join buffer
        pktbuf = []
        mskbuf = []

        offset = 0
        for header in headerList:
            pktbuf.extend(header.Buffer)
            mskbuf.extend(header.Mask)

            offset += len(header.Buffer)

        result = ForgeResult(headerList, pktbuf, mskbuf)

        return result