masque_ip_session_registry (masque v0.7.0)
View SourceCross-session address registry for CONNECT-IP.
Stores {Version, StartAddr, EndAddr, Prefix} -> {SessionPid, ContextId} assignments so an out-of-band consumer (e.g. a TUN device owner) can answer "which session serves destination X?" without reaching into per-session state.
Storage is a single ETS ordered_set keyed by {Version, StartIntAddr}. Lookup does interval inclusion: find the entry with the largest start address =< the probe and verify the end address >= the probe. Because registrations are rejected when their range overlaps an existing entry, at most one interval can cover any given address.
The gen_server itself is only on the write path (register / release / monitor handling). Lookups go directly through ETS so read traffic does not serialise on the server.
All write APIs are tolerant of the registry not being started - they become no-ops when whereis(?MODULE) =:= undefined. This keeps test environments that don't boot the application from needing extra setup, and keeps the proxy handler simple (it always calls register/5 / release/3 without a feature flag).
Summary
Functions
Snapshot of every registration. Intended for diagnostics.
Look up the session serving IP. Reads ETS directly.
Register a host address or prefix as served by SessionPid. Returns {error, conflict} if the proposed range overlaps any existing registration; otherwise ok. Idempotent for an exact repeat by the same pid.
Release a previously-registered range. No-op if the range was not registered.
Release every range owned by Pid. Used by the registry's own `DOWN'' handler; exposed for tests and explicit cleanup.
Functions
-spec all() -> [{4 | 6, non_neg_integer(), non_neg_integer(), non_neg_integer(), pid(), non_neg_integer()}].
Snapshot of every registration. Intended for diagnostics.
-spec lookup(inet:ip_address()) -> {ok, pid(), non_neg_integer()} | not_found.
Look up the session serving IP. Reads ETS directly.
-spec register(4 | 6, inet:ip4_address() | inet:ip6_address(), non_neg_integer(), pid(), non_neg_integer()) -> ok | {error, conflict}.
Register a host address or prefix as served by SessionPid. Returns {error, conflict} if the proposed range overlaps any existing registration; otherwise ok. Idempotent for an exact repeat by the same pid.
-spec release(4 | 6, inet:ip4_address() | inet:ip6_address(), non_neg_integer()) -> ok.
Release a previously-registered range. No-op if the range was not registered.
-spec release_pid(pid()) -> ok.
Release every range owned by Pid. Used by the registry's own `DOWN'' handler; exposed for tests and explicit cleanup.