Skip to content

Commit

Permalink
Pulled v1.8 from code.google
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiran Bandla committed Sep 10, 2013
1 parent 7503ee6 commit 383cdbf
Show file tree
Hide file tree
Showing 12 changed files with 650 additions and 310 deletions.
7 changes: 6 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

dpkg-1.8:
- fix a typo in vrrp.py
- fix IPv4 and IPv6 packet to correctly handle zero payload length
- store cipher_suite as int in TLSServerHello to allow app-specific messages
- improve SSL parsing

dpkt-1.7:
- handle dynamic imports from py2exe/freeze.py/zipped egg
packages, from plotnikoff
Expand Down
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include AUTHORS CHANGES README LICENSE HACKING
recursive-include examples *
recursive-include tests *
4 changes: 2 additions & 2 deletions dpkt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# $Id: __init__.py 82 2011-01-10 03:43:38Z timur.alperovich@gmail.com $
# $Id: __init__.py 89 2013-05-23 01:31:22Z andrewflnr@gmail.com $

"""fast, simple packet creation and parsing."""

__author__ = 'Dug Song <[email protected]>'
__copyright__ = 'Copyright (c) 2004 Dug Song'
__license__ = 'BSD'
__url__ = 'http://dpkt.googlecode.com/'
__version__ = '1.7'
__version__ = '1.8'

from dpkt import *

Expand Down
96 changes: 2 additions & 94 deletions dpkt/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# Cease Subcodes - RFC 4486
# NOPEER Community - RFC 3765
# Multiprotocol Extensions - 2858
# Support for Four-octet AS Number Space - RFC 4893

# Message Types
OPEN = 1
Expand All @@ -36,8 +35,6 @@
CLUSTER_LIST = 10
MP_REACH_NLRI = 14
MP_UNREACH_NLRI = 15
AS4_PATH = 17
AS4_AGGREGATOR = 18

# Origin Types
ORIGIN_IGP = 0
Expand Down Expand Up @@ -72,13 +69,6 @@
# Capability Types
CAP_MULTIPROTOCOL = 1
CAP_ROUTE_REFRESH = 2
CAP_OUTBOUND_ROUTE_FILTER = 3
CAP_MULTI_ROUTE_TO_DEST = 4
CAP_GRACEFUL_RESTART = 64
CAP_SUPPORT_AS4 = 65
CAP_SUPPORT_DYN_CAP = 67
CAP_MULTISESSION_BGP = 68
CAP_ROUTE_REFRESH_OLD = 128

# NOTIFICATION Error Codes
MESSAGE_HEADER_ERROR = 1
Expand Down Expand Up @@ -209,15 +199,6 @@ def unpack(self, buf):
dpkt.Packet.unpack(self, buf)
self.data = self.data[:self.len]

if self.code == CAP_MULTIPROTOCOL:
self.data = self.multiprotocol = self.MultiProtocol(self.data)

class MultiProtocol(dpkt.Packet):
__hdr__ = (
('afi', 'H', 1),
('res', 'B', 0),
('safi', 'B', 1)
)

class Update(dpkt.Packet):
__hdr_defaults__ = {
Expand Down Expand Up @@ -316,12 +297,7 @@ def unpack(self, buf):
if self.type == ORIGIN:
self.data = self.origin = self.Origin(self.data)
elif self.type == AS_PATH:
preserved=self.data
try:
self.data = self.as_path = self.AS4Path(self.data)
except dpkt.UnpackError, (errno):
self.data=preserved
self.data = self.as_path = self.ASPath(self.data)
self.data = self.as_path = self.ASPath(self.data)
elif self.type == NEXT_HOP:
self.data = self.next_hop = self.NextHop(self.data)
elif self.type == MULTI_EXIT_DISC:
Expand All @@ -331,12 +307,7 @@ def unpack(self, buf):
elif self.type == ATOMIC_AGGREGATE:
self.data = self.atomic_aggregate = self.AtomicAggregate(self.data)
elif self.type == AGGREGATOR:
preserved=self.data
try:
self.data = self.as4_aggregator = self.AS4Aggregator(self.data)
except dpkt.UnpackError, (errno):
self.data=preserved
self.data = self.aggregator = self.Aggregator(self.data)
self.data = self.aggregator = self.Aggregator(self.data)
elif self.type == COMMUNITIES:
self.data = self.communities = self.Communities(self.data)
elif self.type == ORIGINATOR_ID:
Expand All @@ -347,10 +318,6 @@ def unpack(self, buf):
self.data = self.mp_reach_nlri = self.MPReachNLRI(self.data)
elif self.type == MP_UNREACH_NLRI:
self.data = self.mp_unreach_nlri = self.MPUnreachNLRI(self.data)
elif self.type == AS4_PATH:
self.data = self.as4_path = self.AS4Path(self.data)
elif self.type == AS4_AGGREGATOR:
self.data = self.as4_aggregator = self.AS4Aggregator(self.data)

def __len__(self):
if self.extended_length:
Expand All @@ -376,7 +343,6 @@ class Origin(dpkt.Packet):
)

class ASPath(dpkt.Packet):
#__hdr__=()
__hdr_defaults__ = {
'segments': []
}
Expand All @@ -402,10 +368,6 @@ class ASPathSegment(dpkt.Packet):
('len', 'B', 0)
)

__hdr_defaults__ = {
'path': []
}

def unpack(self, buf):
dpkt.Packet.unpack(self, buf)
l = []
Expand Down Expand Up @@ -458,7 +420,6 @@ class Aggregator(dpkt.Packet):
)

class Communities(dpkt.Packet):
#__hdr__ = ()
__hdr_defaults__ = {
'list': []
}
Expand Down Expand Up @@ -617,59 +578,6 @@ def __str__(self):
return self.pack_hdr() + \
''.join(map(str, self.data))

class AS4Path(dpkt.Packet):
#__hdr__ = ()
__hdr_defaults__ = {
'segments': []
}

def unpack(self, buf):
self.data = buf
l = []
while self.data:
seg = self.AS4PathSegment(self.data)
self.data = self.data[len(seg):]
l.append(seg)
self.data = self.segments = l

def __len__(self):
return sum(map(len, self.data))

def __str__(self):
return ''.join(map(str, self.data))

class AS4PathSegment(dpkt.Packet):
__hdr__ = (
('type', 'B', 0),
('len', 'B', 0)
)

def unpack(self, buf):
dpkt.Packet.unpack(self, buf)
l = []
for i in range(self.len):
AS = struct.unpack('>L', self.data[:4])[0]
self.data = self.data[4:]
l.append(AS)
self.data = self.path = l

def __len__(self):
return self.__hdr_len__ + \
4 * len(self.path)

def __str__(self):
as_str = ''
for AS in self.path:
as_str += struct.pack('>L', AS)
return self.pack_hdr() + \
as_str

class AS4Aggregator(dpkt.Packet):
__hdr__ = (
('asn', 'L', 0),
('ip', 'I', 0)
)


