mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 11:37:35 +00:00
248c6d54bb
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.
44 lines
1.3 KiB
C
44 lines
1.3 KiB
C
|
|
#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;
|
|
}
|