File: ieutils.py

package info (click to toggle)
python-ipfix 0.9.7-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: python: 1,825; makefile: 149
file content (72 lines) | stat: -rw-r--r-- 2,359 bytes parent folder | download | duplicates (4)
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
#
# python-ipfix (c) 2013 Brian Trammell.
#
# Many thanks to the mPlane consortium (http://www.ict-mplane.eu) for
# its material support of this effort.
# 
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.
#

import xml.etree.ElementTree as etree
import urllib.request as urlreq
from warnings import warn

from . import types
from . import ie

def iana_xml_to_iespec(uri = "http://www.iana.org/assignments/ipfix/ipfix.xml"):
    iespecs = []

    nsmap = { "iana" : "http://www.iana.org/assignments" }

    res = urlreq.urlopen(uri)    
    root = etree.parse(res).getroot()
    
    for recelem in root.iterfind("iana:registry[@id='ipfix-information-elements']/iana:record", nsmap):
        (name, typename, num) = (None, None, None)
        for fieldelem in recelem.iter():
            if fieldelem.tag[-4:] == "name":
                name = fieldelem.text
            elif fieldelem.tag[-8:] == "dataType":
                typename = fieldelem.text
            elif fieldelem.tag[-9:] == "elementId":
                num = fieldelem.text


        if name and typename and num:
            try:
                ietype = types.for_name(typename)
                iespecs.append("%s(%u)<%s>[%u]" % (name, int(num), ietype.name, ietype.length))
            except:
                pass
        
    return iespecs


def reverse_iespec(iespec):
    (name, pen, num, typename, length) = ie.parse_spec(iespec)
    revname = "reverse" + name[0].capitalize() + name[1:]
    if pen:
        num |= 0x4000
    else:
        pen = 29305

    return "%s(%u/%u)<%s>[%u]" % (revname, pen, num, typename, length)    


def write_specfile(filename, iespecs):
    with open(filename, "w") as f:
        for spec in iespecs:
            f.write(spec)
            f.write("\n")