15ffb0c9bSToomas Soome /* -*- Mode: C; tab-width: 4 -*-
25ffb0c9bSToomas Soome  *
3*472cd20dSToomas Soome  * Copyright (c) 2002-2020 Apple Inc. All rights reserved.
45ffb0c9bSToomas Soome  *
5cda73f64SToomas Soome  * Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc.
65ffb0c9bSToomas Soome  * ("Apple") in consideration of your agreement to the following terms, and your
75ffb0c9bSToomas Soome  * use, installation, modification or redistribution of this Apple software
85ffb0c9bSToomas Soome  * constitutes acceptance of these terms.  If you do not agree with these terms,
95ffb0c9bSToomas Soome  * please do not use, install, modify or redistribute this Apple software.
105ffb0c9bSToomas Soome  *
115ffb0c9bSToomas Soome  * In consideration of your agreement to abide by the following terms, and subject
125ffb0c9bSToomas Soome  * to these terms, Apple grants you a personal, non-exclusive license, under Apple's
135ffb0c9bSToomas Soome  * copyrights in this original Apple software (the "Apple Software"), to use,
145ffb0c9bSToomas Soome  * reproduce, modify and redistribute the Apple Software, with or without
155ffb0c9bSToomas Soome  * modifications, in source and/or binary forms; provided that if you redistribute
165ffb0c9bSToomas Soome  * the Apple Software in its entirety and without modifications, you must retain
175ffb0c9bSToomas Soome  * this notice and the following text and disclaimers in all such redistributions of
185ffb0c9bSToomas Soome  * the Apple Software.  Neither the name, trademarks, service marks or logos of
19cda73f64SToomas Soome  * Apple Inc. may be used to endorse or promote products derived from the
205ffb0c9bSToomas Soome  * Apple Software without specific prior written permission from Apple.  Except as
215ffb0c9bSToomas Soome  * expressly stated in this notice, no other rights or licenses, express or implied,
225ffb0c9bSToomas Soome  * are granted by Apple herein, including but not limited to any patent rights that
235ffb0c9bSToomas Soome  * may be infringed by your derivative works or by other works in which the Apple
245ffb0c9bSToomas Soome  * Software may be incorporated.
255ffb0c9bSToomas Soome  *
265ffb0c9bSToomas Soome  * The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
315ffb0c9bSToomas Soome  *
395ffb0c9bSToomas Soome  *
405ffb0c9bSToomas Soome    To build this tool, copy and paste the following into a command line:
415ffb0c9bSToomas Soome 
425ffb0c9bSToomas Soome    OS X:
435ffb0c9bSToomas Soome    gcc dns-sd.c -o dns-sd
445ffb0c9bSToomas Soome 
455ffb0c9bSToomas Soome    POSIX systems:
465ffb0c9bSToomas Soome    gcc dns-sd.c -o dns-sd -I../mDNSShared -ldns_sd
475ffb0c9bSToomas Soome 
485ffb0c9bSToomas Soome    Windows:
495ffb0c9bSToomas Soome    cl dns-sd.c -I../mDNSShared -DNOT_HAVE_GETOPT ws2_32.lib ..\mDNSWindows\DLL\Release\dnssd.lib
505ffb0c9bSToomas Soome    (may require that you run a Visual Studio script such as vsvars32.bat first)
515ffb0c9bSToomas Soome  */
525ffb0c9bSToomas Soome 
535ffb0c9bSToomas Soome // For testing changes to dnssd_clientstub.c, uncomment this line and the code will be compiled
545ffb0c9bSToomas Soome // with an embedded copy of the client stub instead of linking the system library version at runtime.
555ffb0c9bSToomas Soome // This also useful to work around link errors when you're working on an older version of Mac OS X,
565ffb0c9bSToomas Soome // and trying to build a newer version of the "dns-sd" command which uses new API entry points that
575ffb0c9bSToomas Soome // aren't in the system's /usr/lib/libSystem.dylib.
585ffb0c9bSToomas Soome //#define TEST_NEW_CLIENTSTUB 1
595ffb0c9bSToomas Soome 
605ffb0c9bSToomas Soome #include <ctype.h>
615ffb0c9bSToomas Soome #include <stdio.h>          // For stdout, stderr
625ffb0c9bSToomas Soome #include <stdlib.h>         // For exit()
635ffb0c9bSToomas Soome #include <string.h>         // For strlen(), strcpy()
645ffb0c9bSToomas Soome #include <errno.h>          // For errno, EINTR
655ffb0c9bSToomas Soome #include <time.h>
665ffb0c9bSToomas Soome #include <sys/types.h>      // For u_char
67*472cd20dSToomas Soome #ifdef APPLE_OSX_mDNSResponder
68*472cd20dSToomas Soome #include <inttypes.h>       // For PRId64
69*472cd20dSToomas Soome #endif // APPLE_OSX_mDNSResponder
70*472cd20dSToomas Soome 
71*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
72*472cd20dSToomas Soome #include <xpc/xpc.h>
73*472cd20dSToomas Soome #include "xpc_clients.h"
74*472cd20dSToomas Soome #endif
755ffb0c9bSToomas Soome 
765ffb0c9bSToomas Soome #ifdef _WIN32
775ffb0c9bSToomas Soome     #include <winsock2.h>
785ffb0c9bSToomas Soome     #include <ws2tcpip.h>
795ffb0c9bSToomas Soome     #include <Iphlpapi.h>
805ffb0c9bSToomas Soome     #include <process.h>
815ffb0c9bSToomas Soome typedef int pid_t;
825ffb0c9bSToomas Soome     #define getpid     _getpid
835ffb0c9bSToomas Soome     #define strcasecmp _stricmp
845ffb0c9bSToomas Soome     #define snprintf   _snprintf
855ffb0c9bSToomas Soome static const char kFilePathSep = '\\';
865ffb0c9bSToomas Soome     #ifndef HeapEnableTerminationOnCorruption
875ffb0c9bSToomas Soome     #     define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
885ffb0c9bSToomas Soome     #endif
895ffb0c9bSToomas Soome     #if !defined(IFNAMSIZ)
905ffb0c9bSToomas Soome      #define IFNAMSIZ 16
915ffb0c9bSToomas Soome     #endif
925ffb0c9bSToomas Soome     #define if_nametoindex if_nametoindex_win
935ffb0c9bSToomas Soome     #define if_indextoname if_indextoname_win
945ffb0c9bSToomas Soome 
955ffb0c9bSToomas Soome typedef PCHAR (WINAPI * if_indextoname_funcptr_t)(ULONG index, PCHAR name);
965ffb0c9bSToomas Soome typedef ULONG (WINAPI * if_nametoindex_funcptr_t)(PCSTR name);
975ffb0c9bSToomas Soome 
if_nametoindex_win(const char * ifname)985ffb0c9bSToomas Soome unsigned if_nametoindex_win(const char *ifname)
995ffb0c9bSToomas Soome {
1005ffb0c9bSToomas Soome     HMODULE library;
1015ffb0c9bSToomas Soome     unsigned index = 0;
1025ffb0c9bSToomas Soome 
1035ffb0c9bSToomas Soome     // Try and load the IP helper library dll
1045ffb0c9bSToomas Soome     if ((library = LoadLibrary(TEXT("Iphlpapi")) ) != NULL )
1055ffb0c9bSToomas Soome     {
1065ffb0c9bSToomas Soome         if_nametoindex_funcptr_t if_nametoindex_funcptr;
1075ffb0c9bSToomas Soome 
1085ffb0c9bSToomas Soome         // On Vista and above there is a Posix like implementation of if_nametoindex
1095ffb0c9bSToomas Soome         if ((if_nametoindex_funcptr = (if_nametoindex_funcptr_t) GetProcAddress(library, "if_nametoindex")) != NULL )
1105ffb0c9bSToomas Soome         {
1115ffb0c9bSToomas Soome             index = if_nametoindex_funcptr(ifname);
1125ffb0c9bSToomas Soome         }
1135ffb0c9bSToomas Soome 
1145ffb0c9bSToomas Soome         FreeLibrary(library);
1155ffb0c9bSToomas Soome     }
1165ffb0c9bSToomas Soome 
1175ffb0c9bSToomas Soome     return index;
1185ffb0c9bSToomas Soome }
1195ffb0c9bSToomas Soome 
if_indextoname_win(unsigned ifindex,char * ifname)1205ffb0c9bSToomas Soome char * if_indextoname_win( unsigned ifindex, char *ifname)
1215ffb0c9bSToomas Soome {
1225ffb0c9bSToomas Soome     HMODULE library;
1235ffb0c9bSToomas Soome     char * name = NULL;
1245ffb0c9bSToomas Soome 
1255ffb0c9bSToomas Soome     // Try and load the IP helper library dll
1265ffb0c9bSToomas Soome     if ((library = LoadLibrary(TEXT("Iphlpapi")) ) != NULL )
1275ffb0c9bSToomas Soome     {
1285ffb0c9bSToomas Soome         if_indextoname_funcptr_t if_indextoname_funcptr;
1295ffb0c9bSToomas Soome 
1305ffb0c9bSToomas Soome         // On Vista and above there is a Posix like implementation of if_indextoname
1315ffb0c9bSToomas Soome         if ((if_indextoname_funcptr = (if_indextoname_funcptr_t) GetProcAddress(library, "if_indextoname")) != NULL )
1325ffb0c9bSToomas Soome         {
1335ffb0c9bSToomas Soome             name = if_indextoname_funcptr(ifindex, ifname);
1345ffb0c9bSToomas Soome         }
1355ffb0c9bSToomas Soome 
1365ffb0c9bSToomas Soome         FreeLibrary(library);
1375ffb0c9bSToomas Soome     }
1385ffb0c9bSToomas Soome 
1395ffb0c9bSToomas Soome     return name;
1405ffb0c9bSToomas Soome }
1415ffb0c9bSToomas Soome 
_sa_len(const struct sockaddr * addr)1425ffb0c9bSToomas Soome static size_t _sa_len(const struct sockaddr *addr)
1435ffb0c9bSToomas Soome {
1445ffb0c9bSToomas Soome     if (addr->sa_family == AF_INET) return (sizeof(struct sockaddr_in));
1455ffb0c9bSToomas Soome     else if (addr->sa_family == AF_INET6) return (sizeof(struct sockaddr_in6));
1465ffb0c9bSToomas Soome     else return (sizeof(struct sockaddr));
1475ffb0c9bSToomas Soome }
1485ffb0c9bSToomas Soome 
1495ffb0c9bSToomas Soome #   define SA_LEN(addr) (_sa_len(addr))
1505ffb0c9bSToomas Soome 
1515ffb0c9bSToomas Soome #else
1525ffb0c9bSToomas Soome     #include <unistd.h>         // For getopt() and optind
1535ffb0c9bSToomas Soome     #include <netdb.h>          // For getaddrinfo()
1545ffb0c9bSToomas Soome     #include <sys/time.h>       // For struct timeval
1555ffb0c9bSToomas Soome     #include <sys/socket.h>     // For AF_INET
1565ffb0c9bSToomas Soome     #include <netinet/in.h>     // For struct sockaddr_in()
1575ffb0c9bSToomas Soome     #include <arpa/inet.h>      // For inet_addr()
1585ffb0c9bSToomas Soome     #include <net/if.h>         // For if_nametoindex()
1595ffb0c9bSToomas Soome static const char kFilePathSep = '/';
1605ffb0c9bSToomas Soome // #ifndef NOT_HAVE_SA_LEN
1615ffb0c9bSToomas Soome //  #define SA_LEN(addr) ((addr)->sa_len)
1625ffb0c9bSToomas Soome // #else
1635ffb0c9bSToomas Soome     #define SA_LEN(addr) (((addr)->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))
1645ffb0c9bSToomas Soome // #endif
1655ffb0c9bSToomas Soome #endif
1665ffb0c9bSToomas Soome 
1675ffb0c9bSToomas Soome #if (TEST_NEW_CLIENTSTUB && !defined(__APPLE_API_PRIVATE))
1685ffb0c9bSToomas Soome #define __APPLE_API_PRIVATE 1
1695ffb0c9bSToomas Soome #endif
1705ffb0c9bSToomas Soome 
1715ffb0c9bSToomas Soome // DNSServiceSetDispatchQueue is not supported on 10.6 & prior
1735ffb0c9bSToomas Soome #undef _DNS_SD_LIBDISPATCH
1745ffb0c9bSToomas Soome #endif
1755ffb0c9bSToomas Soome #include "dns_sd.h"
176*472cd20dSToomas Soome #include "dns_sd_private.h"
1775ffb0c9bSToomas Soome #include "ClientCommon.h"
178*472cd20dSToomas Soome #include <stdarg.h>
179*472cd20dSToomas Soome 
1805ffb0c9bSToomas Soome 
1815ffb0c9bSToomas Soome #if TEST_NEW_CLIENTSTUB
1825ffb0c9bSToomas Soome #include "../mDNSShared/dnssd_ipc.c"
1835ffb0c9bSToomas Soome #include "../mDNSShared/dnssd_clientlib.c"
1845ffb0c9bSToomas Soome #include "../mDNSShared/dnssd_clientstub.c"
1855ffb0c9bSToomas Soome #endif
1865ffb0c9bSToomas Soome 
187*472cd20dSToomas Soome #ifndef MIN
188*472cd20dSToomas Soome #define	MIN(x, y)	((x) < (y) ? (x) : (y))
189*472cd20dSToomas Soome #endif
1905ffb0c9bSToomas Soome //*************************************************************************************************************
1915ffb0c9bSToomas Soome // Globals
1925ffb0c9bSToomas Soome 
1935ffb0c9bSToomas Soome #define DS_FIXED_SIZE   4
1945ffb0c9bSToomas Soome typedef struct
1955ffb0c9bSToomas Soome {
1965ffb0c9bSToomas Soome     unsigned short keyTag;
197c1de7575SRichard Lowe     unsigned char alg;
1985ffb0c9bSToomas Soome     unsigned char digestType;
1995ffb0c9bSToomas Soome     unsigned char  *digest;
2005ffb0c9bSToomas Soome } rdataDS;
2015ffb0c9bSToomas Soome 
2025ffb0c9bSToomas Soome #define DNSKEY_FIXED_SIZE    4
2035ffb0c9bSToomas Soome typedef struct
2045ffb0c9bSToomas Soome {
2055ffb0c9bSToomas Soome     unsigned short flags;
2065ffb0c9bSToomas Soome     unsigned char proto;
2075ffb0c9bSToomas Soome     unsigned char alg;
2085ffb0c9bSToomas Soome     unsigned char *data;
2095ffb0c9bSToomas Soome } rdataDNSKey;
2105ffb0c9bSToomas Soome 
211c1de7575SRichard Lowe //size of rdataRRSIG excluding signerName and signature (which are variable fields)
2125ffb0c9bSToomas Soome #define RRSIG_FIXED_SIZE      18
2135ffb0c9bSToomas Soome typedef struct
2145ffb0c9bSToomas Soome {
2155ffb0c9bSToomas Soome     unsigned short typeCovered;
216c1de7575SRichard Lowe     unsigned char alg;
2175ffb0c9bSToomas Soome     unsigned char labels;
2185ffb0c9bSToomas Soome     unsigned int origTTL;
2195ffb0c9bSToomas Soome     unsigned int sigExpireTime;
2205ffb0c9bSToomas Soome     unsigned int sigInceptTime;
2215ffb0c9bSToomas Soome     unsigned short keyTag;
2225ffb0c9bSToomas Soome     char signerName[256];
2235ffb0c9bSToomas Soome     //unsigned char *signature
2245ffb0c9bSToomas Soome } rdataRRSig;
2255ffb0c9bSToomas Soome 
2265ffb0c9bSToomas Soome #define RR_TYPE_SIZE 16
2275ffb0c9bSToomas Soome 
2285ffb0c9bSToomas Soome typedef union { unsigned char b[2]; unsigned short NotAnInteger; } Opaque16;
2295ffb0c9bSToomas Soome 
2305ffb0c9bSToomas Soome static int operation;
2315ffb0c9bSToomas Soome static uint32_t opinterface = kDNSServiceInterfaceIndexAny;
2325ffb0c9bSToomas Soome static DNSServiceRef client    = NULL;
2335ffb0c9bSToomas Soome static DNSServiceRef client_pa = NULL;  // DNSServiceRef for RegisterProxyAddressRecord
2345ffb0c9bSToomas Soome static DNSServiceRef sc1, sc2, sc3;     // DNSServiceRefs for kDNSServiceFlagsShareConnection testing
2355ffb0c9bSToomas Soome 
2365ffb0c9bSToomas Soome static int num_printed;
2375ffb0c9bSToomas Soome static char addtest = 0;
2385ffb0c9bSToomas Soome static DNSRecordRef record = NULL;
2395ffb0c9bSToomas Soome static char myhinfoW[14] = "\002PC\012Windows XP";
2405ffb0c9bSToomas Soome static char myhinfoX[ 9] = "\003Mac\004OS X";
2415ffb0c9bSToomas Soome static char updatetest[3] = "\002AA";
2425ffb0c9bSToomas Soome static char bigNULL[8192];  // 8K is maximum rdata we support
2435ffb0c9bSToomas Soome 
2445ffb0c9bSToomas Soome #if _DNS_SD_LIBDISPATCH
2455ffb0c9bSToomas Soome dispatch_queue_t main_queue;
2465ffb0c9bSToomas Soome dispatch_source_t timer_source;
2475ffb0c9bSToomas Soome #endif
2485ffb0c9bSToomas Soome 
2495ffb0c9bSToomas Soome // Note: the select() implementation on Windows (Winsock2) fails with any timeout much larger than this
2505ffb0c9bSToomas Soome #define LONG_TIME 100000000
2515ffb0c9bSToomas Soome 
2525ffb0c9bSToomas Soome static volatile int stopNow = 0;
2535ffb0c9bSToomas Soome static volatile int timeOut = LONG_TIME;
2545ffb0c9bSToomas Soome 
2555ffb0c9bSToomas Soome #if _DNS_SD_LIBDISPATCH
2565ffb0c9bSToomas Soome #define EXIT_IF_LIBDISPATCH_FATAL_ERROR(E) \
2575ffb0c9bSToomas Soome     if (main_queue && (E) == kDNSServiceErr_ServiceNotRunning) { fprintf(stderr, "Error code %d\n", (E)); exit(0); }
2585ffb0c9bSToomas Soome #else
2595ffb0c9bSToomas Soome #define EXIT_IF_LIBDISPATCH_FATAL_ERROR(E)
2605ffb0c9bSToomas Soome #endif
2615ffb0c9bSToomas Soome 
2625ffb0c9bSToomas Soome //*************************************************************************************************************
2635ffb0c9bSToomas Soome // Supporting Utility Functions
GetRRClass(const char * s)264c1de7575SRichard Lowe static uint16_t GetRRClass(const char *s)
2655ffb0c9bSToomas Soome {
266c1de7575SRichard Lowe     if (!strcasecmp(s, "IN"))
2675ffb0c9bSToomas Soome         return kDNSServiceClass_IN;
2685ffb0c9bSToomas Soome     else
2695ffb0c9bSToomas Soome         return(atoi(s));
270c1de7575SRichard Lowe }
2715ffb0c9bSToomas Soome 
GetRRType(const char * s)2725ffb0c9bSToomas Soome static uint16_t GetRRType(const char *s)
2735ffb0c9bSToomas Soome {
2745ffb0c9bSToomas Soome     if      (!strcasecmp(s, "A"       )) return(kDNSServiceType_A);
2755ffb0c9bSToomas Soome     else if (!strcasecmp(s, "NS"      )) return(kDNSServiceType_NS);
2765ffb0c9bSToomas Soome     else if (!strcasecmp(s, "MD"      )) return(kDNSServiceType_MD);
2775ffb0c9bSToomas Soome     else if (!strcasecmp(s, "MF"      )) return(kDNSServiceType_MF);
2785ffb0c9bSToomas Soome     else if (!strcasecmp(s, "CNAME"   )) return(kDNSServiceType_CNAME);
2795ffb0c9bSToomas Soome     else if (!strcasecmp(s, "SOA"     )) return(kDNSServiceType_SOA);
2805ffb0c9bSToomas Soome     else if (!strcasecmp(s, "MB"      )) return(kDNSServiceType_MB);
2815ffb0c9bSToomas Soome     else if (!strcasecmp(s, "MG"      )) return(kDNSServiceType_MG);
2825ffb0c9bSToomas Soome     else if (!strcasecmp(s, "MR"      )) return(kDNSServiceType_MR);
2835ffb0c9bSToomas Soome     else if (!strcasecmp(s, "NULL"    )) return(kDNSServiceType_NULL);
2845ffb0c9bSToomas Soome     else if (!strcasecmp(s, "WKS"     )) return(kDNSServiceType_WKS);
2855ffb0c9bSToomas Soome     else if (!strcasecmp(s, "PTR"     )) return(kDNSServiceType_PTR);
2865ffb0c9bSToomas Soome     else if (!strcasecmp(s, "HINFO"   )) return(kDNSServiceType_HINFO);
2875ffb0c9bSToomas Soome     else if (!strcasecmp(s, "MINFO"   )) return(kDNSServiceType_MINFO);
2885ffb0c9bSToomas Soome     else if (!strcasecmp(s, "MX"      )) return(kDNSServiceType_MX);
2895ffb0c9bSToomas Soome     else if (!strcasecmp(s, "TXT"     )) return(kDNSServiceType_TXT);
2905ffb0c9bSToomas Soome     else if (!strcasecmp(s, "RP"      )) return(kDNSServiceType_RP);
2915ffb0c9bSToomas Soome     else if (!strcasecmp(s, "AFSDB"   )) return(kDNSServiceType_AFSDB);
2925ffb0c9bSToomas Soome     else if (!strcasecmp(s, "X25"     )) return(kDNSServiceType_X25);
2935ffb0c9bSToomas Soome     else if (!strcasecmp(s, "ISDN"    )) return(kDNSServiceType_ISDN);
2945ffb0c9bSToomas Soome     else if (!strcasecmp(s, "RT"      )) return(kDNSServiceType_RT);
2955ffb0c9bSToomas Soome     else if (!strcasecmp(s, "NSAP"    )) return(kDNSServiceType_NSAP);
2965ffb0c9bSToomas Soome     else if (!strcasecmp(s, "NSAP_PTR")) return(kDNSServiceType_NSAP_PTR);
2975ffb0c9bSToomas Soome     else if (!strcasecmp(s, "SIG"     )) return(kDNSServiceType_SIG);
2985ffb0c9bSToomas Soome     else if (!strcasecmp(s, "KEY"     )) return(kDNSServiceType_KEY);
2995ffb0c9bSToomas Soome     else if (!strcasecmp(s, "PX"      )) return(kDNSServiceType_PX);
3005ffb0c9bSToomas Soome     else if (!strcasecmp(s, "GPOS"    )) return(kDNSServiceType_GPOS);
3015ffb0c9bSToomas Soome     else if (!strcasecmp(s, "AAAA"    )) return(kDNSServiceType_AAAA);
3025ffb0c9bSToomas Soome     else if (!strcasecmp(s, "LOC"     )) return(kDNSServiceType_LOC);
3035ffb0c9bSToomas Soome     else if (!strcasecmp(s, "NXT"     )) return(kDNSServiceType_NXT);
3045ffb0c9bSToomas Soome     else if (!strcasecmp(s, "EID"     )) return(kDNSServiceType_EID);
3055ffb0c9bSToomas Soome     else if (!strcasecmp(s, "NIMLOC"  )) return(kDNSServiceType_NIMLOC);
3065ffb0c9bSToomas Soome     else if (!strcasecmp(s, "SRV"     )) return(kDNSServiceType_SRV);
3075ffb0c9bSToomas Soome     else if (!strcasecmp(s, "ATMA"    )) return(kDNSServiceType_ATMA);
3085ffb0c9bSToomas Soome     else if (!strcasecmp(s, "NAPTR"   )) return(kDNSServiceType_NAPTR);
3095ffb0c9bSToomas Soome     else if (!strcasecmp(s, "KX"      )) return(kDNSServiceType_KX);
3105ffb0c9bSToomas Soome     else if (!strcasecmp(s, "CERT"    )) return(kDNSServiceType_CERT);
3115ffb0c9bSToomas Soome     else if (!strcasecmp(s, "A6"      )) return(kDNSServiceType_A6);
3125ffb0c9bSToomas Soome     else if (!strcasecmp(s, "DNAME"   )) return(kDNSServiceType_DNAME);
3135ffb0c9bSToomas Soome     else if (!strcasecmp(s, "SINK"    )) return(kDNSServiceType_SINK);
3145ffb0c9bSToomas Soome     else if (!strcasecmp(s, "OPT"     )) return(kDNSServiceType_OPT);
3155ffb0c9bSToomas Soome     else if (!strcasecmp(s, "TKEY"    )) return(kDNSServiceType_TKEY);
3165ffb0c9bSToomas Soome     else if (!strcasecmp(s, "TSIG"    )) return(kDNSServiceType_TSIG);
3175ffb0c9bSToomas Soome     else if (!strcasecmp(s, "IXFR"    )) return(kDNSServiceType_IXFR);
3185ffb0c9bSToomas Soome     else if (!strcasecmp(s, "AXFR"    )) return(kDNSServiceType_AXFR);
3195ffb0c9bSToomas Soome     else if (!strcasecmp(s, "MAILB"   )) return(kDNSServiceType_MAILB);
3205ffb0c9bSToomas Soome     else if (!strcasecmp(s, "MAILA"   )) return(kDNSServiceType_MAILA);
3215ffb0c9bSToomas Soome     else if (!strcasecmp(s, "dnskey"  )) return(kDNSServiceType_DNSKEY);
3225ffb0c9bSToomas Soome     else if (!strcasecmp(s, "ds"      )) return(kDNSServiceType_DS);
3235ffb0c9bSToomas Soome     else if (!strcasecmp(s, "rrsig"   )) return(kDNSServiceType_RRSIG);
3245ffb0c9bSToomas Soome     else if (!strcasecmp(s, "nsec"    )) return(kDNSServiceType_NSEC);
325*472cd20dSToomas Soome     else if (!strcasecmp(s, "SVCB"    )) return(kDNSServiceType_SVCB);
326*472cd20dSToomas Soome     else if (!strcasecmp(s, "HTTPS"   )) return(kDNSServiceType_HTTPS);
3275ffb0c9bSToomas Soome     else if (!strcasecmp(s, "ANY"     )) return(kDNSServiceType_ANY);
3285ffb0c9bSToomas Soome     else return(atoi(s));
3295ffb0c9bSToomas Soome }
3305ffb0c9bSToomas Soome 
DNSTypeName(unsigned short rr_type)3315ffb0c9bSToomas Soome static char *DNSTypeName(unsigned short rr_type)
3325ffb0c9bSToomas Soome {
3335ffb0c9bSToomas Soome     switch (rr_type)
3345ffb0c9bSToomas Soome     {
335c65ebfc7SToomas Soome         case kDNSServiceType_A:          return("Addr");
336c65ebfc7SToomas Soome         case kDNSServiceType_NS:         return("NS");
337c65ebfc7SToomas Soome         case kDNSServiceType_MD:         return("MD");
338c65ebfc7SToomas Soome         case kDNSServiceType_MF:         return("MF");
339c65ebfc7SToomas Soome         case kDNSServiceType_CNAME:      return("CNAME");
340c65ebfc7SToomas Soome         case kDNSServiceType_SOA:        return("SOA");
341c65ebfc7SToomas Soome         case kDNSServiceType_MB:         return("MB");
342c65ebfc7SToomas Soome         case kDNSServiceType_MG:         return("MG");
343c65ebfc7SToomas Soome         case kDNSServiceType_MR:         return("MR");
344c65ebfc7SToomas Soome         case kDNSServiceType_NULL:       return("NULL");
345c65ebfc7SToomas Soome         case kDNSServiceType_WKS:        return("WKS");
346c65ebfc7SToomas Soome         case kDNSServiceType_PTR:        return("PTR");
347c65ebfc7SToomas Soome         case kDNSServiceType_HINFO:      return("HINFO");
348c65ebfc7SToomas Soome         case kDNSServiceType_MINFO:      return("MINFO");
349c65ebfc7SToomas Soome         case kDNSServiceType_MX:         return("MX");
350c65ebfc7SToomas Soome         case kDNSServiceType_TXT:        return("TXT");
351c65ebfc7SToomas Soome         case kDNSServiceType_RP:         return("RP");
352c65ebfc7SToomas Soome         case kDNSServiceType_AFSDB:      return("AFSDB");
353c65ebfc7SToomas Soome         case kDNSServiceType_X25:        return("X25");
354c65ebfc7SToomas Soome         case kDNSServiceType_ISDN:       return("ISDN");
355c65ebfc7SToomas Soome         case kDNSServiceType_RT:         return("RT");
356c65ebfc7SToomas Soome         case kDNSServiceType_NSAP:       return("NSAP");
357c65ebfc7SToomas Soome         case kDNSServiceType_NSAP_PTR:   return("NSAP_PTR");
358c65ebfc7SToomas Soome         case kDNSServiceType_SIG:        return("SIG");
359c65ebfc7SToomas Soome         case kDNSServiceType_KEY:        return("KEY");
360c65ebfc7SToomas Soome         case kDNSServiceType_PX:         return("PX");
361c65ebfc7SToomas Soome         case kDNSServiceType_GPOS:       return("GPOS");
362c65ebfc7SToomas Soome         case kDNSServiceType_AAAA:       return("AAAA");
363c65ebfc7SToomas Soome         case kDNSServiceType_LOC:        return("LOC");
364c65ebfc7SToomas Soome         case kDNSServiceType_NXT:        return("NXT");
365c65ebfc7SToomas Soome         case kDNSServiceType_EID:        return("EID");
366c65ebfc7SToomas Soome         case kDNSServiceType_NIMLOC:     return("NIMLOC");
367c65ebfc7SToomas Soome         case kDNSServiceType_SRV:        return("SRV");
368c65ebfc7SToomas Soome         case kDNSServiceType_ATMA:       return("ATMA");
369c65ebfc7SToomas Soome         case kDNSServiceType_NAPTR:      return("NAPTR");
370c65ebfc7SToomas Soome         case kDNSServiceType_KX:         return("KX");
371c65ebfc7SToomas Soome         case kDNSServiceType_CERT:       return("CERT");
372c65ebfc7SToomas Soome         case kDNSServiceType_A6:         return("A6");
373c65ebfc7SToomas Soome         case kDNSServiceType_DNAME:      return("DNAME");
374c65ebfc7SToomas Soome         case kDNSServiceType_SINK:       return("SINK");
375c65ebfc7SToomas Soome         case kDNSServiceType_OPT:        return("OPT");
376c65ebfc7SToomas Soome         case kDNSServiceType_APL:        return("APL");
377c65ebfc7SToomas Soome         case kDNSServiceType_DS:         return("DS");
378c65ebfc7SToomas Soome         case kDNSServiceType_SSHFP:      return("SSHFP");
379c65ebfc7SToomas Soome         case kDNSServiceType_IPSECKEY:   return("IPSECKEY");
380c65ebfc7SToomas Soome         case kDNSServiceType_RRSIG:      return("RRSIG");
381c65ebfc7SToomas Soome         case kDNSServiceType_NSEC:       return("NSEC");
382c65ebfc7SToomas Soome         case kDNSServiceType_DNSKEY:     return("DNSKEY");
383c65ebfc7SToomas Soome         case kDNSServiceType_DHCID:      return("DHCID");
384c65ebfc7SToomas Soome         case kDNSServiceType_NSEC3:      return("NSEC3");
385c65ebfc7SToomas Soome         case kDNSServiceType_NSEC3PARAM: return("NSEC3PARAM");
386c65ebfc7SToomas Soome         case kDNSServiceType_HIP:        return("HIP");
387c65ebfc7SToomas Soome         case kDNSServiceType_SPF:        return("SPF");
388c65ebfc7SToomas Soome         case kDNSServiceType_UINFO:      return("UINFO");
389c65ebfc7SToomas Soome         case kDNSServiceType_UID:        return("UID");
390c65ebfc7SToomas Soome         case kDNSServiceType_GID:        return("GID");
391c65ebfc7SToomas Soome         case kDNSServiceType_UNSPEC:     return("UNSPEC");
392c65ebfc7SToomas Soome         case kDNSServiceType_TKEY:       return("TKEY");
393c65ebfc7SToomas Soome         case kDNSServiceType_TSIG:       return("TSIG");
394c65ebfc7SToomas Soome         case kDNSServiceType_IXFR:       return("IXFR");
395c65ebfc7SToomas Soome         case kDNSServiceType_AXFR:       return("AXFR");
396c65ebfc7SToomas Soome         case kDNSServiceType_MAILB:      return("MAILB");
397c65ebfc7SToomas Soome         case kDNSServiceType_MAILA:      return("MAILA");
398*472cd20dSToomas Soome         case kDNSServiceType_SVCB:       return("SVCB");
399*472cd20dSToomas Soome         case kDNSServiceType_HTTPS:      return("HTTPS");
400c65ebfc7SToomas Soome         case kDNSServiceType_ANY:        return("ANY");
401c1de7575SRichard Lowe         default:
4025ffb0c9bSToomas Soome         {
4035ffb0c9bSToomas Soome             static char buffer[RR_TYPE_SIZE];
4045ffb0c9bSToomas Soome             snprintf(buffer, sizeof(buffer), "TYPE%d", rr_type);
4055ffb0c9bSToomas Soome             return(buffer);
4065ffb0c9bSToomas Soome         }
4075ffb0c9bSToomas Soome     }
4085ffb0c9bSToomas Soome }
4095ffb0c9bSToomas Soome 
swap16(unsigned short x)4105ffb0c9bSToomas Soome static unsigned short swap16(unsigned short x)
4115ffb0c9bSToomas Soome {
4125ffb0c9bSToomas Soome     unsigned char *ptr = (unsigned char *)&x;
4135ffb0c9bSToomas Soome     return (unsigned short)((unsigned short)ptr[0] << 8 | ptr[1]);
4145ffb0c9bSToomas Soome }
4155ffb0c9bSToomas Soome 
swap32(unsigned int x)416c1de7575SRichard Lowe static unsigned int swap32(unsigned int x)
4175ffb0c9bSToomas Soome {
4185ffb0c9bSToomas Soome     unsigned char *ptr = (unsigned char *)&x;
4195ffb0c9bSToomas Soome     return (unsigned int)((unsigned int)ptr[0] << 24 | (unsigned int)ptr[1] << 16 | (unsigned int)ptr[2] << 8 | ptr[3]);
4205ffb0c9bSToomas Soome }
keytag(unsigned char * key,unsigned int keysize)421c1de7575SRichard Lowe static unsigned int keytag(unsigned char *key, unsigned int keysize)
4225ffb0c9bSToomas Soome {
4235ffb0c9bSToomas Soome     unsigned long ac;
4245ffb0c9bSToomas Soome     unsigned int i;
4255ffb0c9bSToomas Soome 
4265ffb0c9bSToomas Soome     for (ac = 0, i = 0; i < keysize; ++i)
4275ffb0c9bSToomas Soome         ac += (i & 1) ? key[i] : key[i] << 8;
4285ffb0c9bSToomas Soome     ac += (ac >> 16) & 0xFFFF;
4295ffb0c9bSToomas Soome     return ac & 0xFFFF;
4305ffb0c9bSToomas Soome }
4315ffb0c9bSToomas Soome 
4323b436d06SToomas Soome // Base 64 encoding according to <https://tools.ietf.org/html/rfc4648#section-4>.
4333b436d06SToomas Soome #define kBase64EncodingTable "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
4343b436d06SToomas Soome 
base64Encode(char * buffer,size_t buflen,void * rdata,size_t rdlen)4353b436d06SToomas Soome static void base64Encode(char *buffer, size_t buflen, void *rdata, size_t rdlen)
4365ffb0c9bSToomas Soome {
4373b436d06SToomas Soome     const uint8_t *src = (const uint8_t *)rdata;
4383b436d06SToomas Soome     const uint8_t *const end = &src[rdlen];
4393b436d06SToomas Soome     char *dst = buffer;
4403b436d06SToomas Soome     const char *lim;
4413b436d06SToomas Soome 
4423b436d06SToomas Soome     if (buflen == 0) return;
4433b436d06SToomas Soome     lim = &buffer[buflen - 1];
4443b436d06SToomas Soome     while ((src < end) && (dst < lim))
4453b436d06SToomas Soome     {
4463b436d06SToomas Soome         uint32_t i;
4473b436d06SToomas Soome         const size_t rem = (size_t)(end - src);
4483b436d06SToomas Soome 
4493b436d06SToomas Soome         // Form a 24-bit input group. If less than 24 bits remain, pad with zero bits.
4503b436d06SToomas Soome         if (     rem >= 3) i = (src[0] << 16) | (src[1] << 8) | src[2]; // 24 bits are equal to 4 6-bit groups.
4513b436d06SToomas Soome         else if (rem == 2) i = (src[0] << 16) | (src[1] << 8);          // 16 bits are treated as 3 6-bit groups + 1 pad
4523b436d06SToomas Soome         else               i =  src[0] << 16;                           //  8 bits are treated as 2 6-bit groups + 2 pads
4533b436d06SToomas Soome 
4543b436d06SToomas Soome         // Encode each 6-bit group.
4553b436d06SToomas Soome                        *dst++ =              kBase64EncodingTable[(i >> 18) & 0x3F];
4563b436d06SToomas Soome         if (dst < lim) *dst++ =              kBase64EncodingTable[(i >> 12) & 0x3F];
4573b436d06SToomas Soome         if (dst < lim) *dst++ = (rem >= 2) ? kBase64EncodingTable[(i >>  6) & 0x3F] : '=';
4583b436d06SToomas Soome         if (dst < lim) *dst++ = (rem >= 3) ? kBase64EncodingTable[ i        & 0x3F] : '=';
4593b436d06SToomas Soome         src += (rem > 3) ? 3 : rem;
4603b436d06SToomas Soome     }
4613b436d06SToomas Soome     *dst = '\0';
4625ffb0c9bSToomas Soome }
4635ffb0c9bSToomas Soome 
GetProtocol(const char * s)4645ffb0c9bSToomas Soome static DNSServiceProtocol GetProtocol(const char *s)
4655ffb0c9bSToomas Soome {
4665ffb0c9bSToomas Soome     if      (!strcasecmp(s, "v4"      )) return(kDNSServiceProtocol_IPv4);
4675ffb0c9bSToomas Soome     else if (!strcasecmp(s, "v6"      )) return(kDNSServiceProtocol_IPv6);
4685ffb0c9bSToomas Soome     else if (!strcasecmp(s, "v4v6"    )) return(kDNSServiceProtocol_IPv4 | kDNSServiceProtocol_IPv6);
4695ffb0c9bSToomas Soome     else if (!strcasecmp(s, "v6v4"    )) return(kDNSServiceProtocol_IPv4 | kDNSServiceProtocol_IPv6);
4705ffb0c9bSToomas Soome     else if (!strcasecmp(s, "udp"     )) return(kDNSServiceProtocol_UDP);
4715ffb0c9bSToomas Soome     else if (!strcasecmp(s, "tcp"     )) return(kDNSServiceProtocol_TCP);
4725ffb0c9bSToomas Soome     else if (!strcasecmp(s, "udptcp"  )) return(kDNSServiceProtocol_UDP | kDNSServiceProtocol_TCP);
4735ffb0c9bSToomas Soome     else if (!strcasecmp(s, "tcpudp"  )) return(kDNSServiceProtocol_UDP | kDNSServiceProtocol_TCP);
4745ffb0c9bSToomas Soome     else return(atoi(s));
4755ffb0c9bSToomas Soome }
4765ffb0c9bSToomas Soome 
4775ffb0c9bSToomas Soome 
4785ffb0c9bSToomas Soome //*************************************************************************************************************
4795ffb0c9bSToomas Soome // Sample callback functions for each of the operation types
4805ffb0c9bSToomas Soome 
481c65ebfc7SToomas Soome #define printtimestamp() printtimestamp_F(stdout)
482c65ebfc7SToomas Soome 
printtimestamp_F(FILE * outstream)483c65ebfc7SToomas Soome static void printtimestamp_F(FILE *outstream)
4845ffb0c9bSToomas Soome {
4855ffb0c9bSToomas Soome     struct tm tm;
4865ffb0c9bSToomas Soome     int ms;
4875ffb0c9bSToomas Soome     static char date[16];
4885ffb0c9bSToomas Soome     static char new_date[16];
4895ffb0c9bSToomas Soome #ifdef _WIN32
4905ffb0c9bSToomas Soome     SYSTEMTIME sysTime;
4915ffb0c9bSToomas Soome     time_t uct = time(NULL);
4925ffb0c9bSToomas Soome     tm = *localtime(&uct);
4935ffb0c9bSToomas Soome     GetLocalTime(&sysTime);
4945ffb0c9bSToomas Soome     ms = sysTime.wMilliseconds;
4955ffb0c9bSToomas Soome #else
4965ffb0c9bSToomas Soome     struct timeval tv;
4975ffb0c9bSToomas Soome     gettimeofday(&tv, NULL);
4985ffb0c9bSToomas Soome     localtime_r((time_t*)&tv.tv_sec, &tm);
4995ffb0c9bSToomas Soome     ms = tv.tv_usec/1000;
5005ffb0c9bSToomas Soome #endif
5015ffb0c9bSToomas Soome     strftime(new_date, sizeof(new_date), "%a %d %b %Y", &tm);
5025ffb0c9bSToomas Soome     if (strncmp(date, new_date, sizeof(new_date)))
5035ffb0c9bSToomas Soome     {
504c65ebfc7SToomas Soome         fprintf(outstream, "DATE: ---%s---\n", new_date); //display date only if it has changed
5055ffb0c9bSToomas Soome         strncpy(date, new_date, sizeof(date));
5065ffb0c9bSToomas Soome     }
507c65ebfc7SToomas Soome     fprintf(outstream, "%2d:%02d:%02d.%03d  ", tm.tm_hour, tm.tm_min, tm.tm_sec, ms);
5085ffb0c9bSToomas Soome }
5095ffb0c9bSToomas Soome 
5105ffb0c9bSToomas Soome // formating time to RFC 4034 format
FormatTime(unsigned long te,unsigned char * buf,int bufsize)511c1de7575SRichard Lowe static void FormatTime(unsigned long te, unsigned char *buf, int bufsize)
5125ffb0c9bSToomas Soome {
5135ffb0c9bSToomas Soome     struct tm tmTime;
5145ffb0c9bSToomas Soome #ifdef _WIN32
5155ffb0c9bSToomas Soome 	__time32_t t = (__time32_t) te;
5165ffb0c9bSToomas Soome 	_gmtime32_s(&tmTime, &t);
5175ffb0c9bSToomas Soome #else
5185ffb0c9bSToomas Soome     // Time since epoch : strftime takes "tm". Convert seconds to "tm" using
5195ffb0c9bSToomas Soome     // gmtime_r first and then use strftime
5205ffb0c9bSToomas Soome 	time_t t = (time_t)te;
5215ffb0c9bSToomas Soome 	gmtime_r(&t, &tmTime);
5225ffb0c9bSToomas Soome #endif
5235ffb0c9bSToomas Soome     strftime((char *)buf, bufsize, "%Y%m%d%H%M%S", &tmTime);
5245ffb0c9bSToomas Soome }
5255ffb0c9bSToomas Soome 
print_usage(const char * arg0,int print_all)5265ffb0c9bSToomas Soome static void print_usage(const char *arg0, int print_all)
5275ffb0c9bSToomas Soome {
528c65ebfc7SToomas Soome     // Print the commonly used command line options.  These are listed in "the order they have been in historically".
529*472cd20dSToomas Soome     fprintf(stderr, "%s -E                          (Enumerate recommended registration domains)\n", arg0);
530*472cd20dSToomas Soome     fprintf(stderr, "%s -F                          (Enumerate recommended browsing     domains)\n", arg0);
531*472cd20dSToomas Soome     fprintf(stderr, "%s -R <Name> <Type> <Domain> <Port> [<TXT>...]         (Register a service)\n", arg0);
532*472cd20dSToomas Soome     fprintf(stderr, "%s -P <Name> <Type> <Domain> <Port> <Host> <IP> [<TXT>...] (Register Proxy)\n", arg0);
533*472cd20dSToomas Soome     fprintf(stderr, "%s -B        <Type> <Domain>                 (Browse for service instances)\n", arg0);
534*472cd20dSToomas Soome     fprintf(stderr, "%s -Z        <Type> <Domain>           (Output results in Zone File format)\n", arg0);
535*472cd20dSToomas Soome     fprintf(stderr, "%s -L <Name> <Type> <Domain>        (Resolve (‘lookup’) a service instance)\n", arg0);
536*472cd20dSToomas Soome     fprintf(stderr, "%s -Q <name> <rrtype> <rrclass>         (Generic query for any record type)\n", arg0);
537*472cd20dSToomas Soome     fprintf(stderr, "%s -q <name> <rrtype> <rrclass>     (Generic query, using SuppressUnusable)\n", arg0);
538*472cd20dSToomas Soome     fprintf(stderr, "%s -G v4/v6/v4v6 <hostname>          (Get address information for hostname)\n", arg0);
539*472cd20dSToomas Soome     fprintf(stderr, "%s -X udp/tcp/udptcp <IntPort> <ExtPort> <TTL>           (NAT Port Mapping)\n", arg0);
540*472cd20dSToomas Soome     fprintf(stderr, "%s -H                               (Print usage for complete command list)\n", arg0);
541*472cd20dSToomas Soome     fprintf(stderr, "%s -V            (Get version of currently running daemon / system service)\n", arg0);
542*472cd20dSToomas Soome #ifdef APPLE_OSX_mDNSResponder
543*472cd20dSToomas Soome     fprintf(stderr, "%s -O [-compress|-stdout](Dump the state of mDNSResponder to file / STDOUT)\n", arg0);
544*472cd20dSToomas Soome #endif // APPLE_OSX_mDNSResponder
5455ffb0c9bSToomas Soome 
546c65ebfc7SToomas Soome     if (print_all)  // Print all available options for dns-sd tool.  Keep these in alphabetical order for easier maintenance.
5475ffb0c9bSToomas Soome     {
548c65ebfc7SToomas Soome         fprintf(stderr, "\n");
549*472cd20dSToomas Soome         fprintf(stderr, "%s -A                              (Test Adding/Updating/Deleting a record)\n", arg0);
550*472cd20dSToomas Soome         fprintf(stderr, "%s -C <name> <rrtype> <rrclass>           (Query; reconfirming each result)\n", arg0);
551*472cd20dSToomas Soome         fprintf(stderr, "%s -I           (Test registering and then immediately updating TXT record)\n", arg0);
552*472cd20dSToomas Soome         fprintf(stderr, "%s -N                                     (Test adding a large NULL record)\n", arg0);
553*472cd20dSToomas Soome         fprintf(stderr, "%s -M              (Test creating a registration with multiple TXT records)\n", arg0);
554*472cd20dSToomas Soome         fprintf(stderr, "%s -S                         (Test multiple operations on a shared socket)\n", arg0);
555*472cd20dSToomas Soome         fprintf(stderr, "%s -T                                    (Test creating a large TXT record)\n", arg0);
556*472cd20dSToomas Soome         fprintf(stderr, "%s -U                                          (Test updating a TXT record)\n", arg0);
557*472cd20dSToomas Soome         fprintf(stderr, "%s -ble                                  (Use kDNSServiceInterfaceIndexBLE)\n", arg0);
558*472cd20dSToomas Soome         fprintf(stderr, "%s -i <Interface>         (Run dns-sd cmd on a specific interface (en0/en1)\n", arg0);
559*472cd20dSToomas Soome         fprintf(stderr, "%s -includep2p                        (Set kDNSServiceFlagsIncludeP2P flag)\n", arg0);
560*472cd20dSToomas Soome         fprintf(stderr, "%s -includeAWDL                      (Set kDNSServiceFlagsIncludeAWDL flag)\n", arg0);
561*472cd20dSToomas Soome         fprintf(stderr, "%s -intermediates            (Set kDNSServiceFlagsReturnIntermediates flag)\n", arg0);
562*472cd20dSToomas Soome         fprintf(stderr, "%s -ku                               (Set kDNSServiceFlagsKnownUnique flag)\n", arg0);
563*472cd20dSToomas Soome         fprintf(stderr, "%s -lo                          (Run dns-sd cmd using local only interface)\n", arg0);
564*472cd20dSToomas Soome         fprintf(stderr, "%s -p2p                                  (Use kDNSServiceInterfaceIndexP2P)\n", arg0);
565*472cd20dSToomas Soome         fprintf(stderr, "%s -tc                    (Set kDNSServiceFlagsBackgroundTrafficClass flag)\n", arg0);
566*472cd20dSToomas Soome         fprintf(stderr, "%s -test                                  (Run basic API input range tests)\n", arg0);
567*472cd20dSToomas Soome         fprintf(stderr, "%s -t1                              (Set kDNSServiceFlagsThresholdOne flag)\n", arg0);
568*472cd20dSToomas Soome         fprintf(stderr, "%s -tFinder                      (Set kDNSServiceFlagsThresholdFinder flag)\n", arg0);
569*472cd20dSToomas Soome         fprintf(stderr, "%s -timeout                              (Set kDNSServiceFlagsTimeout flag)\n", arg0);
570*472cd20dSToomas Soome         fprintf(stderr, "%s -unicastResponse              (Set kDNSServiceFlagsUnicastResponse flag)\n", arg0);
571*472cd20dSToomas Soome         fprintf(stderr, "%s -autoTrigger                      (Set kDNSServiceFlagsAutoTrigger flag)\n", arg0);
572*472cd20dSToomas Soome         fprintf(stderr, "%s -enableDNSSEC              (Enable DNSSEC validation for the '-Q' query)\n", arg0);
5735ffb0c9bSToomas Soome     }
5745ffb0c9bSToomas Soome }
5755ffb0c9bSToomas Soome 
5765ffb0c9bSToomas Soome #define DomainMsg(X) (((X) &kDNSServiceFlagsDefault) ? "(Default)" : \
5775ffb0c9bSToomas Soome                       ((X) &kDNSServiceFlagsAdd)     ? "Added"     : "Removed")
5785ffb0c9bSToomas Soome 
5795ffb0c9bSToomas Soome #define MAX_LABELS 128
5805ffb0c9bSToomas Soome 
enum_reply(DNSServiceRef sdref,const DNSServiceFlags flags,uint32_t ifIndex,DNSServiceErrorType errorCode,const char * replyDomain,void * context)5815ffb0c9bSToomas Soome static void DNSSD_API enum_reply(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex,
5825ffb0c9bSToomas Soome                                  DNSServiceErrorType errorCode, const char *replyDomain, void *context)
5835ffb0c9bSToomas Soome {
5845ffb0c9bSToomas Soome     DNSServiceFlags partialflags = flags & ~(kDNSServiceFlagsMoreComing | kDNSServiceFlagsAdd | kDNSServiceFlagsDefault);
5855ffb0c9bSToomas Soome     int labels = 0, depth = 0, i, initial = 0;
5865ffb0c9bSToomas Soome     char text[64];
5875ffb0c9bSToomas Soome     const char *label[MAX_LABELS];
5885ffb0c9bSToomas Soome 
5895ffb0c9bSToomas Soome     (void)sdref;        // Unused
5905ffb0c9bSToomas Soome     (void)ifIndex;      // Unused
5915ffb0c9bSToomas Soome     (void)context;      // Unused
5925ffb0c9bSToomas Soome     EXIT_IF_LIBDISPATCH_FATAL_ERROR(errorCode);
5935ffb0c9bSToomas Soome 
5945ffb0c9bSToomas Soome     // 1. Print the header
5955ffb0c9bSToomas Soome     if (num_printed++ == 0) printf("Timestamp     Recommended %s domain\n", operation == 'E' ? "Registration" : "Browsing");
5965ffb0c9bSToomas Soome     printtimestamp();
5975ffb0c9bSToomas Soome     if (errorCode)
5985ffb0c9bSToomas Soome         printf("Error code %d\n", errorCode);
5995ffb0c9bSToomas Soome     else if (!*replyDomain)
6005ffb0c9bSToomas Soome         printf("Error: No reply domain\n");
6015ffb0c9bSToomas Soome     else
6025ffb0c9bSToomas Soome     {
6035ffb0c9bSToomas Soome         printf("%-10s", DomainMsg(flags));
6045ffb0c9bSToomas Soome         printf("%-8s", (flags & kDNSServiceFlagsMoreComing) ? "(More)" : "");
6055ffb0c9bSToomas Soome         if (partialflags) printf("Flags: %4X  ", partialflags);
6065ffb0c9bSToomas Soome         else printf("             ");
6075ffb0c9bSToomas Soome 
6085ffb0c9bSToomas Soome         // 2. Count the labels
6095ffb0c9bSToomas Soome         while (replyDomain && *replyDomain && labels < MAX_LABELS)
6105ffb0c9bSToomas Soome         {
6115ffb0c9bSToomas Soome             label[labels++] = replyDomain;
6125ffb0c9bSToomas Soome             replyDomain = GetNextLabel(replyDomain, text);
6135ffb0c9bSToomas Soome         }
6145ffb0c9bSToomas Soome 
6155ffb0c9bSToomas Soome         // 3. Decide if we're going to clump the last two or three labels (e.g. "apple.com", or "nicta.com.au")
6165ffb0c9bSToomas Soome         if      (labels >= 3 && replyDomain - label[labels-1] <= 3 && label[labels-1] - label[labels-2] <= 4) initial = 3;
6175ffb0c9bSToomas Soome         else if (labels >= 2 && replyDomain - label[labels-1] <= 4) initial = 2;
6185ffb0c9bSToomas Soome         else initial = 1;
6195ffb0c9bSToomas Soome         labels -= initial;
6205ffb0c9bSToomas Soome 
6215ffb0c9bSToomas Soome         // 4. Print the initial one-, two- or three-label clump
6225ffb0c9bSToomas Soome         for (i=0; i<initial; i++)
6235ffb0c9bSToomas Soome         {
6245ffb0c9bSToomas Soome             GetNextLabel(label[labels+i], text);
6255ffb0c9bSToomas Soome             if (i>0) printf(".");
6265ffb0c9bSToomas Soome             printf("%s", text);
6275ffb0c9bSToomas Soome         }
6285ffb0c9bSToomas Soome         printf("\n");
6295ffb0c9bSToomas Soome 
6305ffb0c9bSToomas Soome         // 5. Print the remainder of the hierarchy
6315ffb0c9bSToomas Soome         for (depth=0; depth<labels; depth++)
6325ffb0c9bSToomas Soome         {
6335ffb0c9bSToomas Soome             printf("                                             ");
6345ffb0c9bSToomas Soome             for (i=0; i<=depth; i++) printf("- ");
6355ffb0c9bSToomas Soome             GetNextLabel(label[labels-1-depth], text);
6365ffb0c9bSToomas Soome             printf("> %s\n", text);
6375ffb0c9bSToomas Soome         }
6385ffb0c9bSToomas Soome     }
6395ffb0c9bSToomas Soome 
6405ffb0c9bSToomas Soome     if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
6415ffb0c9bSToomas Soome }
6425ffb0c9bSToomas Soome 
CopyLabels(char * dst,const char * lim,const char ** srcp,int labels)6435ffb0c9bSToomas Soome static int CopyLabels(char *dst, const char *lim, const char **srcp, int labels)
6445ffb0c9bSToomas Soome {
6455ffb0c9bSToomas Soome     const char *src = *srcp;
6465ffb0c9bSToomas Soome     while (*src != '.' || --labels > 0)
6475ffb0c9bSToomas Soome     {
6485ffb0c9bSToomas Soome         if (*src == '\\') *dst++ = *src++;  // Make sure "\." doesn't confuse us
6495ffb0c9bSToomas Soome         if (!*src || dst >= lim) return -1;
6505ffb0c9bSToomas Soome         *dst++ = *src++;
6515ffb0c9bSToomas Soome         if (!*src || dst >= lim) return -1;
6525ffb0c9bSToomas Soome     }
6535ffb0c9bSToomas Soome     *dst++ = 0;
6545ffb0c9bSToomas Soome     *srcp = src + 1;    // skip over final dot
6555ffb0c9bSToomas Soome     return 0;
6565ffb0c9bSToomas Soome }
6575ffb0c9bSToomas Soome 
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)6585ffb0c9bSToomas Soome static void DNSSD_API zonedata_resolve(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode,
6595ffb0c9bSToomas Soome                                        const char *fullname, const char *hosttarget, uint16_t opaqueport, uint16_t txtLen, const unsigned char *txt, void *context)
6605ffb0c9bSToomas Soome {
6615ffb0c9bSToomas Soome     union { uint16_t s; u_char b[2]; } port = { opaqueport };
6625ffb0c9bSToomas Soome     uint16_t PortAsNumber = ((uint16_t)port.b[0]) << 8 | port.b[1];
6635ffb0c9bSToomas Soome 
6645ffb0c9bSToomas Soome     const char *p = fullname;
6655ffb0c9bSToomas Soome     char n[kDNSServiceMaxDomainName];
6665ffb0c9bSToomas Soome     char t[kDNSServiceMaxDomainName];
6675ffb0c9bSToomas Soome 
6685ffb0c9bSToomas Soome     const unsigned char *max = txt + txtLen;
6695ffb0c9bSToomas Soome 
6705ffb0c9bSToomas Soome     (void)sdref;        // Unused
6715ffb0c9bSToomas Soome     (void)ifIndex;      // Unused
6725ffb0c9bSToomas Soome     (void)context;      // Unused
6735ffb0c9bSToomas Soome 
6745ffb0c9bSToomas Soome     //if (!(flags & kDNSServiceFlagsAdd)) return;
6755ffb0c9bSToomas Soome     if (errorCode) { printf("Error code %d\n", errorCode); return; }
6765ffb0c9bSToomas Soome 
6775ffb0c9bSToomas Soome     if (CopyLabels(n, n + kDNSServiceMaxDomainName, &p, 3)) return;     // Fetch name+type
6785ffb0c9bSToomas Soome     p = fullname;
6795ffb0c9bSToomas Soome     if (CopyLabels(t, t + kDNSServiceMaxDomainName, &p, 1)) return;     // Skip first label
6805ffb0c9bSToomas Soome     if (CopyLabels(t, t + kDNSServiceMaxDomainName, &p, 2)) return;     // Fetch next two labels (service type)
6815ffb0c9bSToomas Soome 
6825ffb0c9bSToomas Soome     if (num_printed++ == 0)
6835ffb0c9bSToomas Soome     {
6845ffb0c9bSToomas Soome         printf("\n");
6855ffb0c9bSToomas Soome         printf("; To direct clients to browse a different domain, substitute that domain in place of '@'\n");
6865ffb0c9bSToomas Soome         printf("%-47s PTR     %s\n", "lb._dns-sd._udp", "@");
6875ffb0c9bSToomas Soome         printf("\n");
6885ffb0c9bSToomas Soome         printf("; In the list of services below, the SRV records will typically reference dot-local Multicast DNS names.\n");
6895ffb0c9bSToomas Soome         printf("; When transferring this zone file data to your unicast DNS server, you'll need to replace those dot-local\n");
6905ffb0c9bSToomas Soome         printf("; names with the correct fully-qualified (unicast) domain name of the target host offering the service.\n");
6915ffb0c9bSToomas Soome     }
6925ffb0c9bSToomas Soome 
6935ffb0c9bSToomas Soome     printf("\n");
6945ffb0c9bSToomas Soome     printf("%-47s PTR     %s\n", t, n);
6955ffb0c9bSToomas Soome     printf("%-47s SRV     0 0 %d %s ; Replace with unicast FQDN of target host\n", n, PortAsNumber, hosttarget);
6965ffb0c9bSToomas Soome     printf("%-47s TXT    ", n);
6975ffb0c9bSToomas Soome 
6985ffb0c9bSToomas Soome     while (txt < max)
6995ffb0c9bSToomas Soome     {
7005ffb0c9bSToomas Soome         const unsigned char *const end = txt + 1 + txt[0];
7015ffb0c9bSToomas Soome         txt++;      // Skip over length byte
7025ffb0c9bSToomas Soome         printf(" \"");
7035ffb0c9bSToomas Soome         while (txt<end)
7045ffb0c9bSToomas Soome         {
7055ffb0c9bSToomas Soome             if (*txt == '\\' || *txt == '\"') printf("\\");
7065ffb0c9bSToomas Soome             printf("%c", *txt++);
7075ffb0c9bSToomas Soome         }
7085ffb0c9bSToomas Soome         printf("\"");
7095ffb0c9bSToomas Soome     }
7105ffb0c9bSToomas Soome     printf("\n");
7115ffb0c9bSToomas Soome 
7125ffb0c9bSToomas Soome     DNSServiceRefDeallocate(sdref);
7135ffb0c9bSToomas Soome     free(context);
7145ffb0c9bSToomas Soome 
7155ffb0c9bSToomas Soome     if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
7165ffb0c9bSToomas Soome }
7175ffb0c9bSToomas Soome 
zonedata_browse(DNSServiceRef sdref,const DNSServiceFlags flags,uint32_t ifIndex,DNSServiceErrorType errorCode,const char * replyName,const char * replyType,const char * replyDomain,void * context)7185ffb0c9bSToomas Soome static void DNSSD_API zonedata_browse(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode,
7195ffb0c9bSToomas Soome                                       const char *replyName, const char *replyType, const char *replyDomain, void *context)
7205ffb0c9bSToomas Soome {
7215ffb0c9bSToomas Soome     DNSServiceRef *newref;
7225ffb0c9bSToomas Soome 
7235ffb0c9bSToomas Soome     (void)sdref;        // Unused
7245ffb0c9bSToomas Soome     (void)context;      // Unused
7255ffb0c9bSToomas Soome     EXIT_IF_LIBDISPATCH_FATAL_ERROR(errorCode);
7265ffb0c9bSToomas Soome 
7275ffb0c9bSToomas Soome     if (!(flags & kDNSServiceFlagsAdd)) return;
7285ffb0c9bSToomas Soome     if (errorCode) { printf("Error code %d\n", errorCode); return; }
7295ffb0c9bSToomas Soome 
7305ffb0c9bSToomas Soome     newref = malloc(sizeof(*newref));
7315ffb0c9bSToomas Soome     *newref = client;
7325ffb0c9bSToomas Soome     DNSServiceResolve(newref, kDNSServiceFlagsShareConnection, ifIndex, replyName, replyType, replyDomain, zonedata_resolve, newref);
7335ffb0c9bSToomas Soome }
7345ffb0c9bSToomas Soome 
browse_reply(DNSServiceRef sdref,const DNSServiceFlags flags,uint32_t ifIndex,DNSServiceErrorType errorCode,const char * replyName,const char * replyType,const char * replyDomain,void * context)7355ffb0c9bSToomas Soome static void DNSSD_API browse_reply(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode,
7365ffb0c9bSToomas Soome                                    const char *replyName, const char *replyType, const char *replyDomain, void *context)
7375ffb0c9bSToomas Soome {
7385ffb0c9bSToomas Soome     char *op = (flags & kDNSServiceFlagsAdd) ? "Add" : "Rmv";
7395ffb0c9bSToomas Soome     (void)sdref;        // Unused
7405ffb0c9bSToomas Soome     (void)context;      // Unused
7415ffb0c9bSToomas Soome     EXIT_IF_LIBDISPATCH_FATAL_ERROR(errorCode);
7425ffb0c9bSToomas Soome 
7435ffb0c9bSToomas Soome     if (num_printed++ == 0) printf("Timestamp     A/R    Flags  if %-20s %-20s %s\n", "Domain", "Service Type", "Instance Name");
7445ffb0c9bSToomas Soome     printtimestamp();
745c1de7575SRichard Lowe     if (errorCode)
7465ffb0c9bSToomas Soome         printf("Error code %d\n", errorCode);
747c1de7575SRichard Lowe     else
748c1de7575SRichard Lowe         printf("%s %8X %3d %-20s %-20s %s\n",
7495ffb0c9bSToomas Soome                 op, flags, ifIndex, replyDomain, replyType, replyName);
7505ffb0c9bSToomas Soome     if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
7515ffb0c9bSToomas Soome 
7525ffb0c9bSToomas Soome     // To test selective cancellation of operations of shared sockets,
7535ffb0c9bSToomas Soome     // cancel the current operation when we've got a multiple of five results
7545ffb0c9bSToomas Soome     //if (operation == 'S' && num_printed % 5 == 0) DNSServiceRefDeallocate(sdref);
7555ffb0c9bSToomas Soome }
7565ffb0c9bSToomas Soome 
ShowTXTRecord(uint16_t txtLen,const unsigned char * txtRecord)7575ffb0c9bSToomas Soome static void ShowTXTRecord(uint16_t txtLen, const unsigned char *txtRecord)
7585ffb0c9bSToomas Soome {
7595ffb0c9bSToomas Soome     const unsigned char *ptr = txtRecord;
7605ffb0c9bSToomas Soome     const unsigned char *max = txtRecord + txtLen;
7615ffb0c9bSToomas Soome     while (ptr < max)
7625ffb0c9bSToomas Soome     {
7635ffb0c9bSToomas Soome         const unsigned char *const end = ptr + 1 + ptr[0];
7645ffb0c9bSToomas Soome         if (end > max) { printf("<< invalid data >>"); break; }
7655ffb0c9bSToomas Soome         if (++ptr < end) printf(" ");   // As long as string is non-empty, begin with a space
7665ffb0c9bSToomas Soome         while (ptr<end)
7675ffb0c9bSToomas Soome         {
7685ffb0c9bSToomas Soome             // We'd like the output to be shell-friendly, so that it can be copied and pasted unchanged into a "dns-sd -R" command.
7695ffb0c9bSToomas Soome             // However, this is trickier than it seems. Enclosing a string in double quotes doesn't necessarily make it
7705ffb0c9bSToomas Soome             // shell-safe, because shells still expand variables like $foo even when they appear inside quoted strings.
7715ffb0c9bSToomas Soome             // Enclosing a string in single quotes is better, but when using single quotes even backslash escapes are ignored,
7725ffb0c9bSToomas Soome             // meaning there's simply no way to represent a single quote (or apostrophe) inside a single-quoted string.
7735ffb0c9bSToomas Soome             // The only remaining solution is not to surround the string with quotes at all, but instead to use backslash
7745ffb0c9bSToomas Soome             // escapes to encode spaces and all other known shell metacharacters.
7755ffb0c9bSToomas Soome             // (If we've missed any known shell metacharacters, please let us know.)
7765ffb0c9bSToomas Soome             // In addition, non-printing ascii codes (0-31) are displayed as \xHH, using a two-digit hex value.
7775ffb0c9bSToomas Soome             // Because '\' is itself a shell metacharacter (the shell escape character), it has to be escaped as "\\" to survive
7785ffb0c9bSToomas Soome             // the round-trip to the shell and back. This means that a single '\' is represented here as EIGHT backslashes:
7795ffb0c9bSToomas Soome             // The C compiler eats half of them, resulting in four appearing in the output.
7805ffb0c9bSToomas Soome             // The shell parses those four as a pair of "\\" sequences, passing two backslashes to the "dns-sd -R" command.
7815ffb0c9bSToomas Soome             // The "dns-sd -R" command interprets this single "\\" pair as an escaped literal backslash. Sigh.
7825ffb0c9bSToomas Soome             if (strchr(" &;`'\"|*?~<>^()[]{}$", *ptr)) printf("\\");
7835ffb0c9bSToomas Soome             if      (*ptr == '\\') printf("\\\\\\\\");
7845ffb0c9bSToomas Soome             else if (*ptr >= ' ' ) printf("%c",        *ptr);
7855ffb0c9bSToomas Soome             else printf("\\\\x%02X", *ptr);
7865ffb0c9bSToomas Soome             ptr++;
7875ffb0c9bSToomas Soome         }
7885ffb0c9bSToomas Soome     }
7895ffb0c9bSToomas Soome }
7905ffb0c9bSToomas Soome 
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)7915ffb0c9bSToomas Soome static void DNSSD_API resolve_reply(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode,
7925ffb0c9bSToomas Soome                                     const char *fullname, const char *hosttarget, uint16_t opaqueport, uint16_t txtLen, const unsigned char *txtRecord, void *context)
7935ffb0c9bSToomas Soome {
7945ffb0c9bSToomas Soome     union { uint16_t s; u_char b[2]; } port = { opaqueport };
7955ffb0c9bSToomas Soome     uint16_t PortAsNumber = ((uint16_t)port.b[0]) << 8 | port.b[1];
7965ffb0c9bSToomas Soome 
7975ffb0c9bSToomas Soome     (void)sdref;        // Unused
7985ffb0c9bSToomas Soome     (void)ifIndex;      // Unused
7995ffb0c9bSToomas Soome     (void)context;      // Unused
8005ffb0c9bSToomas Soome     EXIT_IF_LIBDISPATCH_FATAL_ERROR(errorCode);
8015ffb0c9bSToomas Soome 
802c65ebfc7SToomas Soome     printtimestamp();
803c65ebfc7SToomas Soome 
804c65ebfc7SToomas Soome     printf("%s ", fullname);
805c65ebfc7SToomas Soome 
806c65ebfc7SToomas Soome     if (errorCode == kDNSServiceErr_NoSuchRecord) printf("No Such Record");
807c65ebfc7SToomas Soome     else if (errorCode) printf("error code %d\n", errorCode);
808c65ebfc7SToomas Soome     else printf("can be reached at %s:%u (interface %d)", hosttarget, PortAsNumber, ifIndex);
809c65ebfc7SToomas Soome 
810c65ebfc7SToomas Soome     if (flags) printf(" Flags: %X", flags);
811c65ebfc7SToomas Soome 
812c65ebfc7SToomas Soome     // Don't show degenerate TXT records containing nothing but a single empty string
813c65ebfc7SToomas Soome     if (!errorCode && txtLen > 1) { printf("\n"); ShowTXTRecord(txtLen, txtRecord); }
814c65ebfc7SToomas Soome 
815c65ebfc7SToomas Soome     printf("\n");
8165ffb0c9bSToomas Soome 
8175ffb0c9bSToomas Soome     if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
8185ffb0c9bSToomas Soome }
8195ffb0c9bSToomas Soome 
myTimerCallBack(void)8205ffb0c9bSToomas Soome static void myTimerCallBack(void)
8215ffb0c9bSToomas Soome {
8225ffb0c9bSToomas Soome     DNSServiceErrorType err = kDNSServiceErr_Unknown;
8235ffb0c9bSToomas Soome 
8245ffb0c9bSToomas Soome     switch (operation)
8255ffb0c9bSToomas Soome     {
8265ffb0c9bSToomas Soome     case 'A':
8275ffb0c9bSToomas Soome     {
8285ffb0c9bSToomas Soome         switch (addtest)
8295ffb0c9bSToomas Soome         {
8305ffb0c9bSToomas Soome         case 0: printf("Adding Test HINFO record\n");
8315ffb0c9bSToomas Soome             err = DNSServiceAddRecord(client, &record, 0, kDNSServiceType_HINFO, sizeof(myhinfoW), &myhinfoW[0], 0);
8325ffb0c9bSToomas Soome             addtest = 1;
8335ffb0c9bSToomas Soome             break;
8345ffb0c9bSToomas Soome         case 1: printf("Updating Test HINFO record\n");
8355ffb0c9bSToomas Soome             err = DNSServiceUpdateRecord(client, record, 0, sizeof(myhinfoX), &myhinfoX[0], 0);
8365ffb0c9bSToomas Soome             addtest = 2;
8375ffb0c9bSToomas Soome             break;
8385ffb0c9bSToomas Soome         case 2: printf("Removing Test HINFO record\n");
8395ffb0c9bSToomas Soome             err = DNSServiceRemoveRecord(client, record, 0);
8405ffb0c9bSToomas Soome             addtest = 0;
8415ffb0c9bSToomas Soome             break;
8425ffb0c9bSToomas Soome         }
8435ffb0c9bSToomas Soome     }
8445ffb0c9bSToomas Soome     break;
8455ffb0c9bSToomas Soome 
8465ffb0c9bSToomas Soome     case 'U':
8475ffb0c9bSToomas Soome     {
8485ffb0c9bSToomas Soome         if (updatetest[1] != 'Z') updatetest[1]++;
8495ffb0c9bSToomas Soome         else updatetest[1] = 'A';
850c65ebfc7SToomas Soome         // The following line toggles the string length between 1 and 2 characters.
8515ffb0c9bSToomas Soome         updatetest[0] = 3 - updatetest[0];
8525ffb0c9bSToomas Soome         updatetest[2] = updatetest[1];
8535ffb0c9bSToomas Soome         printtimestamp();
8545ffb0c9bSToomas Soome         printf("Updating Test TXT record to %c\n", updatetest[1]);
8555ffb0c9bSToomas Soome         err = DNSServiceUpdateRecord(client, NULL, 0, 1+updatetest[0], &updatetest[0], 0);
8565ffb0c9bSToomas Soome     }
8575ffb0c9bSToomas Soome     break;
8585ffb0c9bSToomas Soome 
8595ffb0c9bSToomas Soome     case 'N':
8605ffb0c9bSToomas Soome     {
8615ffb0c9bSToomas Soome         printf("Adding big NULL record\n");
8625ffb0c9bSToomas Soome         err = DNSServiceAddRecord(client, &record, 0, kDNSServiceType_NULL, sizeof(bigNULL), &bigNULL[0], 0);
8635ffb0c9bSToomas Soome         if (err) printf("Failed: %d\n", err);else printf("Succeeded\n");
8645ffb0c9bSToomas Soome         timeOut = LONG_TIME;
8655ffb0c9bSToomas Soome #if _DNS_SD_LIBDISPATCH
8665ffb0c9bSToomas Soome         if (timer_source)
8675ffb0c9bSToomas Soome             dispatch_source_set_timer(timer_source, dispatch_time(DISPATCH_TIME_NOW, (uint64_t)timeOut * NSEC_PER_SEC),
8685ffb0c9bSToomas Soome                                       (uint64_t)timeOut * NSEC_PER_SEC, 0);
8695ffb0c9bSToomas Soome #endif
8705ffb0c9bSToomas Soome     }
8715ffb0c9bSToomas Soome     break;
8725ffb0c9bSToomas Soome     }
8735ffb0c9bSToomas Soome 
8745ffb0c9bSToomas Soome     if (err != kDNSServiceErr_NoError)
8755ffb0c9bSToomas Soome     {
8765ffb0c9bSToomas Soome         fprintf(stderr, "DNSService add/update/remove failed %ld\n", (long int)err);
8775ffb0c9bSToomas Soome         stopNow = 1;
8785ffb0c9bSToomas Soome     }
8795ffb0c9bSToomas Soome }
8805ffb0c9bSToomas Soome 
reg_reply(DNSServiceRef sdref,const DNSServiceFlags flags,DNSServiceErrorType errorCode,const char * name,const char * regtype,const char * domain,void * context)8815ffb0c9bSToomas Soome static void DNSSD_API reg_reply(DNSServiceRef sdref, const DNSServiceFlags flags, DNSServiceErrorType errorCode,
8825ffb0c9bSToomas Soome                                 const char *name, const char *regtype, const char *domain, void *context)
8835ffb0c9bSToomas Soome {
8845ffb0c9bSToomas Soome     (void)sdref;    // Unused
8855ffb0c9bSToomas Soome     (void)flags;    // Unused
8865ffb0c9bSToomas Soome     (void)context;  // Unused
8875ffb0c9bSToomas Soome     EXIT_IF_LIBDISPATCH_FATAL_ERROR(errorCode);
8885ffb0c9bSToomas Soome 
8895ffb0c9bSToomas Soome     printtimestamp();
8905ffb0c9bSToomas Soome     printf("Got a reply for service %s.%s%s: ", name, regtype, domain);
8915ffb0c9bSToomas Soome 
8925ffb0c9bSToomas Soome     if (errorCode == kDNSServiceErr_NoError)
8935ffb0c9bSToomas Soome     {
8945ffb0c9bSToomas Soome         if (flags & kDNSServiceFlagsAdd) printf("Name now registered and active\n");
8955ffb0c9bSToomas Soome         else printf("Name registration removed\n");
8965ffb0c9bSToomas Soome         if (operation == 'A' || operation == 'U' || operation == 'N')
8975ffb0c9bSToomas Soome         {
8985ffb0c9bSToomas Soome             timeOut = 5;
8995ffb0c9bSToomas Soome #if _DNS_SD_LIBDISPATCH
9005ffb0c9bSToomas Soome             if (timer_source)
9015ffb0c9bSToomas Soome                 dispatch_source_set_timer(timer_source, dispatch_time(DISPATCH_TIME_NOW, (uint64_t)timeOut * NSEC_PER_SEC),
9025ffb0c9bSToomas Soome                                           (uint64_t)timeOut * NSEC_PER_SEC, 0);
9035ffb0c9bSToomas Soome #endif
9045ffb0c9bSToomas Soome         }
9055ffb0c9bSToomas Soome     }
9065ffb0c9bSToomas Soome     else if (errorCode == kDNSServiceErr_NameConflict)
9075ffb0c9bSToomas Soome     {
9085ffb0c9bSToomas Soome         printf("Name in use, please choose another\n");
9095ffb0c9bSToomas Soome         exit(-1);
9105ffb0c9bSToomas Soome     }
9115ffb0c9bSToomas Soome     else
9125ffb0c9bSToomas Soome         printf("Error %d\n", errorCode);
9135ffb0c9bSToomas Soome 
9145ffb0c9bSToomas Soome     if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
9155ffb0c9bSToomas Soome }
9165ffb0c9bSToomas Soome 
snprintf_safe(char * str,size_t size,const char * format,...)917*472cd20dSToomas Soome static int snprintf_safe(char *str, size_t size, const char *format, ...)
918*472cd20dSToomas Soome {
919*472cd20dSToomas Soome     int length = 0;
920*472cd20dSToomas Soome     va_list ptr;
921*472cd20dSToomas Soome     va_start(ptr, format);
922*472cd20dSToomas Soome     int result = vsnprintf(str, size, format, ptr);
923*472cd20dSToomas Soome     va_end(ptr);
924*472cd20dSToomas Soome     if (result > 0 && size > 0)
925*472cd20dSToomas Soome     {
926*472cd20dSToomas Soome         length = MIN((size_t)result, size-1);
927*472cd20dSToomas Soome     }
928*472cd20dSToomas Soome     return length;
929*472cd20dSToomas Soome }
930*472cd20dSToomas Soome 
9315ffb0c9bSToomas Soome // Output the wire-format domainname pointed to by rd
snprintd(char * p,int max,const unsigned char ** rd)9325ffb0c9bSToomas Soome static int snprintd(char *p, int max, const unsigned char **rd)
9335ffb0c9bSToomas Soome {
9345ffb0c9bSToomas Soome     const char *const buf = p;
9355ffb0c9bSToomas Soome     const char *const end = p + max;
936*472cd20dSToomas Soome     while (**rd)
937*472cd20dSToomas Soome     {
938*472cd20dSToomas Soome         p += snprintf_safe(p, end-p, "%.*s.", **rd, *rd+1);
939*472cd20dSToomas Soome         *rd += 1 + **rd;
9405ffb0c9bSToomas Soome     }
9415ffb0c9bSToomas Soome     *rd += 1;   // Advance over the final zero byte
9425ffb0c9bSToomas Soome     return(p-buf);
9435ffb0c9bSToomas Soome }
9445ffb0c9bSToomas Soome 
ParseDNSSECRecords(uint16_t rrtype,char * rdb,size_t rdb_size,unsigned const char * rd,uint16_t rdlen)9453b436d06SToomas Soome static void ParseDNSSECRecords(uint16_t rrtype, char *rdb, size_t rdb_size, unsigned const char *rd, uint16_t rdlen)
9465ffb0c9bSToomas Soome {
9473b436d06SToomas Soome     char *p = rdb;
9483b436d06SToomas Soome     switch (rrtype)
9495ffb0c9bSToomas Soome     {
9505ffb0c9bSToomas Soome         case kDNSServiceType_DS:
9515ffb0c9bSToomas Soome         {
9525ffb0c9bSToomas Soome             unsigned char *ptr;
9535ffb0c9bSToomas Soome             int i;
9545ffb0c9bSToomas Soome             rdataDS *rrds = (rdataDS *)rd;
955*472cd20dSToomas Soome             p += snprintf_safe(p, rdb + rdb_size - p, "%d  %d  %d  ",
9565ffb0c9bSToomas Soome                           rrds->alg, swap16(rrds->keyTag), rrds->digestType);
9575ffb0c9bSToomas Soome             ptr = (unsigned char *)(rd + DS_FIXED_SIZE);
9585ffb0c9bSToomas Soome             for (i = 0; i < (rdlen - DS_FIXED_SIZE); i++)
959*472cd20dSToomas Soome                 p += snprintf_safe(p, rdb + rdb_size - p, "%x", ptr[i]);
960*472cd20dSToomas Soome             break;
961*472cd20dSToomas Soome         }
962*472cd20dSToomas Soome 
9635ffb0c9bSToomas Soome         case kDNSServiceType_DNSKEY:
9645ffb0c9bSToomas Soome         {
9655ffb0c9bSToomas Soome             rdataDNSKey *rrkey = (rdataDNSKey *)rd;
966*472cd20dSToomas Soome             p += snprintf_safe(p, rdb + rdb_size - p, "%d  %d  %d  %u ", swap16(rrkey->flags), rrkey->proto,
9675ffb0c9bSToomas Soome                           rrkey->alg, (unsigned int)keytag((unsigned char *)rrkey, rdlen));
9685ffb0c9bSToomas Soome             base64Encode(p, rdb + rdb_size - p, (unsigned char *)(rd + DNSKEY_FIXED_SIZE), rdlen - DNSKEY_FIXED_SIZE);
9695ffb0c9bSToomas Soome             break;
9705ffb0c9bSToomas Soome         }
971c1de7575SRichard Lowe 
972c1de7575SRichard Lowe         case kDNSServiceType_NSEC:
9735ffb0c9bSToomas Soome         {
9745ffb0c9bSToomas Soome             unsigned char *next = (unsigned char *)rd;
9755ffb0c9bSToomas Soome             int len, bitmaplen;
9765ffb0c9bSToomas Soome             int win, wlen, type;
9775ffb0c9bSToomas Soome             unsigned char *bmap;
9785ffb0c9bSToomas Soome             char *l = NULL;
979c1de7575SRichard Lowe 
9805ffb0c9bSToomas Soome             l = p;
9815ffb0c9bSToomas Soome             p += snprintd(p, rdb + rdb_size - p, &rd);
9825ffb0c9bSToomas Soome             len = p - l + 1;
983c1de7575SRichard Lowe 
9845ffb0c9bSToomas Soome             bitmaplen = rdlen - len;
9855ffb0c9bSToomas Soome             bmap = (unsigned char *)((unsigned char *)next + len);
986c1de7575SRichard Lowe 
9875ffb0c9bSToomas Soome             while (bitmaplen > 0)
9885ffb0c9bSToomas Soome             {
9895ffb0c9bSToomas Soome                 int i;
990c1de7575SRichard Lowe 
9915ffb0c9bSToomas Soome                 if (bitmaplen < 3)
9925ffb0c9bSToomas Soome                 {
9935ffb0c9bSToomas Soome                     printf("Case NSEC: malformed nsec, bitmaplen %d short\n", bitmaplen);
9945ffb0c9bSToomas Soome                     break;
995c1de7575SRichard Lowe                 }
996c1de7575SRichard Lowe 
9975ffb0c9bSToomas Soome                 win = *bmap++;
9985ffb0c9bSToomas Soome                 wlen = *bmap++;
9995ffb0c9bSToomas Soome                 bitmaplen -= 2;
10005ffb0c9bSToomas Soome                 if (bitmaplen < wlen || wlen < 1 || wlen > 32)
10015ffb0c9bSToomas Soome                 {
10025ffb0c9bSToomas Soome                     printf("Case NSEC: malformed nsec, bitmaplen %d wlen %d\n", bitmaplen, wlen);
10035ffb0c9bSToomas Soome                     break;
10045ffb0c9bSToomas Soome                 }
10055ffb0c9bSToomas Soome                 if (win < 0 || win >= 256)
10065ffb0c9bSToomas Soome                 {
10075ffb0c9bSToomas Soome                     printf("Case NSEC: malformed nsec, bad window win %d\n", win);
10085ffb0c9bSToomas Soome                     break;
10095ffb0c9bSToomas Soome                 }
10105ffb0c9bSToomas Soome                 type = win * 256;
10115ffb0c9bSToomas Soome                 for (i = 0; i < wlen * 8; i++)
10125ffb0c9bSToomas Soome                 {
10135ffb0c9bSToomas Soome                     if (bmap[i>>3] & (128 >> (i&7)))
1014*472cd20dSToomas Soome                         p += snprintf_safe(p, rdb + rdb_size - p, " %s ", DNSTypeName(type + i));
10155ffb0c9bSToomas Soome                 }
10165ffb0c9bSToomas Soome                 bmap += wlen;
10175ffb0c9bSToomas Soome                 bitmaplen -= wlen;
10185ffb0c9bSToomas Soome             }
10195ffb0c9bSToomas Soome             break;
10205ffb0c9bSToomas Soome         }
1021c1de7575SRichard Lowe 
1022c1de7575SRichard Lowe         case kDNSServiceType_RRSIG:
10235ffb0c9bSToomas Soome         {
10245ffb0c9bSToomas Soome             rdataRRSig *rrsig = (rdataRRSig *)rd;
10255ffb0c9bSToomas Soome             unsigned char expTimeBuf[64];
10265ffb0c9bSToomas Soome             unsigned char inceptTimeBuf[64];
10275ffb0c9bSToomas Soome             unsigned long inceptClock;
10285ffb0c9bSToomas Soome             unsigned long expClock;
10295ffb0c9bSToomas Soome             const unsigned char *q = NULL;
10305ffb0c9bSToomas Soome             char *k = NULL;
10315ffb0c9bSToomas Soome             int len;
1032c1de7575SRichard Lowe 
10335ffb0c9bSToomas Soome             expClock = (unsigned long)swap32(rrsig->sigExpireTime);
10345ffb0c9bSToomas Soome             FormatTime(expClock, expTimeBuf, sizeof(expTimeBuf));
1035c1de7575SRichard Lowe 
10365ffb0c9bSToomas Soome             inceptClock = (unsigned long)swap32(rrsig->sigInceptTime);
10375ffb0c9bSToomas Soome             FormatTime(inceptClock, inceptTimeBuf, sizeof(inceptTimeBuf));
1038*472cd20dSToomas Soome 
1039*472cd20dSToomas Soome             p += snprintf_safe(p, rdb + rdb_size - p, " %-7s  %d  %d  %d  %s  %s  %7d  ",
10405ffb0c9bSToomas Soome                           DNSTypeName(swap16(rrsig->typeCovered)), rrsig->alg, rrsig->labels, swap32(rrsig->origTTL),
10415ffb0c9bSToomas Soome                           expTimeBuf, inceptTimeBuf, swap16(rrsig->keyTag));
1042c1de7575SRichard Lowe 
10435ffb0c9bSToomas Soome             q = (const unsigned char *)&rrsig->signerName;
10445ffb0c9bSToomas Soome             k = p;
10455ffb0c9bSToomas Soome             p += snprintd(p, rdb + rdb_size - p, &q);
10465ffb0c9bSToomas Soome             len = p - k + 1;
10473b436d06SToomas Soome 
10483b436d06SToomas Soome             if ((&rdb[rdb_size] - p) >= 2)
10493b436d06SToomas Soome             {
10503b436d06SToomas Soome                 *p++ = ' ';
10513b436d06SToomas Soome                 *p   = '\0';
10523b436d06SToomas Soome             }
10535ffb0c9bSToomas Soome             base64Encode(p, rdb + rdb_size - p, (unsigned char *)(rd + len + RRSIG_FIXED_SIZE), rdlen - (len + RRSIG_FIXED_SIZE));
10545ffb0c9bSToomas Soome             break;
10555ffb0c9bSToomas Soome         }
10565ffb0c9bSToomas Soome     }
1057c1de7575SRichard Lowe     return;
10585ffb0c9bSToomas Soome }
10595ffb0c9bSToomas Soome 
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)10605ffb0c9bSToomas Soome static void DNSSD_API qr_reply(DNSServiceRef sdref, const DNSServiceFlags flags, uint32_t ifIndex, DNSServiceErrorType errorCode,
10615ffb0c9bSToomas Soome                                const char *fullname, uint16_t rrtype, uint16_t rrclass, uint16_t rdlen, const void *rdata, uint32_t ttl, void *context)
10625ffb0c9bSToomas Soome {
10635ffb0c9bSToomas Soome     char *op = (flags & kDNSServiceFlagsAdd) ? "Add" : "Rmv";
10645ffb0c9bSToomas Soome     const unsigned char *rd  = rdata;
10655ffb0c9bSToomas Soome     const unsigned char *end = (const unsigned char *) rdata + rdlen;
10665ffb0c9bSToomas Soome     char rdb[1000] = "", *p = rdb;
10675ffb0c9bSToomas Soome     int unknowntype = 0;
10685ffb0c9bSToomas Soome     char dnssec_status[15] = "Unknown";
10695ffb0c9bSToomas Soome     char rr_type[RR_TYPE_SIZE];
1070*472cd20dSToomas Soome     char rr_class[6];
10715ffb0c9bSToomas Soome     DNSServiceFlags check_flags = flags;//local flags for dnssec status checking
1072*472cd20dSToomas Soome     int8_t enable_dnssec = ((check_flags & kDNSServiceFlagsEnableDNSSEC) != 0);
1073*472cd20dSToomas Soome     static int8_t enabled_dnssec_before = -1;
1074*472cd20dSToomas Soome 
1075*472cd20dSToomas Soome     if (enabled_dnssec_before == -1) {
1076*472cd20dSToomas Soome         enabled_dnssec_before = enable_dnssec;
1077*472cd20dSToomas Soome     }
10785ffb0c9bSToomas Soome 
10795ffb0c9bSToomas Soome     (void)sdref;    // Unused
10805ffb0c9bSToomas Soome     (void)ifIndex;  // Unused
10815ffb0c9bSToomas Soome     (void)ttl;      // Unused
10825ffb0c9bSToomas Soome     (void)context;  // Unused
10835ffb0c9bSToomas Soome     EXIT_IF_LIBDISPATCH_FATAL_ERROR(errorCode);
10845ffb0c9bSToomas Soome 
10855ffb0c9bSToomas Soome     if (num_printed++ == 0)
10865ffb0c9bSToomas Soome     {
1087*472cd20dSToomas Soome         printf("Timestamp     A/R    Flags if %-30s%-6s%-7s%s Rdata\n", "Name", "Type", "Class", enable_dnssec ? " DNSSECResult  " : "");
10885ffb0c9bSToomas Soome     }
10895ffb0c9bSToomas Soome     printtimestamp();
10905ffb0c9bSToomas Soome 
10915ffb0c9bSToomas Soome     switch (rrclass)
10925ffb0c9bSToomas Soome     {
10935ffb0c9bSToomas Soome         case kDNSServiceClass_IN:
10945ffb0c9bSToomas Soome             strncpy(rr_class, "IN", sizeof(rr_class));
10955ffb0c9bSToomas Soome             break;
10965ffb0c9bSToomas Soome         default:
10975ffb0c9bSToomas Soome             snprintf(rr_class, sizeof(rr_class), "%d", rrclass);
10985ffb0c9bSToomas Soome             break;
10995ffb0c9bSToomas Soome     }
11005ffb0c9bSToomas Soome     strncpy(rr_type, DNSTypeName(rrtype), sizeof(rr_type));
11015ffb0c9bSToomas Soome 
11025ffb0c9bSToomas Soome     if (!errorCode) //to avoid printing garbage in rdata
11035ffb0c9bSToomas Soome     {
1104*472cd20dSToomas Soome         switch (rrtype)
11055ffb0c9bSToomas Soome         {
1106*472cd20dSToomas Soome             case kDNSServiceType_A:
1107*472cd20dSToomas Soome                 snprintf_safe(rdb, sizeof(rdb), "%d.%d.%d.%d", rd[0], rd[1], rd[2], rd[3]);
1108*472cd20dSToomas Soome                 break;
1109*472cd20dSToomas Soome 
1110*472cd20dSToomas Soome             case kDNSServiceType_NS:
1111*472cd20dSToomas Soome             case kDNSServiceType_CNAME:
1112*472cd20dSToomas Soome             case kDNSServiceType_PTR:
1113*472cd20dSToomas Soome             case kDNSServiceType_DNAME:
1114*472cd20dSToomas Soome                 snprintd(p, sizeof(rdb), &rd);
1115*472cd20dSToomas Soome                 break;
1116*472cd20dSToomas Soome 
1117*472cd20dSToomas Soome             case kDNSServiceType_SOA:
1118*472cd20dSToomas Soome                 p += snprintd(p, rdb + sizeof(rdb) - p, &rd);           // mname
1119*472cd20dSToomas Soome                 p += snprintf_safe(p, rdb + sizeof(rdb) - p, " ");
1120*472cd20dSToomas Soome                 p += snprintd(p, rdb + sizeof(rdb) - p, &rd);           // rname
1121*472cd20dSToomas Soome                      snprintf(p, rdb + sizeof(rdb) - p, " Ser %d Ref %d Ret %d Exp %d Min %d",
1122*472cd20dSToomas Soome                          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]));
1123*472cd20dSToomas Soome                 break;
1124*472cd20dSToomas Soome 
1125*472cd20dSToomas Soome             case kDNSServiceType_AAAA:
1126*472cd20dSToomas Soome                 snprintf(rdb, sizeof(rdb), "%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X",
1127*472cd20dSToomas Soome                     rd[0x0], rd[0x1], rd[0x2], rd[0x3], rd[0x4], rd[0x5], rd[0x6], rd[0x7],
1128*472cd20dSToomas Soome                     rd[0x8], rd[0x9], rd[0xA], rd[0xB], rd[0xC], rd[0xD], rd[0xE], rd[0xF]);
1129*472cd20dSToomas Soome                 break;
1130*472cd20dSToomas Soome 
1131*472cd20dSToomas Soome             case kDNSServiceType_SRV:
1132*472cd20dSToomas Soome                 p += snprintf_safe(p, rdb + sizeof(rdb) - p, "%d %d %d ",        // priority, weight, port
1133*472cd20dSToomas Soome                          ntohs(*(unsigned short*)rd), ntohs(*(unsigned short*)(rd+2)), ntohs(*(unsigned short*)(rd+4)));
1134*472cd20dSToomas Soome                 rd += 6;
1135*472cd20dSToomas Soome                      snprintd(p, rdb + sizeof(rdb) - p, &rd);               // target host
1136*472cd20dSToomas Soome                 break;
1137*472cd20dSToomas Soome 
1138*472cd20dSToomas Soome             case kDNSServiceType_DS:
1139*472cd20dSToomas Soome             case kDNSServiceType_DNSKEY:
1140*472cd20dSToomas Soome             case kDNSServiceType_NSEC:
1141*472cd20dSToomas Soome             case kDNSServiceType_RRSIG:
1142*472cd20dSToomas Soome                 ParseDNSSECRecords(rrtype, rdb, sizeof(rdb), rd, rdlen);
1143*472cd20dSToomas Soome                 break;
1144*472cd20dSToomas Soome 
1145*472cd20dSToomas Soome             default:
1146*472cd20dSToomas Soome                 snprintf(rdb, sizeof(rdb), "%d bytes%s", rdlen, rdlen ? ":" : "");
1147*472cd20dSToomas Soome                 unknowntype = 1;
1148*472cd20dSToomas Soome                 break;
11495ffb0c9bSToomas Soome         }
11505ffb0c9bSToomas Soome     }
11515ffb0c9bSToomas Soome 
1152*472cd20dSToomas Soome     if (check_flags & kDNSServiceFlagsSecure)
1153*472cd20dSToomas Soome         strncpy(dnssec_status, "Secure            ", sizeof(dnssec_status));
1154*472cd20dSToomas Soome     else if (check_flags & kDNSServiceFlagsInsecure)
1155*472cd20dSToomas Soome         strncpy(dnssec_status, "Insecure          ", sizeof(dnssec_status));
1156*472cd20dSToomas Soome     else if (check_flags & kDNSServiceFlagsIndeterminate)
1157*472cd20dSToomas Soome         strncpy(dnssec_status, "Indeterminate     ", sizeof(dnssec_status));
1158*472cd20dSToomas Soome     else if (check_flags & kDNSServiceFlagsBogus)
1159*472cd20dSToomas Soome         strncpy(dnssec_status, "Bogus             ", sizeof(dnssec_status));
11605ffb0c9bSToomas Soome     else
1161*472cd20dSToomas Soome         strncpy(dnssec_status, "                  ", sizeof(dnssec_status));
1162*472cd20dSToomas Soome 
1163*472cd20dSToomas Soome     printf("%s%9X%3d %-30s%-7s%-6s %s%s",
1164*472cd20dSToomas Soome         op, flags, ifIndex, fullname, rr_type, rr_class, enabled_dnssec_before ? dnssec_status : "", rdb);
1165*472cd20dSToomas Soome 
1166*472cd20dSToomas Soome 
11675ffb0c9bSToomas Soome     if (unknowntype)
1168c1de7575SRichard Lowe     {
1169c1de7575SRichard Lowe         while (rd < end)
11705ffb0c9bSToomas Soome             printf(" %02X", *rd++);
11715ffb0c9bSToomas Soome     }
11725ffb0c9bSToomas Soome     if (errorCode)
11735ffb0c9bSToomas Soome     {
1174c1de7575SRichard Lowe         if (errorCode == kDNSServiceErr_NoSuchRecord)
11755ffb0c9bSToomas Soome             printf("    No Such Record");
1176*472cd20dSToomas Soome         else if (errorCode == kDNSServiceErr_NoAuth)
1177*472cd20dSToomas Soome             printf("    No Authorization");
11785ffb0c9bSToomas Soome         else if (errorCode == kDNSServiceErr_Timeout)
11795ffb0c9bSToomas Soome         {
11805ffb0c9bSToomas Soome             printf("    No Such Record\n");
11815ffb0c9bSToomas Soome             printf("Query Timed Out\n");
11825ffb0c9bSToomas Soome             exit(1);
11835ffb0c9bSToomas Soome         }
11845ffb0c9bSToomas Soome     }
11855ffb0c9bSToomas Soome     printf("\n");
11865ffb0c9bSToomas Soome 
11875ffb0c9bSToomas Soome     if (operation == 'C')
11885ffb0c9bSToomas Soome         if (flags & kDNSServiceFlagsAdd)
11895ffb0c9bSToomas Soome             DNSServiceReconfirmRecord(flags, ifIndex, fullname, rrtype, rrclass, rdlen, rdata);
11905ffb0c9bSToomas Soome 
1191c1de7575SRichard Lowe     if (!(flags & kDNSServiceFlagsMoreComing))
11925ffb0c9bSToomas Soome         fflush(stdout);
11935ffb0c9bSToomas Soome }
11945ffb0c9bSToomas Soome 
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)11955ffb0c9bSToomas Soome 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)
11965ffb0c9bSToomas Soome {
11975ffb0c9bSToomas Soome     (void)sdref;       // Unused
11985ffb0c9bSToomas Soome     (void)flags;       // Unused
11995ffb0c9bSToomas Soome     (void)context;     // Unused
12005ffb0c9bSToomas Soome     EXIT_IF_LIBDISPATCH_FATAL_ERROR(errorCode);
12015ffb0c9bSToomas Soome 
12025ffb0c9bSToomas Soome     if (num_printed++ == 0) printf("Timestamp     if   %-20s %-15s %-15s %-15s %-6s\n", "External Address", "Protocol", "Internal Port", "External Port", "TTL");
12035ffb0c9bSToomas Soome     printtimestamp();
12045ffb0c9bSToomas Soome     if (errorCode && errorCode != kDNSServiceErr_DoubleNAT) printf("Error code %d\n", errorCode);
12055ffb0c9bSToomas Soome     else
12065ffb0c9bSToomas Soome     {
12075ffb0c9bSToomas Soome         const unsigned char *digits = (const unsigned char *)&publicAddress;
12085ffb0c9bSToomas Soome         char addr[256];
12095ffb0c9bSToomas Soome 
12105ffb0c9bSToomas Soome         snprintf(addr, sizeof(addr), "%d.%d.%d.%d", digits[0], digits[1], digits[2], digits[3]);
12115ffb0c9bSToomas Soome         printf("%-4d %-20s %-15d %-15d %-15d %-6d%s\n", ifIndex, addr, protocol, ntohs(privatePort), ntohs(publicPort), ttl, errorCode == kDNSServiceErr_DoubleNAT ? " Double NAT" : "");
12125ffb0c9bSToomas Soome     }
12135ffb0c9bSToomas Soome 
12145ffb0c9bSToomas Soome     if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
12155ffb0c9bSToomas Soome }
12165ffb0c9bSToomas Soome 
addrinfo_reply(DNSServiceRef sdref,const DNSServiceFlags flags,uint32_t interfaceIndex,DNSServiceErrorType errorCode,const char * hostname,const struct sockaddr * address,uint32_t ttl,void * context)12175ffb0c9bSToomas Soome 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)
12185ffb0c9bSToomas Soome {
12195ffb0c9bSToomas Soome     char *op = (flags & kDNSServiceFlagsAdd) ? "Add" : "Rmv";
12205ffb0c9bSToomas Soome     char addr[256] = "";
1221c1de7575SRichard Lowe     char dnssec_status[15] = "Unknown";
12225ffb0c9bSToomas Soome     DNSServiceFlags check_flags = flags;
12235ffb0c9bSToomas Soome 	(void) sdref;
12245ffb0c9bSToomas Soome 	(void) context;
1225*472cd20dSToomas Soome     unsigned char enable_dnssec = ((check_flags & kDNSServiceFlagsEnableDNSSEC) != 0);
12265ffb0c9bSToomas Soome 
12275ffb0c9bSToomas Soome     EXIT_IF_LIBDISPATCH_FATAL_ERROR(errorCode);
12285ffb0c9bSToomas Soome 
12295ffb0c9bSToomas Soome     if (num_printed++ == 0)
12305ffb0c9bSToomas Soome     {
1231*472cd20dSToomas Soome         printf("Timestamp     A/R    Flags if %-38s %-44s %s%s\n", "Hostname", "Address", "TTL", enable_dnssec ? "DNSSECResult" : "");
12325ffb0c9bSToomas Soome     }
12335ffb0c9bSToomas Soome     printtimestamp();
1234c1de7575SRichard Lowe