do not listen on IPv6 when host is 0.0.0.0 (#240)

(https://github.com/aler9/mediamtx/issues/1665)
This commit is contained in:
Alessandro Ros
2023-04-10 22:42:19 +02:00
committed by GitHub
parent 9ca5e130fe
commit c56eee37f8
7 changed files with 34 additions and 16 deletions

View File

@@ -1232,8 +1232,8 @@ func (c *Client) doSetup(
err := cm.allocateUDPListeners(
false,
":"+strconv.FormatInt(int64(rtpPort), 10),
":"+strconv.FormatInt(int64(rtcpPort), 10),
net.JoinHostPort("", strconv.FormatInt(int64(rtpPort), 10)),
net.JoinHostPort("", strconv.FormatInt(int64(rtcpPort), 10)),
)
if err != nil {
return nil, err
@@ -1377,8 +1377,8 @@ func (c *Client) doSetup(
err := cm.allocateUDPListeners(
true,
thRes.Destination.String()+":"+strconv.FormatInt(int64(thRes.Ports[0]), 10),
thRes.Destination.String()+":"+strconv.FormatInt(int64(thRes.Ports[1]), 10),
net.JoinHostPort(thRes.Destination.String(), strconv.FormatInt(int64(thRes.Ports[0]), 10)),
net.JoinHostPort(thRes.Destination.String(), strconv.FormatInt(int64(thRes.Ports[1]), 10)),
)
if err != nil {
return nil, err

View File

@@ -63,7 +63,8 @@ func (cm *clientMedia) allocateUDPListeners(multicast bool, rtpAddress string, r
cm.c.WriteTimeout,
multicast,
rtcpAddress,
cm, false)
cm,
false)
if err != nil {
l1.close()
return err

View File

@@ -322,11 +322,11 @@ func TestClientPlay(t *testing.T) {
clientPorts[i] = inTH.ClientPorts
th.ServerPorts = &[2]int{34556 + i*2, 34557 + i*2}
l1s[i], err = net.ListenPacket("udp", listenIP+":"+strconv.FormatInt(int64(th.ServerPorts[0]), 10))
l1s[i], err = net.ListenPacket("udp", net.JoinHostPort(listenIP, strconv.FormatInt(int64(th.ServerPorts[0]), 10)))
require.NoError(t, err)
defer l1s[i].Close()
l2s[i], err = net.ListenPacket("udp", listenIP+":"+strconv.FormatInt(int64(th.ServerPorts[1]), 10))
l2s[i], err = net.ListenPacket("udp", net.JoinHostPort(listenIP, strconv.FormatInt(int64(th.ServerPorts[1]), 10)))
require.NoError(t, err)
defer l2s[i].Close()

View File

@@ -49,7 +49,7 @@ func newClientUDPListenerPair(
anyPortEnable,
writeTimeout,
false,
":"+strconv.FormatInt(int64(rtpPort), 10),
net.JoinHostPort("", strconv.FormatInt(int64(rtpPort), 10)),
cm,
true)
if err != nil {
@@ -62,7 +62,7 @@ func newClientUDPListenerPair(
anyPortEnable,
writeTimeout,
false,
":"+strconv.FormatInt(int64(rtcpPort), 10),
net.JoinHostPort("", strconv.FormatInt(int64(rtcpPort), 10)),
cm,
false)
if err != nil {
@@ -90,7 +90,7 @@ func newClientUDPListener(
return nil, err
}
tmp, err := listenPacket("udp", "224.0.0.0:"+port)
tmp, err := listenPacket(restrictNetwork("udp", "224.0.0.0:"+port))
if err != nil {
return nil, err
}
@@ -116,7 +116,7 @@ func newClientUDPListener(
pc = tmp.(*net.UDPConn)
} else {
tmp, err := listenPacket("udp", address)
tmp, err := listenPacket(restrictNetwork("udp", address))
if err != nil {
return nil, err
}

17
restrict_network.go Normal file
View File

@@ -0,0 +1,17 @@
package gortsplib
import (
"net"
)
// do not listen on IPv6 when address is 0.0.0.0.
func restrictNetwork(network string, address string) (string, string) {
host, _, err := net.SplitHostPort(address)
if err == nil {
if host == "0.0.0.0" {
return network + "4", address
}
}
return network, address
}

View File

@@ -290,7 +290,7 @@ func (s *Server) Start() error {
}
var err error
s.tcpListener, err = s.Listen("tcp", s.RTSPAddress)
s.tcpListener, err = s.Listen(restrictNetwork("tcp", s.RTSPAddress))
if err != nil {
if s.udpRTPListener != nil {
s.udpRTPListener.close()

View File

@@ -60,7 +60,7 @@ func newServerUDPListenerMulticastPair(
listenPacket,
writeTimeout,
true,
ip.String()+":"+strconv.FormatInt(int64(multicastRTPPort), 10),
net.JoinHostPort(ip.String(), strconv.FormatInt(int64(multicastRTPPort), 10)),
true,
)
if err != nil {
@@ -71,7 +71,7 @@ func newServerUDPListenerMulticastPair(
listenPacket,
writeTimeout,
true,
ip.String()+":"+strconv.FormatInt(int64(multicastRTCPPort), 10),
net.JoinHostPort(ip.String(), strconv.FormatInt(int64(multicastRTCPPort), 10)),
false,
)
if err != nil {
@@ -97,7 +97,7 @@ func newServerUDPListener(
return nil, err
}
tmp, err := listenPacket("udp", "224.0.0.0:"+port)
tmp, err := listenPacket(restrictNetwork("udp", "224.0.0.0:"+port))
if err != nil {
return nil, err
}
@@ -127,7 +127,7 @@ func newServerUDPListener(
pc = tmp.(*net.UDPConn)
} else {
tmp, err := listenPacket("udp", address)
tmp, err := listenPacket(restrictNetwork("udp", address))
if err != nil {
return nil, err
}