from ConfigParser import ConfigParser
from threading import Thread
+TYPE_ARP = 0
+
+OPERATION_REQUEST = 0
+OPERATION_RESPONSE = 1
+
def getAllInterfaces():
filters = ("lo", "any")
ret = []
def decode_arp_packet(data):
operationmap = {
- 1 : "in",
- 2 : "out",
+ 1 : OPERATION_REQUEST,
+ 2 : OPERATION_RESPONSE,
}
if not len(data) == 42:
raise DecodeError, "Data has wrong length"
ret = {
- #"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]),
+ "type" : TYPE_ARP,
}
+ #"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 ret["protocol"] == 0x0806:
+ if not protocol == 0x0806:
raise PacketTypeError, "Not an ARP packet"
# TODO Must check hwtype here...
try:
- ret["operation"] = operationmap[ret["operation"]]
+ ret["operation"] = operationmap[operation]
except KeyError:
raise DecodeError, "Unknown operation type"
- address_length = ret["hw_addr_size"] + ret["hw_prot_size"]
- unpack_str = "!%ss%ss" % (ret["hw_addr_size"], ret["hw_prot_size"])
+ address_length = hw_addr_size + hw_prot_size
+ unpack_str = "!%ss%ss" % (hw_addr_size, hw_prot_size)
ret["source_address"], ret["source_ip_address"] = \
struct.unpack(unpack_str, data[22:22 + address_length])