2018-10-27 06:06:56 +08:00
|
|
|
import asyncio
|
2019-12-20 00:31:18 +08:00
|
|
|
import socket
|
2019-12-03 07:57:22 +08:00
|
|
|
import sys
|
2019-12-20 00:31:18 +08:00
|
|
|
from typing import List, Optional
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-08-02 16:32:33 +08:00
|
|
|
from multiaddr import Multiaddr
|
2019-12-20 00:31:18 +08:00
|
|
|
from multiaddr.protocols import P_IP4, P_IP6, P_TCP, P_UDP
|
|
|
|
from multiaddr.protocols import protocol_with_code as p_code
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
from libp2p.network.connection.raw_connection import RawConnection
|
2019-08-05 11:22:44 +08:00
|
|
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
2019-09-19 21:23:35 +08:00
|
|
|
from libp2p.transport.exceptions import OpenConnectionError
|
2019-08-02 16:32:33 +08:00
|
|
|
from libp2p.transport.listener_interface import IListener
|
|
|
|
from libp2p.transport.transport_interface import ITransport
|
2019-08-05 11:22:44 +08:00
|
|
|
from libp2p.transport.typing import THandler
|
2019-08-02 16:32:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TCPListener(IListener):
|
|
|
|
multiaddrs: List[Multiaddr]
|
|
|
|
server = None
|
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
def __init__(self, handler_function: THandler) -> None:
|
2019-08-02 16:32:33 +08:00
|
|
|
self.multiaddrs = []
|
|
|
|
self.server = None
|
|
|
|
self.handler = handler_function
|
|
|
|
|
|
|
|
async def listen(self, maddr: Multiaddr) -> bool:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
put listener in listening mode and wait for incoming connections.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-08-02 16:32:33 +08:00
|
|
|
:param maddr: maddr of peer
|
|
|
|
:return: return True if successful
|
|
|
|
"""
|
2019-12-20 00:31:18 +08:00
|
|
|
listen_addr = _ip4_or_6_from_multiaddr(maddr)
|
|
|
|
if listen_addr is None:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"Can only start TCP Listener with a IPv4 or IPv6 address"
|
|
|
|
)
|
|
|
|
|
2019-08-02 16:32:33 +08:00
|
|
|
self.server = await asyncio.start_server(
|
2019-12-20 00:31:18 +08:00
|
|
|
self.handler, listen_addr, maddr.value_for_protocol("tcp")
|
2019-08-02 16:32:33 +08:00
|
|
|
)
|
|
|
|
socket = self.server.sockets[0]
|
|
|
|
self.multiaddrs.append(_multiaddr_from_socket(socket))
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_addrs(self) -> List[Multiaddr]:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
retrieve list of addresses the listener is listening on.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-08-02 16:32:33 +08:00
|
|
|
:return: return list of addrs
|
|
|
|
"""
|
|
|
|
# TODO check if server is listening
|
|
|
|
return self.multiaddrs
|
|
|
|
|
2019-08-29 21:38:06 +08:00
|
|
|
async def close(self) -> None:
|
2019-10-24 14:41:10 +08:00
|
|
|
"""close the listener such that no more connections can be open on this
|
|
|
|
transport instance."""
|
2019-08-02 16:32:33 +08:00
|
|
|
if self.server is None:
|
2019-08-29 21:38:06 +08:00
|
|
|
return
|
2019-08-02 16:32:33 +08:00
|
|
|
self.server.close()
|
2019-12-11 09:07:21 +08:00
|
|
|
server = self.server
|
2019-08-02 16:32:33 +08:00
|
|
|
self.server = None
|
2019-12-11 09:07:21 +08:00
|
|
|
if sys.version_info < (3, 7):
|
|
|
|
return
|
|
|
|
await server.wait_closed()
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-10-27 06:06:56 +08:00
|
|
|
|
|
|
|
class TCP(ITransport):
|
2019-09-11 09:01:03 +08:00
|
|
|
async def dial(self, maddr: Multiaddr) -> IRawConnection:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
dial a transport to peer listening on multiaddr.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-07-10 19:33:38 +08:00
|
|
|
:param maddr: multiaddr of peer
|
2019-08-16 14:12:10 +08:00
|
|
|
:return: `RawConnection` if successful
|
2019-09-19 22:19:36 +08:00
|
|
|
:raise OpenConnectionError: raised when failed to open connection
|
2018-10-27 06:06:56 +08:00
|
|
|
"""
|
2019-12-20 00:31:18 +08:00
|
|
|
self.host = _ip4_or_6_from_multiaddr(maddr)
|
|
|
|
if self.host is None:
|
|
|
|
raise ValueError("Cannot find ipv4 or ipv6 host in multiaddress")
|
|
|
|
|
2019-08-25 03:02:30 +08:00
|
|
|
self.port = int(maddr.value_for_protocol("tcp"))
|
2018-11-13 00:00:43 +08:00
|
|
|
|
2019-09-19 21:23:35 +08:00
|
|
|
try:
|
|
|
|
reader, writer = await asyncio.open_connection(self.host, self.port)
|
|
|
|
except (ConnectionAbortedError, ConnectionRefusedError) as error:
|
|
|
|
raise OpenConnectionError(error)
|
2018-11-13 00:00:43 +08:00
|
|
|
|
2019-08-25 03:02:30 +08:00
|
|
|
return RawConnection(reader, writer, True)
|
2018-10-27 06:06:56 +08:00
|
|
|
|
2019-08-05 10:20:30 +08:00
|
|
|
def create_listener(self, handler_function: THandler) -> TCPListener:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
create listener on transport.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2018-11-29 23:06:40 +08:00
|
|
|
:param handler_function: a function called when a new connection is received
|
2019-10-24 14:41:10 +08:00
|
|
|
that takes a connection as argument which implements interface-connection
|
2018-10-27 06:06:56 +08:00
|
|
|
:return: a listener object that implements listener_interface.py
|
|
|
|
"""
|
2019-08-02 16:32:33 +08:00
|
|
|
return TCPListener(handler_function)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
|
|
|
|
2019-12-20 00:31:18 +08:00
|
|
|
def _ip4_or_6_from_multiaddr(maddr: Multiaddr) -> Optional[str]:
|
|
|
|
if P_IP4 in maddr.protocols():
|
|
|
|
return maddr.value_for_protocol(P_IP4)
|
|
|
|
elif P_IP6 in maddr.protocols():
|
|
|
|
return maddr.value_for_protocol(P_IP6)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _multiaddr_from_socket(sock: socket.socket) -> Multiaddr:
|
|
|
|
# Reference: http://man7.org/linux/man-pages/man2/socket.2.html#DESCRIPTION
|
|
|
|
# todo: move this to more generic libp2p.transport helper function
|
|
|
|
|
|
|
|
# Reference: https://stackoverflow.com/questions/5815675/what-is-sock-dgram-and-sock-stream
|
|
|
|
# Selects first protocol in sequence if bitwise AND matches, else None
|
|
|
|
t_proto = next(
|
|
|
|
(
|
|
|
|
v
|
|
|
|
for k, v in {
|
|
|
|
socket.SOCK_STREAM: p_code(P_TCP).name,
|
|
|
|
socket.SOCK_DGRAM: p_code(P_UDP).name,
|
|
|
|
}.items()
|
|
|
|
if k & sock.type != 0
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
|
|
|
|
if t_proto is None:
|
|
|
|
raise NotImplementedError(
|
|
|
|
f"Cannot convert socket to multiaddr, socket type is of {sock.type}"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Reference: https://docs.python.org/3/library/socket.html#socket-families
|
|
|
|
if sock.family == socket.AF_INET:
|
|
|
|
# ipv4: (host, port)
|
|
|
|
addr, port = sock.getsockname()
|
|
|
|
ip = p_code(P_IP4).name
|
|
|
|
|
|
|
|
elif sock.family == socket.AF_INET6:
|
|
|
|
# ipv6: (host, port, flowinfo, scopeid)
|
|
|
|
addr, port = sock.getsockname()[:2]
|
|
|
|
ip = p_code(P_IP6).name
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(
|
|
|
|
f"Cannot convert socket to multiaddr, socket family is of {sock.family}"
|
|
|
|
)
|
|
|
|
|
|
|
|
return Multiaddr(f"/{ip}/{addr}/{t_proto}/{port}")
|