From 86eda3b65c6b8bd287764e6e04ffcdf01a1d8ee0 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 29 Apr 2010 19:30:42 +0200 Subject: [PATCH] Change packet decoding by reading out ethernet type. --- cappie/constants.py | 2 ++ cappie/protocol.py | 27 +++++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/cappie/constants.py b/cappie/constants.py index 50fb016..de00993 100644 --- a/cappie/constants.py +++ b/cappie/constants.py @@ -19,6 +19,8 @@ # # ############################################################################### +ETHERTYPE_ARP = 0x0806 + TYPE_ARP = 0 OPERATION_REQUEST = 0 diff --git a/cappie/protocol.py b/cappie/protocol.py index ed5f4a3..c4252de 100644 --- a/cappie/protocol.py +++ b/cappie/protocol.py @@ -21,6 +21,8 @@ import struct +import database + from constants import * from errors import * @@ -34,15 +36,17 @@ def val2mac(val): return ":".join(["%02x" % ord(i) for i in val]) def decode_packet(data): - for func in (decode_arp_packet,): - try: - p = func(data) - except PacketTypeError: - continue + try: + protocol = val2int(struct.unpack("!2s", data[12:14])[0]) + except: + raise DecodeError - return p + try: + d = protocol2function[protocol](data) + except KeyErrror: + raise PacketTypeError, "Could not determine type of packet" - raise PacketTypeError, "Could not determine type of packet" + return database.Row(d) def decode_arp_packet(data): operationmap = { @@ -58,15 +62,10 @@ def decode_arp_packet(data): } #"hwtype" : data[:2], - protocol = val2int(struct.unpack("!2s", data[12:14])[0]) hw_addr_size = val2int(struct.unpack("!1s", data[18:19])[0]) hw_prot_size = val2int(struct.unpack("!1s", data[19:20])[0]) operation = val2int(struct.unpack("!2s", data[20:22])[0]) - # Sanity checks - if not protocol == 0x0806: - raise PacketTypeError, "Not an ARP packet" - # TODO Must check hwtype here... try: @@ -93,3 +92,7 @@ def decode_arp_packet(data): def decode_ndp_packet(data): raise PacketTypeError + +protocol2function = { + ETHERTYPE_ARP : decode_arp_packet, +} -- 2.39.2