Skip to content

Commit

Permalink
fix: use dual-stack socket instead of IPv4-only
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DerEnderKeks committed Feb 27, 2024
1 parent c162144 commit 1cc0a30
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pychromecast/socket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,16 @@ def mdns_backoff(
self.host,
self.port,
)
self.socket.connect((self.host, self.port))

mapped_host = socket.getaddrinfo(
self.host,
self.port,
socket.AF_INET6,
socket.SOCK_STREAM,
flags=(socket.AI_ADDRCONFIG | socket.AI_V4MAPPED)
)[0][4]

self.socket.connect(mapped_host)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
Expand Down Expand Up @@ -1135,7 +1144,8 @@ def new_socket() -> socket.socket:
Try to set SO_REUSEPORT for BSD-flavored systems if it's an option.
Catches errors if not.
"""
new_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
new_sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
new_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
new_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

try:
Expand Down

0 comments on commit 1cc0a30

Please sign in to comment.