Ctype wrapper is used to call getnetgrent for#34
Ctype wrapper is used to call getnetgrent for#34drmalikabdullah wants to merge 2 commits intomasterfrom
Conversation
the lists of host
| @@ -0,0 +1,31 @@ | |||
|
|
|||
| from ctypes import * | |||
There was a problem hiding this comment.
Don't use '*' imports in libraries. This has multiple disadvantages.
|
|
||
| from ctypes import * | ||
|
|
||
| def find_hostname(netgroup): |
There was a problem hiding this comment.
I prefer get instead of find and the plural because multiple hostnames are returned:
| def find_hostname(netgroup): | |
| def get_hostnames(netgroup): |
| from ctypes import * | ||
|
|
||
| def find_hostname(netgroup): | ||
|
|
There was a problem hiding this comment.
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.
| libc = CDLL("libc.so.6") | ||
| if (libc != None): | ||
| pass | ||
| else: | ||
| libc = CDLL("libc.so.7") |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
The difference between None and [] might be too subtle. Maybe throw an error instead of returning None?
|
|
||
| hosts = find_hostname(b"a3p62-hosts") | ||
| print(hosts) | ||
| print("done") No newline at end of file |
There was a problem hiding this comment.
| hosts = find_hostname(b"a3p62-hosts") | |
| print(hosts) | |
| print("done") |
the lists of host