XMediaLib.Stun (xmedialib v0.1.4)

Copy Markdown View Source

The XMediaLib.Stun module provides the RFC 5389 implementation of the STUN protocol for both encoding and decoding.

Summary

Functions

STUN object structure for per-connection usage

Accepts a STUN binary stream and attempts to convert it to readable packets for use by the greater application.

Accepts data and attempts to convert it to a STUN specific stream as a response to the calling client.

Accepts data and attempts to convert it to a STUN specific stream as a response to the calling client, formatted to a string.

Functions

%XMediaLib.Stun{}

(struct)

STUN object structure for per-connection usage

check_integrity(stun_binary, key)

decode(stun_binary, key \\ nil)

Accepts a STUN binary stream and attempts to convert it to readable packets for use by the greater application.

Example

  iex> request = <<0, 1, 0, 0, 33, 18, 164, 66, 0, 146, 225, 0,
  ...> 61, 62, 163, 87, 45, 150, 223, 8>>
  iex> XMediaLib.Stun.decode(request)
  {:ok, %XMediaLib.Stun{ attrs: %{}, class: :request, fingerprint: false, integrity: false, key: nil, method: :binding, ns: nil, peer_id: nil, transactionid: 177565706535525809372192520}}

encode(config)

Accepts data and attempts to convert it to a STUN specific stream as a response to the calling client.

Example

  iex> response = %Stun{class: :success, method: :binding,
  ...> transactionid: 123456789012, fingerprint: false, attrs: %{
  ...>   xor_mapped_address: {{127,0,0,1}, 12345},
  ...>   mapped_address: {{127,0,0,1}, 12345},
  ...>   source_address: {{127,0,0,1}, 12346},
  ...>   software: <<"XMediaLib-stun">>
  ...> }}
  iex> XMediaLib.Stun.encode(response)
  <<1, 1, 0, 56, 33, 18, 164, 66, 0, 0, 0, 0, 0, 0, 0, 28, 190, 153, 26, 20, 0, 1, 0, 8, 0, 1, 48, 57, 127, 0, 0, 1, 128, 34, 0, 14, 88, 77, 101, 100, 105, 97, 76, 105, 98, 45, 115, 116, 117, 110, 0, 0, 0, 4, 0, 8, 0, 1, 48, 58, 127, 0, 0, 1, 0, 32, 0, 8, 0, 1, 17, 43, 94, 18, 164, 67>>

pretty(stun_binary)

Accepts data and attempts to convert it to a STUN specific stream as a response to the calling client, formatted to a string.

Example

  iex> response = %Stun{class: :success, method: :binding,
  ...> transactionid: 123456789012, fingerprint: false, attrs: %{
  ...>   xor_mapped_address: {{127,0,0,1}, 12345},
  ...>   mapped_address: {{127,0,0,1}, 12345},
  ...>   source_address: {{127,0,0,1}, 12346},
  ...>   software: <<"XMediaLib-stun">>
  ...> }} |> Stun.encode()
  iex> XMediaLib.Stun.pretty(response)
  {:ok,
    %{
      attrs: %{
        mapped_address: "127.0.0.1:12345",
        software: "XMediaLib-stun",
        source_address: "127.0.0.1:12346",
        xor_mapped_address: "127.0.0.1:12345"
      },
      class: :success,
      method: :binding,
      transactionid: 123456789012
    }}

process_stun(stun_binary, key, fingerprint, integrity)