root/branch/UMPA/umpa/protocols/_decoder.py @ 4988

Revision 4988, 2.6 kB (checked in by getxsick, 4 years ago)

Check if there is any payload. Also allow to not keeping order of layers

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2009 Adriano Monteiro Marques.
5#
6# Author: Bartosz SKOWRON <getxsick at gmail dot com>
7#
8# This library is free software; you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published
10# by the Free Software Foundation; either version 2.1 of the License, or
11# (at your option) any later version.
12#
13# This library is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16# License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with this library; if not, write to the Free Software Foundation,
20# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22import umpa
23from umpa.protocols import Ethernet, IP, TCP, UDP, Payload
24from umpa.protocols import _consts as consts
25
26def decode(buffer, linktype):
27    # TODO: rewrite to check what protocols are available (also local from $HOME)
28    # and keep it in a dict
29    # XXX propably type of upper layer should be unified
30    packet = umpa.Packet(strict=False)
31
32    # 2nd layer
33    next_type = None
34    if linktype == consts.DLT_EN10MB:
35        header = Ethernet()
36        buffer = header.load_raw(buffer)
37        next_type = header._type
38    #elif linktype == consts.DLT_LINUX_SLL:
39    #    raise Exception("got SLL")
40    #    header = SLL()
41    #    header.load_raw(buffer)
42    #    next_type = header.ltype
43    else:
44        header = Payload()
45        header.load_raw(buffer)
46        packet.include(header)
47        return packet
48    packet.include(header)
49
50    # 3rd layer
51    if next_type == consts.ETHERTYPE_IP:
52        header = IP()
53        buffer = header.load_raw(buffer)
54        next_type = header._proto
55    #elif next_type == consts.ETHERTYPE_IPV6:
56    #    header = IPv6()
57    #    header.load_raw(buffer)
58    #    next_type = header.nxt
59    else:
60        header = Payload()
61        header.load_raw(buffer)
62        packet.include(header)
63        return packet
64    packet.include(header)
65
66    # 4th layer
67    if next_type == consts.PROTOCOL_TCP:
68        header = TCP()
69        buffer = header.load_raw(buffer)
70    elif next_type == consts.PROTOCOL_UDP:
71        header = UDP()
72        buffer = header.load_raw(buffer)
73    else:
74        header = Payload()
75        header.load_raw(buffer)
76        packet.include(header)
77        return packet
78    packet.include(header)
79
80    # payload
81    if len(buffer) > 0:
82        data = Payload()
83        data.load_raw(buffer)
84        packet.include(data)
85
86    return packet
Note: See TracBrowser for help on using the browser.