@@ -25,6 +25,12 @@ const (
2525 stunGatherTimeout = time .Second * 5
2626)
2727
28+ type connAndPort struct {
29+ conn net.PacketConn
30+ port int
31+ tcpType TCPType
32+ }
33+
2834// Close a net.Conn and log if we have a failure
2935func closeConnAndLog (c io.Closer , log logging.LeveledLogger , msg string , args ... interface {}) {
3036 if c == nil || (reflect .ValueOf (c ).Kind () == reflect .Ptr && reflect .ValueOf (c ).IsNil ()) {
@@ -155,53 +161,21 @@ func (a *Agent) gatherCandidatesLocal(ctx context.Context, networkTypes []Networ
155161 }
156162
157163 for network := range networks {
158- type connAndPort struct {
159- conn net.PacketConn
160- port int
161- }
162- var (
163- conns []connAndPort
164- tcpType TCPType
165- )
164+ var conns []connAndPort
166165
167166 switch network {
168167 case tcp :
169- if a .tcpMux == nil {
170- continue
171- }
168+ // Handle ICE TCP active mode
169+ conns = append (conns , connAndPort {nil , 0 , TCPTypeActive })
172170
173171 // Handle ICE TCP passive mode
174- var muxConns []net.PacketConn
175- if multi , ok := a .tcpMux .(AllConnsGetter ); ok {
176- a .log .Debugf ("GetAllConns by ufrag: %s" , a .localUfrag )
177- muxConns , err = multi .GetAllConns (a .localUfrag , mappedIP .To4 () == nil , ip )
178- if err != nil {
179- a .log .Warnf ("Failed to get all TCP connections by ufrag: %s %s %s" , network , ip , a .localUfrag )
180- continue
181- }
182- } else {
183- a .log .Debugf ("GetConn by ufrag: %s" , a .localUfrag )
184- conn , err := a .tcpMux .GetConnByUfrag (a .localUfrag , mappedIP .To4 () == nil , ip )
185- if err != nil {
186- a .log .Warnf ("Failed to get TCP connections by ufrag: %s %s %s" , network , ip , a .localUfrag )
187- continue
188- }
189- muxConns = []net.PacketConn {conn }
190- }
191-
192- // Extract the port for each PacketConn we got.
193- for _ , conn := range muxConns {
194- if tcpConn , ok := conn .LocalAddr ().(* net.TCPAddr ); ok {
195- conns = append (conns , connAndPort {conn , tcpConn .Port })
196- } else {
197- a .log .Warnf ("Failed to get port of connection from TCPMux: %s %s %s" , network , ip , a .localUfrag )
198- }
172+ if a .tcpMux != nil {
173+ conns = a .getTCPMuxConnections (mappedIP , ip , network , conns )
199174 }
200175 if len (conns ) == 0 {
201176 // Didn't succeed with any, try the next network.
202177 continue
203178 }
204- tcpType = TCPTypePassive
205179 // Is there a way to verify that the listen address is even
206180 // accessible from the current interface.
207181 case udp :
@@ -212,7 +186,7 @@ func (a *Agent) gatherCandidatesLocal(ctx context.Context, networkTypes []Networ
212186 }
213187
214188 if udpConn , ok := conn .LocalAddr ().(* net.UDPAddr ); ok {
215- conns = append (conns , connAndPort {conn , udpConn .Port })
189+ conns = append (conns , connAndPort {conn , udpConn .Port , TCPTypeUnspecified })
216190 } else {
217191 a .log .Warnf ("Failed to get port of UDPAddr from ListenUDPInPortRange: %s %s %s" , network , ip , a .localUfrag )
218192 continue
@@ -225,7 +199,7 @@ func (a *Agent) gatherCandidatesLocal(ctx context.Context, networkTypes []Networ
225199 Address : address ,
226200 Port : connAndPort .port ,
227201 Component : ComponentRTP ,
228- TCPType : tcpType ,
202+ TCPType : connAndPort . tcpType ,
229203 }
230204
231205 c , err := NewCandidateHost (& hostConfig )
@@ -252,6 +226,37 @@ func (a *Agent) gatherCandidatesLocal(ctx context.Context, networkTypes []Networ
252226 }
253227}
254228
229+ func (a * Agent ) getTCPMuxConnections (mappedIP net.IP , ip net.IP , network string , conns []connAndPort ) []connAndPort {
230+ var muxConns []net.PacketConn
231+ if multi , ok := a .tcpMux .(AllConnsGetter ); ok {
232+ a .log .Debugf ("GetAllConns by ufrag: %s" , a .localUfrag )
233+ var err error
234+ muxConns , err = multi .GetAllConns (a .localUfrag , mappedIP .To4 () == nil , ip )
235+ if err != nil {
236+ a .log .Warnf ("Failed to get all TCP connections by ufrag: %s %s %s" , network , ip , a .localUfrag )
237+ return conns
238+ }
239+ } else {
240+ a .log .Debugf ("GetConn by ufrag: %s" , a .localUfrag )
241+ conn , err := a .tcpMux .GetConnByUfrag (a .localUfrag , mappedIP .To4 () == nil , ip )
242+ if err != nil {
243+ a .log .Warnf ("Failed to get TCP connections by ufrag: %s %s %s" , network , ip , a .localUfrag )
244+ return conns
245+ }
246+ muxConns = []net.PacketConn {conn }
247+ }
248+
249+ // Extract the port for each PacketConn we got.
250+ for _ , conn := range muxConns {
251+ if tcpConn , ok := conn .LocalAddr ().(* net.TCPAddr ); ok {
252+ conns = append (conns , connAndPort {conn , tcpConn .Port , TCPTypePassive })
253+ } else {
254+ a .log .Warnf ("Failed to get port of connection from TCPMux: %s %s %s" , network , ip , a .localUfrag )
255+ }
256+ }
257+ return conns
258+ }
259+
255260func (a * Agent ) gatherCandidatesLocalUDPMux (ctx context.Context ) error { //nolint:gocognit
256261 if a .udpMux == nil {
257262 return errUDPMuxDisabled
0 commit comments