Commit 48346dcf authored by Pedro Oliveira's avatar Pedro Oliveira

First try

parent dd3e14f8
import struct
class PacketPimHello:
PIM_TYPE = 0
PIM_HDR_OPTS = "! HH"
PIM_HDR_OPTS_LEN = struct.calcsize(PIM_HDR_OPTS)
PIM_MSG_TYPES_LENGTH = {1: 2,
20: 4,
}
def __init__(self):
self.options = {}
def add_option(self, option_type: int, option_value: int):
if option_value is None:
del self.options[option_type]
return
self.options[option_type] = option_value
def get_options(self):
return self.options
def bytes(self) -> bytes:
res = b''
for (option_type, option_value) in self.options.items():
option_length = self.PIM_MSG_TYPES_LENGTH[option_type]
type_length_hdr = struct.pack(self.PIM_HDR_OPTS, option_type, option_length)
res += type_length_hdr + struct.pack("! " + str(option_length) + "s", option_value.to_bytes(option_length, byteorder='big'))
return res
import struct
class PacketPimJoinPrune:
PIM_HDR_JOIN_PRUNE = "! 4S BBH "
PIM_HDR_JOIN_PRUNE_LEN = struct.calcsize(PIM_HDR_JOIN_PRUNE)
def __init__(self, upstream_neighbor_address, hold_time):
self.groups = []
self.upstream_neighbor_address = upstream_neighbor_address
self.hold_time = hold_time
def add_multicast_group(self, group):
# TODO verificar se grupo ja esta na msg
self.groups.append(group)
def bytes(self) -> bytes:
msg = struct.pack(self.PIM_HDR_JOIN_PRUNE, self.upstream_neighbor_address, 0, len(self.groups), self.hold_time)
for multicast_group in self.groups:
msg += multicast_group.bytes()
return msg
import struct
class PacketPimJoinPruneMulticastGroup:
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP = "! 4s HH"
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_LEN = struct.calcsize(PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP)
PIM_HDR_JOINED_PRUNED_SOURCE = "! 4s"
PIM_HDR_JOINED_PRUNED_SOURCE_LEN = struct.calcsize(PIM_HDR_JOINED_PRUNED_SOURCE)
def __init__(self, multicast_group, joined_src_addresses, pruned_src_addresses):
self.multicast_group = multicast_group
self.joined_src_addresses = joined_src_addresses
self.pruned_src_addresses = pruned_src_addresses
def bytes(self) -> bytes:
# TODO: verificar multicast_group
msg = struct.pack(self.PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP, self.multicast_group, len(self.joined_src_addresses), len(self.pruned_src_addresses))
for joined_src_address in self.joined_src_addresses:
msg = struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, joined_src_address)
for pruned_src_address in self.pruned_src_addresses:
msg = struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, pruned_src_address)
# TODO: verificar pruned e joined addrss
return msg
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