py-libp2p/network/multiaddr.py

122 lines
3.9 KiB
Python
Raw Normal View History

2018-10-26 06:45:41 +08:00
class MultiAddr:
2018-10-22 02:06:56 +08:00
# Validates input string and constructs internal representation.
2018-10-22 02:06:56 +08:00
def __init__(self, addr):
# Empty multiaddrs are valid.
if not addr:
self.protocol_map = dict()
return
2018-10-22 02:06:56 +08:00
if not addr[0] == "/":
2018-10-26 06:45:41 +08:00
raise MultiAddrValueError("Invalid input multiaddr.")
2018-10-22 02:06:56 +08:00
addr = addr[1:]
protocol_map = dict()
split_addr = addr.split("/")
2018-10-22 02:16:26 +08:00
if not split_addr or len(split_addr) % 2 != 0:
2018-10-26 06:45:41 +08:00
raise MultiAddrValueError("Invalid input multiaddr.")
2018-10-22 02:06:56 +08:00
is_protocol = True
curr_protocol = ""
for addr_part in split_addr:
if is_protocol:
curr_protocol = addr_part
else:
protocol_map[curr_protocol] = addr_part
is_protocol = not is_protocol
2018-10-28 03:53:00 +08:00
# Basic validation of protocols
# TODO(rzajac): Add more validation as necessary.
if 'ip4' in self.protocol_map and 'ip6' in self.protocol_map:
raise MultiAddrValueError("Multiaddr should not specify two IP layers.")
if 'tcp' in self.protocol_map and 'udp' in self.protocol_map:
raise MultiAddrValueError("Multiaddr should not specify two transport layers.")
2018-10-22 02:06:56 +08:00
self.protocol_map = protocol_map
def get_protocols(self):
"""
:return: List of protocols contained in this multiaddr.
"""
2018-10-22 02:06:56 +08:00
return list(self.protocol_map.keys())
def get_protocol_value(self, protocol):
"""
Getter for protocol values in this multiaddr.
:param protocol: the protocol whose value to retrieve
:return: value of input protocol
"""
2018-10-22 02:06:56 +08:00
if protocol not in self.protocol_map:
return None
return self.protocol_map[protocol]
def add_protocol(self, protocol, value):
"""
Setter for protocol values in this multiaddr.
:param protocol: the protocol whose value to set or add
:param value: the value for the input protocol
:return: True if successful
"""
self.protocol_map[protocol] = value
return True
def remove_protocol(self, protocol):
"""
Remove protocol and its value from this multiaddr.
:param protocol: the protocol to remove
:return: True if remove succeeded, False if protocol was not contained in this multiaddr
"""
del self.protocol_map[protocol]
def get_multiaddr_string(self):
"""
:return: the string representation of this multiaddr.
"""
addr = ""
for protocol in self.protocol_map:
addr += "/" + protocol + "/" + self.get_protocol_value(protocol)
return addr
2018-10-28 03:53:00 +08:00
def to_options(self):
"""
Gives back a dictionary with access to transport information from this multiaddr.
Example: MultiAddr('/ip4/127.0.0.1/tcp/4001').to_options()
= { family: 'ipv4', host: '127.0.0.1', transport: 'tcp', port: '4001' }
2018-11-01 06:31:52 +08:00
:return: {{family: String, host: String, transport: String, port: String}}
with None if field does not exist
2018-10-28 03:53:00 +08:00
"""
options = dict()
if 'ip4' in self.protocol_map:
options['family'] = 'ipv4'
options['host'] = self.protocol_map['ip4']
elif 'ip6' in self.protocol_map:
options['family'] = 'ipv6'
options['host'] = self.protocol_map['ip6']
else:
options['family'] = None
options['host'] = None
if 'tcp' in self.protocol_map:
options['transport'] = 'tcp'
options['port'] = self.protocol_map['tcp']
elif 'udp' in self.protocol_map:
options['transport'] = 'udp'
options['port'] = self.protocol_map['udp']
else:
options['transport'] = None
options['port'] = None
return options
2018-10-22 02:06:56 +08:00
2018-10-26 06:45:41 +08:00
class MultiAddrValueError(ValueError):
"""Raised when the input string to the MultiAddr constructor was invalid."""
2018-10-22 02:06:56 +08:00
pass