]>
Commit | Line | Data |
---|---|---|
53478050 MT |
1 | #!/usr/bin/python |
2 | ############################################################################### | |
3 | # # | |
4 | # Cappie # | |
5 | # Copyright (C) 2010 Michael Tremer # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | import struct | |
23 | ||
24 | from constants import * | |
25 | from errors import * | |
26 | ||
27 | def val2int(val): | |
28 | return int("".join(["%02d" % ord(c) for c in val]), 16) | |
29 | ||
30 | def val2ip4(val): | |
31 | return ".".join(["%d" % ord(i) for i in val]) | |
32 | ||
33 | def val2mac(val): | |
34 | return ":".join(["%02x" % ord(i) for i in val]) | |
35 | ||
36 | def decode_packet(data): | |
37 | for func in (decode_arp_packet,): | |
38 | try: | |
39 | p = func(data) | |
40 | except PacketTypeError: | |
41 | continue | |
42 | ||
43 | return p | |
44 | ||
45 | raise PacketTypeError, "Could not determine type of packet" | |
46 | ||
47 | def decode_arp_packet(data): | |
48 | operationmap = { | |
49 | 1 : OPERATION_REQUEST, | |
50 | 2 : OPERATION_RESPONSE, | |
51 | } | |
52 | ||
53 | #if not len(data) == 42: | |
54 | # raise DecodeError, "Data has wrong length: %d" % len(data) | |
55 | ||
56 | ret = { | |
57 | "type" : TYPE_ARP, | |
58 | } | |
59 | ||
60 | #"hwtype" : data[:2], | |
61 | protocol = val2int(struct.unpack("!2s", data[12:14])[0]) | |
62 | hw_addr_size = val2int(struct.unpack("!1s", data[18:19])[0]) | |
63 | hw_prot_size = val2int(struct.unpack("!1s", data[19:20])[0]) | |
64 | operation = val2int(struct.unpack("!2s", data[20:22])[0]) | |
65 | ||
66 | # Sanity checks | |
67 | if not protocol == 0x0806: | |
68 | raise PacketTypeError, "Not an ARP packet" | |
69 | ||
70 | # TODO Must check hwtype here... | |
71 | ||
72 | try: | |
73 | ret["operation"] = operationmap[operation] | |
74 | except KeyError: | |
75 | raise DecodeError, "Unknown operation type" | |
76 | ||
77 | address_length = hw_addr_size + hw_prot_size | |
78 | unpack_str = "!%ss%ss" % (hw_addr_size, hw_prot_size) | |
79 | ||
80 | ret["source_address"], ret["source_ip_address"] = \ | |
81 | struct.unpack(unpack_str, data[22:22 + address_length]) | |
82 | ||
83 | ret["destination_address"], ret["destination_ip_address"] = \ | |
84 | struct.unpack(unpack_str, data[22 + address_length:22 + address_length * 2]) | |
85 | ||
86 | for i in ("source_address", "destination_address"): | |
87 | ret[i] = val2mac(ret[i]) | |
88 | ||
89 | for i in ("source_ip_address", "destination_ip_address"): | |
90 | ret[i] = val2ip4(ret[i]) | |
91 | ||
92 | return ret | |
93 | ||
94 | def decode_ndp_packet(data): | |
95 | raise PacketTypeError |