Inbound Connections¶
Some devices connect to the adapter themselves: a reader posts its events to an address the adapter gives it, a controller in client mode dials the host. Such a device needs an address of the computer the adapter runs on, in the device's own network.
Problem: An adapter in a container sees only its Docker address (for example 172.18.0.5). A device on the
LAN cannot reach it. The adapter cannot read the host's addresses from inside the container.
Solution: The framework service LocalAddresses holds the IPv4 addresses of the host, and Toward picks the
one in the network of a given device.
Where the addresses come from¶
The one source is the environment variable Host__Addresses (configuration key Host:Addresses), for example
192.168.10.7/24,10.20.30.7/16. Whoever starts the adapter sets it:
| Run | Who sets Host__Addresses |
|---|---|
| Compose, deployed by the installer | the installer, on every deploy, from the interfaces of the host that are up (no loopback, no 169.254.x) |
lab run on the computer |
the lab, from the interfaces of the computer |
lab rig in a container |
you: docker run -e Host__Addresses=<host-ip>/<prefix> (see pq-tools); the lab passes it on |
| Rider without the lab | nobody; set it in launchSettings.json when you need it |
Without the variable the list is empty, and the adapter logs one warning at start. An entry that is not an IPv4
address/prefix is logged and skipped.
Never parse the variable yourself and never read the network interfaces in an adapter: in a container they are the Docker ones.
API¶
public sealed class LocalAddresses
{
public IReadOnlyList<LocalAddress> All { get; }
public IPAddress? Toward(IPAddress remoteAddress);
}
public sealed record LocalAddress(IPAddress Address, IPNetwork Network);
| Member | Gives |
|---|---|
All |
every host address with its network, in the order of the variable; Network is a System.Net.IPNetwork (BaseAddress, PrefixLength, Contains) |
Toward(remoteAddress) |
the host address whose network contains remoteAddress; the narrowest network when several do; null when none does |
Toward returns null in two cases:
- the device is behind a router, in a network the host has no address in;
- no host address is known (the variable is empty).
An IPv4 address mapped to IPv6 (::ffff:192.168.10.15) is accepted.
Usage¶
LocalAddresses is a singleton; inject it like any other framework service.
public sealed class AcmeReader(LocalAddresses localAddresses, ILogger<AcmeReader> logger)
{
/// <summary>
/// Gives the reader the address to post its events to.
/// </summary>
private async Task<bool> SetPushTarget(IPAddress readerAddress, int port, CancellationToken ct)
{
var target = localAddresses.Toward(readerAddress);
if (target is null)
{
logger.LogError("No host address in the network of reader {Reader}; set Host__Addresses", readerAddress);
return false;
}
return await protocol.SetPushTarget(target, port, ct);
}
}
The argument is the address the adapter itself connects to, from the device configuration. When the configured
host is a name, resolve it first (Dns.GetHostAddressesAsync) and pass the IPv4 result.
All serves a device that the adapter does not configure, for example to show the integrator which address to
enter into the device:
foreach (var host in localAddresses.All)
logger.LogInformation("Host address {Address} in {Network}", host.Address, host.Network);
Related¶
- Publishing an image —
Host__Addressesamong the variables every adapter gets - pq-tools — starting the lab rig with
Host__Addresses