jsoncomm: switch to use sequenced-packet sockets

Switch to use a connection oriented datagram based protocol, i.e.
`SOCK_SEQPACKET`, instead of `SOCK_DGRAM`. It sill preserves
message boundaries, but since it is connection oriented the client
nor the server do not need to specify the destination addresses
of the peer in sendmsg/recvmesg. Moreover, the host will be able
to send messages to the client, even if the latter is sandboxed
with a separate network namespace. In the `SOCK_DRAM` case the
auto-bound address of the client would not be visible to the host
and thus sending messages would to it would fail.

Adapt the jsoncomm tests as well as `BaseAPI`.
This commit is contained in:
Christian Kellner 2020-07-26 22:09:39 +02:00 committed by Tom Gundersen
parent fcf3ec4502
commit abbdf06ba5
3 changed files with 39 additions and 9 deletions

View file

@ -69,13 +69,24 @@ class BaseAPI(abc.ABC):
def _dispatch(self, sock: jsoncomm.Socket):
"""Called when data is available on the socket"""
msg, fds, addr = sock.recv()
if msg is None:
# Peer closed the connection
self.event_loop.remove_reader(sock)
return
self._message(msg, fds, sock, addr)
fds.close()
def _accept(self, server):
client = server.accept()
if client:
self.event_loop.add_reader(client, self._dispatch, client)
def _run_event_loop(self):
with jsoncomm.Socket.new_server(self.socket_address) as server:
server.blocking = False
server.listen()
self.barrier.wait()
self.event_loop.add_reader(server, self._dispatch, server)
self.event_loop.add_reader(server, self._accept, server)
asyncio.set_event_loop(self.event_loop)
self.event_loop.run_forever()
self.event_loop.remove_reader(server)