@@ -943,63 +943,92 @@ defmodule ExICE.Priv.ICEAgent do
943943
944944 def handle_ex_turn_msg ( ice_agent , client_ref , msg ) do
945945 tr_id_tr = find_gathering_transaction ( ice_agent . gathering_transactions , client_ref )
946-
947946 cand = find_relay_cand_by_client ( Map . values ( ice_agent . local_cands ) , client_ref )
948947
949948 case { tr_id_tr , cand } do
950- { nil , nil } ->
951- ice_agent
949+ { nil , nil } -> ice_agent
950+ { { tr_id , tr } , nil } -> handle_ex_turn_msg_for_gathering_tx ( ice_agent , tr_id , tr , msg )
951+ { nil , cand } -> handle_ex_turn_msg_for_relay_cand ( ice_agent , cand , msg )
952+ end
953+ end
952954
953- { { tr_id , tr } , nil } ->
954- case ExTURN.Client . handle_message ( tr . client , msg ) do
955- { :ok , client } ->
956- tr = % { tr | client: client }
957- put_in ( ice_agent . gathering_transactions [ tr_id ] , tr )
955+ defp handle_ex_turn_msg_for_gathering_tx ( ice_agent , tr_id , tr , msg ) do
956+ case ExTURN.Client . handle_message ( tr . client , msg ) do
957+ { :ok , client } ->
958+ put_in ( ice_agent . gathering_transactions [ tr_id ] , % { tr | client: client } )
958959
959- { :send , dst , data , client } ->
960- tr = % { tr | client: client }
961- :ok = ice_agent . transport_module . send ( tr . socket , dst , data )
962- put_in ( ice_agent . gathering_transactions [ tr_id ] , tr )
960+ { :send , dst , data , client } ->
961+ send_on_gathering_tx ( ice_agent , tr_id , % { tr | client: client } , dst , data )
963962
964- { :error , _reason , _client } ->
965- { _ , ice_agent } = pop_in ( ice_agent . gathering_transactions [ tr_id ] )
966- update_gathering_state ( ice_agent )
967- end
963+ { :error , _reason , _client } ->
964+ drop_gathering_transaction ( ice_agent , tr_id )
965+ end
966+ end
968967
969- { nil , cand } ->
970- case ExTURN.Client . handle_message ( cand . client , msg ) do
971- { :ok , client } ->
972- cand = % { cand | client: client }
973- put_in ( ice_agent . local_cands [ cand . base . id ] , cand )
974-
975- { :send , dst , data , client } ->
976- cand = % { cand | client: client }
977- ice_agent = put_in ( ice_agent . local_cands [ cand . base . id ] , cand )
978- # we can't use do_send here as it will try to create permission for the turn address
979- :ok = ice_agent . transport_module . send ( cand . base . socket , dst , data )
980- ice_agent
968+ defp send_on_gathering_tx ( ice_agent , tr_id , tr , dst , data ) do
969+ case ice_agent . transport_module . send ( tr . socket , dst , data ) do
970+ :ok ->
971+ put_in ( ice_agent . gathering_transactions [ tr_id ] , tr )
972+
973+ { :error , _reason } ->
974+ drop_gathering_transaction ( ice_agent , tr_id )
975+ end
976+ end
977+
978+ defp drop_gathering_transaction ( ice_agent , tr_id ) do
979+ { _ , ice_agent } = pop_in ( ice_agent . gathering_transactions [ tr_id ] )
980+ update_gathering_state ( ice_agent )
981+ end
981982
982- { :permission_expired , _ip , client } ->
983- cand = % { cand | client: client }
984- put_in ( ice_agent . local_cands [ cand . base . id ] , cand )
983+ defp handle_ex_turn_msg_for_relay_cand ( ice_agent , cand , msg ) do
984+ case ExTURN.Client . handle_message ( cand . client , msg ) do
985+ { :ok , client } ->
986+ update_relay_cand_client ( ice_agent , cand , client )
985987
986- { :channel_expired , _addr , client } ->
987- cand = % { cand | client: client }
988- put_in ( ice_agent . local_cands [ cand . base . id ] , cand )
988+ { :send , dst , data , client } ->
989+ send_on_relay_cand ( ice_agent , cand , client , dst , data )
989990
990- { :error , _reason , client } ->
991- Logger . debug ( """
992- Couldn't handle TURN message on candidate: #{ inspect ( cand ) } . \
993- Closing candidate.\
994- """ )
991+ { :permission_expired , _ip , client } ->
992+ update_relay_cand_client ( ice_agent , cand , client )
995993
996- cand = % { cand | client: client }
997- ice_agent = put_in ( ice_agent . local_cands [ cand . base . id ] , cand )
998- close_candidate ( ice_agent , cand )
999- end
994+ { :channel_expired , _addr , client } ->
995+ update_relay_cand_client ( ice_agent , cand , client )
996+
997+ { :error , _reason , client } ->
998+ Logger . debug ( """
999+ Couldn't handle TURN message on candidate: #{ inspect ( cand ) } . \
1000+ Closing candidate.\
1001+ """ )
1002+
1003+ ice_agent
1004+ |> update_relay_cand_client ( cand , client )
1005+ |> close_candidate ( cand )
10001006 end
10011007 end
10021008
1009+ # we can't use do_send here as it will try to create permission for the turn address
1010+ defp send_on_relay_cand ( ice_agent , cand , client , dst , data ) do
1011+ cand = % { cand | client: client }
1012+ ice_agent = put_in ( ice_agent . local_cands [ cand . base . id ] , cand )
1013+
1014+ case ice_agent . transport_module . send ( cand . base . socket , dst , data ) do
1015+ :ok ->
1016+ ice_agent
1017+
1018+ { :error , _reason } ->
1019+ Logger . debug ( """
1020+ Couldn't send TURN message on candidate: #{ inspect ( cand ) } . \
1021+ Closing candidate.\
1022+ """ )
1023+
1024+ close_candidate ( ice_agent , cand )
1025+ end
1026+ end
1027+
1028+ defp update_relay_cand_client ( ice_agent , cand , client ) do
1029+ put_in ( ice_agent . local_cands [ cand . base . id ] , % { cand | client: client } )
1030+ end
1031+
10031032 @ spec close ( t ( ) ) :: t ( )
10041033 def close ( % __MODULE__ { state: :closed } = ice_agent ) do
10051034 ice_agent
@@ -1283,8 +1312,16 @@ defmodule ExICE.Priv.ICEAgent do
12831312
12841313 { :send , turn_addr , data , client } ->
12851314 tr = % { tr | client: client }
1286- :ok = ice_agent . transport_module . send ( tr . socket , turn_addr , data )
1287- put_in ( ice_agent . gathering_transactions [ tr_id ] , tr )
1315+
1316+ case ice_agent . transport_module . send ( tr . socket , turn_addr , data ) do
1317+ :ok ->
1318+ put_in ( ice_agent . gathering_transactions [ tr_id ] , tr )
1319+
1320+ { :error , _reason } ->
1321+ Logger . debug ( "Failed to create TURN allocation." )
1322+ { _ , ice_agent } = pop_in ( ice_agent . gathering_transactions [ tr_id ] )
1323+ ice_agent
1324+ end
12881325
12891326 { :error , _reason , _client } ->
12901327 Logger . debug ( "Failed to create TURN allocation." )
@@ -2019,16 +2056,27 @@ defmodule ExICE.Priv.ICEAgent do
20192056 ice_agent
20202057 end
20212058
2022- defp handle_keepalive_response (
2059+ defp handle_keepalive_response ( ice_agent , local_cand , src_ip , src_port , msg ) do
2060+ { pair_id , ice_agent } = pop_in ( ice_agent . keepalives [ msg . transaction_id ] )
2061+
2062+ case Map . fetch ( ice_agent . checklist , pair_id ) do
2063+ { :ok , % CandidatePair { } = pair } ->
2064+ handle_keepalive_response_on_pair ( ice_agent , local_cand , src_ip , src_port , msg , pair )
2065+
2066+ :error ->
2067+ Logger . debug ( "Ignoring keepalive response for pruned pair #{ inspect ( pair_id ) } " )
2068+ ice_agent
2069+ end
2070+ end
2071+
2072+ defp handle_keepalive_response_on_pair (
20232073 ice_agent ,
20242074 local_cand ,
20252075 src_ip ,
20262076 src_port ,
2027- % Message { type: % Type { class: :success_response } } = msg
2077+ % Message { type: % Type { class: :success_response } } = msg ,
2078+ pair
20282079 ) do
2029- { pair_id , ice_agent } = pop_in ( ice_agent . keepalives [ msg . transaction_id ] )
2030- % CandidatePair { } = pair = Map . fetch! ( ice_agent . checklist , pair_id )
2031-
20322080 with true <- symmetric? ( ice_agent , local_cand . base . socket , { src_ip , src_port } , pair ) ,
20332081 :ok <- authenticate_msg ( msg , ice_agent . remote_pwd ) do
20342082 Logger . debug ( "Received keepalive success response on: #{ pair_info ( ice_agent , pair ) } " )
@@ -2045,7 +2093,7 @@ defmodule ExICE.Priv.ICEAgent do
20452093 ka_local_cand = Map . fetch! ( ice_agent . local_cands , pair . local_cand_id )
20462094 ka_remote_cand = Map . fetch! ( ice_agent . remote_cands , pair . remote_cand_id )
20472095
2048- pair = % CandidatePair {
2096+ pair = % {
20492097 pair
20502098 | non_symmetric_responses_received: pair . non_symmetric_responses_received + 1
20512099 }
@@ -2068,20 +2116,19 @@ defmodule ExICE.Priv.ICEAgent do
20682116 Not refreshing last_seen time.\
20692117 """ )
20702118
2071- pair = % CandidatePair { pair | responses_received: pair . responses_received + 1 }
2119+ pair = % { pair | responses_received: pair . responses_received + 1 }
20722120 put_in ( ice_agent . checklist [ pair . id ] , pair )
20732121 end
20742122 end
20752123
2076- defp handle_keepalive_response (
2124+ defp handle_keepalive_response_on_pair (
20772125 ice_agent ,
20782126 local_cand ,
20792127 src_ip ,
20802128 src_port ,
2081- % Message { type: % Type { class: :error_response } } = msg
2129+ % Message { type: % Type { class: :error_response } } ,
2130+ pair
20822131 ) do
2083- { pair_id , ice_agent } = pop_in ( ice_agent . keepalives [ msg . transaction_id ] )
2084- % CandidatePair { } = pair = Map . fetch! ( ice_agent . checklist , pair_id )
20852132 pair = % { pair | responses_received: pair . responses_received + 1 }
20862133 ice_agent = put_in ( ice_agent . checklist [ pair . id ] , pair )
20872134
0 commit comments