Skip to content

Commit

Permalink
Fix IPv6 binding to any IP
Browse files Browse the repository at this point in the history
  • Loading branch information
danijar committed Sep 16, 2024
1 parent 1deb57c commit c77f893
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion portal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.2.4'
__version__ = '3.3.0'

import multiprocessing as mp
try:
Expand Down
4 changes: 2 additions & 2 deletions portal/server_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def __init__(self, port, name='Server', **kwargs):
self.options = Options(**{**contextlib.context.serverkw, **kwargs})
if self.options.ipv6:
self.sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
self.addr = (self.options.host, port, 0, 0)
self.addr = (self.options.host or '::', port, 0, 0)
else:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.addr = (self.options.host, port)
self.addr = (self.options.host or '0.0.0.0', port)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self._log(f'Binding to {self.addr[0]}:{self.addr[1]}')
Expand Down
8 changes: 5 additions & 3 deletions portal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ def free_port():
# Return a port that is currently free. This function is not thread or
# process safe, because there is no way to guarantee that the port will still
# be free at the time it will be used.
if contextlib.context.serverkw.get('ipv6', False):
family, addr = socket.AF_INET6, ('localhost', 0, 0, 0)
ipv6 = contextlib.context.serverkw.get('ipv6', False)
host = contextlib.context.serverkw.get('host', '')
if ipv6:
family, addr = socket.AF_INET6, (host or '::', 0, 0, 0)
else:
family, addr = socket.AF_INET, ('localhost', 0)
family, addr = socket.AF_INET, (host or '0.0.0.0', 0)
sock = socket.socket(family, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(addr)
Expand Down

0 comments on commit c77f893

Please sign in to comment.