#include "ils.h"
#include "netdb.h"
#include "si_module.h"
#include <assert.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <arpa/nameser_compat.h>
#include <libkern/OSAtomic.h>
#include <netinet/in.h>
#include <ctype.h>
#include <dns_sd.h>
#include <dnsinfo.h>
#include <errno.h>
#include <ifaddrs.h>
#include <nameser.h>
#include <notify.h>
#include <pthread.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/event.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <time.h>
#include <unistd.h>
#include <os/log.h>
#include <dns.h>
#include <dns_util.h>
#include <TargetConditionals.h>
#include <dispatch/dispatch.h>
#define DNS_MAX_RECEIVE_SIZE 65536
#define INET_NTOP_AF_INET_OFFSET 4
#define INET_NTOP_AF_INET6_OFFSET 8
#define IPPROTO_UNSPEC 0
#define GOT_DATA 1
#define GOT_ERROR 2
#define SHORT_AAAA_EXTRA 2
#define MEDIUM_AAAA_EXTRA 5
#define LONG_AAAA_EXTRA 10
#define MDNS_DEBUG_FILE "/etc/.mdns_debug"
#define MDNS_DEBUG_STDOUT 0x00000001
#define MDNS_DEBUG_STDERR 0x00000002
#define MDNS_DEBUG_ASL 0x00000004
#define MDNS_DEBUG_OUT 0x00000007
static int _mdns_debug = 0;
static pthread_mutex_t _mdns_mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct
{
uint16_t priority;
uint16_t weight;
uint16_t port;
uint8_t target[0];
} mdns_rr_srv_t;
typedef struct mdns_srv_t mdns_srv_t;
struct mdns_srv_t
{
si_srv_t srv;
mdns_srv_t *next;
};
typedef struct
{
struct hostent host;
int alias_count;
int addr_count;
} mdns_hostent_t;
typedef struct
{
mdns_hostent_t *h4;
mdns_hostent_t *h6;
mdns_srv_t *srv;
uint64_t ttl;
uint32_t ifnum;
} mdns_reply_t;
static uint32_t _mdns_generation = 0;
static DNSServiceRef _mdns_sdref;
static DNSServiceRef _mdns_old_sdref;
static void _mdns_hostent_clear(mdns_hostent_t *h);
static void _mdns_reply_clear(mdns_reply_t *r);
static int _mdns_search(const char *name, int class, int type, const char *interface, DNSServiceFlags flags, uint8_t *answer, uint32_t *anslen, mdns_reply_t *reply);
static int _mdns_search_ex(const char *name, int class, int type, uint32_t ifindex, DNSServiceFlags flags, uint8_t *answer, uint32_t *anslen, mdns_reply_t *reply);
static const char hexchar[] = "0123456789abcdef";
#define BILLION 1000000000
#define IPv6_REVERSE_LEN 72
#define IPv6_REVERSE_LINK_LOCAL_TRAILING_CHAR 58
#define IPv6_REVERSE_LINK_LOCAL_SCOPE_ID_LOW 48
const static uint8_t hexval[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static void
_mdns_debug_message(const char *str, ...)
{
va_list v;
char *out = NULL;
if (str == NULL) return;
if ((_mdns_debug & MDNS_DEBUG_OUT) == 0) return;
va_start(v, str);
vasprintf(&out, str, v);
if (out == NULL) return;
if (_mdns_debug & MDNS_DEBUG_STDOUT) fprintf(stdout, "%s", out);
if (_mdns_debug & MDNS_DEBUG_STDERR) fprintf(stderr, "%s", out);
if (_mdns_debug & MDNS_DEBUG_ASL) os_log(OS_LOG_DEFAULT, "%s", out);
free(out);
va_end(v);
}
static char *
_mdns_reverse_ipv4(const uint8_t *addr)
{
union
{
uint32_t a;
unsigned char b[4];
} ab;
char *p;
if (addr == NULL) return NULL;
memcpy(&(ab.a), addr, 4);
asprintf(&p, "%u.%u.%u.%u.in-addr.arpa.", ab.b[3], ab.b[2], ab.b[1], ab.b[0]);
return p;
}
static char *
_mdns_reverse_ipv6(const uint8_t *addr)
{
char x[65], *p;
int i, j;
u_int8_t d, hi, lo;
if (addr == NULL) return NULL;
x[64] = '\0';
j = 63;
for (i = 0; i < 16; i++)
{
d = addr[i];
lo = d & 0x0f;
hi = d >> 4;
x[j--] = '.';
x[j--] = hexchar[hi];
x[j--] = '.';
x[j--] = hexchar[lo];
}
asprintf(&p, "%sip6.arpa.", x);
return p;
}
static char *
_mdns_canonicalize(const char *s)
{
int i;
char *t;
if (s == NULL) return NULL;
t = strdup(s);
if (t == NULL) return NULL;
if (t[0] == '\0') return t;
for (i = 0; t[i] != '\0'; i++)
{
if (t[i] >= 'A' && t[i] <= 'Z') t[i] += 32;
}
if (t[i-1] == '.') t[i-1] = '\0';
return t;
}
static int
_mdns_hostent_append_alias(mdns_hostent_t *h, const char *alias)
{
int i;
char *name;
_mdns_debug_message(";; _mdns_hostent_append_alias(%p, %s)\n", h, alias);
if ((h == NULL) || (alias == NULL)) return 0;
name = _mdns_canonicalize(alias);
if (name == NULL) return -1;
if ((h->host.h_name != NULL) && string_equal(h->host.h_name, name))
{
free(name);
return 0;
}
for (i = 0; i < h->alias_count; ++i)
{
if (string_equal(h->host.h_aliases[i], name))
{
free(name);
return 0;
}
}
h->host.h_aliases = (char **)reallocf(h->host.h_aliases, (h->alias_count + 2) * sizeof(char *));
if (h->host.h_aliases == NULL)
{
h->alias_count = 0;
free(name);
return -1;
}
h->host.h_aliases[h->alias_count] = name;
h->alias_count++;
h->host.h_aliases[h->alias_count] = NULL;
return 0;
}
static int
_mdns_hostent_append_addr(mdns_hostent_t *h, const uint8_t *addr, uint32_t len)
{
_mdns_debug_message(";; _mdns_hostent_append_addr(%p, %p, %u)\n", h, addr, len);
if ((h == NULL) || (addr == NULL) || (len == 0)) return 0;
uint8_t *buf = malloc(len);
if (buf == NULL) return -1;
memcpy(buf, addr, len);
h->host.h_addr_list = (char **)reallocf(h->host.h_addr_list, (h->addr_count + 2) * sizeof(char *));
if (h->host.h_addr_list == NULL)
{
h->addr_count = 0;
free(buf);
return -1;
}
h->host.h_addr_list[h->addr_count] = (char*)buf;
h->addr_count++;
h->host.h_addr_list[h->addr_count] = NULL;
return 0;
}
static void
_mdns_hostent_clear(mdns_hostent_t *h)
{
if (h == NULL) return;
free(h->host.h_name);
h->host.h_name = NULL;
char **aliases = h->host.h_aliases;
while (aliases && *aliases) free(*aliases++);
free(h->host.h_aliases);
h->host.h_aliases = NULL;
h->alias_count = 0;
char **addrs = h->host.h_addr_list;
while (addrs && *addrs) free(*addrs++);
free(h->host.h_addr_list);
h->host.h_addr_list = NULL;
h->addr_count = 0;
}
static void
_mdns_reply_clear(mdns_reply_t *r)
{
if (r == NULL) return;
r->ifnum = 0;
_mdns_hostent_clear(r->h4);
_mdns_hostent_clear(r->h6);
mdns_srv_t *srv = r->srv;
r->srv = NULL;
while (srv != NULL)
{
mdns_srv_t *next = srv->next;
free(srv->srv.target);
free(srv);
srv = next;
}
}
static si_item_t *
mdns_hostbyname(si_mod_t *si, const char *name, int af, const char *interface, uint32_t *err)
{
uint32_t type;
mdns_hostent_t h;
mdns_reply_t reply;
si_item_t *out = NULL;
uint64_t bb;
int status;
DNSServiceFlags flags = 0;
if (err != NULL) *err = SI_STATUS_NO_ERROR;
if ((name == NULL) || (si == NULL))
{
if (err != NULL) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return NULL;
}
memset(&h, 0, sizeof(h));
memset(&reply, 0, sizeof(reply));
switch (af)
{
case AF_INET:
type = ns_t_a;
h.host.h_length = 4;
reply.h4 = &h;
break;
case AF_INET6:
type = ns_t_aaaa;
h.host.h_length = 16;
reply.h6 = &h;
break;
default:
if (err != NULL) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return NULL;
}
_mdns_debug_message(";; mdns_hostbyname %s type %u class %u\n", name, type, ns_c_in);
h.host.h_addrtype = af;
status = _mdns_search(name, ns_c_in, type, interface, flags, NULL, NULL, &reply);
if ((status != 0) || (h.addr_count == 0))
{
_mdns_reply_clear(&reply);
if (err != NULL) *err = SI_STATUS_H_ERRNO_HOST_NOT_FOUND;
return NULL;
}
bb = reply.ttl + time(NULL);
switch (af)
{
case AF_INET:
out = (si_item_t *)LI_ils_create("L4488s*44a", (unsigned long)si, CATEGORY_HOST_IPV4, 1, bb, 0LL, h.host.h_name, h.host.h_aliases, h.host.h_addrtype, h.host.h_length, h.host.h_addr_list);
break;
case AF_INET6:
out = (si_item_t *)LI_ils_create("L4488s*44c", (unsigned long)si, CATEGORY_HOST_IPV6, 1, bb, 0LL, h.host.h_name, h.host.h_aliases, h.host.h_addrtype, h.host.h_length, h.host.h_addr_list);
break;
}
_mdns_reply_clear(&reply);
if ((out == NULL) && (err != NULL)) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return out;
}
static bool _is_v4addr_ifaddr(const uint8_t addrBytes[4])
{
int err;
struct ifaddrs *ifaddrs;
const struct ifaddrs *ifa;
const struct sockaddr_in *sa4;
in_addr_t addr;
bool found = false;
err = getifaddrs(&ifaddrs);
if (err != 0) goto exit;
memcpy(&addr, addrBytes, 4);
for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next)
{
if ((ifa->ifa_flags & IFF_UP) == 0) continue;
if (!ifa->ifa_addr || (ifa->ifa_addr->sa_family != AF_INET)) continue;
sa4 = (const struct sockaddr_in *)ifa->ifa_addr;
if (sa4->sin_addr.s_addr == addr)
{
found = true;
break;
}
}
freeifaddrs(ifaddrs);
exit:
return found;
}
static bool _is_v6addr_ifaddr(const uint8_t addrBytes[16], uint32_t ifindex)
{
int err;
struct ifaddrs *ifaddrs;
const struct ifaddrs *ifa;
const struct sockaddr_in6 *sa6;
bool found = false;
err = getifaddrs(&ifaddrs);
if (err != 0) goto exit;
for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next)
{
if ((ifa->ifa_flags & IFF_UP) == 0) continue;
if (!ifa->ifa_addr || (ifa->ifa_addr->sa_family != AF_INET6)) continue;
sa6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
if ((sa6->sin6_scope_id == ifindex) && (memcmp(&sa6->sin6_addr.s6_addr, addrBytes, 16) == 0))
{
found = true;
break;
}
}
freeifaddrs(ifaddrs);
exit:
return found;
}
static si_item_t *
mdns_hostbyaddr(si_mod_t *si, const void *addr, int af, const char *interface, uint32_t *err)
{
mdns_hostent_t h;
mdns_reply_t reply;
char *name;
si_item_t *out;
uint64_t bb;
int cat;
int status;
DNSServiceFlags flags = 0;
uint32_t ifindex = 0;
if (err != NULL) *err = SI_STATUS_NO_ERROR;
if ((addr == NULL) || (si == NULL))
{
if (err != NULL) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return NULL;
}
memset(&h, 0, sizeof(h));
memset(&reply, 0, sizeof(reply));
switch (af)
{
case AF_INET:
{
const uint8_t * const target_addr = (const uint8_t *)addr;
if (!interface && (target_addr[0] == 169) && (target_addr[1] == 254) && _is_v4addr_ifaddr(target_addr))
{
ifindex = kDNSServiceInterfaceIndexLocalOnly;
}
h.host.h_length = 4;
reply.h4 = &h;
name = _mdns_reverse_ipv4(target_addr);
cat = CATEGORY_HOST_IPV4;
break;
}
case AF_INET6:
{
const uint8_t *target_addr = (const uint8_t *)addr;
uint8_t fixed_addr[16];
if (!interface && (target_addr[0] == 0xFE) && ((target_addr[1] & 0xC0) == 0x80))
{
const uint32_t embedded_index = (target_addr[2] << 8) | target_addr[3];
memcpy(fixed_addr, target_addr, 16);
fixed_addr[2] = 0;
fixed_addr[3] = 0;
if ((embedded_index != 0) && _is_v6addr_ifaddr(fixed_addr, embedded_index))
{
target_addr = fixed_addr;
ifindex = kDNSServiceInterfaceIndexLocalOnly;
}
else
{
ifindex = embedded_index;
}
}
h.host.h_length = 16;
reply.h6 = &h;
name = _mdns_reverse_ipv6(target_addr);
cat = CATEGORY_HOST_IPV6;
break;
}
default:
if (err != NULL) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return NULL;
}
h.host.h_addrtype = af;
_mdns_debug_message(";; mdns_hostbyaddr %s type %u class %u\n", name, ns_t_ptr, ns_c_in);
if (interface)
{
status = _mdns_search(name, ns_c_in, ns_t_ptr, interface, flags, NULL, NULL, &reply);
}
else
{
status = _mdns_search_ex(name, ns_c_in, ns_t_ptr, ifindex, flags, NULL, NULL, &reply);
}
free(name);
if (status != 0)
{
_mdns_reply_clear(&reply);
if (err != NULL) *err = SI_STATUS_H_ERRNO_HOST_NOT_FOUND;
return NULL;
}
status = _mdns_hostent_append_addr(&h, addr, h.host.h_length);
if (status != 0)
{
_mdns_hostent_clear(&h);
if (err != NULL) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return NULL;
}
bb = reply.ttl + time(NULL);
switch (af)
{
case AF_INET:
out = (si_item_t *)LI_ils_create("L4488s*44a", (unsigned long)si, CATEGORY_HOST_IPV4, 1, bb, 0LL, h.host.h_name, h.host.h_aliases, h.host.h_addrtype, h.host.h_length, h.host.h_addr_list);
break;
case AF_INET6:
out = (si_item_t *)LI_ils_create("L4488s*44c", (unsigned long)si, CATEGORY_HOST_IPV6, 1, bb, 0LL, h.host.h_name, h.host.h_aliases, h.host.h_addrtype, h.host.h_length, h.host.h_addr_list);
break;
}
_mdns_hostent_clear(&h);
if ((out == NULL) && (err != NULL)) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return out;
}
static si_list_t *
mdns_addrinfo(si_mod_t *si, const void *node, const void *serv, uint32_t family, uint32_t socktype, uint32_t proto, uint32_t flags, const char *interface, uint32_t *err)
{
bool wantv4 = true;
bool wantv6 = true;
struct in_addr a4;
struct in6_addr a6;
mdns_hostent_t h4;
mdns_hostent_t h6;
mdns_reply_t reply;
uint32_t type;
uint16_t port;
if (si == NULL)
{
if (err != NULL) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return NULL;
}
if (family == AF_INET6)
{
if ((flags & AI_V4MAPPED) == 0) wantv4 = false;
}
else if (family == AF_INET)
{
wantv6 = false;
}
else if (family != AF_UNSPEC)
{
return NULL;
}
if (err != NULL) *err = SI_STATUS_NO_ERROR;
_mdns_debug_message(";; mdns_addrinfo node %s serv %s\n", (const char *)node, (const char *)serv);
si_list_t *out = NULL;
memset(&h4, 0, sizeof(h4));
memset(&h6, 0, sizeof(h6));
memset(&reply, 0, sizeof(reply));
h4.host.h_addrtype = AF_INET;
h4.host.h_length = 4;
h6.host.h_addrtype = AF_INET6;
h6.host.h_length = 16;
if (wantv4 && wantv6)
{
type = 0;
reply.h4 = &h4;
reply.h6 = &h6;
}
else if (wantv4)
{
reply.h4 = &h4;
type = ns_t_a;
}
else if (wantv6)
{
type = ns_t_aaaa;
reply.h6 = &h6;
}
else
{
if (err != NULL) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return NULL;
}
if ((flags & AI_NUMERICSERV) != 0)
{
if (serv == NULL) port = 0;
else port = *(uint16_t *)serv;
}
else
{
if (_gai_serv_to_port(serv, proto, &port) != 0)
{
if (err) *err = SI_STATUS_EAI_NONAME;
return NULL;
}
}
if ((flags & AI_NUMERICHOST) != 0)
{
char *cname = NULL;
struct in_addr *p4 = NULL;
struct in6_addr *p6 = NULL;
if (node == NULL) return NULL;
if (family == AF_INET)
{
p4 = &a4;
memcpy(p4, node, sizeof(a4));
}
else if (family == AF_INET6)
{
p6 = &a6;
memcpy(p6, node, sizeof(a6));
}
out = si_addrinfo_list(si, flags, socktype, proto, p4, p6, port, 0, cname, cname);
}
else
{
int res;
DNSServiceFlags dns_flags = 0;
if (node == NULL) return NULL;
if (flags & AI_ADDRCONFIG)
{
dns_flags |= kDNSServiceFlagsSuppressUnusable;
}
res = _mdns_search(node, ns_c_in, type, interface, dns_flags, NULL, NULL, &reply);
if ((res == 0) && ((h4.addr_count > 0) || (h6.addr_count > 0)))
{
out = si_addrinfo_list_from_hostent(si, flags, socktype, proto, port, 0, (wantv4 ? &h4.host : NULL), (wantv6 ? &h6.host : NULL));
}
else if (err != NULL)
{
*err = SI_STATUS_EAI_NONAME;
}
_mdns_reply_clear(&reply);
}
return out;
}
static si_list_t *
mdns_srv_byname(si_mod_t* si, const char *qname, const char *interface, uint32_t *err)
{
si_list_t *out = NULL;
mdns_reply_t reply;
mdns_srv_t *srv;
int res;
const uint64_t unused = 0;
DNSServiceFlags flags = 0;
if (si == NULL)
{
if (err != NULL) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return NULL;
}
if (err != NULL) *err = SI_STATUS_NO_ERROR;
_mdns_debug_message(";; mdns_srv_byname %s type %u class %u\n", qname, ns_t_srv, ns_c_in);
memset(&reply, 0, sizeof(reply));
res = _mdns_search(qname, ns_c_in, ns_t_srv, interface, flags, NULL, NULL, &reply);
if (res == 0)
{
srv = reply.srv;
while (srv != NULL)
{
si_item_t *item;
item = (si_item_t *)LI_ils_create("L4488222s", (unsigned long)si, CATEGORY_SRV, 1, unused, unused, srv->srv.priority, srv->srv.weight, srv->srv.port, srv->srv.target);
out = si_list_add(out, item);
si_item_release(item);
srv = srv->next;
}
}
_mdns_reply_clear(&reply);
return out;
}
static si_item_t *
mdns_item_call(si_mod_t *si, int call, const char *name, const char *ignored, const char *interface, uint32_t class, uint32_t type, uint32_t *err)
{
int res;
uint8_t buf[DNS_MAX_RECEIVE_SIZE];
uint32_t len = sizeof(buf);
mdns_reply_t reply;
mdns_hostent_t h4;
mdns_hostent_t h6;
si_item_t *out;
DNSServiceFlags flags = 0;
if ((si == NULL) || (name == NULL))
{
if (err != NULL) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
return NULL;
}
if (err != NULL) *err = SI_STATUS_NO_ERROR;
_mdns_debug_message(";; mdns_item_call %s type %u class %u\n", name, type, class);
memset(&h4, 0, sizeof(h4));
memset(&h6, 0, sizeof(h6));
memset(&reply, 0, sizeof(reply));
h4.host.h_addrtype = AF_INET;
h4.host.h_length = 4;
h6.host.h_addrtype = AF_INET6;
h6.host.h_length = 16;
reply.h4 = &h4;
reply.h6 = &h6;
res = _mdns_search(name, class, type, interface, flags, buf, &len, &reply);
if ((res != 0) || (len <= 0) || (len > DNS_MAX_RECEIVE_SIZE))
{
_mdns_reply_clear(&reply);
if (err != NULL) *err = SI_STATUS_H_ERRNO_HOST_NOT_FOUND;
return NULL;
}
struct sockaddr_in6 from;
uint32_t fromlen = sizeof(from);
memset(&from, 0, fromlen);
from.sin6_len = fromlen;
from.sin6_family = AF_INET6;
from.sin6_addr.__u6_addr.__u6_addr8[15] = 1;
if (reply.ifnum != 0)
{
from.sin6_addr.__u6_addr.__u6_addr16[0] = htons(0xfe80);
from.sin6_scope_id = reply.ifnum;
}
out = (si_item_t *)LI_ils_create("L4488@@", (unsigned long)si, CATEGORY_DNSPACKET, 1, 0LL, 0LL, len, buf, fromlen, &from);
if ((out == NULL) && (err != NULL)) *err = SI_STATUS_H_ERRNO_NO_RECOVERY;
_mdns_reply_clear(&reply);
return out;
}
static int
mdns_is_valid(si_mod_t *si, si_item_t *item)
{
return 0;
}
static void
mdns_close(si_mod_t *si)
{
}
static void
_mdns_atfork_prepare(void)
{
pthread_mutex_lock(&_mdns_mutex);
}
static void
_mdns_atfork_parent(void)
{
pthread_mutex_unlock(&_mdns_mutex);
}
static void
_mdns_atfork_child(void)
{
_mdns_old_sdref = _mdns_sdref; _mdns_sdref = NULL;
pthread_mutex_unlock(&_mdns_mutex);
}
static void
_mdns_init(void)
{
pthread_atfork(_mdns_atfork_prepare, _mdns_atfork_parent, _mdns_atfork_child);
if (getenv("RES_DEBUG") != NULL) _mdns_debug |= MDNS_DEBUG_STDOUT;
int fd = open(MDNS_DEBUG_FILE, O_RDONLY, 0);
errno = 0;
if (fd >= 0)
{
int i, n;
char c[5];
memset(c, 0, sizeof(c));
n = read(fd, c, 4);
for (i = 0; i < n; i++)
{
if ((c[i] == 'o') || (c[i] == 'O')) _mdns_debug |= MDNS_DEBUG_STDOUT;
if ((c[i] == 'e') || (c[i] == 'E')) _mdns_debug |= MDNS_DEBUG_STDERR;
if ((c[i] == 'a') || (c[i] == 'A')) _mdns_debug |= MDNS_DEBUG_ASL;
}
}
}
si_mod_t *
si_module_static_mdns(void)
{
static const struct si_mod_vtable_s mdns_vtable =
{
.sim_close = &mdns_close,
.sim_is_valid = &mdns_is_valid,
.sim_host_byname = &mdns_hostbyname,
.sim_host_byaddr = &mdns_hostbyaddr,
.sim_item_call = &mdns_item_call,
.sim_addrinfo = &mdns_addrinfo,
.sim_srv_byname = &mdns_srv_byname,
};
static si_mod_t si =
{
.vers = 1,
.refcount = 1,
.flags = SI_MOD_FLAG_STATIC,
.private = NULL,
.vtable = &mdns_vtable,
};
static dispatch_once_t once;
dispatch_once(&once, ^{
si.name = strdup("mdns");
_mdns_init();
});
return (si_mod_t*)&si;
}
static char *
_mdns_parse_domain_name(const uint8_t *data, uint32_t datalen)
{
int i = 0, j = 0;
uint32_t len;
uint32_t domainlen = 0;
char *domain = NULL;
if ((data == NULL) || (datalen == 0)) return NULL;
while (datalen-- > 0)
{
len = data[i++];
domainlen += (len + 1);
domain = reallocf(domain, domainlen);
if (domain == NULL) return NULL;
if (len == 0) break;
if (j > 0)
{
domain[j++] = datalen ? '.' : '\0';
}
while ((len-- > 0) && (0 != datalen--))
{
if (data[i] == '.')
{
domain = reallocf(domain, ++domainlen);
if (domain == NULL) return NULL;
domain[j++] = '\\';
}
domain[j++] = data[i++];
}
}
domain[j] = '\0';
return domain;
}
static int
_mdns_pack_domain_name(const char *str, uint8_t *buf, size_t buflen)
{
int i = 0;
uintptr_t len = 0;
if ((str == NULL) || (buf == NULL)) return -1;
while (i < buflen)
{
char *dot = strchr(str, '.');
if (dot == NULL) dot = strchr(str, '\0');
len = (dot - str);
if (len > NS_MAXLABEL) return -1;
buf[i++] = len;
while (str < dot && i < buflen)
{
buf[i++] = *str++;
}
if (*str++ == '\0') break;
}
if (i >= buflen) return -1;
if (len > 0)
{
buf[i++] = 0;
if (i >= buflen) return -1;
}
buf[i] = '\0';
return i;
}
static int
_is_rev_link_local(const char *name)
{
int len, i;
if (name == NULL) return 0;
len = strlen(name);
if (len == 0) return 0;
if (name[len - 1] == '.') len--;
if (len != IPv6_REVERSE_LEN) return 0;
i = IPv6_REVERSE_LINK_LOCAL_TRAILING_CHAR;
if ((name[i] != '8') && (name[i] != '9') && (name[i] != 'A') && (name[i] != 'a') && (name[i] != 'B') && (name[i] != 'b')) return 0;
i = IPv6_REVERSE_LINK_LOCAL_TRAILING_CHAR + 1;
if (strncasecmp(name + i, ".e.f.ip6.arpa", 13)) return 0;
for (i = 0; i < IPv6_REVERSE_LINK_LOCAL_TRAILING_CHAR; i += 2)
{
if (name[i] < '0') return 0;
if ((name[i] > '9') && (name[i] < 'A')) return 0;
if ((name[i] > 'F') && (name[i] < 'a')) return 0;
if (name[i] > 'f') return 0;
if (name[i + 1] != '.') return 0;
}
return 1;
}
static char *
_mdns_ipv6_extract_scope_id(const char *name, uint32_t *out_ifnum)
{
char *qname = NULL;
uint16_t nibble;
uint32_t iface;
int i;
if (out_ifnum != NULL) *out_ifnum = 0;
if (name == NULL) return NULL;
if (_is_rev_link_local(name))
{
i = IPv6_REVERSE_LINK_LOCAL_SCOPE_ID_LOW;
nibble = hexval[(uint32_t)name[i]];
iface = nibble;
i += 2;
nibble = hexval[(uint32_t)name[i]];
iface += (nibble << 4);
i += 2;
nibble = hexval[(uint32_t)name[i]];
iface += (nibble << 8);
i += 2;
nibble = hexval[(uint32_t)name[i]];
iface += (nibble << 12);
if (iface != 0)
{
qname = strdup(name);
if (qname == NULL) return NULL;
i = IPv6_REVERSE_LINK_LOCAL_SCOPE_ID_LOW;
qname[i] = '0';
qname[i + 2] = '0';
qname[i + 4] = '0';
qname[i + 6] = '0';
if (out_ifnum) *out_ifnum = iface;
}
}
return qname;
}
static int
_mdns_make_query(const char* name, int class, int type, uint8_t *buf, uint32_t buflen)
{
uint32_t len = 0;
if ((buf == NULL) || (buflen < (NS_HFIXEDSZ + NS_QFIXEDSZ))) return -1;
memset(buf, 0, NS_HFIXEDSZ);
HEADER *hp = (HEADER *)buf;
len += NS_HFIXEDSZ;
hp->id = arc4random();
hp->qr = 1;
hp->opcode = ns_o_query;
hp->rd = 1;
hp->rcode = ns_r_noerror;
hp->qdcount = htons(1);
int n = _mdns_pack_domain_name(name, &buf[len], buflen - len);
if (n < 0) return -1;
len += n;
uint16_t word;
word = htons(type);
memcpy(&buf[len], &word, sizeof(word));
len += sizeof(word);
word = htons(class);
memcpy(&buf[len], &word, sizeof(word));
len += sizeof(word);
return len;
}
typedef struct mdns_query_context_s
{
mdns_reply_t *reply;
mdns_hostent_t *host;
uint8_t *answer; size_t anslen; size_t ansmaxlen; int type; uint16_t last_type; uint32_t sd_gen;
DNSServiceRef sd;
DNSServiceFlags flags;
DNSServiceErrorType error;
int kq; struct mdns_query_context_s *next; } mdns_query_context_t;
static mdns_query_context_t *in_flight;
static void
_mdns_query_callback(DNSServiceRef, DNSServiceFlags, uint32_t, DNSServiceErrorType, const char *, uint16_t, uint16_t, uint16_t, const void *, uint32_t, void *);
static DNSServiceErrorType
_mdns_query_start(mdns_query_context_t *ctx, mdns_reply_t *reply, uint8_t *answer, uint32_t *anslen, const char* name, int class, int type, uint32_t ifindex, DNSServiceFlags flags, int kq)
{
DNSServiceErrorType status;
flags |= kDNSServiceFlagsShareConnection;
flags |= kDNSServiceFlagsReturnIntermediates;
flags |= kDNSServiceFlagsTimeout;
memset(ctx, 0, sizeof(mdns_query_context_t));
if ((answer != NULL) && (anslen != NULL))
{
ctx->answer = answer;
ctx->ansmaxlen = *anslen;
ctx->anslen = _mdns_make_query(name, class, type, answer, ctx->ansmaxlen);
if (ctx->anslen <= 0) return -1;
}
ctx->type = type;
ctx->sd = _mdns_sdref;
ctx->sd_gen = _mdns_generation;
ctx->kq = kq;
if (reply != NULL)
{
ctx->reply = reply;
if (type == ns_t_a) ctx->host = reply->h4;
else if (type == ns_t_aaaa) ctx->host = reply->h6;
else if (type == ns_t_ptr && reply->h4) ctx->host = reply->h4;
else if (type == ns_t_ptr && reply->h6) ctx->host = reply->h6;
else if (type != ns_t_srv && type != ns_t_cname) return -1;
}
uint32_t iface = 0;
char *qname = _mdns_ipv6_extract_scope_id(name, &iface);
if (qname == NULL) qname = (char *)name;
if (ifindex != 0)
{
if ((iface != 0) && (iface != ifindex)) return -1;
iface = ifindex;
}
_mdns_debug_message(";; mdns query %s type %d class %d ifindex %d [ctx %p]\n", qname, type, class, (int)iface, ctx);
status = DNSServiceQueryRecord(&ctx->sd, flags, iface, qname, type, class, _mdns_query_callback, ctx);
if (qname != name) free(qname);
ctx->next = in_flight;
in_flight = ctx;
return status;
}
static bool
_mdns_query_is_complete(mdns_query_context_t *ctx, bool *more)
{
bool complete = false;
if (ctx == NULL) return true;
if (ctx->flags & kDNSServiceFlagsMoreComing)
{
if (more != NULL) *more = true;
_mdns_debug_message(";; mdns is_complete type %d ctx %p more coming - incomplete\n", ctx->type, ctx);
return false;
} else {
if (more != NULL) *more = false;
_mdns_debug_message(";; mdns is_complete type %d ctx %p clear more coming - complete\n", ctx->type, ctx);
}
if (ctx->last_type != ctx->type)
{
_mdns_debug_message(";; mdns is_complete ctx %p type mismatch (%d != %d) - incomplete\n", ctx, ctx->last_type, ctx->type);
return false;
}
switch (ctx->type)
{
case ns_t_a:
case ns_t_aaaa:
if (ctx->host != NULL && ctx->host->addr_count > 0)
{
_mdns_debug_message(";; mdns is_complete type %d ctx %p host addr count %d complete -> true\n", ctx->type, ctx, ctx->host->addr_count);
complete = true;
}
break;
case ns_t_ptr:
if (ctx->host != NULL && ctx->host->host.h_name != NULL)
{
complete = true;
_mdns_debug_message(";; mdns is_complete type %d ctx %p host name %s complete -> true\n", ctx->type, ctx, ctx->host->host.h_name);
}
break;
case ns_t_srv:
if (ctx->reply != NULL && ctx->reply->srv != NULL)
{
_mdns_debug_message(";; mdns is_complete type %d ctx %p srv %s complete -> true\n", ctx->type, ctx, ctx->reply->srv);
complete = true;
}
break;
default:
_mdns_debug_message(";; mdns is_complete unexpected type %d ctx %p\n", ctx->type, ctx);
}
_mdns_debug_message(";; mdns is_complete type %d ctx %p %scomplete\n", ctx->type, ctx, complete ? " - " : " - in");
return complete;
}
static bool
_mdns_query_clear(mdns_query_context_t *ctx)
{
mdns_query_context_t *p;
if (ctx == NULL) return true;
bool more = false;
bool complete = _mdns_query_is_complete(ctx, &more);
if (ctx->sd != NULL)
{
if (ctx->sd != NULL && ctx->sd_gen == _mdns_generation)
{
DNSServiceRefDeallocate(ctx->sd);
}
}
ctx->sd = NULL;
ctx->sd_gen = 0;
ctx->flags = 0;
ctx->kq = -1;
if (in_flight == ctx)
{
in_flight = ctx->next;
}
else
{
p = in_flight;
while ((p != NULL) && (p->next != ctx)) p = p->next;
if (p != NULL) p->next = ctx->next;
}
ctx->next = NULL;
if (!complete && !more)
{
_mdns_hostent_clear(ctx->host);
ctx->anslen = -1;
}
return complete | more;
}
static void
_mdns_query_callback(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode, const char *fullname, uint16_t rrtype, uint16_t rrclass, uint16_t rdlen, const void *rdata, uint32_t ttl, void *ctx)
{
mdns_query_context_t *p, *context;
struct in6_addr a6;
context = (mdns_query_context_t *)ctx;
_mdns_debug_message(";; _mdns_query_callback ctx %p flags=0x%08x%s\n", context, flags, (flags & kDNSServiceFlagsMoreComing) ? " (kDNSServiceFlagsMoreComing is set)" : "");
context->flags = flags;
context->error = errorCode;
context->last_type = rrtype;
if (!(flags & kDNSServiceFlagsMoreComing))
{
for (p = in_flight; p != NULL; p = p->next)
{
if (p->flags & kDNSServiceFlagsMoreComing)
{
_mdns_debug_message(";; cleared kDNSServiceFlagsMoreComing flag for ctx %p\n", p);
p->flags &= ~kDNSServiceFlagsMoreComing;
}
}
}
if (errorCode != kDNSServiceErr_NoError)
{
_mdns_debug_message(";; [%s type %hu class %hu]: error %d [ctx %p]\n", fullname, rrtype, rrclass, errorCode, context);
goto wakeup_kevent;
}
if ((rrtype == ns_t_aaaa) && (rdlen == sizeof(struct in6_addr)) && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)rdata))
{
memcpy(&a6, rdata, rdlen);
a6.__u6_addr.__u6_addr16[1] = htons(ifIndex);
rdata = &a6;
}
if (context->reply != NULL)
{
char *name;
int malformed = 0;
mdns_reply_t *reply = context->reply;
if (reply->ifnum == 0) reply->ifnum = ifIndex;
_mdns_hostent_append_alias(context->host, fullname);
if ((reply->ttl == 0) || (ttl < reply->ttl)) reply->ttl = ttl;
switch (rrtype)
{
case ns_t_a:
case ns_t_aaaa:
{
if ((context->host != NULL) &&
((((rrtype == ns_t_a) && (context->host->host.h_addrtype == AF_INET)) || ((rrtype == ns_t_aaaa) && (context->host->host.h_addrtype == AF_INET6))) &&
(rdlen >= context->host->host.h_length)))
{
if (context->host->host.h_name == NULL)
{
int i;
mdns_hostent_t *h = context->host;
char *h_name = _mdns_canonicalize(fullname);
context->host->host.h_name = h_name;
for (i = 0; i < h->alias_count; ++i)
{
if (h_name == NULL) break;
if (string_equal(h->host.h_aliases[i], h_name))
{
int sz = sizeof(char *) * (h->alias_count - i);
free(h->host.h_aliases[i]);
memmove(&h->host.h_aliases[i], &h->host.h_aliases[i+1], sz);
h->alias_count -= 1;
break;
}
}
}
_mdns_hostent_append_addr(context->host, rdata, context->host->host.h_length);
}
else
{
malformed = 1;
}
break;
}
case ns_t_cname:
{
name = _mdns_parse_domain_name(rdata, rdlen);
if (name == NULL) malformed = 1;
_mdns_hostent_append_alias(context->host, name);
_mdns_debug_message(";; [%s type %hu class %hu] cname %s [ctx %p]\n", fullname, rrtype, rrclass, name, context);
free(name);
break;
}
case ns_t_ptr:
{
name = _mdns_parse_domain_name(rdata, rdlen);
if (name == NULL) malformed = 1;
if ((context->host != NULL) && (context->host->host.h_name == NULL))
{
context->host->host.h_name = _mdns_canonicalize(name);
}
_mdns_hostent_append_alias(context->host, name);
free(name);
break;
}
case ns_t_srv:
{
mdns_rr_srv_t *p = (mdns_rr_srv_t *)rdata;
mdns_srv_t *srv = calloc(1, sizeof(mdns_srv_t));
if (srv == NULL) break;
if (rdlen < sizeof(mdns_rr_srv_t))
{
malformed = 1;
free(srv);
break;
}
srv->srv.priority = ntohs(p->priority);
srv->srv.weight = ntohs(p->weight);
srv->srv.port = ntohs(p->port);
srv->srv.target = _mdns_parse_domain_name(&p->target[0], rdlen - 3*sizeof(uint16_t));
if (srv->srv.target == NULL)
{
malformed = 1;
free(srv);
break;
}
if (reply->srv == NULL)
{
reply->srv = srv;
}
else
{
mdns_srv_t *iter = reply->srv;
while (iter->next) iter = iter->next;
iter->next = srv;
}
break;
}
default:
{
malformed = _mdns_debug;
break;
}
}
if (malformed != 0)
{
_mdns_debug_message(";; [%s type %hu class %hu]: malformed reply [ctx %p]\n", fullname, rrtype, rrclass, context);
goto wakeup_kevent;
}
}
if (context->answer != NULL)
{
int n;
uint8_t *cp;
HEADER *ans;
size_t buflen = context->ansmaxlen - context->anslen;
if (buflen < NS_HFIXEDSZ)
{
_mdns_debug_message(";; [%s type %hu class %hu]: malformed reply (too small) [ctx %p]\n", fullname, rrtype, rrclass, context);
goto wakeup_kevent;
}
cp = context->answer + context->anslen;
n = _mdns_pack_domain_name(fullname, cp, buflen);
if (n < 0)
{
_mdns_debug_message(";; [%s type %hu class %hu]: name mismatch [ctx %p]\n", fullname, rrtype, rrclass, context);
goto wakeup_kevent;
}
if (buflen < (n + rdlen + 10))
{
_mdns_debug_message(";; [%s type %hu class %hu]: insufficient buffer space for reply [ctx %p]\n", fullname, rrtype, rrclass, context);
goto wakeup_kevent;
}
cp += n;
buflen -= n;
uint16_t word;
uint32_t longword;
word = htons(rrtype);
memcpy(cp, &word, sizeof(word));
cp += sizeof(word);
word = htons(rrclass);
memcpy(cp, &word, sizeof(word));
cp += sizeof(word);
longword = htonl(ttl);
memcpy(cp, &longword, sizeof(longword));
cp += sizeof(longword);
word = htons(rdlen);
memcpy(cp, &word, sizeof(word));
cp += sizeof(word);
memcpy(cp, rdata, rdlen);
cp += rdlen;
ans = (HEADER *)context->answer;
ans->ancount = htons(ntohs(ans->ancount) + 1);
context->anslen = (size_t)(cp - context->answer);
}
_mdns_debug_message(";; [%s type %hu class %hu] reply [ctx %p]\n", fullname, rrtype, rrclass, context);
wakeup_kevent:
if (context->kq != -1)
{
_mdns_debug_message(";; _mdns_query_callback sending kevent wakeup\n");
struct kevent ev;
EV_SET(&ev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, 0);
int res = kevent(context->kq, &ev, 1, NULL, 0, NULL);
if (res != 0) _mdns_debug_message(";; kevent EV_TRIGGER: %s [ctx %p]\n", strerror(errno), context);
}
}
static void
_mdns_now(struct timespec *now)
{
struct timeval tv;
gettimeofday(&tv, NULL);
now->tv_sec = tv.tv_sec;
now->tv_nsec = tv.tv_usec * 1000;
}
static void
_mdns_add_time(struct timespec *sum, const struct timespec *a, const struct timespec *b)
{
sum->tv_sec = a->tv_sec + b->tv_sec;
sum->tv_nsec = a->tv_nsec + b->tv_nsec;
if (sum->tv_nsec > 1000000000)
{
sum->tv_sec += (sum->tv_nsec / 1000000000);
sum->tv_nsec %= 1000000000;
}
}
static void
_mdns_deadline(struct timespec *deadline, const struct timespec *delta)
{
struct timespec now;
_mdns_now(&now);
_mdns_add_time(deadline, &now, delta);
}
static void
_mdns_sub_time(struct timespec *delta, const struct timespec *a, const struct timespec *b)
{
delta->tv_sec = a->tv_sec - b->tv_sec;
delta->tv_nsec = a->tv_nsec - b->tv_nsec;
if (delta->tv_nsec < 0)
{
delta->tv_nsec += 1000000000;
delta->tv_sec -= 1;
}
}
static void
_mdns_timeout(struct timespec *timeout, const struct timespec *deadline)
{
struct timespec now;
_mdns_now(&now);
_mdns_sub_time(timeout, deadline, &now);
}
extern int
si_inet_config(uint32_t *inet4, uint32_t *inet6);
static int
_mdns_search(const char *name, int class, int type, const char *interface, DNSServiceFlags flags, uint8_t *answer, uint32_t *anslen, mdns_reply_t *reply)
{
uint32_t ifindex;
if (interface)
{
ifindex = if_nametoindex(interface);
if (ifindex == 0) return -1;
}
else
{
ifindex = 0;
}
return _mdns_search_ex(name, class, type, ifindex, flags, answer, anslen, reply);
}
static int
_mdns_search_ex(const char *name, int class, int type, uint32_t ifindex, DNSServiceFlags flags, uint8_t *answer, uint32_t *anslen, mdns_reply_t *reply)
{
DNSServiceErrorType err = 0;
int kq, n;
struct kevent ev;
struct timespec start, finish, delta, timeout;
int res = 0;
int i, got_a_response = 0;
bool complete, initialize = true;
bool wait = true;
uint32_t n_iface_4 = 0;
si_inet_config(&n_iface_4, NULL);
if (n_iface_4 > 0) n_iface_4--;
int initialize_retries = 3;
int n_ctx = 0;
mdns_query_context_t ctx[2];
bool more_coming[2];
if (name == NULL) return -1;
#if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
if (pthread_is_threaded_np() && pthread_main_np()) os_log(OS_LOG_DEFAULT, "Warning: Libinfo call to mDNSResponder on main thread");
#endif
delta.tv_sec = RES_MAXRETRANS + 5;
delta.tv_nsec = 0;
_mdns_deadline(&finish, &delta);
timeout = delta;
_mdns_now(&start);
for (i = 0; i < 2; ++i) {
memset(&ctx[i], 0 , sizeof(mdns_query_context_t));
more_coming[i] = false;
}
kq = kqueue();
EV_SET(&ev, 1, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, 0);
n = kevent(kq, &ev, 1, NULL, 0, NULL);
if (n != 0) wait = false;
while (wait)
{
_mdns_debug_message(";; _mdns_search wait loop\n");
if (initialize)
{
initialize = false;
pthread_mutex_lock(&_mdns_mutex);
for (i = 0; i < n_ctx; ++i) _mdns_query_clear(&ctx[i]);
n_ctx = 0;
if (_mdns_sdref == NULL)
{
if (_mdns_old_sdref != NULL)
{
_mdns_generation++;
DNSServiceRefDeallocate(_mdns_old_sdref);
_mdns_old_sdref = NULL;
}
err = DNSServiceCreateConnection(&_mdns_sdref);
if ((initialize_retries-- <= 0) && (err == 0)) err = kDNSServiceErr_Unknown;
if (err != 0)
{
wait = false;
pthread_mutex_unlock(&_mdns_mutex);
break;
}
}
if (err == 0)
{
err = _mdns_query_start(&ctx[n_ctx++], reply, answer, anslen, name, class, (type == 0) ? ns_t_a : type, ifindex, flags, kq);
}
if ((err == 0) && (type == 0))
{
err = _mdns_query_start(&ctx[n_ctx++], reply, answer, anslen, name, class, ns_t_aaaa, ifindex, flags, kq);
}
if (err != 0) _mdns_debug_message(";; initialization error %d\n", err);
if ((err == kDNSServiceErr_Unknown) || (err == kDNSServiceErr_ServiceNotRunning) || (err == kDNSServiceErr_BadReference) || (err == kDNSServiceErr_DefunctConnection))
{
if (_mdns_sdref != NULL)
{
_mdns_generation++;
DNSServiceRefDeallocate(_mdns_sdref);
_mdns_sdref = NULL;
}
err = 0;
initialize = true;
pthread_mutex_unlock(&_mdns_mutex);
continue;
}
else if (err != 0)
{
pthread_mutex_unlock(&_mdns_mutex);
break;
}
int fd = DNSServiceRefSockFD(_mdns_sdref);
EV_SET(&ev, fd, EVFILT_READ, EV_ADD, 0, 0, 0);
n = kevent(kq, &ev, 1, NULL, 0, NULL);
pthread_mutex_unlock(&_mdns_mutex);
if (err != 0 || n != 0) break;
}
_mdns_debug_message(";; set kevent timeout %ld.%ld [ctx %p %p]\n", timeout.tv_sec, timeout.tv_nsec, (n_ctx > 0) ? &(ctx[0]) : NULL, (n_ctx > 1) ? &(ctx[1]) : NULL);
errno = 0;
n = kevent(kq, NULL, 0, &ev, 1, &timeout);
if ((n < 0) && (errno != EINTR))
{
res = -1;
break;
}
pthread_mutex_lock(&_mdns_mutex);
const struct timespec notimeout = { 0, 0 };
int m = kevent(kq, NULL, 0, &ev, 1, ¬imeout);
if (_mdns_sdref == NULL)
{
initialize = true;
_mdns_debug_message(";; _mdns_sdref is NULL, initialize = true\n");
}
else if (m > 0 && ev.filter == EVFILT_READ)
{
_mdns_debug_message(";; _mdns_search calling DNSServiceProcessResult\n", err);
err = DNSServiceProcessResult(_mdns_sdref);
_mdns_debug_message(";; DNSServiceProcessResult -> %s\n", err);
if ((err == kDNSServiceErr_ServiceNotRunning) || (err == kDNSServiceErr_BadReference) || (err == kDNSServiceErr_DefunctConnection))
{
_mdns_debug_message(";; DNSServiceProcessResult status %d [ctx %p %p]\n", err, (n_ctx > 0) ? &(ctx[0]) : NULL, (n_ctx > 1) ? &(ctx[1]) : NULL);
err = 0;
_mdns_generation++;
DNSServiceRefDeallocate(_mdns_sdref);
_mdns_sdref = NULL;
initialize = true;
}
}
else if (m == 0 && ev.filter == EVFILT_USER)
{
_mdns_debug_message(";; kevent wakeup\n", m, (int)ev.filter);
}
else
{
_mdns_debug_message(";; kevent m=%d ev.filter=0x%08x\n", m, ev.filter);
}
complete = true;
for (i = 0; i < n_ctx; ++i)
{
bool qc = _mdns_query_is_complete(&ctx[i], &more_coming[i]);
_mdns_debug_message(";; ctx %d %p error=%d complete=%s\n", i, &(ctx[i]), ctx[i].error, qc ? "true" : "false");
if ((ctx[i].error != 0) || qc)
{
if (ctx[i].type == ns_t_a)
{
got_a_response = GOT_DATA;
if (ctx[i].error != 0) got_a_response = GOT_ERROR;
_mdns_debug_message(";; type ns_t_a got_a_response=%s ctx %p\n", (got_a_response == GOT_DATA) ? "GOT_DATA" : "GOT_ERROR", &(ctx[i]));
}
_mdns_debug_message(";; [%s type %d class %d] finished processing ctx %p\n", name, type, class, &(ctx[i]));
}
else
{
_mdns_debug_message(";; [%s type %d class %d] continuing ctx %p\n", name, type, class, &(ctx[i]));
complete = false;
}
}
pthread_mutex_unlock(&_mdns_mutex);
if (err != 0)
{
_mdns_debug_message(";; DNSServiceProcessResult error status %d [ctx %p %p]\n", err, (n_ctx > 0) ? &(ctx[0]) : NULL, (n_ctx > 1) ? &(ctx[1]) : NULL);
break;
}
else if (complete)
{
_mdns_debug_message(";; [%s type %d class %d] done [ctx %p %p]\n", name, type, class, (n_ctx > 0) ? &(ctx[0]) : NULL, (n_ctx > 1) ? &(ctx[1]) : NULL);
break;
}
else if (more_coming[0] || more_coming[1])
{
struct timespec now, tmp, extra;
_mdns_now(&now);
_mdns_sub_time(&tmp, &now, &start);
extra.tv_sec = MEDIUM_AAAA_EXTRA;
extra.tv_nsec = 0;
_mdns_add_time(&delta, &tmp, &extra);
_mdns_sub_time(&tmp, &timeout, &delta);
if (tmp.tv_sec >= 0)
{
_mdns_debug_message(";; new timeout [%s type %d class %d] (waiting for more) %ld.%ld [ctx %p %p]\n", name, type, class, delta.tv_sec, delta.tv_nsec, (n_ctx > 0) ? &(ctx[0]) : NULL, (n_ctx > 1) ? &(ctx[1]) : NULL);
_mdns_deadline(&finish, &delta);
}
}
else if (got_a_response == GOT_DATA)
{
struct timespec now, tn, extra;
_mdns_now(&now);
_mdns_sub_time(&delta, &now, &start);
extra.tv_sec = SHORT_AAAA_EXTRA;
extra.tv_nsec = 0;
if ((delta.tv_sec == 0) && (delta.tv_nsec <= 20000000))
{
extra.tv_sec = MEDIUM_AAAA_EXTRA;
}
else if (n_iface_4 == 0)
{
extra.tv_sec = LONG_AAAA_EXTRA;
}
else if (got_a_response == GOT_ERROR)
{
extra.tv_sec = MEDIUM_AAAA_EXTRA;
}
_mdns_add_time(&tn, &delta, &delta);
_mdns_add_time(&delta, &tn, &extra);
_mdns_sub_time(&tn, &timeout, &delta);
if (tn.tv_sec >= 0)
{
_mdns_debug_message(";; new timeout [%s type %d class %d] (waiting for AAAA) %ld.%ld [ctx %p %p]\n", name, type, class, delta.tv_sec, delta.tv_nsec, (n_ctx > 0) ? &(ctx[0]) : NULL, (n_ctx > 1) ? &(ctx[1]) : NULL);
_mdns_deadline(&finish, &delta);
}
}
_mdns_timeout(&timeout, &finish);
if (timeout.tv_sec < 0)
{
_mdns_debug_message(";; [%s type %d class %d] timeout [ctx %p %p]\n", name, type, class, (n_ctx > 0) ? &(ctx[0]) : NULL, (n_ctx > 1) ? &(ctx[1]) : NULL);
break;
}
}
_mdns_debug_message(";; finished _mdns_search loop [ctx %p %p]\n", (n_ctx > 0) ? &(ctx[0]) : NULL, (n_ctx > 1) ? &(ctx[1]) : NULL);
complete = false;
pthread_mutex_lock(&_mdns_mutex);
for (i = 0; i < n_ctx; ++i)
{
bool cc = _mdns_query_clear(&ctx[i]);
complete = cc | complete;
_mdns_debug_message(";; _mdns_search ctx %p %scomplete\n", &ctx[i], cc ? "" : "in");
}
if (more_coming[0] || more_coming[1]) complete = false;
_mdns_debug_message(";; _mdns_search overall %scomplete\n", complete ? "" : "in");
pthread_mutex_unlock(&_mdns_mutex);
close(kq);
if (!complete) res = -1;
if (anslen != NULL) *anslen = ctx[0].anslen;
_mdns_debug_message(";; _mdns_search exit res %d\n", res);
return res;
}