mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 07:18:30 +00:00
Simplify getnameinfo (#196)
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.
This commit is contained in:
parent
1f87640d17
commit
98c53ae526
7 changed files with 21 additions and 46 deletions
|
@ -8,11 +8,6 @@
|
|||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
static int hoststxtcmpaddr(const uint8_t *ip1, const struct HostsTxtEntry *he2) {
|
||||
uint32_t v1 = *((uint32_t *)ip1), v2 = *((uint32_t *)he2->ip);
|
||||
return (v1 == v2 ? 0 : (v1 > v2 ? 1 : -1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds name associated with address in HOSTS.TXT table.
|
||||
*
|
||||
|
@ -27,15 +22,21 @@ static int hoststxtcmpaddr(const uint8_t *ip1, const struct HostsTxtEntry *he2)
|
|||
*/
|
||||
int ResolveHostsReverse(const struct HostsTxt *ht, int af, const uint8_t *ip,
|
||||
char *buf, size_t bufsize) {
|
||||
struct HostsTxtEntry *entry;
|
||||
struct HostsTxtEntry *entry = NULL;
|
||||
uint32_t v1, v2;
|
||||
|
||||
if (af != AF_INET && af != AF_UNSPEC) return eafnosupport();
|
||||
if (!ht->entries.p) return -1;
|
||||
if (!ht->entries.p || !buf || bufsize == 0) return -1;
|
||||
|
||||
if (ht->sorted_by != HOSTSTXT_SORTEDBYADDR)
|
||||
SortHostsTxt(ht, HOSTSTXT_SORTEDBYADDR);
|
||||
v1 = *((uint32_t *)ip);
|
||||
for (size_t j = 0; j < ht->entries.i; j++) {
|
||||
v2 = *((uint32_t *)ht->entries.p[j].ip);
|
||||
if (v1 == v2) {
|
||||
entry = &(ht->entries.p[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
entry = bsearch(ip, ht->entries.p, ht->entries.i,
|
||||
sizeof(struct HostsTxtEntry), (void *)hoststxtcmpaddr);
|
||||
if (entry) {
|
||||
strncpy(buf, &ht->strings.p[entry->name], bufsize);
|
||||
return 1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue