-
Notifications
You must be signed in to change notification settings - Fork 6
Allow for defining public ips for 1:1 NAT #87
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| defmodule ExICE.Priv.NATMapper do | ||
| @moduledoc false | ||
|
|
||
| require Logger | ||
|
|
||
| alias ExICE.ICEAgent | ||
| alias ExICE.Priv.Candidate | ||
|
|
||
| @spec create_srflx_candidates([Candidate.Host.t()], ICEAgent.map_to_nat_ip(), %{ | ||
| :inet.ip_address() => non_neg_integer() | ||
| }) :: [Candidate.Srflx.t()] | ||
| def create_srflx_candidates(_host_cands, nil, _local_preferences) do | ||
| [] | ||
| end | ||
|
|
||
| def create_srflx_candidates(host_cands, map_to_nat_ip, local_preferences) do | ||
| {cands, _external_ips} = | ||
| Enum.reduce(host_cands, {[], []}, fn host_cand, {cands, external_ips} -> | ||
mickel8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| external_ip = map_to_nat_ip.(host_cand.base.address) | ||
|
|
||
| if valid_external_ip?(external_ip, host_cand.base.address, external_ips) do | ||
| priority = | ||
| Candidate.priority!(local_preferences, host_cand.base.address, :srflx) | ||
|
|
||
| cand = | ||
| Candidate.Srflx.new( | ||
| address: external_ip, | ||
| port: host_cand.base.port, | ||
| base_address: host_cand.base.address, | ||
| base_port: host_cand.base.port, | ||
| priority: priority, | ||
| transport_module: host_cand.base.transport_module, | ||
| socket: host_cand.base.socket | ||
| ) | ||
|
|
||
| Logger.debug("New srflx candidate from NAT mapping: #{inspect(cand)}") | ||
|
|
||
| {[cand | cands], [external_ip | external_ips]} | ||
| else | ||
| {cands, external_ips} | ||
| end | ||
| end) | ||
|
|
||
| cands | ||
| end | ||
|
|
||
| defp valid_external_ip?(external_ip, host_ip, external_ips) do | ||
| same_type? = :inet.is_ipv4_address(external_ip) == :inet.is_ipv4_address(host_ip) | ||
|
|
||
| cond do | ||
| host_ip == external_ip -> | ||
| log_warning(host_ip, external_ip, "external IP is the same as local IP") | ||
| false | ||
|
|
||
| not :inet.is_ip_address(external_ip) or not same_type? -> | ||
| log_warning(host_ip, external_ip, "not valid IP address") | ||
| false | ||
|
|
||
| external_ip in external_ips -> | ||
| log_warning(host_ip, external_ip, "address already in use") | ||
| false | ||
|
|
||
| true -> | ||
| true | ||
| end | ||
| end | ||
|
|
||
| defp log_warning(host_ip, external_ip, reason), | ||
| do: | ||
| Logger.warning( | ||
| "Ignoring NAT mapping: #{inspect(host_ip)} to #{inspect(external_ip)}, #{inspect(reason)}" | ||
| ) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2631,6 +2631,127 @@ defmodule ExICE.Priv.ICEAgentTest do | |
| end | ||
| end | ||
|
|
||
| describe "NAT mapping" do | ||
mickel8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| alias ExICE.Priv.Candidate | ||
|
|
||
| @ipv4 {10, 10, 10, 10} | ||
| @ipv6 {0, 0, 0, 0, 0, 0, 0, 1} | ||
mickel8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @invalid_ip :invalid_ip | ||
|
|
||
| test "adds srflx candidate" do | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, all tests except for the first one and the last one could be moved to gatherer_test.exs |
||
| ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn _ip -> @ipv4 end) | ||
|
|
||
| assert [%Candidate.Srflx{base: %{address: @ipv4}}] = srflx_candidates(ice_agent) | ||
|
|
||
| assert_receive {:ex_ice, _pid, {:new_candidate, host_cand}} | ||
| assert_receive {:ex_ice, _pid, {:new_candidate, srflx_cand}} | ||
|
|
||
| assert host_cand =~ "typ host" | ||
| assert srflx_cand =~ "typ srflx" | ||
| end | ||
|
|
||
| test "creates only one candidate if external ip repeats itself" do | ||
mickel8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ice_agent = spawn_ice_agent(IfDiscovery.MockMulti, fn _ip -> @ipv4 end) | ||
|
|
||
| assert [%Candidate.Srflx{base: %{address: @ipv4}}] = srflx_candidates(ice_agent) | ||
|
|
||
| assert_receive {:ex_ice, _pid, {:new_candidate, host_cand}} | ||
| assert_receive {:ex_ice, _pid, {:new_candidate, host_cand_2}} | ||
| assert_receive {:ex_ice, _pid, {:new_candidate, srflx_cand}} | ||
|
|
||
| assert host_cand =~ "typ host" | ||
| assert host_cand_2 =~ "typ host" | ||
| assert srflx_cand =~ "typ srflx" | ||
| end | ||
|
|
||
| test "ignores one to one mapping" do | ||
| ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn ip -> ip end) | ||
|
|
||
| assert [] == srflx_candidates(ice_agent) | ||
|
|
||
| assert_receive {:ex_ice, _pid, {:new_candidate, host_cand}} | ||
| refute_receive {:ex_ice, _pid, {:new_candidate, _srflx_cand}} | ||
|
|
||
| assert host_cand =~ "typ host" | ||
| end | ||
|
|
||
| test "ignores if ip types is not the same" do | ||
| ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn _ip -> @ipv6 end) | ||
|
|
||
| assert [] == srflx_candidates(ice_agent) | ||
| end | ||
|
|
||
| test "ignores when function returns nil value" do | ||
| ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn _ip -> nil end) | ||
|
|
||
| assert [] == srflx_candidates(ice_agent) | ||
| end | ||
|
|
||
| test "ignores when function returns invalid value" do | ||
| ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn _ip -> @invalid_ip end) | ||
|
|
||
| assert [] == srflx_candidates(ice_agent) | ||
| end | ||
|
|
||
| test "works with STUN enabled" do | ||
| ice_agent = | ||
| ICEAgent.new( | ||
| controlling_process: self(), | ||
| role: :controlled, | ||
| transport_module: Transport.Mock, | ||
| if_discovery_module: IfDiscovery.MockSingle, | ||
| ice_servers: [%{urls: "stun:192.168.0.3:19302"}], | ||
| map_to_nat_ip: fn _ip -> @ipv4 end | ||
| ) | ||
| |> ICEAgent.set_remote_credentials("remoteufrag", "remotepwd") | ||
| |> ICEAgent.gather_candidates() | ||
|
|
||
| [%Candidate.Srflx{base: %{address: @ipv4, port: srflx_port}}] = srflx_candidates(ice_agent) | ||
|
|
||
| [socket] = ice_agent.sockets | ||
|
|
||
| # assert no transactions are started until handle_ta_timeout is called | ||
| assert nil == Transport.Mock.recv(socket) | ||
|
|
||
| # assert ice agent started gathering transaction by sending a binding request | ||
| ice_agent = ICEAgent.handle_ta_timeout(ice_agent) | ||
| assert packet = Transport.Mock.recv(socket) | ||
| assert {:ok, req} = ExSTUN.Message.decode(packet) | ||
| assert req.type.class == :request | ||
| assert req.type.method == :binding | ||
|
|
||
| resp = | ||
| Message.new(req.transaction_id, %Type{class: :success_response, method: :binding}, [ | ||
| %XORMappedAddress{address: @ipv4, port: srflx_port} | ||
| ]) | ||
| |> Message.encode() | ||
|
|
||
| ice_agent = ICEAgent.handle_udp(ice_agent, socket, @stun_ip, @stun_port, resp) | ||
|
|
||
| # assert there isn't new srflx candidate | ||
| assert [%Candidate.Srflx{}] = srflx_candidates(ice_agent) | ||
| end | ||
|
|
||
| defp spawn_ice_agent(discovery_module, map_to_nat_ip) do | ||
| %ICEAgent{gathering_state: :complete} = | ||
| ICEAgent.new( | ||
| controlling_process: self(), | ||
| role: :controlled, | ||
| transport_module: Transport.Mock, | ||
| if_discovery_module: discovery_module, | ||
| map_to_nat_ip: map_to_nat_ip | ||
| ) | ||
| |> ICEAgent.set_remote_credentials("remoteufrag", "remotepwd") | ||
| |> ICEAgent.gather_candidates() | ||
| end | ||
|
|
||
| defp srflx_candidates(ice_agent) do | ||
| ice_agent.local_cands | ||
| |> Map.values() | ||
| |> Enum.filter(&(&1.base.type == :srflx)) | ||
| end | ||
| end | ||
|
|
||
| test "relay ice_transport_policy" do | ||
| ice_agent = | ||
| ICEAgent.new( | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.