View Source Exrpc.MFALookup (exrpc v0.4.2)

Function allowlist and lookup table.

Each functions exposed to clients is assigned a unique integer ID, This allows the server to enforce a function allowlist and the client can make RPC call by transmitting function ID instead of module and function atoms.

Internal details: the lookup table is a tuple of two ETS tables, {mfa2id, id2mfa}, both of which are owned by the pid that created the table. The first table maps MFAs to IDs, the second table vice versa.

Example

iex> {:ok, lookup} = Exrpc.MFALookup.create([
...>   {Greeter, :hello, 1},
...>   {Greeter, :goodbye, 1}
...> ])
iex> Exrpc.MFALookup.mfa_to_id(lookup, {Greeter, :hello, 1})
0
iex> Exrpc.MFALookup.id_to_mfa(lookup, 0)
{Greeter, :hello, 1}

Summary

Functions

Create a lookup table from a list of MFAs.

Get MFA by ID.

Get the ID of an MFA.

Get a list of MFAs.

Types

Functions

@spec create([mfa()]) :: {:ok, t()} | {:error, atom()}

Create a lookup table from a list of MFAs.

Examples

iex> Exrpc.MFALookup.create([])
{:error, :empty_list}
iex> {:ok, {_mfa2id, _id2mfa}} = Exrpc.MFALookup.create([
...>   {Greeter, :hello, 1},
...>   {Greeter, :goodbye, 1}
...> ])
Link to this function

id_to_mfa(mfa_lookup, id)

View Source
@spec id_to_mfa(t(), integer()) :: mfa() | nil | {:error, :invalid_mfa_lookup}

Get MFA by ID.

Examples

iex> {:ok, lookup} = Exrpc.MFALookup.create([
...>   {Greeter, :hello, 1},
...>   {Greeter, :goodbye, 1}
...> ])
iex> Exrpc.MFALookup.id_to_mfa(lookup, 0)
{Greeter, :hello, 1}
iex> Exrpc.MFALookup.id_to_mfa(lookup, 1)
{Greeter, :goodbye, 1}
iex> Exrpc.MFALookup.id_to_mfa(lookup, 2)
nil
Link to this function

mfa_to_id(mfa_lookup, mfa)

View Source
@spec mfa_to_id(t(), mfa()) :: integer() | nil | {:error, :invalid_mfa_lookup}

Get the ID of an MFA.

Examples

iex> {:ok, lookup} = Exrpc.MFALookup.create([
...>   {Greeter, :hello, 1},
...>   {Greeter, :goodbye, 1}
...> ])
iex> Exrpc.MFALookup.mfa_to_id(lookup, {Greeter, :hello, 1})
0
iex> Exrpc.MFALookup.mfa_to_id(lookup, {Greeter, :goodbye, 1})
1
iex> Exrpc.MFALookup.mfa_to_id(lookup, {Greeter, :heyyyy, 1})
nil
@spec to_list(t()) :: [mfa()] | {:error, :invalid_mfa_lookup}

Get a list of MFAs.

Examples

iex> {:ok, lookup} = Exrpc.MFALookup.create([
...>   {Greeter, :hello, 1},
...>   {Greeter, :goodbye, 1}
...> ])
iex> Exrpc.MFALookup.to_list(lookup)
[{Greeter, :hello, 1}, {Greeter, :goodbye, 1}]