dns-sd.c revision 5ffb0c9b03b5149ff4f5821a62be4a52408ada2a
/* -*- Mode: C; tab-width: 4 -*-
*
* Copyright (c) 2002-2013 Apple Inc. All rights reserved.
*
* Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
* ("Apple") in consideration of your agreement to the following terms, and your
* use, installation, modification or redistribution of this Apple software
* constitutes acceptance of these terms. If you do not agree with these terms,
* please do not use, install, modify or redistribute this Apple software.
*
* In consideration of your agreement to abide by the following terms, and subject
* to these terms, Apple grants you a personal, non-exclusive license, under Apple's
* copyrights in this original Apple software (the "Apple Software"), to use,
* reproduce, modify and redistribute the Apple Software, with or without
* the Apple Software in its entirety and without modifications, you must retain
* this notice and the following text and disclaimers in all such redistributions of
* the Apple Software. Neither the name, trademarks, service marks or logos of
* Apple Computer, Inc. may be used to endorse or promote products derived from the
* Apple Software without specific prior written permission from Apple. Except as
* expressly stated in this notice, no other rights or licenses, express or implied,
* are granted by Apple herein, including but not limited to any patent rights that
* may be infringed by your derivative works or by other works in which the Apple
* Software may be incorporated.
*
* The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
* COMBINATION WITH YOUR PRODUCTS.
*
* IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
To build this tool, copy and paste the following into a command line:
OS X:
gcc dns-sd.c -o dns-sd
POSIX systems:
gcc dns-sd.c -o dns-sd -I../mDNSShared -ldns_sd
Windows:
cl dns-sd.c -I../mDNSShared -DNOT_HAVE_GETOPT ws2_32.lib ..\mDNSWindows\DLL\Release\dnssd.lib
(may require that you run a Visual Studio script such as vsvars32.bat first)
*/
// For testing changes to dnssd_clientstub.c, uncomment this line and the code will be compiled
// with an embedded copy of the client stub instead of linking the system library version at runtime.
// This also useful to work around link errors when you're working on an older version of Mac OS X,
// and trying to build a newer version of the "dns-sd" command which uses new API entry points that
// aren't in the system's /usr/lib/libSystem.dylib.
//#define TEST_NEW_CLIENTSTUB 1
// When building mDNSResponder for Mac OS X 10.4 and earlier, /usr/lib/libSystem.dylib is built using its own private
// copy of dnssd_clientstub.c, which is old and doesn't have all the entry points defined in the latest version, so
// when we're building dns-sd.c on Mac OS X 10.4 or earlier, we automatically set TEST_NEW_CLIENTSTUB so that we'll
// embed a copy of the latest dnssd_clientstub.c instead of trying to link to the incomplete version in libSystem.dylib
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1040
#define TEST_NEW_CLIENTSTUB 1
#endif
#include <ctype.h>
#include <stdio.h> // For stdout, stderr
#include <stdlib.h> // For exit()
#include <string.h> // For strlen(), strcpy()
#include <errno.h> // For errno, EINTR
#include <time.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Iphlpapi.h>
#include <process.h>
typedef int pid_t;
#define strcasecmp _stricmp
static const char kFilePathSep = '\\';
#endif
#if !defined(IFNAMSIZ)
#define IFNAMSIZ 16
#endif
#define if_nametoindex if_nametoindex_win
#define if_indextoname if_indextoname_win
unsigned if_nametoindex_win(const char *ifname)
{
unsigned index = 0;
// Try and load the IP helper library dll
{
// On Vista and above there is a Posix like implementation of if_nametoindex
if ((if_nametoindex_funcptr = (if_nametoindex_funcptr_t) GetProcAddress(library, "if_nametoindex")) != NULL )
{
}
}
return index;
}
{
// Try and load the IP helper library dll
{
// On Vista and above there is a Posix like implementation of if_indextoname
if ((if_indextoname_funcptr = (if_indextoname_funcptr_t) GetProcAddress(library, "if_indextoname")) != NULL )
{
}
}
return name;
}
{
else return (sizeof(struct sockaddr));
}
#else
#include <unistd.h> // For getopt() and optind
#include <netdb.h> // For getaddrinfo()
static const char kFilePathSep = '/';
// #ifndef NOT_HAVE_SA_LEN
// #define SA_LEN(addr) ((addr)->sa_len)
// #else
#define SA_LEN(addr) (((addr)->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))
// #endif
#endif
#if (TEST_NEW_CLIENTSTUB && !defined(__APPLE_API_PRIVATE))
#define __APPLE_API_PRIVATE 1
#endif
// DNSServiceSetDispatchQueue is not supported on 10.6 & prior
#if !TEST_NEW_CLIENTSTUB && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ % 10) <= 1060)
#endif
#include "dns_sd.h"
#include "ClientCommon.h"
#include "../mDNSShared/dnssd_ipc.c"
#include "../mDNSShared/dnssd_clientlib.c"
#include "../mDNSShared/dnssd_clientstub.c"
#endif
#endif
// The "+0" is to cope with the case where _DNS_SD_H is defined but empty (e.g. on Mac OS X 10.4 and earlier)
#if _DNS_SD_H+0 >= 116
#define HAS_NAT_PMP_API 1
#define HAS_ADDRINFO_API 1
#else
#define kDNSServiceFlagsReturnIntermediates 0
#endif
//*************************************************************************************************************
// Globals
#define DS_FIXED_SIZE 4
typedef struct
{
unsigned short keyTag;
unsigned char alg;
unsigned char digestType;
unsigned char *digest;
} rdataDS;
#define DNSKEY_FIXED_SIZE 4
typedef struct
{
unsigned short flags;
unsigned char proto;
unsigned char alg;
unsigned char *data;
} rdataDNSKey;
//size of rdataRRSIG excluding signerName and signature (which are variable fields)
#define RRSIG_FIXED_SIZE 18
typedef struct
{
unsigned short typeCovered;
unsigned char alg;
unsigned char labels;
unsigned int origTTL;
unsigned int sigExpireTime;
unsigned int sigInceptTime;
unsigned short keyTag;
char signerName[256];
//unsigned char *signature
} rdataRRSig;
#define RR_TYPE_SIZE 16
static int operation;
static int num_printed;
static char addtest = 0;
#endif
// Note: the select() implementation on Windows (Winsock2) fails with any timeout much larger than this
#define LONG_TIME 100000000
static volatile int stopNow = 0;
#define EXIT_IF_LIBDISPATCH_FATAL_ERROR(E) \
if (main_queue && (E) == kDNSServiceErr_ServiceNotRunning) { fprintf(stderr, "Error code %d\n", (E)); exit(0); }
#else
#define EXIT_IF_LIBDISPATCH_FATAL_ERROR(E)
#endif
//*************************************************************************************************************
// Supporting Utility Functions
static uint16_t GetRRClass(const char *s)
{
if (!strcasecmp(s, "IN"))
return kDNSServiceClass_IN;
else
return(atoi(s));
}
{
else return(atoi(s));
}
static char *DNSTypeName(unsigned short rr_type)
{
switch (rr_type)
{
case kDNSServiceType_A: return("Addr");
case kDNSServiceType_NS: return("NS");
case kDNSServiceType_MX: return("MX");
case kDNSServiceType_CNAME: return("CNAME");
case kDNSServiceType_SOA: return("SOA");
case kDNSServiceType_PTR: return("PTR");
case kDNSServiceType_AAAA: return("AAAA");
case kDNSServiceType_NSEC: return("NSEC");
case kDNSServiceType_TSIG: return("TSIG");
case kDNSServiceType_RRSIG: return("RRSIG");
case kDNSServiceType_DNSKEY: return("DNSKEY");
case kDNSServiceType_DS: return("DS");
default:
{
static char buffer[RR_TYPE_SIZE];
return(buffer);
}
}
}
static unsigned short swap16(unsigned short x)
{
unsigned char *ptr = (unsigned char *)&x;
}
static unsigned int swap32(unsigned int x)
{
unsigned char *ptr = (unsigned char *)&x;
return (unsigned int)((unsigned int)ptr[0] << 24 | (unsigned int)ptr[1] << 16 | (unsigned int)ptr[2] << 8 | ptr[3]);
}
{
unsigned long ac;
unsigned int i;
return ac & 0xFFFF;
}
{
if (!src_data)
goto done;
dest_data = dispatch_data_create_with_transform(src_data, DISPATCH_DATA_FORMAT_TYPE_NONE, DISPATCH_DATA_FORMAT_TYPE_BASE64);
if (!dest_data)
goto done;
if (!null_str)
goto done;
if (!data)
goto done;
if (!map)
goto done;
done:
return;
#else //_DNS_SD_LIBDISPATCH
return;
#endif //_DNS_SD_LIBDISPATCH
}
static DNSServiceProtocol GetProtocol(const char *s)
{
else return(atoi(s));
}
#endif
//*************************************************************************************************************
// Sample callback functions for each of the operation types
static void printtimestamp(void)
{
int ms;
static char date[16];
static char new_date[16];
#ifdef _WIN32
#else
#endif
{
}
}
// formating time to RFC 4034 format
{
#ifdef _WIN32
_gmtime32_s(&tmTime, &t);
#else
// Time since epoch : strftime takes "tm". Convert seconds to "tm" using
// gmtime_r first and then use strftime
#endif
}
{
fprintf(stderr, "%s -D <name> <rrtype> <rrclass>(Validate query for any record type with DNSSEC)\n", arg0);
#if HAS_ADDRINFO_API
fprintf(stderr, "%s -g v4/v6/v4v6 <name> (Validate address info for hostname with DNSSEC)\n", arg0);
#endif
if (print_all) //Print all available options for dns-sd tool
{
#if HAS_NAT_PMP_API
#endif
}
}
#define MAX_LABELS 128
static void DNSSD_API enum_reply(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex,
{
DNSServiceFlags partialflags = flags & ~(kDNSServiceFlagsMoreComing | kDNSServiceFlagsAdd | kDNSServiceFlagsDefault);
char text[64];
const char *label[MAX_LABELS];
(void)sdref; // Unused
(void)ifIndex; // Unused
(void)context; // Unused
// 1. Print the header
if (num_printed++ == 0) printf("Timestamp Recommended %s domain\n", operation == 'E' ? "Registration" : "Browsing");
if (errorCode)
else if (!*replyDomain)
printf("Error: No reply domain\n");
else
{
else printf(" ");
// 2. Count the labels
{
}
// 3. Decide if we're going to clump the last two or three labels (e.g. "apple.com", or "nicta.com.au")
if (labels >= 3 && replyDomain - label[labels-1] <= 3 && label[labels-1] - label[labels-2] <= 4) initial = 3;
else initial = 1;
// 4. Print the initial one-, two- or three-label clump
for (i=0; i<initial; i++)
{
if (i>0) printf(".");
}
printf("\n");
// 5. Print the remainder of the hierarchy
{
printf(" ");
}
}
}
{
{
}
*dst++ = 0;
return 0;
}
static void DNSSD_API zonedata_resolve(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode,
const char *fullname, const char *hosttarget, uint16_t opaqueport, uint16_t txtLen, const unsigned char *txt, void *context)
{
const char *p = fullname;
char n[kDNSServiceMaxDomainName];
char t[kDNSServiceMaxDomainName];
(void)sdref; // Unused
(void)ifIndex; // Unused
(void)context; // Unused
//if (!(flags & kDNSServiceFlagsAdd)) return;
p = fullname;
if (CopyLabels(t, t + kDNSServiceMaxDomainName, &p, 2)) return; // Fetch next two labels (service type)
if (num_printed++ == 0)
{
printf("\n");
printf("; To direct clients to browse a different domain, substitute that domain in place of '@'\n");
printf("\n");
printf("; In the list of services below, the SRV records will typically reference dot-local Multicast DNS names.\n");
printf("; When transferring this zone file data to your unicast DNS server, you'll need to replace those dot-local\n");
printf("; names with the correct fully-qualified (unicast) domain name of the target host offering the service.\n");
}
printf("\n");
printf("%-47s PTR %s\n", t, n);
printf("%-47s SRV 0 0 %d %s ; Replace with unicast FQDN of target host\n", n, PortAsNumber, hosttarget);
printf("%-47s TXT ", n);
{
txt++; // Skip over length byte
printf(" \"");
{
}
printf("\"");
}
printf("\n");
}
static void DNSSD_API zonedata_browse(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode,
{
(void)sdref; // Unused
(void)context; // Unused
if (!(flags & kDNSServiceFlagsAdd)) return;
DNSServiceResolve(newref, kDNSServiceFlagsShareConnection, ifIndex, replyName, replyType, replyDomain, zonedata_resolve, newref);
}
static void DNSSD_API browse_reply(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode,
{
(void)sdref; // Unused
(void)context; // Unused
if (num_printed++ == 0) printf("Timestamp A/R Flags if %-20s %-20s %s\n", "Domain", "Service Type", "Instance Name");
if (errorCode)
else
printf("%s %8X %3d %-20s %-20s %s\n",
// To test selective cancellation of operations of shared sockets,
// cancel the current operation when we've got a multiple of five results
//if (operation == 'S' && num_printed % 5 == 0) DNSServiceRefDeallocate(sdref);
}
{
{
{
// We'd like the output to be shell-friendly, so that it can be copied and pasted unchanged into a "dns-sd -R" command.
// However, this is trickier than it seems. Enclosing a string in double quotes doesn't necessarily make it
// shell-safe, because shells still expand variables like $foo even when they appear inside quoted strings.
// Enclosing a string in single quotes is better, but when using single quotes even backslash escapes are ignored,
// meaning there's simply no way to represent a single quote (or apostrophe) inside a single-quoted string.
// The only remaining solution is not to surround the string with quotes at all, but instead to use backslash
// escapes to encode spaces and all other known shell metacharacters.
// (If we've missed any known shell metacharacters, please let us know.)
// In addition, non-printing ascii codes (0-31) are displayed as \xHH, using a two-digit hex value.
// Because '\' is itself a shell metacharacter (the shell escape character), it has to be escaped as "\\" to survive
// the round-trip to the shell and back. This means that a single '\' is represented here as EIGHT backslashes:
// The C compiler eats half of them, resulting in four appearing in the output.
// The shell parses those four as a pair of "\\" sequences, passing two backslashes to the "dns-sd -R" command.
// The "dns-sd -R" command interprets this single "\\" pair as an escaped literal backslash. Sigh.
ptr++;
}
}
}
static void DNSSD_API resolve_reply(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode,
const char *fullname, const char *hosttarget, uint16_t opaqueport, uint16_t txtLen, const unsigned char *txtRecord, void *context)
{
(void)sdref; // Unused
(void)ifIndex; // Unused
(void)context; // Unused
if (errorCode)
else
{
// Don't show degenerate TXT records containing nothing but a single empty string
printf("\n");
}
}
static void myTimerCallBack(void)
{
switch (operation)
{
case 'A':
{
switch (addtest)
{
case 0: printf("Adding Test HINFO record\n");
err = DNSServiceAddRecord(client, &record, 0, kDNSServiceType_HINFO, sizeof(myhinfoW), &myhinfoW[0], 0);
addtest = 1;
break;
addtest = 2;
break;
addtest = 0;
break;
}
}
break;
case 'U':
{
}
break;
case 'N':
{
printf("Adding big NULL record\n");
err = DNSServiceAddRecord(client, &record, 0, kDNSServiceType_NULL, sizeof(bigNULL), &bigNULL[0], 0);
if (timer_source)
dispatch_source_set_timer(timer_source, dispatch_time(DISPATCH_TIME_NOW, (uint64_t)timeOut * NSEC_PER_SEC),
#endif
}
break;
}
if (err != kDNSServiceErr_NoError)
{
stopNow = 1;
}
}
static void DNSSD_API reg_reply(DNSServiceRef sdref, const DNSServiceFlags flags, DNSServiceErrorType errorCode,
{
(void)sdref; // Unused
(void)flags; // Unused
(void)context; // Unused
if (errorCode == kDNSServiceErr_NoError)
{
else printf("Name registration removed\n");
{
timeOut = 5;
if (timer_source)
dispatch_source_set_timer(timer_source, dispatch_time(DISPATCH_TIME_NOW, (uint64_t)timeOut * NSEC_PER_SEC),
#endif
}
}
else if (errorCode == kDNSServiceErr_NameConflict)
{
printf("Name in use, please choose another\n");
exit(-1);
}
else
}
// Output the wire-format domainname pointed to by rd
{
const char *const buf = p;
while (**rd)
{
}
return(p-buf);
}
static void ParseDNSSECRecords(uint16_t rrtype, char *rdb, char *p, unsigned const char *rd, uint16_t rdlen)
{
int rdb_size = 1000;
switch (rrtype)
{
case kDNSServiceType_DS:
{
unsigned char *ptr;
int i;
for (i = 0; i < (rdlen - DS_FIXED_SIZE); i++)
break;
}
case kDNSServiceType_DNSKEY:
{
base64Encode(p, rdb + rdb_size - p, (unsigned char *)(rd + DNSKEY_FIXED_SIZE), rdlen - DNSKEY_FIXED_SIZE);
break;
}
case kDNSServiceType_NSEC:
{
unsigned char *bmap;
char *l = NULL;
l = p;
len = p - l + 1;
while (bitmaplen > 0)
{
int i;
if (bitmaplen < 3)
{
break;
}
bitmaplen -= 2;
{
break;
}
{
break;
}
for (i = 0; i < wlen * 8; i++)
{
}
}
break;
}
case kDNSServiceType_RRSIG:
{
unsigned char expTimeBuf[64];
unsigned char inceptTimeBuf[64];
unsigned long inceptClock;
unsigned long expClock;
const unsigned char *q = NULL;
char *k = NULL;
int len;
q = (const unsigned char *)&rrsig->signerName;
k = p;
len = p - k + 1;
base64Encode(p, rdb + rdb_size - p, (unsigned char *)(rd + len + RRSIG_FIXED_SIZE), rdlen - (len + RRSIG_FIXED_SIZE));
break;
}
}
return;
}
static void DNSSD_API qr_reply(DNSServiceRef sdref, const 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 *context)
{
int unknowntype = 0;
char rr_type[RR_TYPE_SIZE];
char rr_class[3];
(void)sdref; // Unused
(void)ifIndex; // Unused
(void)ttl; // Unused
(void)context; // Unused
if (num_printed++ == 0)
{
if (operation == 'D')
else
}
switch (rrclass)
{
case kDNSServiceClass_IN:
break;
default:
break;
}
if (!errorCode) //to avoid printing garbage in rdata
{
{
switch (rrtype)
{
case kDNSServiceType_A:
break;
case kDNSServiceType_NS:
case kDNSServiceType_CNAME:
case kDNSServiceType_PTR:
case kDNSServiceType_DNAME:
break;
case kDNSServiceType_SOA:
ntohl(((uint32_t*)rd)[0]), ntohl(((uint32_t*)rd)[1]), ntohl(((uint32_t*)rd)[2]), ntohl(((uint32_t*)rd)[3]), ntohl(((uint32_t*)rd)[4]));
break;
case kDNSServiceType_AAAA:
snprintf(rdb, sizeof(rdb), "%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X",
break;
case kDNSServiceType_SRV:
rd += 6;
break;
case kDNSServiceType_DS:
case kDNSServiceType_DNSKEY:
case kDNSServiceType_NSEC:
case kDNSServiceType_RRSIG:
break;
default:
unknowntype = 1;
break;
}
}
else
{
//Clear all o/p bits, and then check for dnssec status
else if (check_flags & kDNSServiceFlagsInsecure)
else if (check_flags & kDNSServiceFlagsIndeterminate)
else if (check_flags & kDNSServiceFlagsBogus)
}
}
if (operation == 'D')
printf("%s%3d %-30s%-6s%-7s%-18s %s", op, ifIndex, fullname, rr_type, rr_class, dnssec_status, rdb);
else
if (unknowntype)
{
}
if (errorCode)
{
if (errorCode == kDNSServiceErr_NoSuchRecord)
printf(" No Such Record");
else if (errorCode == kDNSServiceErr_Timeout)
{
printf(" No Such Record\n");
printf("Query Timed Out\n");
exit(1);
}
}
printf("\n");
if (operation == 'C')
if (flags & kDNSServiceFlagsAdd)
if (!(flags & kDNSServiceFlagsMoreComing))
}
#if HAS_NAT_PMP_API
static void DNSSD_API port_mapping_create_reply(DNSServiceRef sdref, DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode, uint32_t publicAddress, uint32_t protocol, uint16_t privatePort, uint16_t publicPort, uint32_t ttl, void *context)
{
(void)sdref; // Unused
(void)flags; // Unused
(void)context; // Unused
if (num_printed++ == 0) printf("Timestamp if %-20s %-15s %-15s %-15s %-6s\n", "External Address", "Protocol", "Internal Port", "External Port", "TTL");
else
{
const unsigned char *digits = (const unsigned char *)&publicAddress;
char addr[256];
printf("%-4d %-20s %-15d %-15d %-15d %-6d%s\n", ifIndex, addr, protocol, ntohs(privatePort), ntohs(publicPort), ttl, errorCode == kDNSServiceErr_DoubleNAT ? " Double NAT" : "");
}
}
#endif
#if HAS_ADDRINFO_API
static void DNSSD_API addrinfo_reply(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *hostname, const struct sockaddr *address, uint32_t ttl, void *context)
{
(void) sdref;
(void) context;
if (num_printed++ == 0)
{
if (operation == 'g')
else
}
{
}
{
snprintf(addr, sizeof(addr), "%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X%%%s",
b[0x0], b[0x1], b[0x2], b[0x3], b[0x4], b[0x5], b[0x6], b[0x7],
}
//go through this only if you have a dnssec validation status
{
//Clear all o/p bits, and then check for dnssec status
else if (check_flags & kDNSServiceFlagsInsecure)
else if (check_flags & kDNSServiceFlagsIndeterminate)
else if (check_flags & kDNSServiceFlagsBogus)
}
if (operation == 'g')
else
if (errorCode)
{
if (errorCode == kDNSServiceErr_NoSuchRecord)
printf(" No Such Record");
else
}
printf("\n");
if (!(flags & kDNSServiceFlagsMoreComing))
}
#endif
//*************************************************************************************************************
// The main test function
static void HandleEvents(void)
{
{
if (timer_source)
{
// Start the timer "timeout" seconds into the future and repeat it every "timeout" seconds
dispatch_source_set_timer(timer_source, dispatch_time(DISPATCH_TIME_NOW, (uint64_t)timeOut * NSEC_PER_SEC),
}
}
}
#else
{
int result;
while (!stopNow)
{
// 1. Set up the fd_set as usual here.
// This example client has no file descriptors of its own,
// but a real application would call FD_SET to add them to the set here
// 2. Add the fd for our client(s) to the fd_set
// 3. Set up the timeout.
if (result > 0)
{
}
else if (result == 0)
else
{
}
}
}
#endif
// Return the recognized option in optstr and the option index of the next arg.
#if NOT_HAVE_GETOPT
{
int i;
for (i=1; i < argc; i++)
{
{
*pOptInd = i + 1;
return argv[i][1];
}
}
return -1;
}
#else
{
return o;
}
#endif
static void DNSSD_API MyRegisterRecordCallback(DNSServiceRef service, DNSRecordRef rec, const DNSServiceFlags flags,
{
(void)service; // Unused
(void)rec; // Unused
(void)flags; // Unused
switch (errorCode)
{
}
// DNSServiceRemoveRecord(service, rec, 0); to test record removal
#if 0 // To test updating of individual records registered via DNSServiceRegisterRecord
if (!errorCode)
{
int x = 0x11111111;
printf("Updating\n");
}
#endif
}
{
}
static DNSServiceErrorType RegisterProxyAddressRecord(DNSServiceRef sdref, const char *host, const char *ip, DNSServiceFlags flags)
{
// Call getip() after the call DNSServiceCreateConnection().
// On the Win32 platform, WinSock must be initialized for getip() to succeed.
// Any DNSService* call will initialize WinSock for us, so we make sure
// DNSServiceCreateConnection() is called before getip() is.
struct sockaddr_storage hostaddr;
kDNSServiceType_A, kDNSServiceClass_IN, 4, &((struct sockaddr_in *)&hostaddr)->sin_addr, 240, MyRegisterRecordCallback, (void*)host));
kDNSServiceType_AAAA, kDNSServiceClass_IN, 16, &((struct sockaddr_in6*)&hostaddr)->sin6_addr, 240, MyRegisterRecordCallback, (void*)host));
else return(kDNSServiceErr_BadParam);
}
((X) >= 'A' && (X) <= 'F') ? ((X) - 'A' + 10) : \
((X) >= 'a' && (X) <= 'f') ? ((X) - 'a' + 10) : 0)
const char *nam, const char *typ, const char *dom, const char *host, const char *port, int argc, char **argv, DNSServiceFlags flags)
{
int i;
if (nam[0] == '.' && nam[1] == 0) nam = ""; // We allow '.' on the command line as a synonym for empty string
if (dom[0] == '.' && dom[1] == 0) dom = ""; // We allow '.' on the command line as a synonym for empty string
if (argc)
{
for (i = 0; i < argc; i++)
{
const char *p = argv[i];
*ptr = 0;
{
}
}
printf(" TXT");
}
printf("\n");
//flags |= kDNSServiceFlagsAllowRemoteQuery;
//flags |= kDNSServiceFlagsNoAutoRename;
return(DNSServiceRegister(sdref, flags, opinterface, nam, typ, dom, host, registerPort.NotAnInteger, (uint16_t) (ptr-txt), txt, reg_reply, NULL));
}
#define TypeBufferSize 80
{
return(typ);
}
{
int opi;
DNSServiceFlags flags = 0;
int optional = 0;
// Extract the program name from argv[0], which by convention contains the path to this executable.
// Note that this is just a voluntary convention, not enforced by the kernel --
// the process calling exec() can pass bogus data in argv[0] if it chooses to.
#if defined(_WIN32)
#endif
printf("Using embedded copy of dnssd_clientstub instead of system library\n");
#endif
// Test code for TXTRecord functions
//TXTRecordRef txtRecord;
//TXTRecordCreate(&txtRecord, 0, NULL);
//TXTRecordSetValue(&txtRecord, "aaa", 1, "b");
//printf("%d\n", TXTRecordContainsKey(TXTRecordGetLength(&txtRecord), TXTRecordGetBytesPtr(&txtRecord), "Aaa"));
{
argc--;
argv++;
printf("Using LocalOnly\n");
}
{
argc--;
argv++;
}
{
argc--;
argv++;
printf("Setting kDNSServiceFlagsIncludeP2P\n");
}
{
argc--;
argv++;
printf("Setting kDNSServiceFlagsIncludeAWDL\n");
}
{
argc--;
argv++;
printf("Setting kDNSServiceFlagsBackgroundTrafficClass\n");
}
{
argc--;
argv++;
printf("Setting kDNSServiceFlagsThresholdOne\n");
}
{
argc--;
argv++;
printf("Setting kDNSServiceFlagsThresholdFinder\n");
}
{
argc--;
argv++;
printf("Setting kDNSServiceFlagsWakeOnlyService\n");
}
{
argc--;
argv++;
printf("Setting kDNSServiceFlagsUnicastResponse\n");
}
{
argc--;
argv++;
printf("Setting kDNSServiceFlagsTimeout\n");
}
{
argc--;
argv++;
optional = 1;
printf("Setting DNSSEC optional flag\n");
}
{
argc -= 2;
argv += 2;
}
#if HAS_NAT_PMP_API
"X"
#endif
#if HAS_ADDRINFO_API
"Gg"
#endif
, &opi);
switch (operation)
{
err = DNSServiceEnumerateDomains(&client, kDNSServiceFlagsRegistrationDomains, opinterface, enum_reply, NULL);
break;
err = DNSServiceEnumerateDomains(&client, kDNSServiceFlagsBrowseDomains, opinterface, enum_reply, NULL);
//enum_reply(client, kDNSServiceFlagsAdd, 0, 0, "nicta.com.au.", NULL);
//enum_reply(client, kDNSServiceFlagsAdd, 0, 0, "bonjour.nicta.com.au.", NULL);
//enum_reply(client, kDNSServiceFlagsAdd, 0, 0, "ibm.com.", NULL);
//enum_reply(client, kDNSServiceFlagsAdd, 0, 0, "dns-sd.ibm.com.", NULL);
break;
dom = (argc < opi+2) ? "" : argv[opi+1]; // Missing domain argument is the same as empty string i.e. use system default(s)
if (dom[0] == '.' && dom[1] == 0) dom[0] = 0; // We allow '.' on the command line as a synonym for empty string
break;
dom = (argc < opi+2) ? "" : argv[opi+1]; // Missing domain argument is the same as empty string i.e. use system default(s)
if (dom[0] == '.' && dom[1] == 0) dom[0] = 0; // We allow '.' on the command line as a synonym for empty string
err = DNSServiceBrowse(&sc1, kDNSServiceFlagsShareConnection, opinterface, typ, dom, zonedata_browse, NULL);
break;
case 'l':
case 'L': {
if (dom[0] == '.' && dom[1] == 0) dom = "local"; // We allow '.' on the command line as a synonym for "local"
break;
}
if (dom[0] == '.' && dom[1] == 0) dom[0] = 0; // We allow '.' on the command line as a synonym for empty string
err = RegisterService(&client, argv[opi+0], typ, dom, NULL, argv[opi+3], argc-(opi+4), argv+(opi+4), flags);
break;
if (err) break;
err = RegisterService(&client, argv[opi+0], gettype(buffer, argv[opi+1]), argv[opi+2], argv[opi+4], argv[opi+3], argc-(opi+6), argv+(opi+6), flags);
break;
case 'D':
case 'q':
case 'Q':
case 'C': {
if (operation == 'q')
goto Fail;
if (operation == 'D')
{
if (optional)
else
}
err = DNSServiceQueryRecord(&client, flags, opinterface, argv[opi+0], rrtype, rrclass, qr_reply, NULL);
break;
}
case 'A':
case 'U':
case 'N': {
static const char TXT[] = "\xC" "First String" "\xD" "Second String" "\xC" "Third String";
printf("Registering Service Test._testupdate._tcp.local.\n");
err = DNSServiceRegister(&client, 0, opinterface, "Test", "_testupdate._tcp.", "", NULL, registerPort.NotAnInteger, sizeof(TXT)-1, TXT, reg_reply, NULL);
break;
}
case 'T': {
char TXT[1024];
unsigned int i;
for (i=0; i<sizeof(TXT); i++)
printf("Registering Service Test._testlargetxt._tcp.local.\n");
err = DNSServiceRegister(&client, 0, opinterface, "Test", "_testlargetxt._tcp.", "", NULL, registerPort.NotAnInteger, sizeof(TXT), TXT, reg_reply, NULL);
break;
}
case 'M': {
static const char TXT1[] = "\xC" "First String" "\xD" "Second String" "\xC" "Third String";
static const char TXT2[] = "\xD" "Fourth String" "\xC" "Fifth String" "\xC" "Sixth String";
printf("Registering Service Test._testdualtxt._tcp.local.\n");
err = DNSServiceRegister(&client, flags, opinterface, "Test", "_testdualtxt._tcp.", "", NULL, registerPort.NotAnInteger, sizeof(TXT1)-1, TXT1, reg_reply, NULL);
if (!err) err = DNSServiceAddRecord(client, &record, flags, kDNSServiceType_TXT, sizeof(TXT2)-1, TXT2, 0);
break;
}
case 'I': {
static const char TXT[] = "\x09" "Test Data";
printf("Registering Service Test._testtxt._tcp.local.\n");
err = DNSServiceRegister(&client, 0, opinterface, "Test", "_testtxt._tcp.", "", NULL, registerPort.NotAnInteger, 0, NULL, reg_reply, NULL);
break;
}
#if HAS_NAT_PMP_API
case 'X': {
{
uint16_t ExtPortAsNumber = (argc < opi+3) ? 0 : atoi(argv[opi+2]); // Optional desired external port
err = DNSServiceNATPortMappingCreate(&client, 0, 0, prot, intp.NotAnInteger, extp.NotAnInteger, ttl, port_mapping_create_reply, NULL);
}
else goto Fail;
break;
}
#endif
#if HAS_ADDRINFO_API
case 'g':
case 'G': {
if (operation == 'g')
{
if (optional)
else
}
goto Fail;
else
err = DNSServiceGetAddrInfo(&client, flags, opinterface, GetProtocol(argv[opi+0]), argv[opi+1], addrinfo_reply, NULL);
break;
}
#endif
case 'S': {
if (err) { fprintf(stderr, "DNSServiceCreateConnection failed %ld\n", (long int)err); return (-1); }
err = DNSServiceBrowse(&sc1, kDNSServiceFlagsShareConnection, opinterface, "_http._tcp", "", browse_reply, NULL);
if (err) { fprintf(stderr, "DNSServiceBrowse _http._tcp failed %ld\n", (long int)err); return (-1); }
err = DNSServiceBrowse(&sc2, kDNSServiceFlagsShareConnection, opinterface, "_ftp._tcp", "", browse_reply, NULL);
if (err) { fprintf(stderr, "DNSServiceBrowse _ftp._tcp failed %ld\n", (long int)err); return (-1); }
err = DNSServiceRegister(&sc3, kDNSServiceFlagsShareConnection, opinterface, "kDNSServiceFlagsShareConnection",
if (err) { fprintf(stderr, "SharedConnection DNSServiceRegister failed %ld\n", (long int)err); return (-1); }
if (err) { fprintf(stderr, "SharedConnection DNSServiceUpdateRecord failed %ld\n", (long int)err); return (-1); }
if (err) { fprintf(stderr, "SharedConnection DNSServiceAddRecord failed %ld\n", (long int)err); return (-1); }
if (err) { fprintf(stderr, "SharedConnection DNSServiceRemoveRecord failed %ld\n", (long int)err); return (-1); }
break;
}
case 'V': {
uint32_t v;
else printf("Currently running daemon (system service) is version %d.%d.%d\n", v / 10000, v / 100 % 100, v % 100);
exit(0);
}
case 'H': goto Fail;
default: goto Fail;
}
{
return (-1);
}
printf("...STARTING...\n");
HandleEvents();
// Be sure to deallocate the DNSServiceRef when you're finished
return 0;
Fail:
else print_usage(a0,0);
return 0;
}
// Note: The C preprocessor stringify operator ('#') makes a string from its argument, without macro expansion
// e.g. If "version" is #define'd to be "4", then STRINGIFY_AWE(version) will return the string "version", not "4"
// To expand "version" to its value before making the string, use STRINGIFY(version) instead
#define STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) # s
#define STRINGIFY(s) STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s)
// NOT static -- otherwise the compiler may optimize it out
// The "@(#) " pattern is a special prefix the "what" command looks for
const char VersionString_SCCS[] = "@(#) dns-sd " STRINGIFY(mDNSResponderVersion) " (" __DATE__ " " __TIME__ ")";
// If the process crashes, then this string will be magically included in the automatically-generated crash log
asm (".desc ___crashreporter_info__, 0x10");
#endif