File: udp.py

package info (click to toggle)
python-os-ken 2.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 21,288 kB
  • sloc: python: 100,257; erlang: 14,517; ansic: 594; sh: 338; makefile: 136
file content (87 lines) | stat: -rw-r--r-- 3,193 bytes parent folder | download | duplicates (5)
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
# Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
#
# 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.

import struct

from . import packet_base
from . import packet_utils
from . import dhcp
from . import dhcp6
from . import vxlan
from . import geneve


class udp(packet_base.PacketBase):
    """UDP (RFC 768) header encoder/decoder class.

    An instance has the following attributes at least.
    Most of them are same to the on-wire counterparts but in host byte order.
    __init__ takes the corresponding args in this order.

    ============== ====================
    Attribute      Description
    ============== ====================
    src_port       Source Port
    dst_port       Destination Port
    total_length   Length \
                   (0 means automatically-calculate when encoding)
    csum           Checksum \
                   (0 means automatically-calculate when encoding)
    ============== ====================
    """

    _PACK_STR = '!HHHH'
    _MIN_LEN = struct.calcsize(_PACK_STR)

    def __init__(self, src_port=1, dst_port=1, total_length=0, csum=0):
        super(udp, self).__init__()
        self.src_port = src_port
        self.dst_port = dst_port
        self.total_length = total_length
        self.csum = csum

    @staticmethod
    def get_packet_type(src_port, dst_port):
        if ((src_port in [67, 68] and dst_port == 67) or
                (dst_port in [67, 68] and src_port == 67)):
            return dhcp.dhcp
        if ((src_port in [546, 547] and dst_port == 547) or
                (dst_port in [546, 547] and src_port == 547)):
            return dhcp6.dhcp6
        if (dst_port == vxlan.UDP_DST_PORT or
                dst_port == vxlan.UDP_DST_PORT_OLD):
            return vxlan.vxlan
        if dst_port == geneve.UDP_DST_PORT:
            return geneve.geneve
        return None

    @classmethod
    def parser(cls, buf):
        (src_port, dst_port, total_length, csum) = struct.unpack_from(
            cls._PACK_STR, buf)
        msg = cls(src_port, dst_port, total_length, csum)
        return msg, cls.get_packet_type(src_port, dst_port), buf[msg._MIN_LEN:total_length]

    def serialize(self, payload, prev):
        if self.total_length == 0:
            self.total_length = udp._MIN_LEN + len(payload)
        h = struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
                        self.total_length, self.csum)
        if self.csum == 0:
            self.csum = packet_utils.checksum_ip(
                prev, self.total_length, h + payload)
            h = struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
                            self.total_length, self.csum)
        return h