Skip to content

Commit 2524248

Browse files
committed
Teredo: Basic parsing of teredo IPv6 packet header
1 parent 4cbc278 commit 2524248

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

xcloud/protocol/ipv6.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import dpkt
2+
3+
class IPv6Packet:
4+
def __init__(
5+
self,
6+
ipv6_base: dpkt.ip6.IP6
7+
) -> None:
8+
self.ipv6_base = ipv6_base
9+
10+
@property
11+
def data(self) -> bytes:
12+
return self.ipv6_base.data
13+
14+
def __repr__(self):
15+
return (
16+
f"IPv6Packet(V={self.ipv6_base.v}, SRC={self.ipv6_base.src}, DST={self.ipv6_base.dst}, PLEN={self.ipv6_base.plen} NEXT={self.ipv6_base.nxt} HLIM={self.ipv6_base.hlim}) DATA={self.data}"
17+
)
18+
19+
@classmethod
20+
def parse(cls, data: bytes):
21+
ipv6_base = dpkt.ip6.IP6(data)
22+
if ipv6_base.v != 6:
23+
raise ValueError(
24+
f'Invalid IP version: Not 6'
25+
)
26+
return cls(ipv6_base)

xcloud/protocol/teredo.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
from dataclasses import dataclass, field
3+
from struct import pack, unpack, unpack_from
4+
from typing import Any, List, Optional, Tuple, Union
5+
6+
from . import ipv6
7+
8+
TEREDO_PORT = 3544
9+
TEREDO_HEADER_LENGTH = 22
10+
11+
class TeredoPacket:
12+
def __init__(
13+
self,
14+
ipv6_base: ipv6.IPv6Packet
15+
) -> None:
16+
self.ipv6 = ipv6_base
17+
18+
def __repr__(self) -> str:
19+
return (
20+
f"TeredoPacket(IPv6={self.ipv6})"
21+
)
22+
23+
@classmethod
24+
def parse(cls, data: bytes):
25+
if len(data) < TEREDO_HEADER_LENGTH:
26+
raise ValueError(
27+
f"Teredo packet length is less than {TEREDO_HEADER_LENGTH} bytes"
28+
)
29+
base = ipv6.IPv6Packet.parse(data)
30+
return cls(base)
31+

xcloud/scripts/pcap_reader.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from aioice import stun
1010
from construct.lib import containers
1111

12-
from ..protocol import packets
12+
from ..protocol import packets, teredo
1313

1414

1515
logging.basicConfig(level=logging.DEBUG)
@@ -27,9 +27,13 @@ def print_rtp(rtp: rtp.RtpPacket) -> None:
2727

2828
print(f'RTP: {payload_name.name} {rtp}')
2929

30+
def print_teredo(teredo: teredo.TeredoPacket) -> None:
31+
print(f'TEREDO: {teredo}')
32+
3033
PACKET_TYPES = [
3134
(stun.parse_message, print_stun),
32-
(rtp.RtpPacket.parse, print_rtp)
35+
(rtp.RtpPacket.parse, print_rtp),
36+
(teredo.TeredoPacket.parse, print_teredo)
3337
]
3438

3539
def packet_filter(filepath):
@@ -56,6 +60,7 @@ def parse_file(pcap_filepath: str) -> None:
5660
instance = cls(packet)
5761
print_func(instance)
5862
except:
63+
#LOG.exception('Failed to parse')
5964
pass
6065

6166

0 commit comments

Comments
 (0)