Karta.IPPools (Karta v1.0.0)

IP pools: the addresses mail servers send from.

A pool (Karta.IPPools.IPPool) has one or more addresses (Karta.IPPools.IPAddress), IPv4, IPv6 or both, each with the name Karta gives in EHLO from it. A server assigned to a pool (assign_server/2) has its mail delivered from the pool's addresses; a server without a pool sends from the node's own address, with KARTA_OUTBOUND_HOSTNAME in EHLO, as before pools existed.

HELO names. A receiving server compares the EHLO name with the sending address's PTR record. add_address/3 therefore looks both up (check_dns/3): the address's PTR record must name the HELO name, and the HELO name's A or AAAA record must lead back to the address (forward-confirmed reverse DNS). An address that fails is refused, unless check_dns: false is given (for example while the PTR record is still being set up); check_dns/3 tells later whether it is right.

Sending. A node delivers a pool's mail only from the pool's addresses that are on its own network interfaces (local_addresses/1; an address's local_address behind one-to-one NAT): each connection is bound to one of them, and the EHLO name is that address's. The pool's address families limit which addresses of the recipient's mail servers are tried (ip_family/2). A node without any of the pool's addresses does not deliver its mail; the mail waits.

SPF. A receiving server checks the sending address against the SPF record of the envelope sender's domain. check_spf/3 says, for each of a pool's addresses, whether a domain's SPF record lets it send: a pool with an IPv6 address whose senders' SPF records list IPv4 networks only fails there, though its IPv4 addresses pass. Every address should get :pass. Karta does not check SPF on its own when it delivers.

Warm-up. A new address is used for few recipients a day at first, more each day, until receiving servers know it: a warm-up plan (Karta.IPPools.WarmupPlan, create_warmup_plan/1) gives the daily limits, and start_warmup/3 puts an address under one from a given day. Delivery keeps to the limits (Karta.Outbound.Warmup).

Queues. The delivery jobs of a server in a pool run in the pool's own Oban queue, outbound_<name> (queue/1); those of servers without a pool in outbound. A pool's queue runs only on the nodes that hold one of its addresses (Karta.IPPools.Monitor), so an address that moves between nodes takes the pool's delivery with it. Assigning a server to another pool moves its waiting jobs along (Karta.Outbound.reroute/1).

Every function that changes something returns {:ok, record} or {:error, changeset}.

Summary

Functions

Adds an address to a pool: %{address: "192.0.2.25", helo_name: "mta1.example.com"}, and local_address behind one-to-one NAT. The HELO name is checked in DNS first (check_dns/3) unless check_dns: false; dns: gives options for Karta.DNS.lookup/3.

Assigns a server to a pool, or with nil back to the node's own address. Its delivery jobs that wait move to the new queue in the same transaction; one that is running moves when it runs next.

Checks that helo_name is the right EHLO name for address: the address's PTR record names it, and its A (IPv4) or AAAA (IPv6) record leads back to the address. :ok, or {:error, problem} with the problem in words. The lookups bypass the DNS cache; opts are options for Karta.DNS.lookup/3.

The SPF result (Karta.IPPools.SPF.check/3) of each of the pool's addresses for mail from domain (the envelope sender's domain, or the return path domain): %{"192.0.2.25" => :pass, "2001:db8::25" => :fail}. The lookups bypass the DNS cache; opts are options for Karta.DNS.lookup/3.

Creates a pool: %{name: "bulk"}.

Creates a warm-up plan: %{name: "standard", daily_limits: [50, 100, 200]} (see Karta.IPPools.WarmupPlan).

Deletes a pool and its addresses; refused while servers send from it.

Deletes a warm-up plan; refused while addresses warm up under it.

A pool with its addresses, by id or name, or nil.

A warm-up plan by id or name, or nil.

The pool's addresses whose bind address (Karta.IPPools.IPAddress.bind_address/1) is in local, by IP family (4 or 8, the size of the address tuple).

Which of the pool's addresses this node holds now, by address text: %{"192.0.2.25" => true, "2001:db8::25" => false}. It is this node's view (local_addresses/1, whose options opts are); the holder metric (Karta.PromEx.IPPools) shows every node's, which no node stores (no node writes holders to the database: the metric carries them).

The addresses of the interfaces that are up, from an :inet.getifaddrs/0 result.

The IP family policy for delivery from a pool (Karta.Outbound.MXResolver): a pool with only IPv4 addresses delivers to IPv4 addresses only (:ipv4_only), one with only IPv6 addresses to IPv6 only; a pool with both (or none) keeps default.

Every pool with its addresses, by name.

Every warm-up plan with its addresses, by name.

The addresses on this node's network interfaces that are up: :local_addresses in opts when given (a list); otherwise those the monitor of the Oban instance :oban (default Oban) read last (Karta.IPPools.Monitor); without a monitor, from :inet.getifaddrs/0.

The Oban queue of a pool's delivery jobs (by the pool or its name): "outbound_<name>"; "outbound" for nil, no pool.

Removes an address from its pool.

The name of an address's PTR record: 25.2.0.192.in-addr.arpa, or the 32 nibbles of an IPv6 address under ip6.arpa.

Puts an address under a warm-up plan from the UTC day on: (default today): its first day's limit applies that day. A day in the past continues a warm-up begun elsewhere; before a day in the future the address is not used.

Ends an address's warm-up: it has no daily limit from now on.

Changes a pool's settings: %{concurrency: 4, destination_limits: "*.example.com connections=2"} (see Karta.IPPools.IPPool); nil returns a setting to its default.

Changes a warm-up plan's name or daily limits; the addresses under it follow the new limits from now on.

Where an address's warm-up stands on the UTC day date (default today): %{plan: "standard", day: 3, days: 30, limit: 200, sent: 57} (day 1 is the first; limit nil once the plan is over, 0 before it starts), or nil for an address without a plan.

Functions

add_address(pool, attrs, opts \\ [])

@spec add_address(Karta.IPPools.IPPool.t(), map(), keyword()) ::
  {:ok, Karta.IPPools.IPAddress.t()} | {:error, Ecto.Changeset.t()}

Adds an address to a pool: %{address: "192.0.2.25", helo_name: "mta1.example.com"}, and local_address behind one-to-one NAT. The HELO name is checked in DNS first (check_dns/3) unless check_dns: false; dns: gives options for Karta.DNS.lookup/3.

assign_server(server, pool)

@spec assign_server(Karta.Servers.Server.t(), Karta.IPPools.IPPool.t() | nil) ::
  {:ok, Karta.Servers.Server.t()} | {:error, Ecto.Changeset.t()}

Assigns a server to a pool, or with nil back to the node's own address. Its delivery jobs that wait move to the new queue in the same transaction; one that is running moves when it runs next.

check_dns(address, helo_name, opts \\ [])

@spec check_dns(:inet.ip_address(), String.t(), keyword()) ::
  :ok | {:error, String.t()}

Checks that helo_name is the right EHLO name for address: the address's PTR record names it, and its A (IPv4) or AAAA (IPv6) record leads back to the address. :ok, or {:error, problem} with the problem in words. The lookups bypass the DNS cache; opts are options for Karta.DNS.lookup/3.

check_spf(pool, domain, opts \\ [])

@spec check_spf(Karta.IPPools.IPPool.t(), String.t(), keyword()) :: %{
  required(String.t()) => Karta.IPPools.SPF.result()
}

The SPF result (Karta.IPPools.SPF.check/3) of each of the pool's addresses for mail from domain (the envelope sender's domain, or the return path domain): %{"192.0.2.25" => :pass, "2001:db8::25" => :fail}. The lookups bypass the DNS cache; opts are options for Karta.DNS.lookup/3.

create_pool(attrs)

@spec create_pool(map()) ::
  {:ok, Karta.IPPools.IPPool.t()} | {:error, Ecto.Changeset.t()}

Creates a pool: %{name: "bulk"}.

create_warmup_plan(attrs)

@spec create_warmup_plan(map()) ::
  {:ok, Karta.IPPools.WarmupPlan.t()} | {:error, Ecto.Changeset.t()}

Creates a warm-up plan: %{name: "standard", daily_limits: [50, 100, 200]} (see Karta.IPPools.WarmupPlan).

delete_pool(pool)

@spec delete_pool(Karta.IPPools.IPPool.t()) ::
  {:ok, Karta.IPPools.IPPool.t()} | {:error, Ecto.Changeset.t()}

Deletes a pool and its addresses; refused while servers send from it.

delete_warmup_plan(plan)

@spec delete_warmup_plan(Karta.IPPools.WarmupPlan.t()) ::
  {:ok, Karta.IPPools.WarmupPlan.t()} | {:error, Ecto.Changeset.t()}

Deletes a warm-up plan; refused while addresses warm up under it.

get_pool(id_or_name)

@spec get_pool(term()) :: Karta.IPPools.IPPool.t() | nil

A pool with its addresses, by id or name, or nil.

get_warmup_plan(id_or_name)

@spec get_warmup_plan(term()) :: Karta.IPPools.WarmupPlan.t() | nil

A warm-up plan by id or name, or nil.

held(ip_pool, local)

@spec held(Karta.IPPools.IPPool.t(), MapSet.t()) :: %{
  required(4 | 8) => [Karta.IPPools.IPAddress.t()]
}

The pool's addresses whose bind address (Karta.IPPools.IPAddress.bind_address/1) is in local, by IP family (4 or 8, the size of the address tuple).

held_here(pool, opts \\ [])

@spec held_here(Karta.IPPools.IPPool.t(), keyword()) :: %{
  required(String.t()) => boolean()
}

Which of the pool's addresses this node holds now, by address text: %{"192.0.2.25" => true, "2001:db8::25" => false}. It is this node's view (local_addresses/1, whose options opts are); the holder metric (Karta.PromEx.IPPools) shows every node's, which no node stores (no node writes holders to the database: the metric carries them).

interface_addresses(arg)

@spec interface_addresses({:ok, list()} | {:error, term()}) ::
  MapSet.t(:inet.ip_address())

The addresses of the interfaces that are up, from an :inet.getifaddrs/0 result.

ip_family(ip_pool, default)

@spec ip_family(Karta.IPPools.IPPool.t(), atom()) :: atom()

The IP family policy for delivery from a pool (Karta.Outbound.MXResolver): a pool with only IPv4 addresses delivers to IPv4 addresses only (:ipv4_only), one with only IPv6 addresses to IPv6 only; a pool with both (or none) keeps default.

list_pools()

@spec list_pools() :: [Karta.IPPools.IPPool.t()]

Every pool with its addresses, by name.

list_warmup_plans()

@spec list_warmup_plans() :: [Karta.IPPools.WarmupPlan.t()]

Every warm-up plan with its addresses, by name.

local_addresses(opts \\ [])

@spec local_addresses(keyword()) :: MapSet.t(:inet.ip_address())

The addresses on this node's network interfaces that are up: :local_addresses in opts when given (a list); otherwise those the monitor of the Oban instance :oban (default Oban) read last (Karta.IPPools.Monitor); without a monitor, from :inet.getifaddrs/0.

queue(name)

@spec queue(Karta.IPPools.IPPool.t() | String.t() | nil) :: String.t()

The Oban queue of a pool's delivery jobs (by the pool or its name): "outbound_<name>"; "outbound" for nil, no pool.

remove_address(address)

@spec remove_address(Karta.IPPools.IPAddress.t()) ::
  {:ok, Karta.IPPools.IPAddress.t()} | {:error, Ecto.Changeset.t()}

Removes an address from its pool.

reverse_name(ip)

@spec reverse_name(:inet.ip_address()) :: String.t()

The name of an address's PTR record: 25.2.0.192.in-addr.arpa, or the 32 nibbles of an IPv6 address under ip6.arpa.

start_warmup(address, plan, opts \\ [])

Puts an address under a warm-up plan from the UTC day on: (default today): its first day's limit applies that day. A day in the past continues a warm-up begun elsewhere; before a day in the future the address is not used.

stop_warmup(address)

@spec stop_warmup(Karta.IPPools.IPAddress.t()) ::
  {:ok, Karta.IPPools.IPAddress.t()} | {:error, Ecto.Changeset.t()}

Ends an address's warm-up: it has no daily limit from now on.

update_pool(pool, attrs)

@spec update_pool(Karta.IPPools.IPPool.t(), map()) ::
  {:ok, Karta.IPPools.IPPool.t()} | {:error, Ecto.Changeset.t()}

Changes a pool's settings: %{concurrency: 4, destination_limits: "*.example.com connections=2"} (see Karta.IPPools.IPPool); nil returns a setting to its default.

update_warmup_plan(plan, attrs)

@spec update_warmup_plan(Karta.IPPools.WarmupPlan.t(), map()) ::
  {:ok, Karta.IPPools.WarmupPlan.t()} | {:error, Ecto.Changeset.t()}

Changes a warm-up plan's name or daily limits; the addresses under it follow the new limits from now on.

warmup_status(address, date \\ Date.utc_today())

@spec warmup_status(Karta.IPPools.IPAddress.t(), Date.t()) :: map() | nil

Where an address's warm-up stands on the UTC day date (default today): %{plan: "standard", day: 3, days: 30, limit: 200, sent: 57} (day 1 is the first; limit nil once the plan is over, 0 before it starts), or nil for an address without a plan.