Commit 6cbbc7ef authored by Pedro Oliveira's avatar Pedro Oliveira

minor improvements - len(pkt)

parent f3d3c0ef
......@@ -41,7 +41,6 @@ class Hello:
hello_timer = random.uniform(0, Hello.TRIGGERED_HELLO_DELAY)
Timer(hello_timer, self.packet_send_handle, args=[interface]).start()
# TODO: ver melhor este metodo
def force_send_remove(self, interface: Interface):
pim_payload = PacketPimHello()
pim_payload.add_option(1, HELLO_HOLD_TIME_TIMEOUT)
......@@ -52,9 +51,6 @@ class Hello:
# receive handler
def receive_handle(self, packet: ReceivedPacket):
if packet.ip_header is None:
return # TODO: MAYBE EXCEPCAO??
ip = packet.ip_header.ip_src
print("ip = ", ip)
options = packet.pim_header.payload.get_options()
......
......@@ -32,6 +32,9 @@ class PacketIpHeader:
self.ip_src = ip_src
self.ip_dst = ip_dst
def __len__(self):
return self.hdr_length
@staticmethod
def parse_bytes(data: bytes):
(verhlen, tos, iplen, ipid, frag, ttl, proto, cksum, src, dst) = \
......
......@@ -57,6 +57,15 @@ class PacketPimEncodedGroupAddress:
else:
raise Exception
def __len__(self):
version = ipaddress.ip_address(self.group_address).version
if version == 4:
return self.PIM_ENCODED_GROUP_ADDRESS_HDR_LEN
elif version == 6:
return self.PIM_ENCODED_GROUP_ADDRESS_HDR_LEN_IPv6
else:
raise Exception
@staticmethod
def parse_bytes(data: bytes):
data_without_group_addr = data[0:PacketPimEncodedGroupAddress.PIM_ENCODED_GROUP_ADDRESS_HDR_WITHOUT_GROUP_ADDRESS_LEN]
......
......@@ -59,6 +59,15 @@ class PacketPimEncodedSourceAddress:
else:
raise Exception
def __len__(self):
version = ipaddress.ip_address(self.source_address).version
if version == 4:
return self.PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN
elif version == 6:
return self.PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN_IPV6
else:
raise Exception
@staticmethod
def parse_bytes(data: bytes):
data_without_source_addr = data[0:PacketPimEncodedSourceAddress.PIM_ENCODED_SOURCE_ADDRESS_HDR_WITHOUT_SOURCE_ADDRESS_LEN]
......
......@@ -48,6 +48,15 @@ class PacketPimEncodedUnicastAddress:
else:
raise Exception
def __len__(self):
version = ipaddress.ip_address(self.unicast_address).version
if version == 4:
return self.PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN
elif version == 6:
return self.PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN_IPV6
else:
raise Exception
@staticmethod
def parse_bytes(data: bytes):
data_without_unicast_addr = data[0:PacketPimEncodedUnicastAddress.PIM_ENCODED_UNICAST_ADDRESS_HDR_WITHOUT_UNICAST_ADDRESS_LEN]
......
......@@ -33,9 +33,11 @@ class PacketPimHeader:
msg = msg_without_chcksum[0:2] + struct.pack("! H", pim_checksum) + msg_without_chcksum[4:]
return msg
def __len__(self):
return len(self.bytes())
@staticmethod
def parse_bytes(data: bytes):
# print("parsePimHdr: ", msg.encode("hex"))
print("parsePimHdr: ", data)
pim_hdr = data[0:PacketPimHeader.PIM_HDR_LEN]
......
......@@ -48,6 +48,9 @@ class PacketPimHello:
res += type_length_hdr + struct.pack("! " + str(option_length) + "s", option_value.to_bytes(option_length, byteorder='big'))
return res
def __len__(self):
return len(self.bytes())
@staticmethod
def parse_bytes(data: bytes):
pim_payload = PacketPimHello()
......
......@@ -49,7 +49,7 @@ class PacketPimJoinPrune:
@staticmethod
def parse_bytes(data: bytes):
upstream_neighbor_addr_obj = PacketPimEncodedUnicastAddress.parse_bytes(data)
upstream_neighbor_addr_len = len(upstream_neighbor_addr_obj.bytes())
upstream_neighbor_addr_len = len(upstream_neighbor_addr_obj)
data = data[upstream_neighbor_addr_len:]
(_, num_groups, hold_time) = struct.unpack(PacketPimJoinPrune.PIM_HDR_JOIN_PRUNE_WITHOUT_ADDRESS, data[:PacketPimJoinPrune.PIM_HDR_JOIN_PRUNE_WITHOUT_ADDRESS_LEN])
......@@ -58,7 +58,7 @@ class PacketPimJoinPrune:
for i in range(0, num_groups):
group = PacketPimJoinPruneMulticastGroup.parse_bytes(data)
group_len = len(group.bytes())
group_len = len(group)
pim_payload.add_multicast_group(group)
data = data[group_len:]
......
......@@ -72,10 +72,13 @@ class PacketPimJoinPruneMulticastGroup:
msg += pruned_src_address_bytes
return msg
def __len__(self):
return len(self.bytes())
@staticmethod
def parse_bytes(data: bytes):
multicast_group_addr_obj = PacketPimEncodedGroupAddress.parse_bytes(data)
multicast_group_addr_len = len(multicast_group_addr_obj.bytes())
multicast_group_addr_len = len(multicast_group_addr_obj)
data = data[multicast_group_addr_len:]
number_join_prune_data = data[:PacketPimJoinPruneMulticastGroup.PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_WITHOUT_GROUP_ADDRESS_LEN]
......@@ -86,13 +89,13 @@ class PacketPimJoinPruneMulticastGroup:
data = data[PacketPimJoinPruneMulticastGroup.PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_WITHOUT_GROUP_ADDRESS_LEN:]
for i in range(0, number_joined_sources):
joined_obj = PacketPimEncodedSourceAddress.parse_bytes(data)
joined_obj_len = len(joined_obj.bytes())
joined_obj_len = len(joined_obj)
data = data[joined_obj_len:]
joined.append(joined_obj.source_address)
for i in range(0, number_pruned_sources):
pruned_obj = PacketPimEncodedSourceAddress.parse_bytes(data)
pruned_obj_len = len(pruned_obj.bytes())
pruned_obj_len = len(pruned_obj)
data = data[pruned_obj_len:]
pruned.append(pruned_obj.source_address)
......
......@@ -15,7 +15,9 @@ class ReceivedPacket(Packet):
# Parse ao packet e preencher objeto Packet
packet_ip_hdr = raw_packet[:PacketIpHeader.IP_HDR_LEN]
self.ip_header = PacketIpHeader.parse_bytes(packet_ip_hdr)
ip_header = PacketIpHeader.parse_bytes(packet_ip_hdr)
packet_without_ip_hdr = raw_packet[self.ip_header.hdr_length:]
self.pim_header = PacketPimHeader.parse_bytes(packet_without_ip_hdr)
packet_without_ip_hdr = raw_packet[ip_header.hdr_length:]
pim_header = PacketPimHeader.parse_bytes(packet_without_ip_hdr)
super().__init__(ip_header=ip_header, pim_header=pim_header)
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment