-
-
Notifications
You must be signed in to change notification settings - Fork 645
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
Added getnameinfo #172
Added getnameinfo #172
Conversation
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.
Thanks for sending this! The /etc/services stuff looks orthogonal to this change. Could we remove that and do it as a separate PR if you need it?
Ok, I'll look to add a separate PR for parsing What do you think about |
getnameinfo is a reverse lookup function so that other function seems on-topic. |
Added necessary constants (DNS_TYPE_PTR, NI_NUMERICHOST etc.). Implementation of getnameinfo is similar to getaddrinfo, with internal functions: * ResolveDnsReverse: performs rDNS query and parses the PTR record * ResolveHostsReverse: reads /etc/hosts to map hostname to address Earlier, the HOSTS.txt would only need to be sorted at loading time, because the only kind of lookup was name -> address. Now since address -> name lookups are also possible, so the HostsTxt struct, the sorting method (and the related tests) was changed to reflect this.
Removed the services lookup stub. I used the HostsTxt internal structure for the address -> name lookup, so the logarithmic-time lookup of names is lost if one keeps alternating between name and address lookups. |
We can always make it bidirectional if performance ends up being an issue for someone's use case. Will review as soon as I get a chance. |
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.
LGTM. Thank you!
I tested getnameinfo on the system glibc and looks like it performs a linear lookup: if I have multiple entries with the same IP, glibc picks the first one that matches, but this implementation picks a different one because it starts at the middle. |
You'd have to replace |
I'll submit a PR that uses just brute-force linear search for Even then, the localhost 127.0.0.1
ahost 127.0.0.1
bhost 127.0.0.1
mhost 127.0.0.1
xhost 127.0.0.1
yhost 127.0.0.1
zhost 127.0.0.1 Now if I call |
The getnameinfo implementation requires an address -> name lookup on the hosts file (ie struct HostsTxt) and the previous implementation used flags to check whether HostsTxt was sorted according to address or name, and then re-sorted it if necessary. Now getnameinfo lookup does not require sorting, it does a simple linear lookup, and so the related code was simplified See jart#172 for discussion.
The getnameinfo implementation requires an address -> name lookup on the hosts file (ie struct HostsTxt) and the previous implementation used flags to check whether HostsTxt was sorted according to address or name, and then re-sorted it if necessary. Now getnameinfo lookup does not require sorting, it does a simple linear lookup, and so the related code was simplified See #172 for discussion.
implementation of getnameinfo is similar to getaddrinfo, with additional internal functions. I tested it with an example program on
1.1.1.1
and8.8.4.4
and the output matches glibc.Two helper functions are currently stub:
When performing an rDNS lookup, I have to look through
/etc/hosts
first, butstruct HostsTxt
is already sorted according to name. Should I do a linear search or re-sort it according to address (and then sort back)?The service name is currently mapped to
0
always. I'll try to add astruct ServicesTxt
similar tostruct HostsTxt
if I figure it out, or just a simple parse of/etc/services
.