-
Notifications
You must be signed in to change notification settings - Fork 3
Ctype wrapper is used to call getnetgrent for #34
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
|
|
||
| from ctypes import * | ||
|
|
||
| def get_hostname(netgroup): | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| libc = CDLL("libc.so.6") | ||
| if (libc != None): | ||
| pass | ||
| else: | ||
| libc = CDLL("libc.so.7") | ||
|
Comment on lines
+6
to
+10
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One needs to check that |
||
| # returns 1 if successful and 0 otherwise | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| host = c_char_p() | ||
| user = c_char_p() | ||
| domain = c_char_p() | ||
|
|
||
| hosts = [] | ||
| while libc.getnetgrent(byref(host), byref(user), byref(domain)): | ||
| if host: | ||
| # check is host is not a NULL pointer | ||
| hosts.append(host.value) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this always safe? What about interleaving calls from multiple threads? |
||
|
|
||
| return(hosts) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The difference between |
||
|
|
||
|
|
||
| hosts = get_hostname(b"a3p62-hosts") | ||
| print(hosts) | ||
| print("done") | ||
There was a problem hiding this comment.
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.