backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / xflow / netflow.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import struct
17
18 NETFLOW_V1 = 0x01
19 NETFLOW_V5 = 0x05
20 NETFLOW_V6 = 0x06
21 NETFLOW_V7 = 0x07
22 NETFLOW_V8 = 0x08
23 NETFLOW_V9 = 0x09
24
25
26 class NetFlow(object):
27     _PACK_STR = '!H'
28     _NETFLOW_VERSIONS = {}
29
30     @staticmethod
31     def register_netflow_version(version):
32         def _register_netflow_version(cls):
33             NetFlow._NETFLOW_VERSIONS[version] = cls
34             return cls
35         return _register_netflow_version
36
37     def __init__(self):
38         super(NetFlow, self).__init__()
39
40     @classmethod
41     def parser(cls, buf):
42         (version,) = struct.unpack_from(cls._PACK_STR, buf)
43
44         cls_ = cls._NETFLOW_VERSIONS.get(version, None)
45         if cls_:
46             return cls_.parser(buf)
47         else:
48             return None
49
50
51 @NetFlow.register_netflow_version(NETFLOW_V5)
52 class NetFlowV5(object):
53     _PACK_STR = '!HHIIIIBBH'
54     _MIN_LEN = struct.calcsize(_PACK_STR)
55
56     def __init__(self, version, count, sys_uptime, unix_secs,
57                  unix_nsecs, flow_sequence, engine_type, engine_id,
58                  sampling_interval, flows=None):
59         self.version = version
60         self.count = count
61         self.sys_uptime = sys_uptime
62         self.unix_secs = unix_secs
63         self.unix_nsecs = unix_nsecs
64         self.flow_sequence = flow_sequence
65         self.engine_type = engine_type
66         self.engine_id = engine_id
67         self.sampling_interval = sampling_interval
68
69     @classmethod
70     def parser(cls, buf):
71         (version, count, sys_uptime, unix_secs, unix_nsecs,
72          flow_sequence, engine_type, engine_id, sampling_interval) = \
73             struct.unpack_from(cls._PACK_STR, buf)
74
75         msg = cls(version, count, sys_uptime, unix_secs, unix_nsecs,
76                   flow_sequence, engine_type, engine_id,
77                   sampling_interval)
78         offset = cls._MIN_LEN
79         msg.flows = []
80         while len(buf) > offset:
81             f = NetFlowV5Flow.parser(buf, offset)
82             offset += NetFlowV5Flow._MIN_LEN
83             msg.flows.append(f)
84
85         return msg
86
87
88 class NetFlowV5Flow(object):
89     _PACK_STR = '!IIIHHIIIIHHxBBBHHBB2x'
90     _MIN_LEN = struct.calcsize(_PACK_STR)
91
92     def __init__(self, srcaddr, dstaddr, nexthop, input_, output,
93                  dpkts, doctets, first, last, srcport, dstport,
94                  tcp_flags, prot, tos, src_as, dst_as, src_mask,
95                  dst_mask):
96         self.srcaddr = srcaddr
97         self.dstaddr = dstaddr
98         self.nexthop = nexthop
99         self.input = input_
100         self.output = output
101         self.dpkts = dpkts
102         self.doctets = doctets
103         self.first = first
104         self.last = last
105         self.srcport = srcport
106         self.dstport = dstport
107         self.tcp_flags = tcp_flags
108         self.prot = prot
109         self.tos = tos
110         self.src_as = src_as
111         self.dst_as = dst_as
112         self.src_mask = src_mask
113         self.dst_mask = dst_mask
114
115     @classmethod
116     def parser(cls, buf, offset):
117         (srcaddr, dstaddr, nexthop, input_, output, dpkts, doctets,
118          first, last, srcport, dstport, tcp_flags, prot, tos, src_as,
119          dst_as, src_mask, dst_mask) = struct.unpack_from(
120             cls._PACK_STR, buf, offset)
121         msg = cls(srcaddr, dstaddr, nexthop, input_, output, dpkts,
122                   doctets, first, last, srcport, dstport, tcp_flags,
123                   prot, tos, src_as, dst_as, src_mask, dst_mask)
124
125         return msg