mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +00:00
Added getnameinfo with only name lookup (#172)
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.
This commit is contained in:
parent
05350eca60
commit
248c6d54bb
12 changed files with 334 additions and 10 deletions
44
libc/dns/resolvehostsreverse.c
Normal file
44
libc/dns/resolvehostsreverse.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
|
||||
#include "libc/alg/alg.h"
|
||||
#include "libc/dns/consts.h"
|
||||
#include "libc/dns/dns.h"
|
||||
#include "libc/dns/hoststxt.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/str/str.h"
|
||||
#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.
|
||||
*
|
||||
* @param ht can be GetHostsTxt()
|
||||
* @param af can be AF_INET
|
||||
* @param ip is IP address in binary (sin_addr)
|
||||
* @param buf is buffer to store the name
|
||||
* @param bufsize is length of buf
|
||||
*
|
||||
* @return 1 if found, 0 if not found, or -1 w/ errno
|
||||
* @error EAFNOSUPPORT
|
||||
*/
|
||||
int ResolveHostsReverse(const struct HostsTxt *ht, int af, const uint8_t *ip,
|
||||
char *buf, size_t bufsize) {
|
||||
struct HostsTxtEntry *entry;
|
||||
if (af != AF_INET && af != AF_UNSPEC) return eafnosupport();
|
||||
if (!ht->entries.p) return -1;
|
||||
|
||||
if (ht->sorted_by != HOSTSTXT_SORTEDBYADDR)
|
||||
SortHostsTxt(ht, HOSTSTXT_SORTEDBYADDR);
|
||||
|
||||
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;
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue