Skip to content

Commit

Permalink
merging jonathanslenders:python3-compatibility, kbandla:migrate_py3 a…
Browse files Browse the repository at this point in the history
…nd kbandla:master

All tests pass in Python 2.7 and Python 3.5
  • Loading branch information
Kyle Keppler committed Oct 26, 2016
1 parent b6dd802 commit 433cd5d
Show file tree
Hide file tree
Showing 82 changed files with 1,245 additions and 996 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
@@ -1,11 +1,12 @@
language: python
python: 2.7
python: 3.5
env:
global:
LD_PRELOAD=/lib/x86_64-linux-gnu/libSegFault.so
matrix:
- TOXENV=py26,coveralls
- TOXENV=py27,coveralls
- TOXENV=py35,coveralls
- TOXENV=pypy,coveralls
- TOXENV=docs
before_install:
Expand Down
3 changes: 3 additions & 0 deletions AUTHORS
Expand Up @@ -61,3 +61,6 @@ Tim Yardley <yardley@gmail.com>

obormot <oscar.ibatullin@gmail.com>
pcapng module, Packet repr improvements

Kyle Keppler <kyle.keppler@gmail.com>
Python 3 port
5 changes: 4 additions & 1 deletion CHANGES
@@ -1,4 +1,7 @@
dpkg-1.8:
dpkt-1.9:
- add support for Python 3.5. Python 2.7 is still supported.

dpkt-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
Expand Down
130 changes: 67 additions & 63 deletions dpkt/__init__.py
@@ -1,72 +1,76 @@
"""fast, simple packet creation and parsing."""
from __future__ import absolute_import
from __future__ import division

__author__ = 'Dug Song'
__author_email__ = 'dugsong@monkey.org'
__license__ = 'BSD'
__url__ = 'http://dpkt.googlecode.com/'
__version__ = '1.8.8'

from dpkt import *

import ah
import aoe
import aim
import arp
import asn1
import bgp
import cdp
import dhcp
import diameter
import dns
import dtp
import esp
import ethernet
import gre
import gzip
import h225
import hsrp
import http
import icmp
import icmp6
import ieee80211
import igmp
import ip
import ip6
import ipx
import llc
import loopback
import mrt
import netbios
import netflow
import ntp
import ospf
import pcap
import pcapng
import pim
import pmap
import ppp
import pppoe
import qq
import radiotap
import radius
import rfb
import rip
import rpc
import rtp
import rx
import sccp
import sctp
import sip
import sll
import smb
import ssl
import stp
import stun
import tcp
import telnet
import tftp
import tns
import tpkt
import udp
import vrrp
import yahoo

from .dpkt import *

from . import ah
from . import aoe
from . import aim
from . import arp
from . import asn1
from . import bgp
from . import cdp
from . import dhcp
from . import diameter
from . import dns
from . import dtp
from . import esp
from . import ethernet
from . import gre
from . import gzip
from . import h225
from . import hsrp
from . import http
from . import icmp
from . import icmp6
from . import ieee80211
from . import igmp
from . import ip
from . import ip6
from . import ipx
from . import llc
from . import loopback
from . import mrt
from . import netbios
from . import netflow
from . import ntp
from . import ospf
from . import pcap
from . import pcapng
from . import pim
from . import pmap
from . import ppp
from . import pppoe
from . import qq
from . import radiotap
from . import radius
from . import rfb
from . import rip
from . import rpc
from . import rtp
from . import rx
from . import sccp
from . import sctp
from . import sip
from . import sll
from . import smb
from . import ssl
from . import stp
from . import stun
from . import tcp
from . import telnet
from . import tftp
from . import tns
from . import tpkt
from . import udp
from . import vrrp
from . import yahoo
11 changes: 7 additions & 4 deletions dpkt/ah.py
Expand Up @@ -3,7 +3,7 @@

"""Authentication Header."""

import dpkt
from . import dpkt


class AH(dpkt.Packet):
Expand All @@ -24,13 +24,13 @@ class AH(dpkt.Packet):
('spi', 'I', 0),
('seq', 'I', 0)
)
auth = ''
auth = b''

def unpack(self, buf):
dpkt.Packet.unpack(self, buf)
self.auth = self.data[:self.len]
buf = self.data[self.len:]
import ip
from . import ip

try:
self.data = ip.IP.get_proto(self.nxt)(buf)
Expand All @@ -42,4 +42,7 @@ def __len__(self):
return self.__hdr_len__ + len(self.auth) + len(self.data)

def __str__(self):
return self.pack_hdr() + str(self.auth) + str(self.data)
return str(self.__bytes__())

def __bytes__(self):
return self.pack_hdr() + bytes(self.auth) + bytes(self.data)
4 changes: 2 additions & 2 deletions dpkt/aim.py
Expand Up @@ -3,7 +3,7 @@

"""AOL Instant Messenger."""

import dpkt
from . import dpkt
import struct

# OSCAR: http://iserverd1.khstu.ru/oscar/
Expand Down Expand Up @@ -32,7 +32,7 @@ def unpack(self, buf):
if self.ast != 0x2a:
raise dpkt.UnpackError('invalid FLAP header')
if len(self.data) < self.len:
raise dpkt.NeedData, '%d left, %d needed' % (len(self.data), self.len)
raise dpkt.NeedData('%d left, %d needed' % (len(self.data), self.len))


class SNAC(dpkt.Packet):
Expand Down
12 changes: 8 additions & 4 deletions dpkt/aoe.py
Expand Up @@ -2,8 +2,8 @@
"""ATA over Ethernet Protocol."""

import struct
import dpkt
from decorators import deprecated
from . import dpkt
from .decorators import deprecated


class AOE(dpkt.Packet):
Expand Down Expand Up @@ -58,7 +58,7 @@ def unpack(self, buf):
def pack_hdr(self):
try:
return dpkt.Packet.pack_hdr(self)
except struct.error, e:
except struct.error as e:
raise dpkt.PackError(str(e))

# Deprecated methods, will be removed in the future
Expand Down Expand Up @@ -86,7 +86,11 @@ def _set_fl(self, fl): self.fl = fl
def __load_cmds():
prefix = 'AOE_CMD_'
g = globals()
for k, v in g.iteritems():
try:
gi = g.iteritems()
except:
gi = g.items()
for k, v in gi:
if k.startswith(prefix):
name = 'aoe' + k[len(prefix):].lower()
try:
Expand Down
9 changes: 5 additions & 4 deletions dpkt/aoeata.py
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
"""ATA over Ethernet ATA command"""
from __future__ import print_function

import dpkt
from . import dpkt

ATA_DEVICE_IDENTIFY = 0xec

Expand Down Expand Up @@ -35,11 +36,11 @@ class AOEATA(dpkt.Packet):


def test_aoeata():
s = '\x03\x0a\x6b\x19\x00\x00\x00\x00\x45\x00\x00\x28\x94\x1f\x00\x00\xe3\x06\x99\xb4\x23\x2b\x24\x00\xde\x8e\x84\x42\xab\xd1\x00\x50\x00\x35\xe1\x29\x20\xd9\x00\x00\x00\x22\x9b\xf0\xe2\x04\x65\x6b'
s = b'\x03\x0a\x6b\x19\x00\x00\x00\x00\x45\x00\x00\x28\x94\x1f\x00\x00\xe3\x06\x99\xb4\x23\x2b\x24\x00\xde\x8e\x84\x42\xab\xd1\x00\x50\x00\x35\xe1\x29\x20\xd9\x00\x00\x00\x22\x9b\xf0\xe2\x04\x65\x6b'
aoeata = AOEATA(s)
assert (str(aoeata) == s)
assert (bytes(aoeata) == s)


if __name__ == '__main__':
test_aoeata()
print 'Tests Successful...'
print('Tests Successful...')
7 changes: 4 additions & 3 deletions dpkt/aoecfg.py
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
"""ATA over Ethernet ATA command"""
from __future__ import print_function

import dpkt
from . import dpkt


class AOECFG(dpkt.Packet):
Expand All @@ -25,11 +26,11 @@ class AOECFG(dpkt.Packet):


