]>
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 | ||
86eda3b6 MT |
24 | import database |
25 | ||
53478050 MT |
26 | from constants import * |
27 | from errors import * | |
28 | ||
29 | def val2int(val): | |
30 | return int("".join(["%02d" % ord(c) for c in val]), 16) | |
31 | ||
32 | def val2ip4(val): | |
33 | return ".".join(["%d" % ord(i) for i in val]) | |
34 | ||
35 | def val2mac(val): | |
36 | return ":".join(["%02x" % ord(i) for i in val]) | |
37 | ||
38 | def decode_packet(data): | |
86eda3b6 MT |
39 | try: |
40 | protocol = val2int(struct.unpack("!2s", data[12:14])[0]) | |
41 | except: | |
42 | raise DecodeError | |
53478050 | 43 | |
86eda3b6 MT |
44 | try: |
45 | d = protocol2function[protocol](data) | |
46 | except KeyErrror: | |
47 | raise PacketTypeError, "Could not determine type of packet" | |
53478050 | 48 | |
86eda3b6 | 49 | return database.Row(d) |
53478050 MT |
50 | |
51 | def decode_arp_packet(data): | |
52 | operationmap = { | |
53 | 1 : OPERATION_REQUEST, | |
54 | 2 : OPERATION_RESPONSE, | |
55 | } | |
56 | ||
57 | #if not len(data) == 42: | |
58 | # raise DecodeError, "Data has wrong length: %d" % len(data) | |
59 | ||
60 | ret = { | |
61 | "type" : TYPE_ARP, | |
62 | } | |
63 | ||
64 | #"hwtype" : data[:2], | |
53478050 MT |
65 | hw_addr_size = val2int(struct.unpack("!1s", data[18:19])[0]) |
66 | hw_prot_size = val2int(struct.unpack("!1s", data[19:20])[0]) | |
67 | operation = val2int(struct.unpack("!2s", data[20:22])[0]) | |
68 | ||
53478050 MT |
69 | # TODO Must check hwtype here... |
70 | ||
71 | try: | |
72 | ret["operation"] = operationmap[operation] | |
73 | except KeyError: | |
74 | raise DecodeError, "Unknown operation type" | |
75 | ||
76 | address_length = hw_addr_size + hw_prot_size | |
77 | unpack_str = "!%ss%ss" % (hw_addr_size, hw_prot_size) | |
78 | ||
79 | ret["source_address"], ret["source_ip_address"] = \ | |
80 | struct.unpack(unpack_str, data[22:22 + address_length]) | |
81 | ||
82 | ret["destination_address"], ret["destination_ip_address"] = \ | |
83 | struct.unpack(unpack_str, data[22 + address_length:22 + address_length * 2]) | |
84 | ||
85 | for i in ("source_address", "destination_address"): | |
86 | ret[i] = val2mac(ret[i]) | |
87 | ||
88 | for i in ("source_ip_address", "destination_ip_address"): | |
89 | ret[i] = val2ip4(ret[i]) | |
90 | ||
91 | return ret | |
92 | ||
93 | def decode_ndp_packet(data): | |
94 | raise PacketTypeError | |
86eda3b6 MT |
95 | |
96 | protocol2function = { | |
97 | ETHERTYPE_ARP : decode_arp_packet, | |
98 | } |