]>
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 ###############################################################################
26 from constants
import *
30 return int("".join(["%02d" % ord(c
) for c
in val
]), 16)
33 return ".".join(["%d" % ord(i
) for i
in val
])
36 return ":".join(["%02x" % ord(i
) for i
in val
])
38 def decode_packet(data
):
40 protocol
= val2int(struct
.unpack("!2s", data
[12:14])[0])
45 d
= protocol2function
[protocol
](data
)
47 raise PacketTypeError
, "Could not determine type of packet"
49 return database
.Row(d
)
51 def decode_arp_packet(data
):
53 1 : OPERATION_REQUEST
,
54 2 : OPERATION_RESPONSE
,
57 #if not len(data) == 42:
58 # raise DecodeError, "Data has wrong length: %d" % len(data)
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])
69 # TODO Must check hwtype here...
72 ret
["operation"] = operationmap
[operation
]
74 raise DecodeError
, "Unknown operation type"
76 address_length
= hw_addr_size
+ hw_prot_size
77 unpack_str
= "!%ss%ss" % (hw_addr_size
, hw_prot_size
)
79 ret
["source_address"], ret
["source_ip_address"] = \
80 struct
.unpack(unpack_str
, data
[22:22 + address_length
])
82 ret
["destination_address"], ret
["destination_ip_address"] = \
83 struct
.unpack(unpack_str
, data
[22 + address_length
:22 + address_length
* 2])
85 for i
in ("source_address", "destination_address"):
86 ret
[i
] = val2mac(ret
[i
])
88 for i
in ("source_ip_address", "destination_ip_address"):
89 ret
[i
] = val2ip4(ret
[i
])
93 def decode_ndp_packet(data
):
97 ETHERTYPE_ARP
: decode_arp_packet
,