+#include <net/if.h>
+#include <ctype.h>
+#include <resolv.h>
+#include "lookup.h"
+#include "stdio_impl.h"
+
+#define PTR_MAX (64 + sizeof ".in-addr.arpa")
+#define RR_PTR 12
+
+static char *itoa(char *p, unsigned x) {
+ p += 3*sizeof(int);
+ *--p = 0;
+ do {
+ *--p = '0' + x % 10;
+ x /= 10;
+ } while (x);
+ return p;
+}
+
+static void mkptr4(char *s, const unsigned char *ip)
+{
+ sprintf(s, "%d.%d.%d.%d.in-addr.arpa",
+ ip[3], ip[2], ip[1], ip[0]);
+}
+
+static void mkptr6(char *s, const unsigned char *ip)
+{
+ static const char xdigits[] = "0123456789abcdef";
+ int i;
+ for (i=15; i>=0; i--) {
+ *s++ = xdigits[ip[i]&15]; *s++ = '.';
+ *s++ = xdigits[ip[i]>>4]; *s++ = '.';
+ }
+ strcpy(s, "ip6.arpa");
+}
+
+static void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, int family)
+{
+ char line[512], *p, *z;
+ unsigned char _buf[1032], atmp[16];
+ struct address iplit;
+ FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
+ if (!f) return;
+ if (family == AF_INET) {
+ memcpy(atmp+12, a, 4);
+ memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
+ a = atmp;
+ }
+ while (fgets(line, sizeof line, f)) {
+ if ((p=strchr(line, '#'))) *p++='\n', *p=0;
+
+ for (p=line; *p && !isspace(*p); p++);
+ *p++ = 0;
+ if (__lookup_ipliteral(&iplit, line, AF_UNSPEC)<=0)
+ continue;
+
+ if (iplit.family == AF_INET) {
+ memcpy(iplit.addr+12, iplit.addr, 4);
+ memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
+ iplit.scopeid = 0;
+ }
+
+ if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid)
+ continue;
+
+ for (; *p && isspace(*p); p++);
+ for (z=p; *z && !isspace(*z); z++);
+ *z = 0;
+ if (z-p < 256) {
+ memcpy(buf, p, z-p+1);
+ break;
+ }
+ }
+ __fclose_ca(f);
+}
+
+static void reverse_services(char *buf, int port, int dgram)
+{
+ unsigned long svport;
+ char line[128], *p, *z;
+ unsigned char _buf[1032];
+ FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
+ if (!f) return;
+ while (fgets(line, sizeof line, f)) {
+ if ((p=strchr(line, '#'))) *p++='\n', *p=0;
+
+ for (p=line; *p && !isspace(*p); p++);
+ if (!*p) continue;
+ *p++ = 0;
+ svport = strtoul(p, &z, 10);
+
+ if (svport != port || z==p) continue;
+ if (dgram && strncmp(z, "/udp", 4)) continue;
+ if (!dgram && strncmp(z, "/tcp", 4)) continue;
+ if (p-line > 32) continue;
+
+ memcpy(buf, line, p-line);
+ break;
+ }
+ __fclose_ca(f);
+}
+
+static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
+{
+ if (rr != RR_PTR) return 0;
+ if (__dn_expand(packet, (const unsigned char *)packet + 512,
+ data, c, 256) <= 0)
+ *(char *)c = 0;
+ return 0;
+
+}