Refactor duplicative code re #110

This commit is contained in:
Jeff Forcier 2013-02-27 19:54:22 -08:00
parent b9242c654a
commit 3563fca994
1 changed files with 9 additions and 17 deletions

View File

@ -25,6 +25,7 @@ import os
import re import re
import socket import socket
SSH_PORT = 22 SSH_PORT = 22
proxy_re = re.compile(r"^(proxycommand)\s*=*\s*(.*)", re.I) proxy_re = re.compile(r"^(proxycommand)\s*=*\s*(.*)", re.I)
@ -48,30 +49,21 @@ class LazyFqdn(object):
# IPv4 and IPv6 are checked. # IPv4 and IPv6 are checked.
# #
# Handle specific option
address_family = self.config.get('addressfamily', 'any').lower() address_family = self.config.get('addressfamily', 'any').lower()
if address_family != 'any':
if address_family == 'inet': family = socket.AF_INET if address_family == 'inet' else socket.AF_INET6
ipv4_results = socket.getaddrinfo(host, None, socket.AF_INET, results = socket.getaddrinfo(host, None, family,
socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME)
socket.AI_CANONNAME) for res in results:
for res in ipv4_results:
af, socktype, proto, canonname, sa = res af, socktype, proto, canonname, sa = res
if canonname and '.' in canonname: if canonname and '.' in canonname:
fqdn = canonname fqdn = canonname
break break
elif address_family == 'inet6': # Handle 'any' / unspecified
ipv6_results = socket.getaddrinfo(host, None, socket.AF_INET6,
socket.SOCK_DGRAM,
socket.IPPROTO_IP,
socket.AI_CANONNAME)
for res in ipv6_results:
af, socktype, proto, canonname, sa = res
if canonname and '.' in canonname:
fqdn = canonname
break
if fqdn is None: if fqdn is None:
fqdn = socket.getfqdn() fqdn = socket.getfqdn()
# Cache
self.fqdn = fqdn self.fqdn = fqdn
return self.fqdn return self.fqdn