Skip to content

Conversation

@drmalikabdullah
Copy link
Contributor

the lists of host

@@ -0,0 +1,31 @@

from ctypes import *
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't use '*' imports in libraries. This has multiple disadvantages.


from ctypes import *

def find_hostname(netgroup):
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer get instead of find and the plural because multiple hostnames are returned:

Suggested change
def find_hostname(netgroup):
def get_hostnames(netgroup):

from ctypes import *

def find_hostname(netgroup):

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a doctring that describes what the function does, what are the inputs and outputs and any special or surprising behavior one needs to know to use it. Best would be to also add an example.

Comment on lines +6 to +10
libc = CDLL("libc.so.6")
if (libc != None):
pass
else:
libc = CDLL("libc.so.7")
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a better way that going through a fixed list of library names? Why are "libc.so.5" or libc.so.8 not included in that list?

Also it is probably better to not do the lookup at every function call but only once at module load time.



hosts = None
if libc.setnetgrent(netgroup):
Copy link
Contributor

Choose a reason for hiding this comment

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

One needs to check that netgroup is of the correct type, otherwise this might cause a segfault.


hosts = None
if libc.setnetgrent(netgroup):
# returns 1 if successful and 0 otherwise
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment should be above or next to the line it refers to.

while libc.getnetgrent(byref(host), byref(user), byref(domain)):
if host:
# check is host is not a NULL pointer
hosts.append(host.value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this always safe? What about interleaving calls from multiple threads?
Should this return str instead of bytes?

# check is host is not a NULL pointer
hosts.append(host.value)

return(hosts)
Copy link
Contributor

Choose a reason for hiding this comment

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

The difference between None and [] might be too subtle. Maybe throw an error instead of returning None?

Comment on lines 28 to 31

hosts = find_hostname(b"a3p62-hosts")
print(hosts)
print("done") No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
hosts = find_hostname(b"a3p62-hosts")
print(hosts)
print("done")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants