Commit 179c9207 authored by Pedro Oliveira's avatar Pedro Oliveira

encoding addresses

parent 05a69151
...@@ -60,8 +60,6 @@ def list_neighbors(): ...@@ -60,8 +60,6 @@ def list_neighbors():
check_time = time.time() check_time = time.time()
t = PrettyTable(['Neighbor IP', 'KeepAlive', "Generation ID", "Uptime"]) t = PrettyTable(['Neighbor IP', 'KeepAlive', "Generation ID", "Uptime"])
for ip, neighbor in list(neighbors.items()): for ip, neighbor in list(neighbors.items()):
import socket, struct # TODO atualmente conversao manual de numero para string ip
ip = socket.inet_ntoa(struct.pack('!L', ip))
uptime = check_time - neighbor.time_of_last_update uptime = check_time - neighbor.time_of_last_update
uptime = 0 if (uptime < 0) else uptime uptime = 0 if (uptime < 0) else uptime
...@@ -78,8 +76,8 @@ def list_enabled_interfaces(): ...@@ -78,8 +76,8 @@ def list_enabled_interfaces():
from Packet.PacketPimJoinPrune import PacketPimJoinPrune from Packet.PacketPimJoinPrune import PacketPimJoinPrune
from Packet.PacketPimJoinPruneMulticastGroup import PacketPimJoinPruneMulticastGroup from Packet.PacketPimJoinPruneMulticastGroup import PacketPimJoinPruneMulticastGroup
ph = PacketPimJoinPrune(167772173, 20) ph = PacketPimJoinPrune("10.0.0.13", 210)
ph.add_multicast_group(PacketPimJoinPruneMulticastGroup(3708422657, [3708422657], [])) ph.add_multicast_group(PacketPimJoinPruneMulticastGroup("239.123.123.123", ["1.1.1.1"], []))
pckt = Packet(pim_header=PacketPimHeader(ph)) pckt = Packet(pim_header=PacketPimHeader(ph))
interfaces[interface].send(pckt.bytes()) interfaces[interface].send(pckt.bytes())
......
...@@ -2,7 +2,7 @@ import struct ...@@ -2,7 +2,7 @@ import struct
class PacketIpHeader: class PacketIpHeader:
IP_HDR = "! BBH HH BBH LL" IP_HDR = "! BBH HH BBH 4s 4s"
#IP_HDR2 = "! B" #IP_HDR2 = "! B"
IP_HDR_LEN = struct.calcsize(IP_HDR) IP_HDR_LEN = struct.calcsize(IP_HDR)
......
import ipaddress
import struct
import socket
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Addr Family | Encoding Type |B| Reserved |Z| Mask Len |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Group Multicast Address
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
'''
class PacketPimEncodedGroupAddress:
PIM_ENCODED_GROUP_ADDRESS_HDR = "! BBBB 4s"
PIM_ENCODED_GROUP_ADDRESS_HDR_LEN = struct.calcsize(PIM_ENCODED_GROUP_ADDRESS_HDR)
FAMILY_RESERVED = 0
FAMILY_IPV4 = 1
FAMILY_IPV6 = 2
RESERVED = 0
def __init__(self, group_address, mask_len=32):
if type(group_address) not in (str, bytes):
raise Exception
if type(group_address) is bytes:
group_address = socket.inet_ntoa(group_address)
self.group_address = group_address
self.mask_len = mask_len
def bytes(self) -> bytes:
addr_family = self.get_addr_family(self.group_address)
ip = socket.inet_aton(self.group_address)
msg = struct.pack(PacketPimEncodedGroupAddress.PIM_ENCODED_GROUP_ADDRESS_HDR, addr_family, 0,
PacketPimEncodedGroupAddress.RESERVED, self.mask_len, ip)
return msg
def get_addr_family(self, ip):
version = ipaddress.ip_address(ip).version
if version == 4:
return PacketPimEncodedGroupAddress.FAMILY_IPV4
elif version == 6:
return PacketPimEncodedGroupAddress.FAMILY_IPV6
else:
raise Exception
import ipaddress
import struct
import socket
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Addr Family | Encoding Type | Rsrvd |S|W|R| Mask Len |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
'''
class PacketPimEncodedSourceAddress:
PIM_ENCODED_SOURCE_ADDRESS_HDR = "! BBBB 4s"
PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN = struct.calcsize(PIM_ENCODED_SOURCE_ADDRESS_HDR)
FAMILY_RESERVED = 0
FAMILY_IPV4 = 1
FAMILY_IPV6 = 2
RESERVED_AND_SWR_BITS = 0
def __init__(self, source_address, mask_len=32):
if type(source_address) not in (str, bytes):
raise Exception
if type(source_address) is bytes:
source_address = socket.inet_ntoa(source_address)
self.source_address = source_address
self.mask_len = mask_len
def bytes(self) -> bytes:
addr_family = self.get_addr_family(self.source_address)
ip = socket.inet_aton(self.source_address)
msg = struct.pack(PacketPimEncodedSourceAddress.PIM_ENCODED_SOURCE_ADDRESS_HDR, addr_family, 0,
PacketPimEncodedSourceAddress.RESERVED_AND_SWR_BITS, self.mask_len, ip)
return msg
def get_addr_family(self, ip):
version = ipaddress.ip_address(ip).version
if version == 4:
return PacketPimEncodedSourceAddress.FAMILY_IPV4
elif version == 6:
return PacketPimEncodedSourceAddress.FAMILY_IPV6
else:
raise Exception
import ipaddress
import struct
import socket
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Addr Family | Encoding Type | Unicast Address
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
'''
class PacketPimEncodedUnicastAddress:
PIM_ENCODED_UNICAST_ADDRESS_HDR = "! BB 4s"
PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN = struct.calcsize(PIM_ENCODED_UNICAST_ADDRESS_HDR)
FAMILY_RESERVED = 0
FAMILY_IPV4 = 1
FAMILY_IPV6 = 2
def __init__(self, unicast_address):
if type(unicast_address) not in (str, bytes):
raise Exception
if type(unicast_address) is bytes:
unicast_address = socket.inet_ntoa(unicast_address)
self.unicast_address = unicast_address
def bytes(self) -> bytes:
addr_family = self.get_addr_family(self.unicast_address)
ip = socket.inet_aton(self.unicast_address)
msg = struct.pack(PacketPimEncodedUnicastAddress.PIM_ENCODED_UNICAST_ADDRESS_HDR, addr_family, 0, ip)
return msg
def get_addr_family(self, ip):
version = ipaddress.ip_address(ip).version
if version == 4:
return PacketPimEncodedUnicastAddress.FAMILY_IPV4
elif version == 6:
return PacketPimEncodedUnicastAddress.FAMILY_IPV6
else:
raise Exception
import struct import struct
from utils import checksum from utils import checksum
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|PIM Ver| Type | Reserved | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class PacketPimHeader: class PacketPimHeader:
PIM_VERSION = 2 PIM_VERSION = 2
......
import struct import struct
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Type | Option Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Value |
| ... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| . |
| . |
| . |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Type | Option Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Value |
| ... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class PacketPimHello: class PacketPimHello:
PIM_TYPE = 0 PIM_TYPE = 0
PIM_HDR_OPTS = "! HH" PIM_HDR_OPTS = "! HH"
......
import struct import struct
import socket
from Packet.PacketPimEncodedUnicastAddress import PacketPimEncodedUnicastAddress
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Upstream Neighbor Address (Encoded Unicast Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reserved | Num Groups | Hold Time |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class PacketPimJoinPrune: class PacketPimJoinPrune:
PIM_TYPE = 3 PIM_TYPE = 3
PIM_HDR_JOIN_PRUNE = "! 4s BBH " PIM_HDR_JOIN_PRUNE = "! " + str(PacketPimEncodedUnicastAddress.PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN) + "s BBH"
PIM_HDR_JOIN_PRUNE_LEN = struct.calcsize(PIM_HDR_JOIN_PRUNE) PIM_HDR_JOIN_PRUNE_LEN = struct.calcsize(PIM_HDR_JOIN_PRUNE)
def __init__(self, upstream_neighbor_address, hold_time): def __init__(self, upstream_neighbor_address, hold_time):
if type(upstream_neighbor_address) not in (str, bytes):
raise Exception
if type(upstream_neighbor_address) is bytes:
upstream_neighbor_address = socket.inet_ntoa(upstream_neighbor_address)
self.groups = [] self.groups = []
self.upstream_neighbor_address = upstream_neighbor_address self.upstream_neighbor_address = upstream_neighbor_address
self.hold_time = hold_time self.hold_time = hold_time
...@@ -16,8 +29,8 @@ class PacketPimJoinPrune: ...@@ -16,8 +29,8 @@ class PacketPimJoinPrune:
self.groups.append(group) self.groups.append(group)
def bytes(self) -> bytes: def bytes(self) -> bytes:
# TODO esta a converter upstream neighbor (considera que recebe int) para bytes upstream_neighbor_address = PacketPimEncodedUnicastAddress(self.upstream_neighbor_address).bytes()
msg = struct.pack(PacketPimJoinPrune.PIM_HDR_JOIN_PRUNE, self.upstream_neighbor_address.to_bytes(4, byteorder='big'), 0, len(self.groups), self.hold_time) msg = struct.pack(PacketPimJoinPrune.PIM_HDR_JOIN_PRUNE, upstream_neighbor_address, 0, len(self.groups), self.hold_time)
for multicast_group in self.groups: for multicast_group in self.groups:
msg += multicast_group.bytes() msg += multicast_group.bytes()
......
import struct import struct
import socket
from Packet.PacketPimEncodedGroupAddress import PacketPimEncodedGroupAddress
from Packet.PacketPimEncodedSourceAddress import PacketPimEncodedSourceAddress
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Multicast Group Address 1 (Encoded Group Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Number of Joined Sources | Number of Pruned Sources |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Joined Source Address 1 (Encoded Source Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| . |
| . |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Joined Source Address n (Encoded Source Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Pruned Source Address 1 (Encoded Source Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| . |
| . |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Pruned Source Address n (Encoded Source Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class PacketPimJoinPruneMulticastGroup: class PacketPimJoinPruneMulticastGroup:
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP = "! 4s HH" PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP = "! " + str(PacketPimEncodedGroupAddress.PIM_ENCODED_GROUP_ADDRESS_HDR_LEN) + "s HH"
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_LEN = struct.calcsize(PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP) 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 = "! " + str(PacketPimEncodedSourceAddress.PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN) + "s"
PIM_HDR_JOINED_PRUNED_SOURCE_LEN = struct.calcsize(PIM_HDR_JOINED_PRUNED_SOURCE) PIM_HDR_JOINED_PRUNED_SOURCE_LEN = struct.calcsize(PIM_HDR_JOINED_PRUNED_SOURCE)
def __init__(self, multicast_group, joined_src_addresses, pruned_src_addresses): def __init__(self, multicast_group, joined_src_addresses : list, pruned_src_addresses : list):
if type(multicast_group) not in (str, bytes):
raise Exception
elif type(multicast_group) is bytes:
self.multicast_group = socket.inet_ntoa(self.multicast_group)
if type(joined_src_addresses) is not list:
raise Exception
if type(pruned_src_addresses) is not list:
raise Exception
self.multicast_group = multicast_group self.multicast_group = multicast_group
self.joined_src_addresses = joined_src_addresses self.joined_src_addresses = joined_src_addresses
self.pruned_src_addresses = pruned_src_addresses self.pruned_src_addresses = pruned_src_addresses
def bytes(self) -> bytes: def bytes(self) -> bytes:
# TODO: verificar multicast_group multicast_group_address = PacketPimEncodedGroupAddress(self.multicast_group).bytes()
msg = struct.pack(self.PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP, self.multicast_group.to_bytes(4, byteorder='big'), len(self.joined_src_addresses), len(self.pruned_src_addresses)) msg = struct.pack(self.PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP, multicast_group_address, len(self.joined_src_addresses), len(self.pruned_src_addresses))
for joined_src_address in self.joined_src_addresses: for joined_src_address in self.joined_src_addresses:
msg += struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, joined_src_address.to_bytes(4, byteorder='big')) joined_src_address_bytes = PacketPimEncodedSourceAddress(joined_src_address).bytes()
msg += struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, joined_src_address_bytes)
for pruned_src_address in self.pruned_src_addresses: for pruned_src_address in self.pruned_src_addresses:
msg += struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, pruned_src_address.to_bytes(4, byteorder='big')) pruned_src_address_bytes = PacketPimEncodedSourceAddress(pruned_src_address).bytes()
# TODO: verificar pruned e joined addrss msg += struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, pruned_src_address_bytes)
return msg return msg
...@@ -5,7 +5,7 @@ from Packet.PacketPimHeader import PacketPimHeader ...@@ -5,7 +5,7 @@ from Packet.PacketPimHeader import PacketPimHeader
from Packet.PacketPimHello import PacketPimHello from Packet.PacketPimHello import PacketPimHello
from Packet.PacketPimJoinPrune import PacketPimJoinPrune from Packet.PacketPimJoinPrune import PacketPimJoinPrune
from Packet.PacketPimJoinPruneMulticastGroup import PacketPimJoinPruneMulticastGroup from Packet.PacketPimJoinPruneMulticastGroup import PacketPimJoinPruneMulticastGroup
import socket
from utils import checksum from utils import checksum
...@@ -55,8 +55,8 @@ class ReceivedPacket(Packet): ...@@ -55,8 +55,8 @@ class ReceivedPacket(Packet):
"TTL": ttl, "TTL": ttl,
"PROTO": proto, "PROTO": proto,
"CKSUM": cksum, "CKSUM": cksum,
"SRC": src, "SRC": socket.inet_ntoa(src),
"DST": dst "DST": socket.inet_ntoa(dst)
} }
def parsePimHdr(msg): def parsePimHdr(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