Skip to content

Commit

Permalink
Hub: Support IPv4-mapped-on-IPv6 addresses
Browse files Browse the repository at this point in the history
When the Hub is started with systemd socket activation, then the socket
is an IPv6 socket, that also accepts IPv4 connections. The IPv4 source
address is then an IPv4-mapped-on-IPv6 address, which needs to be
unmapped first, to get the real source address.
  • Loading branch information
holesch committed Feb 8, 2024
1 parent f97ca01 commit b556e95
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
12 changes: 10 additions & 2 deletions not_my_board/_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import contextlib
import contextvars
import ipaddress
import itertools
import logging
import random
Expand Down Expand Up @@ -98,7 +99,7 @@ async def exporter_communicate(self, client_ip, rpc):
@contextlib.contextmanager
def _register_place(self, export_desc, rpc, client_ip):
id_ = next(self._id_generator)
place = models.Place(id=id_, host=client_ip, **export_desc)
place = models.Place(id=id_, host=_unmap_ip(client_ip), **export_desc)

try:
logger.info("New place registered: %d", id_)
Expand Down Expand Up @@ -162,7 +163,7 @@ async def reserve(self, candidate_ids):
client_ip = client_ip_var.get()
async with util.on_error(self.return_reservation, reserved_id):
rpc = self._exporters[reserved_id]
await rpc.set_allowed_ips([client_ip])
await rpc.set_allowed_ips([_unmap_ip(client_ip)])

return reserved_id

Expand All @@ -188,5 +189,12 @@ async def return_reservation(self, place_id):
_hub = Hub()


def _unmap_ip(ip_str):
"""Resolve IPv4-mapped-on-IPv6 to an IPv4 address"""
ip = ipaddress.ip_address(ip_str)
unmapped = getattr(ip, "ipv4_mapped", None) or ip
return str(unmapped)


class ProtocolError(Exception):
pass
25 changes: 19 additions & 6 deletions tests/test_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ def allowed_ips(self):

# pylint: disable=redefined-outer-name
@contextlib.asynccontextmanager
async def register_exporter(hub):
exporter_ip = DEFAULT_EXPORTER_IP
async def register_exporter(hub, ip=DEFAULT_EXPORTER_IP):
register_event = asyncio.Event()
fake_exporter = FakeExporter(register_event)
coro = hub.exporter_communicate(exporter_ip, fake_exporter)
coro = hub.exporter_communicate(ip, fake_exporter)
async with util.background_task(coro) as exporter_task:
async with asyncio.timeout(2):
await register_event.wait()
Expand Down Expand Up @@ -95,10 +94,9 @@ async def receive_iter(queue):

# pylint: disable=redefined-outer-name
@contextlib.asynccontextmanager
async def register_agent(hub):
agent_ip = DEFAULT_AGENT_IP
async def register_agent(hub, ip=DEFAULT_AGENT_IP):
server, proxy = fake_rpc_pair()
coro = hub.agent_communicate(agent_ip, server)
coro = hub.agent_communicate(ip, server)
async with util.background_task(coro):
async with util.background_task(proxy.io_loop()):
yield proxy
Expand Down Expand Up @@ -206,3 +204,18 @@ async def test_return_non_candidate(hub):
await agent.return_reservation(candidate_ids[1])
# ... then the queued reservation is still active
assert not reserve_task.done()


async def test_mapped_ip_exporter(hub):
async with register_exporter(hub, ip="::FFFF:10.0.0.8"):
places = await hub.get_places()
assert places["places"][0]["host"] == "10.0.0.8"


async def test_mapped_ip_agent(hub):
async with register_exporter(hub) as (exporter, _):
async with register_agent(hub, ip="::FFFF:10.0.0.9") as agent:
places = await hub.get_places()
candidate_ids = [places["places"][0]["id"]]
await agent.reserve(candidate_ids)
assert exporter.allowed_ips == ["10.0.0.9"]

0 comments on commit b556e95

Please sign in to comment.