class Notification(dpkt.Packet):
__hdr__ = (
Expand Down
1 change: 0 additions & 1 deletion dpkt/ethernet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
ETH_TYPE_MPLS_MCAST = 0x8848 # MPLS Multicast
ETH_TYPE_PPPoE_DISC = 0x8863 # PPP Over Ethernet Discovery Stage
ETH_TYPE_PPPoE = 0x8864 # PPP Over Ethernet Session Stage
ETH_TYPE_LLDP = 0x88CC # Link Layer Discovery Protocol

# MPLS label stack fields
MPLS_LABEL_MASK = 0xfffff000
Expand Down
10 changes: 7 additions & 3 deletions dpkt/http.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $Id: http.py 80 2011-01-06 16:50:42Z jon.oberheide $
# $Id: http.py 86 2013-03-05 19:25:19Z [email protected] $

"""Hypertext Transfer Protocol."""

Expand Down Expand Up @@ -193,9 +193,13 @@ def test_format_request(self):
r.headers['content-type'] = 'text/plain'
r.headers['content-length'] = '5'
r.body = 'hello'
assert str(r) == 'POST /foo/bar/baz.html HTTP/1.0\r\ncontent-length: 5\r\ncontent-type: text/plain\r\n\r\nhello'
s = str(r)
assert s.startswith('POST /foo/bar/baz.html HTTP/1.0\r\n')
assert s.endswith('\r\n\r\nhello')
assert '\r\ncontent-length: 5\r\n' in s
assert '\r\ncontent-type: text/plain\r\n' in s
r = Request(str(r))
assert str(r) == 'POST /foo/bar/baz.html HTTP/1.0\r\ncontent-length: 5\r\ncontent-type: text/plain\r\n\r\nhello'
assert str(r) == s

def test_chunked_response(self):
s = """HTTP/1.1 200 OK\r\nCache-control: no-cache\r\nPragma: no-cache\r\nContent-Type: text/javascript; charset=utf-8\r\nContent-Encoding: gzip\r\nTransfer-Encoding: chunked\r\nSet-Cookie: S=gmail=agg:gmail_yj=v2s:gmproxy=JkU; Domain=.google.com; Path=/\r\nServer: GFE/1.3\r\nDate: Mon, 12 Dec 2005 22:33:23 GMT\r\n\r\na\r\n\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00\r\n152\r\nm\x91MO\xc4 \x10\x86\xef\xfe\n\x82\xc9\x9eXJK\xe9\xb6\xee\xc1\xe8\x1e6\x9e4\xf1\xe0a5\x86R\xda\x12Yh\x80\xba\xfa\xef\x85\xee\x1a/\xf21\x99\x0c\xef0<\xc3\x81\xa0\xc3\x01\xe6\x10\xc1<\xa7eYT5\xa1\xa4\xac\xe1\xdb\x15:\xa4\x9d\x0c\xfa5K\x00\xf6.\xaa\xeb\x86\xd5y\xcdHY\x954\x8e\xbc*h\x8c\x8e!L7Y\xe6\'\xeb\x82WZ\xcf>8\x1ed\x87\x851X\xd8c\xe6\xbc\x17Z\x89\x8f\xac \x84e\xde\n!]\x96\x17i\xb5\x02{{\xc2z0\x1e\x0f#7\x9cw3v\x992\x9d\xfc\xc2c8\xea[/EP\xd6\xbc\xce\x84\xd0\xce\xab\xf7`\'\x1f\xacS\xd2\xc7\xd2\xfb\x94\x02N\xdc\x04\x0f\xee\xba\x19X\x03TtW\xd7\xb4\xd9\x92\n\xbcX\xa7;\xb0\x9b\'\x10$?F\xfd\xf3CzPt\x8aU\xef\xb8\xc8\x8b-\x18\xed\xec<\xe0\x83\x85\x08!\xf8"[\xb0\xd3j\x82h\x93\xb8\xcf\xd8\x9b\xba\xda\xd0\x92\x14\xa4a\rc\reM\xfd\x87=X;h\xd9j;\xe0db\x17\xc2\x02\xbd\xb0F\xc2in#\xfb:\xb6\xc4x\x15\xd6\x9f\x8a\xaf\xcf)\x0b^\xbc\xe7i\x11\x80\x8b\x00D\x01\xd8/\x82x\xf6\xd8\xf7J(\xae/\x11p\x1f+\xc4p\t:\xfe\xfd\xdf\xa3Y\xfa\xae4\x7f\x00\xc5\xa5\x95\xa1\xe2\x01\x00\x00\r\n0\r\n\r\n"""
Expand Down
17 changes: 14 additions & 3 deletions dpkt/ip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $Id: ip.py 65 2010-03-26 02:53:51Z dugsong $
# $Id: ip.py 87 2013-03-05 19:41:04Z [email protected] $

"""Internet Protocol."""

Expand Down Expand Up @@ -55,7 +55,10 @@ def unpack(self, buf):
if ol < 0:
raise dpkt.UnpackError, 'invalid header length'
self.opts = buf[self.__hdr_len__:self.__hdr_len__ + ol]
buf = buf[self.__hdr_len__ + ol:self.len]
if self.len:
buf = buf[self.__hdr_len__ + ol:self.len]
else: # very likely due to TCP segmentation offload
buf = buf[self.__hdr_len__ + ol:]
try:
self.data = self._protosw[self.p](buf)
setattr(self, self.data.__class__.__name__.lower(), self.data)
Expand Down Expand Up @@ -285,6 +288,14 @@ def test_opt(self):
s = '\x4f\x00\x00\x50\xae\x08\x00\x00\x40\x06\x17\xfc\xc0\xa8\x0a\x26\xc0\xa8\x0a\x01\x07\x27\x08\x01\x02\x03\x04\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
ip = IP(s)
ip.sum = 0
self.failUnless(str(ip) == s)
self.failUnless(str(ip) == s)

def test_zerolen(self):
import tcp
d = 'X' * 2048
s = 'E\x00\x00\x004\xce@\x00\x80\x06\x00\x00\x7f\x00\x00\x01\x7f\x00\x00\x01\xccN\x0c8`\xff\xc6N_\x8a\x12\x98P\x18@):\xa3\x00\x00' + d
ip = IP(s)
self.failUnless(isinstance(ip.data, tcp.TCP))
self.failUnless(ip.tcp.data == d)

unittest.main()
22 changes: 17 additions & 5 deletions dpkt/ip6.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $Id: ip6.py 58 2010-03-06 00:06:14Z dugsong $
# $Id: ip6.py 87 2013-03-05 19:41:04Z [email protected] $

"""Internet Protocol, version 6."""

Expand All @@ -13,7 +13,11 @@ class IP6(dpkt.Packet):
('src', '16s', ''),
('dst', '16s', '')
)
_protosw = {} # XXX - shared with IP

# XXX - to be shared with IP. We cannot refer to the ip module
# right now because ip.__load_protos() expects the IP6 class to be
# defined.
_protosw = None

def _get_v(self):
return self.v_fc_flow >> 28
Expand All @@ -37,7 +41,10 @@ def unpack(self, buf):
dpkt.Packet.unpack(self, buf)
self.extension_hdrs = dict(((i, None) for i in ext_hdrs))

buf = self.data[:self.plen]
if self.plen:
buf = self.data[:self.plen]
else: # due to jumbo payload or TSO
buf = self.data

next = self.nxt

Expand Down Expand Up @@ -91,9 +98,14 @@ def get_proto(cls, p):
return cls._protosw[p]
get_proto = classmethod(get_proto)

# XXX - auto-load IP6 dispatch table from IP dispatch table
import ip
IP6._protosw.update(ip.IP._protosw)
# We are most likely still in the middle of ip.__load_protos() which
# implicitly loads this module through __import__(), so the content of
# ip.IP._protosw is still incomplete at the moment. By sharing the
# same dictionary by reference as opposed to making a copy, when
# ip.__load_protos() finishes, we will also automatically get the most
# up-to-date dictionary.
IP6._protosw = ip.IP._protosw

class IP6ExtensionHeader(dpkt.Packet):
"""
Expand Down
Loading

0 comments on commit 383cdbf

Please sign in to comment.