+static int res_using_defaults()
+{
+ int i;
+
+ if (_res.nscount != _res_shadow.nscount)
+ return 0;
+
+ for (i = 0; i < _res.nscount; i++) {
+ /* Check that the address and port have not changed */
+ if ((_res.nsaddr_list[i].sin_addr.s_addr !=
+ _res_shadow.nsaddr_list[i].sin_addr.s_addr) ||
+ (_res.nsaddr_list[i].sin_port !=
+ _res_shadow.nsaddr_list[i].sin_port)) {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+/* Returns whether a dns encoded name should be sent to multicast or not */
+static int dns_is_local_name(const u_int8_t *name)
+{
+ const u_int8_t *d0 = NULL; // Top-Level Domain
+ const u_int8_t *d1 = NULL; // Second-Level Domain
+ const u_int8_t *d2 = NULL; // etc.
+ const u_int8_t *d3 = NULL;
+
+ while (*name)
+ {
+ d3 = d2;
+ d2 = d1;
+ d1 = d0;
+ d0 = name;
+ name += 1 + *name;
+ }
+
+ // "local" domains need to be resolved with multicast
+ // "local."
+ if (d0[0] == 5 && strncasecmp((char *)d0+1, "local", 5) == 0) return 1;
+
+ // "local.arpa."
+ if (d1 && d1[0] == 5 && strncasecmp((char *)d1+1, "local", 5) == 0 &&
+ d0[0] == 4 && strncasecmp((char *)d0+1, "arpa", 4) == 0) return 1;
+
+ // "local.int."
+ if (d1 && d1[0] == 5 && strncasecmp((char *)d1+1, "local", 5) == 0 &&
+ d0[0] == 3 && strncasecmp((char *)d0+1, "int", 3) == 0) return 1;
+
+ // The network 169.254/16 is defined to be link-local,
+ // so lookups in 254.169.in-addr.arpa. also need to be resolved with local multicast
+ if (d3 && d3[0] == 3 && strncasecmp((char *)d3+1, "254", 3) == 0 &&
+ d2 && d2[0] == 3 && strncasecmp((char *)d2+1, "169", 3) == 0 &&
+ d1 && d1[0] == 7 && strncasecmp((char *)d1+1, "in-addr", 7) == 0 &&
+ d0[0] == 4 && strncasecmp((char *)d0+1, "arpa", 4) == 0) return 1;
+
+ return 0;
+}
+
+#define DNS_LOCAL_DOMAIN_SERVICE_PORT 5353
+#define DNS_HEADER_SIZE 12
+
+#if BYTE_ORDER == BIG_ENDIAN
+#define my_htons(x) (x)
+#define my_htonl(x) (x)
+#else
+#define my_htons(x) ((((u_int16_t)x) >> 8) | (((u_int16_t)x) << 8))
+#define my_htonl(x) (((x) >> 24) | (((x) & 0x00FF0000) >> 16) | \
+ (((x) & 0x0000FF00) << 16) | ((x) << 24))
+#endif
+
+static const struct sockaddr_in mDNS_addr =
+{
+ sizeof(mDNS_addr),
+ AF_INET,
+ my_htons(DNS_LOCAL_DOMAIN_SERVICE_PORT),
+ {my_htonl(0xE00000FB)} /* 224.0.0.251 */
+};
+