def test_aoecfg():
s = '\x01\x02\x03\x04\x05\x06\x11\x12\x13\x14\x15\x16\x88\xa2\x10\x00\x00\x01\x02\x01\x80\x00\x00\x00\x12\x34\x00\x00\x00\x00\x04\x00' + '\0xed' * 1024
s = b'\x01\x02\x03\x04\x05\x06\x11\x12\x13\x14\x15\x16\x88\xa2\x10\x00\x00\x01\x02\x01\x80\x00\x00\x00\x12\x34\x00\x00\x00\x00\x04\x00' + b'\0xed' * 1024
aoecfg = AOECFG(s[14 + 10:])
assert (aoecfg.bufcnt == 0x1234)


if __name__ == '__main__':
test_aoecfg()
print 'Tests Successful...'
print('Tests Successful...')
2 changes: 1 addition & 1 deletion dpkt/arp.py
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
"""Address Resolution Protocol."""

import dpkt
from . import dpkt

# Hardware address format
ARP_HRD_ETH = 0x0001 # ethernet hardware
Expand Down
19 changes: 11 additions & 8 deletions dpkt/asn1.py
@@ -1,10 +1,13 @@
# $Id: asn1.py 23 2006-11-08 15:45:33Z dugsong $
# -*- coding: utf-8 -*-
"""Abstract Syntax Notation #1."""
from __future__ import print_function

import struct
import time
import dpkt
from . import dpkt
import sys
from . import compatible

# Type class
CLASSMASK = 0xc0
Expand Down Expand Up @@ -80,18 +83,18 @@ def decode(buf):

msg = []
while buf:
t = ord(buf[0])
t = compatible.compatible_ord(buf[0])
constructed = t & CONSTRUCTED
tag = t & TAGMASK
l = ord(buf[1])
l = compatible.compatible_ord(buf[1])
c = 0
if constructed and l == 128:
# XXX - constructed, indefinite length
msg.append((t, decode(buf[2:])))
elif l >= 128:
c = l & 127
if c == 1:
l = ord(buf[2])
l = compatible.compatible_ord(buf[2])
elif c == 2:
l = struct.unpack('>H', buf[2:4])[0]
elif c == 3:
Expand All @@ -113,7 +116,7 @@ def decode(buf):
if l == 0:
n = 0
elif l == 1:
n = ord(buf[0])
n = compatible.compatible_ord(buf[0])
elif l == 2:
n = struct.unpack('>H', buf[:2])[0]
elif l == 3:
Expand All @@ -134,9 +137,9 @@ def decode(buf):


