]>
git.ipfire.org Git - oddments/cappie.git/blob - cappie/protocol.py
2 ###############################################################################
5 # Copyright (C) 2010 Michael Tremer #
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. #
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. #
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/>. #
20 ###############################################################################
24 from constants
import *
28 return int("".join(["%02d" % ord(c
) for c
in val
]), 16)
31 return ".".join(["%d" % ord(i
) for i
in val
])
34 return ":".join(["%02x" % ord(i
) for i
in val
])
36 def decode_packet(data
):
37 for func
in (decode_arp_packet
,):
40 except PacketTypeError
:
45 raise PacketTypeError
, "Could not determine type of packet"
47 def decode_arp_packet(data
):
49 1 : OPERATION_REQUEST
,
50 2 : OPERATION_RESPONSE
,
53 #if not len(data) == 42:
54 # raise DecodeError, "Data has wrong length: %d" % len(data)
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])
67 if not protocol
== 0x0806:
68 raise PacketTypeError
, "Not an ARP packet"
70 # TODO Must check hwtype here...
73 ret
["operation"] = operationmap
[operation
]
75 raise DecodeError
, "Unknown operation type"
77 address_length
= hw_addr_size
+ hw_prot_size
78 unpack_str
= "!%ss%ss" % (hw_addr_size
, hw_prot_size
)
80 ret
["source_address"], ret
["source_ip_address"] = \
81 struct
.unpack(unpack_str
, data
[22:22 + address_length
])
83 ret
["destination_address"], ret
["destination_ip_address"] = \
84 struct
.unpack(unpack_str
, data
[22 + address_length
:22 + address_length
* 2])
86 for i
in ("source_address", "destination_address"):
87 ret
[i
] = val2mac(ret
[i
])
89 for i
in ("source_ip_address", "destination_ip_address"):
90 ret
[i
] = val2ip4(ret
[i
])
94 def decode_ndp_packet(data
):