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

encoding addresses

parent 05a69151
......@@ -60,8 +60,6 @@ def list_neighbors():
check_time = time.time()
t = PrettyTable(['Neighbor IP', 'KeepAlive', "Generation ID", "Uptime"])
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 = 0 if (uptime < 0) else uptime
......@@ -78,8 +76,8 @@ def list_enabled_interfaces():
from Packet.PacketPimJoinPrune import PacketPimJoinPrune
from Packet.PacketPimJoinPruneMulticastGroup import PacketPimJoinPruneMulticastGroup
ph = PacketPimJoinPrune(167772173, 20)
ph.add_multicast_group(PacketPimJoinPruneMulticastGroup(3708422657, [3708422657], []))
ph = PacketPimJoinPrune("10.0.0.13", 210)
ph.add_multicast_group(PacketPimJoinPruneMulticastGroup("239.123.123.123", ["1.1.1.1"], []))
pckt = Packet(pim_header=PacketPimHeader(ph))
interfaces[interface].send(pckt.bytes())
......
......@@ -2,7 +2,7 @@ import struct
class PacketIpHeader:
IP_HDR = "! BBH HH BBH LL"
IP_HDR = "! BBH HH BBH 4s 4s"
#IP_HDR2 = "! B"
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
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:
PIM_VERSION = 2
......
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:
PIM_TYPE = 0
PIM_HDR_OPTS = "! HH"
......
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:
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)
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.upstream_neighbor_address = upstream_neighbor_address
self.hold_time = hold_time
......@@ -16,8 +29,8 @@ class PacketPimJoinPrune:
self.groups.append(group)
def bytes(self) -> bytes:
# TODO esta a converter upstream neighbor (considera que recebe int) para 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)
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)
for multicast_group in self.groups:
msg += multicast_group.bytes()
......
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:
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_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)
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.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.to_bytes(4, byteorder='big'), len(self.joined_src_addresses), len(self.pruned_src_addresses))
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))
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:
msg += struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, pruned_src_address.to_bytes(4, byteorder='big'))
# TODO: verificar pruned e joined addrss
pruned_src_address_bytes = PacketPimEncodedSourceAddress(pruned_src_address).bytes()
msg += struct.pack(self.PIM_HDR_JOINED_PRUNED_SOURCE, pruned_src_address_bytes)
return msg
......@@ -5,7 +5,7 @@ from Packet.PacketPimHeader import PacketPimHeader
from Packet.PacketPimHello import PacketPimHello
from Packet.PacketPimJoinPrune import PacketPimJoinPrune
from Packet.PacketPimJoinPruneMulticastGroup import PacketPimJoinPruneMulticastGroup
import socket
from utils import checksum
......@@ -55,8 +55,8 @@ class ReceivedPacket(Packet):
"TTL": ttl,
"PROTO": proto,
"CKSUM": cksum,
"SRC": src,
"DST": dst
"SRC": socket.inet_ntoa(src),
"DST": socket.inet_ntoa(dst)
}
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