def test_asn1():
s = '0\x82\x02Q\x02\x01\x0bc\x82\x02J\x04xcn=Douglas J Song 1, ou=Information Technology Division, ou=Faculty and Staff, ou=People, o=University of Michigan, c=US\n\x01\x00\n\x01\x03\x02\x01\x00\x02\x01\x00\x01\x01\x00\x87\x0bobjectclass0\x82\x01\xb0\x04\rmemberOfGroup\x04\x03acl\x04\x02cn\x04\x05title\x04\rpostalAddress\x04\x0ftelephoneNumber\x04\x04mail\x04\x06member\x04\thomePhone\x04\x11homePostalAddress\x04\x0bobjectClass\x04\x0bdescription\x04\x18facsimileTelephoneNumber\x04\x05pager\x04\x03uid\x04\x0cuserPassword\x04\x08joinable\x04\x10associatedDomain\x04\x05owner\x04\x0erfc822ErrorsTo\x04\x08ErrorsTo\x04\x10rfc822RequestsTo\x04\nRequestsTo\x04\tmoderator\x04\nlabeledURL\x04\nonVacation\x04\x0fvacationMessage\x04\x05drink\x04\x0elastModifiedBy\x04\x10lastModifiedTime\x04\rmodifiersname\x04\x0fmodifytimestamp\x04\x0ccreatorsname\x04\x0fcreatetimestamp'
assert (decode(s) == [(48, [(2, 11), (99, [(4, 'cn=Douglas J Song 1, ou=Information Technology Division, ou=Faculty and Staff, ou=People, o=University of Michigan, c=US'), (10, '\x00'), (10, '\x03'), (2, 0), (2, 0), (1, '\x00'), (135, 'objectclass'), (48, [(4, 'memberOfGroup'), (4, 'acl'), (4, 'cn'), (4, 'title'), (4, 'postalAddress'), (4, 'telephoneNumber'), (4, 'mail'), (4, 'member'), (4, 'homePhone'), (4, 'homePostalAddress'), (4, 'objectClass'), (4, 'description'), (4, 'facsimileTelephoneNumber'), (4, 'pager'), (4, 'uid'), (4, 'userPassword'), (4, 'joinable'), (4, 'associatedDomain'), (4, 'owner'), (4, 'rfc822ErrorsTo'), (4, 'ErrorsTo'), (4, 'rfc822RequestsTo'), (4, 'RequestsTo'), (4, 'moderator'), (4, 'labeledURL'), (4, 'onVacation'), (4, 'vacationMessage'), (4, 'drink'), (4, 'lastModifiedBy'), (4, 'lastModifiedTime'), (4, 'modifiersname'), (4, 'modifytimestamp'), (4, 'creatorsname'), (4, 'createtimestamp')])])])])
s = b'0\x82\x02Q\x02\x01\x0bc\x82\x02J\x04xcn=Douglas J Song 1, ou=Information Technology Division, ou=Faculty and Staff, ou=People, o=University of Michigan, c=US\n\x01\x00\n\x01\x03\x02\x01\x00\x02\x01\x00\x01\x01\x00\x87\x0bobjectclass0\x82\x01\xb0\x04\rmemberOfGroup\x04\x03acl\x04\x02cn\x04\x05title\x04\rpostalAddress\x04\x0ftelephoneNumber\x04\x04mail\x04\x06member\x04\thomePhone\x04\x11homePostalAddress\x04\x0bobjectClass\x04\x0bdescription\x04\x18facsimileTelephoneNumber\x04\x05pager\x04\x03uid\x04\x0cuserPassword\x04\x08joinable\x04\x10associatedDomain\x04\x05owner\x04\x0erfc822ErrorsTo\x04\x08ErrorsTo\x04\x10rfc822RequestsTo\x04\nRequestsTo\x04\tmoderator\x04\nlabeledURL\x04\nonVacation\x04\x0fvacationMessage\x04\x05drink\x04\x0elastModifiedBy\x04\x10lastModifiedTime\x04\rmodifiersname\x04\x0fmodifytimestamp\x04\x0ccreatorsname\x04\x0fcreatetimestamp'
assert (decode(s) == [(48, [(2, 11), (99, [(4, b'cn=Douglas J Song 1, ou=Information Technology Division, ou=Faculty and Staff, ou=People, o=University of Michigan, c=US'), (10, b'\x00'), (10, b'\x03'), (2, 0), (2, 0), (1, b'\x00'), (135, b'objectclass'), (48, [(4, b'memberOfGroup'), (4, b'acl'), (4, b'cn'), (4, b'title'), (4, b'postalAddress'), (4, b'telephoneNumber'), (4, b'mail'), (4, b'member'), (4, b'homePhone'), (4, b'homePostalAddress'), (4, b'objectClass'), (4, b'description'), (4, b'facsimileTelephoneNumber'), (4, b'pager'), (4, b'uid'), (4, b'userPassword'), (4, b'joinable'), (4, b'associatedDomain'), (4, b'owner'), (4, b'rfc822ErrorsTo'), (4, b'ErrorsTo'), (4, b'rfc822RequestsTo'), (4, b'RequestsTo'), (4, b'moderator'), (4, b'labeledURL'), (4, b'onVacation'), (4, b'vacationMessage'), (4, b'drink'), (4, b'lastModifiedBy'), (4, b'lastModifiedTime'), (4, b'modifiersname'), (4, b'modifytimestamp'), (4, b'creatorsname'), (4, b'createtimestamp')])])])])

if __name__ == '__main__':
test_asn1()
print 'Tests Successful...'
print('Tests Successful...')

0 comments on commit 433cd5d

Please sign in to comment.