Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

fix filename for IPv6 on win32 #526

Merged
merged 1 commit into from Feb 6, 2022

Conversation

HynekPetrak
Copy link
Contributor

On MS Windows, NTFS filesystem the : is a special characters used for Alternate Data Streams.

If the host has enabled IPv6 support then gethost_addrinfo

def gethost_addrinfo(hostname):
    try:
        for res in socket.getaddrinfo(hostname, None, socket.AF_INET6,
                socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME):
            af, socktype, proto, canonname, sa = res
    except socket.gaierror:
        for res in socket.getaddrinfo(hostname, None, socket.AF_INET,
                socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME):
            af, socktype, proto, canonname, sa = res
    if canonname == '':
        return sa[0]
    return canonname

will convert IPv4 address or hostname from the hosts file into an IPv6 address.

The output_filename in ldap.py and smb.py is formatted with IPv6 address in the name and therefore we need to replace any occurrence of : in the file name with some harmless character. I've arbitrary chosen the -

@mpgn
Copy link
Contributor

mpgn commented Feb 5, 2022

Does it means that with IPV6 the domain resolution fail ? Because the file should be like this: hostname_IPV6_date etc

@HynekPetrak
Copy link
Contributor Author

HynekPetrak commented Feb 6, 2022

Resolution does not fail, but you can't create a file with : (colon) in the name on ms windows. So colon must be replaced with another character.

Colon is part of ipv6 address.

Copy link
Contributor

@mpgn mpgn left a comment

Choose a reason for hiding this comment

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

Why no just call self.output_filename = self.output_filename.replace(":","-") directly after self.output_filename = os.path.expanduser('~/.cme/logs/{}_{}_{}'.format(self.hostname, self.host, datetime.now().strftime("%Y-%m-%d_%H%M%S"))) ?

This solution avoid a new import and a condition. Bonus point, formating will be the same on windows and linux.

@HynekPetrak
Copy link
Contributor Author

That's up to you, my intention was not to brake current naming convention on Linux. I can update the pull request later today if agreed

@mpgn
Copy link
Contributor

mpgn commented Feb 6, 2022

Yeah update with the most simple change :)

@HynekPetrak
Copy link
Contributor Author

@mpgn done.

@mpgn mpgn merged commit b4c17c0 into byt3bl33d3r:master Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants