Skip to content

Commit 91ebe3f

Browse files
committed
fix: use dual-stack socket instead of IPv4-only
By using an INET6 socket with IPV6_V6ONLY disabled, legacy IPv4 can still be reached. To connect to v4 IPs, the addresses are mapped to IPv6. IPv6 addresses are only used when a hostname resolves to one and the system has a non-local IPv6 available.
1 parent 29f11cc commit 91ebe3f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pychromecast/socket_client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,12 @@ def mdns_backoff(
352352
self.host,
353353
self.port,
354354
)
355-
self.socket.connect((self.host, self.port))
355+
356+
mapped_host = socket.getaddrinfo(self.host, self.port,
357+
socket.AF_INET6, socket.SOCK_STREAM,
358+
flags=(socket.AI_ADDRCONFIG | socket.AI_V4MAPPED))[0][4]
359+
360+
self.socket.connect(mapped_host)
356361
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
357362
context.check_hostname = False
358363
context.verify_mode = ssl.CERT_NONE
@@ -1135,7 +1140,8 @@ def new_socket() -> socket.socket:
11351140
Try to set SO_REUSEPORT for BSD-flavored systems if it's an option.
11361141
Catches errors if not.
11371142
"""
1138-
new_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1143+
new_sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
1144+
new_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
11391145
new_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
11401146

11411147
try:

0 commit comments

Comments
 (0)