-
Notifications
You must be signed in to change notification settings - Fork 0
/
Udp.py
57 lines (45 loc) · 1.71 KB
/
Udp.py
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
import struct
from Layer import Layer
from HelperFuncs import RandShort, ProtocolTypesIP, chksum16bit
# noinspection SpellCheckingInspection
# https://en.wikipedia.org/wiki/User_Datagram_Protocol#UDP_datagram_structure
class UDP(Layer):
_my__protocol = ProtocolTypesIP.UDP
sport = None
dport = None
length = None
checksum = None
data = b''
def __init__(self, *, sport=None, dport=None):
self.sport = sport
self.dport = dport
def __bytes__(self):
self._autocomplete()
pkt = struct.pack("!HHHH", self.sport, self.dport, self.length, 0)
pkt += bytes(self.data)
return pkt
def toBytes(self, srcbytes, dstbytes):
self._autocomplete()
pkt = struct.pack("!HHHH", self.sport, self.dport, self.length, 0)
pkt += bytes(self.data)
tochecksum = self._get_pseudo_header(srcbytes, dstbytes)
tochecksum += pkt
self.checksum = chksum16bit(tochecksum)
pkt = pkt[:6] + struct.pack("!H", self.checksum) + pkt[8:]
return pkt
def _get_pseudo_header(self, ipsrcbytes, ipdstbytes):
pseudoIpv6Header = b""
pseudoIpv6Header += ipsrcbytes
pseudoIpv6Header += ipdstbytes
pseudoIpv6Header += struct.pack("!H", len(self))
pseudoIpv6Header += b"\x00"
pseudoIpv6Header += struct.pack("!B", ProtocolTypesIP.UDP)
return pseudoIpv6Header
def _autocomplete(self):
if self.sport is None:
self.sport = RandShort()
if self.dport is None:
raise ValueError("Destionation Port in UDP header empty.")
self.length = 8 + len(self.data)
def __len__(self):
return 2+2+2+2+len(self.data)