generated from membraneframework/membrane_template_plugin
/
handshake.ex
46 lines (37 loc) · 1.09 KB
/
handshake.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
defmodule Membrane.DTLS.Handshake do
@moduledoc """
Module responsible for performing DTLS and DTLS-SRTP handshake.
`handshake_opts` in `membrane_ice_plugin` should be the same as in `t:ExDTLS.opts_t/0`.
"""
@behaviour Membrane.ICE.Handshake
alias Membrane.ICE.Handshake
require Membrane.Logger
@impl Handshake
def init(_id, _parent, opts) do
{:ok, dtls} = ExDTLS.start_link(opts)
{:ok, fingerprint} = ExDTLS.get_cert_fingerprint(dtls)
state = %{:dtls => dtls, :client_mode => opts[:client_mode]}
{:ok, fingerprint, state}
end
@impl Handshake
def connection_ready(%{client_mode: false}), do: :ok
@impl Handshake
def connection_ready(%{dtls: dtls}) do
ExDTLS.do_handshake(dtls)
end
@impl Handshake
def process(data, %{dtls: dtls}) do
case ExDTLS.process(dtls, data) do
:handshake_want_read -> :ok
other -> other
end
end
@impl Handshake
def is_hsk_packet(<<head, _rest::binary()>> = packet, _state) do
head in 20..63 and byte_size(packet) >= 13
end
@impl Handshake
def stop(%{dtls: dtls}) do
ExDTLS.stop(dtls)
end
end