Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPv4 vs IPv6 selector upon SSHServer initiation #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions sshutil/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,12 @@ def __init__ (self,
extra_args=None,
port=None,
host_key=None,
debug=False):

debug=False,
ip_version='IPv4'):
if ip_version == 'IPv6':
proto_tuple = [("IPv6", '::', socket.AF_INET6)]
else:
proto_tuple = [("IPv4", '', socket.AF_INET)]
if server_ctl is None:
server_ctl = SSHUserPassController()
self.server_ctl = server_ctl
Expand Down Expand Up @@ -358,10 +362,9 @@ def __init__ (self,
if os.path.exists(keypath):
self.host_key = ssh.RSAKey.from_private_key_file(keypath)
break

# Bind first to IPv6, if the OS supports binding per AF then the IPv4
# will succeed, otherwise the IPv6 will support both AF.
for pname, host, proto in [ ("IPv6", '::', socket.AF_INET6), ("IPv4", '', socket.AF_INET) ]:
for pname, host, proto in proto_tuple:
protosocket = socket.socket(proto, socket.SOCK_STREAM)
protosocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if self.debug:
Expand Down