Skip to content

Add basic keepalives #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/candidate_pair.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ defmodule ExICE.CandidatePair do
alias ExICE.ICEAgent
alias ExICE.Candidate

# Tr timeout (keepalives) in ms
@tr_timeout 15 * 1000

@type state() :: :waiting | :in_progress | :succeeded | :failed | :frozen

@type t() :: %__MODULE__{
Expand All @@ -19,7 +22,8 @@ defmodule ExICE.CandidatePair do
state: state(),
valid?: boolean,
succeeded_pair_id: integer() | nil,
discovered_pair_id: integer() | nil
discovered_pair_id: integer() | nil,
keepalive_timer: reference() | nil
}

@enforce_keys [:id, :local_cand, :remote_cand, :priority]
Expand All @@ -30,7 +34,8 @@ defmodule ExICE.CandidatePair do
state: :frozen,
valid?: false,
succeeded_pair_id: nil,
discovered_pair_id: nil
discovered_pair_id: nil,
keepalive_timer: nil
]

@doc false
Expand All @@ -50,6 +55,19 @@ defmodule ExICE.CandidatePair do
}
end

@spec schedule_keepalive(t(), Process.dest()) :: :ok
def schedule_keepalive(pair, dest \\ self())

def schedule_keepalive(%{keepalive_timer: timer} = pair, dest) when is_reference(timer) do
Process.cancel_timer(timer)
schedule_keepalive(%{pair | keepalive_timer: nil}, dest)
end

def schedule_keepalive(pair, dest) do
ref = Process.send_after(dest, {:keepalive, pair.id}, @tr_timeout)
%{pair | keepalive_timer: ref}
end

@spec recompute_priority(t(), ICEAgent.role()) :: t()
def recompute_priority(pair, role) do
%__MODULE__{pair | priority: priority(role, pair.local_cand, pair.remote_cand)}
Expand Down
51 changes: 51 additions & 0 deletions lib/ice_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,42 @@ defmodule ExICE.ICEAgent do
end
end

@impl true
def handle_info({:keepalive, id}, %{selected_pair: s_pair} = state)
when not is_nil(s_pair) and s_pair.id == id do
# if pair was selected, send keepalives only on that pair
pair = CandidatePair.schedule_keepalive(s_pair)
send_keepalive(state.checklist[id])
state = put_in(state.checklist[id], pair)
{:noreply, state}
end

@impl true
def handle_info({:keepalive, _id}, %{selected_pair: s_pair} = state) when not is_nil(s_pair) do
# note: current implementation assumes that, if selected pair exists, none of the already existing
# valid pairs will ever become selected (only new appearing valid pairs)
# that's why there's no call to `CandidatePair.schedule_keepalive/1`
{:noreply, state}
end

@impl true
def handle_info({:keepalive, id}, state) do
# TODO: keepalives should be send only if no data has been send for @tr_timeout
# atm, we send keepalives anyways, also it might be better to pace them with ta_timer
# TODO: candidates not in a valid pair also should be kept alive (RFC 8445, sect 5.1.1.4)
case Map.fetch(state.checklist, id) do
{:ok, pair} ->
pair = CandidatePair.schedule_keepalive(pair)
state = put_in(state.checklist[id], pair)
send_keepalive(pair)
{:noreply, state}

:error ->
Logger.warning("Received keepalive request for non-existant candidate pair")
{:noreply, state}
end
end

@impl true
def handle_info({:udp, socket, src_ip, src_port, packet}, state) do
if ExSTUN.is_stun(packet) do
Expand Down Expand Up @@ -774,6 +810,9 @@ defmodule ExICE.ICEAgent do

{pair_id, state} = add_valid_pair(valid_pair, conn_check_pair, checklist_pair, state)

pair = CandidatePair.schedule_keepalive(state.checklist[pair_id])
state = put_in(state.checklist[pair_id], pair)

# get new conn check pair as it will have updated
# discovered and succeeded pair fields
conn_check_pair = Map.fetch!(state.checklist, conn_check_pair.id)
Expand Down Expand Up @@ -1445,6 +1484,18 @@ defmodule ExICE.ICEAgent do
%{state | ta_timer: nil}
end

defp send_keepalive(pair) do
type = %Type{class: :indication, method: :binding}

req =
type
|> Message.new()
|> Message.with_fingerprint()

dst = {pair.remote_cand.address, pair.remote_cand.port}
do_send(pair.local_cand.socket, dst, Message.encode(req))
end

@doc false
@spec send_conn_check(CandidatePair.t(), map()) :: {CandidatePair.t(), map()}
def send_conn_check(pair, state) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule ExICE.MixProject do
defp deps do
[
{:ex_stun, "~> 0.1.0"},
{:excoveralls, "~> 0.14.6", only: :test, runtime: false},
{:excoveralls, "~> 0.15", only: :test, runtime: false},
{:ex_doc, "~> 0.27", only: :dev, runtime: false},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false}
]
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"earmark_parser": {:hex, :earmark_parser, "1.4.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"},
"ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"},
"ex_stun": {:hex, :ex_stun, "0.1.0", "252474bf4c8519fbf4bc0fbfc6a1b846a634b1478c65dbbfb4b6ab4e33c2a95a", [:mix], [], "hexpm", "629fc8be45b624a92522f81d85ba001877b1f0745889a2419bdb678790d7480c"},
"excoveralls": {:hex, :excoveralls, "0.14.6", "610e921e25b180a8538229ef547957f7e04bd3d3e9a55c7c5b7d24354abbba70", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "0eceddaa9785cfcefbf3cd37812705f9d8ad34a758e513bb975b081dce4eb11e"},
"excoveralls": {:hex, :excoveralls, "0.17.0", "279f124dba347903bb654bc40745c493ae265d45040001b4899ea1edf88078c7", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "08b638d114387a888f9cb8d65f2a0021ec04c3e447b793efa7c1e734aba93004"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
Expand Down