2021-06-10 02:35:44 +00:00
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2021-06-22 19:35:58 +00:00
|
|
|
struct HostsTxtEntry *entry = NULL;
|
|
|
|
uint32_t v1, v2;
|
|
|
|
|
2021-06-10 02:35:44 +00:00
|
|
|
if (af != AF_INET && af != AF_UNSPEC) return eafnosupport();
|
2021-06-22 19:35:58 +00:00
|
|
|
if (!ht->entries.p || !buf || bufsize == 0) return -1;
|
2021-06-10 02:35:44 +00:00
|
|
|
|
2021-06-22 19:35:58 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 02:35:44 +00:00
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
strncpy(buf, &ht->strings.p[entry->name], bufsize);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|