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

Here are a few fixes utils.network #31196

Merged
merged 2 commits into from
Feb 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions salt/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,14 +973,17 @@ def ip_addrs6(interface=None, include_loopback=False, interface_data=None):
def hex2ip(hex_ip, invert=False):
'''
Convert a hex string to an ip, if a failure occurs the original hex is
returned
returned. If 'invert=True' assume that ip from /proc/net/<proto>
'''
if len(hex_ip) == 32: # ipv6
ip = []
for i in range(0, 32, 8):
ip_part = hex_ip[i:i + 8]
ip_part = [ip_part[x:x + 2] for x in range(0, 8, 2)]
ip.append("{0[3]}{0[2]}:{0[1]}{0[0]}".format(ip_part))
if invert:
ip.append("{0[3]}{0[2]}:{0[1]}{0[0]}".format(ip_part))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the default for invert is False, doesn't this imply a change in behavior here? Is this a bug fix or was this unintentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!
I looked at all of the code and all calls made with hex2ip invert=True, these changes simply retain the previous agreement of the call hex2ip.
I do not know the original intentions of the author of the hex2ip but it is my opinion that the setting invert can be removed together with the excess code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Just wanted to make sure we covered our bases there. Thanks for the reply.

else:
ip.append("{0[0]}{0[1]}:{0[2]}{0[3]}".format(ip_part))
try:
return ipaddress.IPv6Address(":".join(ip)).compressed
except ipaddress.AddressValueError as ex:
Expand Down Expand Up @@ -1061,8 +1064,10 @@ def _remotes_on(port, which_end):
port = int(port)
ret = set()

proc_available = False
for statf in ['/proc/net/tcp', '/proc/net/tcp6']:
if os.path.isfile(statf):
proc_available = True
with salt.utils.fopen(statf, 'rb') as fp_:
for line in fp_:
if line.strip().startswith('sl'):
Expand All @@ -1072,7 +1077,7 @@ def _remotes_on(port, which_end):
if iret[sl][which_end] == port:
ret.add(iret[sl]['remote_addr'])

if not ret: # Fallback to use 'lsof' if /proc not available
if not proc_available: # Fallback to use OS specific tools
if salt.utils.is_sunos():
return _sunos_remotes_on(port, which_end)
if salt.utils.is_freebsd():
Expand Down Expand Up @@ -1298,6 +1303,11 @@ def _linux_remotes_on(port, which_end):
['lsof', '-iTCP:{0:d}'.format(port), '-n', '-P'] # pylint: disable=minimum-python-version
)
except subprocess.CalledProcessError as ex:
if ex.returncode == 1:
# Lsof return 1 if any error was detected, including the failure
# to locate Internet addresses, and it is not an error in this case.
log.warning('"lsof" returncode = 1, likely no active TCP sessions.')
return remotes
log.error('Failed "lsof" with returncode = {0}'.format(ex.returncode))
raise

Expand Down