Commit ee35f93e authored by Pedro Oliveira's avatar Pedro Oliveira

fix ipv6 support in address encoding

parent 506bb655
......@@ -17,9 +17,8 @@ def add_interface(interface_name):
interfaces[interface_name] = interface
protocols[0].force_send(interface)
# TODO: verificar melhor este metodo:
def remove_interface(interface_name):
# TODO remover neighbors desta interface
global interfaces
global neighbors
if (interface_name in interfaces) or interface_name == "*":
......@@ -34,12 +33,10 @@ def remove_interface(interface_name):
print("removido interface")
for (ip_neighbor, neighbor) in list(neighbors.items()):
# TODO ver melhor este algoritmo
if neighbor.contact_interface not in interfaces or interface_name == "*":
if neighbor.contact_interface not in interfaces:
neighbor.remove()
def add_neighbor(contact_interface, ip, random_number, hello_hold_time):
global neighbors
if ip not in neighbors:
......@@ -77,7 +74,7 @@ def list_neighbors():
def list_enabled_interfaces():
global interfaces
# TESTE DE PIM JOIN/PRUNE
for interface in interfaces:
from Packet.Packet import Packet
from Packet.PacketPimHeader import PacketPimHeader
......@@ -85,7 +82,14 @@ def list_enabled_interfaces():
from Packet.PacketPimJoinPruneMulticastGroup import PacketPimJoinPruneMulticastGroup
ph = PacketPimJoinPrune("10.0.0.13", 210)
ph.add_multicast_group(PacketPimJoinPruneMulticastGroup("239.123.123.123", ["1.1.1.1"], []))
ph.add_multicast_group(PacketPimJoinPruneMulticastGroup("239.123.123.123", ["1.1.1.1", "10.1.1.1"], []))
ph.add_multicast_group(PacketPimJoinPruneMulticastGroup("239.123.123.124", ["1.1.1.2", "10.1.1.2"], []))
pckt = Packet(pim_header=PacketPimHeader(ph))
interfaces[interface].send(pckt.bytes())
ph = PacketPimJoinPrune("ff08::1", 210)
ph.add_multicast_group(PacketPimJoinPruneMulticastGroup("2001:1:a:b:c::1", ["1.1.1.1", "2001:1:a:b:c::2"], []))
ph.add_multicast_group(PacketPimJoinPruneMulticastGroup("239.123.123.123", ["1.1.1.1"], ["2001:1:a:b:c::3"]))
pckt = Packet(pim_header=PacketPimHeader(ph))
interfaces[interface].send(pckt.bytes())
......
......@@ -11,8 +11,15 @@ import socket
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
'''
class PacketPimEncodedGroupAddress:
PIM_ENCODED_GROUP_ADDRESS_HDR = "! BBBB 4s"
PIM_ENCODED_GROUP_ADDRESS_HDR_LEN = struct.calcsize(PIM_ENCODED_GROUP_ADDRESS_HDR)
PIM_ENCODED_GROUP_ADDRESS_HDR = "! BBBB %s"
PIM_ENCODED_GROUP_ADDRESS_HDR_WITHOUT_GROUP_MULTICAST_ADDRESS = "! BBBB"
IPV4_HDR = "4s"
IPV6_HDR = "16s"
# TODO ver melhor versao ip
PIM_ENCODED_GROUP_ADDRESS_HDR_LEN = struct.calcsize(PIM_ENCODED_GROUP_ADDRESS_HDR % IPV4_HDR)
PIM_ENCODED_GROUP_ADDRESS_HDR_LEN_IPv6 = struct.calcsize(PIM_ENCODED_GROUP_ADDRESS_HDR % IPV6_HDR)
FAMILY_RESERVED = 0
FAMILY_IPV4 = 1
......@@ -20,7 +27,7 @@ class PacketPimEncodedGroupAddress:
RESERVED = 0
def __init__(self, group_address, mask_len=32):
def __init__(self, group_address, mask_len=None):
if type(group_address) not in (str, bytes):
raise Exception
if type(group_address) is bytes:
......@@ -29,18 +36,21 @@ class PacketPimEncodedGroupAddress:
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)
(string_ip_hdr, hdr_addr_family, socket_family) = PacketPimEncodedGroupAddress.get_ip_info(self.group_address)
if self.mask_len is None:
mask_len = 8 * struct.calcsize(string_ip_hdr)
ip = socket.inet_pton(socket_family, self.group_address)
msg = struct.pack(PacketPimEncodedGroupAddress.PIM_ENCODED_GROUP_ADDRESS_HDR % string_ip_hdr, hdr_addr_family, 0,
PacketPimEncodedGroupAddress.RESERVED, mask_len, ip)
return msg
def get_addr_family(self, ip):
@staticmethod
def get_ip_info(ip):
version = ipaddress.ip_address(ip).version
if version == 4:
return PacketPimEncodedGroupAddress.FAMILY_IPV4
return (PacketPimEncodedGroupAddress.IPV4_HDR, PacketPimEncodedGroupAddress.FAMILY_IPV4, socket.AF_INET)
elif version == 6:
return PacketPimEncodedGroupAddress.FAMILY_IPV6
return (PacketPimEncodedGroupAddress.IPV6_HDR, PacketPimEncodedGroupAddress.FAMILY_IPV6, socket.AF_INET6)
else:
raise Exception
......@@ -11,8 +11,16 @@ import socket
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
'''
class PacketPimEncodedSourceAddress:
PIM_ENCODED_SOURCE_ADDRESS_HDR = "! BBBB 4s"
PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN = struct.calcsize(PIM_ENCODED_SOURCE_ADDRESS_HDR)
PIM_ENCODED_SOURCE_ADDRESS_HDR = "! BBBB %s"
PIM_ENCODED_SOURCE_ADDRESS_HDR_WITHOUT_SOURCE_ADDRESS = "! BBBB"
IPV4_HDR = "4s"
IPV6_HDR = "16s"
# TODO ver melhor versao ip
PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN = struct.calcsize(PIM_ENCODED_SOURCE_ADDRESS_HDR % IPV4_HDR)
PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN_IPV6 = struct.calcsize(PIM_ENCODED_SOURCE_ADDRESS_HDR % IPV6_HDR)
FAMILY_RESERVED = 0
FAMILY_IPV4 = 1
......@@ -20,7 +28,7 @@ class PacketPimEncodedSourceAddress:
RESERVED_AND_SWR_BITS = 0
def __init__(self, source_address, mask_len=32):
def __init__(self, source_address, mask_len=None):
if type(source_address) not in (str, bytes):
raise Exception
if type(source_address) is bytes:
......@@ -29,18 +37,22 @@ class PacketPimEncodedSourceAddress:
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)
(string_ip_hdr, hdr_addr_family, socket_family) = PacketPimEncodedSourceAddress.get_ip_info(self.source_address)
if self.mask_len is None:
mask_len = 8 * struct.calcsize(string_ip_hdr)
ip = socket.inet_pton(socket_family, self.source_address)
msg = struct.pack(PacketPimEncodedSourceAddress.PIM_ENCODED_SOURCE_ADDRESS_HDR % string_ip_hdr, hdr_addr_family, 0,
PacketPimEncodedSourceAddress.RESERVED_AND_SWR_BITS, mask_len, ip)
return msg
def get_addr_family(self, ip):
@staticmethod
def get_ip_info(ip):
version = ipaddress.ip_address(ip).version
if version == 4:
return PacketPimEncodedSourceAddress.FAMILY_IPV4
return (PacketPimEncodedSourceAddress.IPV4_HDR, PacketPimEncodedSourceAddress.FAMILY_IPV4, socket.AF_INET)
elif version == 6:
return PacketPimEncodedSourceAddress.FAMILY_IPV6
return (PacketPimEncodedSourceAddress.IPV6_HDR, PacketPimEncodedSourceAddress.FAMILY_IPV6, socket.AF_INET6)
else:
raise Exception
raise Exception
\ No newline at end of file
......@@ -9,8 +9,15 @@ import socket
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
'''
class PacketPimEncodedUnicastAddress:
PIM_ENCODED_UNICAST_ADDRESS_HDR = "! BB 4s"
PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN = struct.calcsize(PIM_ENCODED_UNICAST_ADDRESS_HDR)
PIM_ENCODED_UNICAST_ADDRESS_HDR = "! BB %s"
PIM_ENCODED_UNICAST_ADDRESS_HDR_WITHOUT_UNICAST_ADDRESS = "! BB"
IPV4_HDR = "4s"
IPV6_HDR = "16s"
# TODO ver melhor versao ip
PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN = struct.calcsize(PIM_ENCODED_UNICAST_ADDRESS_HDR % IPV4_HDR)
PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN_IPV6 = struct.calcsize(PIM_ENCODED_UNICAST_ADDRESS_HDR % IPV6_HDR)
FAMILY_RESERVED = 0
FAMILY_IPV4 = 1
......@@ -24,17 +31,18 @@ class PacketPimEncodedUnicastAddress:
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)
(string_ip_hdr, hdr_addr_family, socket_family) = PacketPimEncodedUnicastAddress.get_ip_info(self.unicast_address)
ip = socket.inet_pton(socket_family, self.unicast_address)
msg = struct.pack(PacketPimEncodedUnicastAddress.PIM_ENCODED_UNICAST_ADDRESS_HDR % string_ip_hdr, hdr_addr_family, 0, ip)
return msg
def get_addr_family(self, ip):
@staticmethod
def get_ip_info(ip):
version = ipaddress.ip_address(ip).version
if version == 4:
return PacketPimEncodedUnicastAddress.FAMILY_IPV4
return (PacketPimEncodedUnicastAddress.IPV4_HDR, PacketPimEncodedUnicastAddress.FAMILY_IPV4, socket.AF_INET)
elif version == 6:
return PacketPimEncodedUnicastAddress.FAMILY_IPV6
return (PacketPimEncodedUnicastAddress.IPV6_HDR, PacketPimEncodedUnicastAddress.FAMILY_IPV6, socket.AF_INET6)
else:
raise Exception
import struct
import socket
from Packet.PacketPimEncodedUnicastAddress import PacketPimEncodedUnicastAddress
from Packet.PacketPimJoinPruneMulticastGroup import PacketPimJoinPruneMulticastGroup
'''
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
......@@ -12,8 +13,15 @@ from Packet.PacketPimEncodedUnicastAddress import PacketPimEncodedUnicastAddress
'''
class PacketPimJoinPrune:
PIM_TYPE = 3
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 = "! %ss BBH"
PIM_HDR_JOIN_PRUNE_WITHOUT_ADDRESS = "! BBH"
PIM_HDR_JOIN_PRUNE_v4 = PIM_HDR_JOIN_PRUNE % PacketPimEncodedUnicastAddress.PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN
PIM_HDR_JOIN_PRUNE_v6 = PIM_HDR_JOIN_PRUNE % PacketPimEncodedUnicastAddress.PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN_IPV6
PIM_HDR_JOIN_PRUNE_WITHOUT_ADDRESS_LEN = struct.calcsize(PIM_HDR_JOIN_PRUNE_WITHOUT_ADDRESS)
PIM_HDR_JOIN_PRUNE_v4_LEN = struct.calcsize(PIM_HDR_JOIN_PRUNE_v4)
PIM_HDR_JOIN_PRUNE_v6_LEN = struct.calcsize(PIM_HDR_JOIN_PRUNE_v6)
def __init__(self, upstream_neighbor_address, hold_time):
if type(upstream_neighbor_address) not in (str, bytes):
......@@ -24,13 +32,14 @@ class PacketPimJoinPrune:
self.upstream_neighbor_address = upstream_neighbor_address
self.hold_time = hold_time
def add_multicast_group(self, group):
def add_multicast_group(self, group: PacketPimJoinPruneMulticastGroup):
# TODO verificar se grupo ja esta na msg
self.groups.append(group)
def bytes(self) -> bytes:
upstream_neighbor_address = PacketPimEncodedUnicastAddress(self.upstream_neighbor_address).bytes()
msg = struct.pack(PacketPimJoinPrune.PIM_HDR_JOIN_PRUNE, upstream_neighbor_address, 0, len(self.groups), self.hold_time)
msg = upstream_neighbor_address + struct.pack(PacketPimJoinPrune.PIM_HDR_JOIN_PRUNE_WITHOUT_ADDRESS, 0,
len(self.groups), self.hold_time)
for multicast_group in self.groups:
msg += multicast_group.bytes()
......
......@@ -2,6 +2,7 @@ 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
......@@ -25,15 +26,24 @@ from Packet.PacketPimEncodedSourceAddress import PacketPimEncodedSourceAddress
| Pruned Source Address n (Encoded Source Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class PacketPimJoinPruneMulticastGroup:
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 = "! %ss HH"
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_WITHOUT_GROUP_ADDRESS = "! HH"
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_JOIN_PRUNE_MULTICAST_GROUP_v4_LEN_ = struct.calcsize(
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP % PacketPimEncodedGroupAddress.PIM_ENCODED_GROUP_ADDRESS_HDR_LEN)
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_v6_LEN_ = struct.calcsize(
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP % PacketPimEncodedGroupAddress.PIM_ENCODED_GROUP_ADDRESS_HDR_LEN_IPv6)
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_WITHOUT_GROUP_ADDRESS_LEN = struct.calcsize(
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_WITHOUT_GROUP_ADDRESS)
PIM_HDR_JOINED_PRUNED_SOURCE = "! %ss"
PIM_HDR_JOINED_PRUNED_SOURCE_v4_LEN = PacketPimEncodedSourceAddress.PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN
PIM_HDR_JOINED_PRUNED_SOURCE_v6_LEN = PacketPimEncodedSourceAddress.PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN_IPV6
def __init__(self, multicast_group, joined_src_addresses : list, pruned_src_addresses : list):
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:
......@@ -50,13 +60,14 @@ class PacketPimJoinPruneMulticastGroup:
def bytes(self) -> bytes:
multicast_group_address = PacketPimEncodedGroupAddress(self.multicast_group).bytes()
msg = struct.pack(self.PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP, multicast_group_address, len(self.joined_src_addresses), len(self.pruned_src_addresses))
msg = multicast_group_address + struct.pack(self.PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_WITHOUT_GROUP_ADDRESS,
len(self.joined_src_addresses), len(self.pruned_src_addresses))
for joined_src_address in self.joined_src_addresses:
joined_src_address_bytes = PacketPimEncodedSourceAddress(joined_src_address).bytes()
msg += struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, joined_src_address_bytes)
msg += joined_src_address_bytes
for pruned_src_address in self.pruned_src_addresses:
pruned_src_address_bytes = PacketPimEncodedSourceAddress(pruned_src_address).bytes()
msg += struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, pruned_src_address_bytes)
msg += pruned_src_address_bytes
return msg
......@@ -7,7 +7,8 @@ import socket
import sys
import os
import argparse
from argparse import Namespace
import traceback
def client_socket(data_to_send):
# Create a UDS socket
......@@ -71,6 +72,7 @@ class MyDaemon(Daemon):
connection.shutdown(socket.SHUT_RDWR)
except Exception:
connection.shutdown(socket.SHUT_RDWR)
traceback.print_exc()
finally:
# Clean up the connection
connection.close()
......
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