1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2006 Apple Computer, Inc. All rights reserved.
5 * Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
6 * ("Apple") in consideration of your agreement to the following terms, and your
7 * use, installation, modification or redistribution of this Apple software
8 * constitutes acceptance of these terms. If you do not agree with these terms,
9 * please do not use, install, modify or redistribute this Apple software.
11 * In consideration of your agreement to abide by the following terms, and subject
12 * to these terms, Apple grants you a personal, non-exclusive license, under Apple's
13 * copyrights in this original Apple software (the "Apple Software"), to use,
14 * reproduce, modify and redistribute the Apple Software, with or without
15 * modifications, in source and/or binary forms; provided that if you redistribute
16 * the Apple Software in its entirety and without modifications, you must retain
17 * this notice and the following text and disclaimers in all such redistributions of
18 * the Apple Software. Neither the name, trademarks, service marks or logos of
19 * Apple Computer, Inc. may be used to endorse or promote products derived from the
20 * Apple Software without specific prior written permission from Apple. Except as
21 * expressly stated in this notice, no other rights or licenses, express or implied,
22 * are granted by Apple herein, including but not limited to any patent rights that
23 * may be infringed by your derivative works or by other works in which the Apple
24 * Software may be incorporated.
26 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
27 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
28 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
30 * COMBINATION WITH YOUR PRODUCTS.
32 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
34 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
36 * OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
37 * (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
38 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * This code follows the "Whitesmiths style" C indentation rules. Plenty of discussion
42 * on C indentation can be found on the web, such as <http://www.kafejo.com/komp/1tbs.htm>,
43 * but for the sake of brevity here I will say just this: Curly braces are not syntactially
44 * part of an "if" statement; they are the beginning and ending markers of a compound statement;
45 * therefore common sense dictates that if they are part of a compound statement then they
46 * should be indented to the same level as everything else in that compound statement.
47 * Indenting curly braces at the same level as the "if" implies that curly braces are
48 * part of the "if", which is false. (This is as misleading as people who write "char* x,y;"
49 * thinking that variables x and y are both of type "char*" -- and anyone who doesn't
50 * understand why variable y is not of type "char*" just proves the point that poor code
51 * layout leads people to unfortunate misunderstandings about how the C language really works.)
53 To build this tool, copy and paste the following into a command line:
56 gcc dns-sd.c -o dns-sd
59 gcc dns-sd.c -o dns-sd -I../mDNSShared -ldns_sd
62 cl dns-sd.c -I../mDNSShared -DNOT_HAVE_GETOPT ws2_32.lib ..\mDNSWindows\DLL\Release\dnssd.lib
63 (may require that you run a Visual Studio script such as vsvars32.bat first)
66 // For testing changes to dnssd_clientstub.c, uncomment this line and the code will be compiled
67 // with an embedded copy of the client stub instead of linking the system library version at runtime.
68 // This also useful to work around link errors when you're working on an older version of Mac OS X,
69 // and trying to build a newer version of the "dns-sd" command which uses new API entry points that
70 // aren't in the system's /usr/lib/libSystem.dylib.
71 //#define TEST_NEW_CLIENTSTUB 1
74 #include <stdio.h> // For stdout, stderr
75 #include <stdlib.h> // For exit()
76 #include <string.h> // For strlen(), strcpy(), bzero()
77 #include <errno.h> // For errno, EINTR
79 #include <sys/types.h> // For u_char
86 #define getpid _getpid
87 #define strcasecmp _stricmp
88 #define snprintf _snprintf
89 static const char kFilePathSep
= '\\';
91 #include <unistd.h> // For getopt() and optind
92 #include <netdb.h> // For getaddrinfo()
93 #include <sys/time.h> // For struct timeval
94 #include <sys/socket.h> // For AF_INET
95 #include <netinet/in.h> // For struct sockaddr_in()
96 #include <arpa/inet.h> // For inet_addr()
97 #include <net/if.h> // For if_nametoindex()
98 static const char kFilePathSep
= '/';
101 #if (TEST_NEW_CLIENTSTUB && !defined(__APPLE_API_PRIVATE))
102 #define __APPLE_API_PRIVATE 1
107 #if TEST_NEW_CLIENTSTUB
108 #include "../mDNSShared/dnssd_ipc.c"
109 #include "../mDNSShared/dnssd_clientlib.c"
110 #include "../mDNSShared/dnssd_clientstub.c"
113 // 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)
114 #if _DNS_SD_H+0 >= 116
115 #define HAS_NAT_PMP_API 1
116 #define HAS_ADDRINFO_API 1
118 #define kDNSServiceFlagsReturnIntermediates 0
121 //*************************************************************************************************************
124 typedef union { unsigned char b
[2]; unsigned short NotAnInteger
; } Opaque16
;
126 static int operation
;
127 static uint32_t opinterface
= kDNSServiceInterfaceIndexAny
;
128 static DNSServiceRef client
= NULL
;
129 static DNSServiceRef client_pa
= NULL
; // DNSServiceRef for RegisterProxyAddressRecord
130 static DNSServiceRef sc1
, sc2
, sc3
; // DNSServiceRefs for kDNSServiceFlagsShareConnection testing
132 static int num_printed
;
133 static char addtest
= 0;
134 static DNSRecordRef record
= NULL
;
135 static char myhinfoW
[14] = "\002PC\012Windows XP";
136 static char myhinfoX
[ 9] = "\003Mac\004OS X";
137 static char updatetest
[3] = "\002AA";
138 static char bigNULL
[8200];
140 // Note: the select() implementation on Windows (Winsock2) fails with any timeout much larger than this
141 #define LONG_TIME 100000000
143 static volatile int stopNow
= 0;
144 static volatile int timeOut
= LONG_TIME
;
146 //*************************************************************************************************************
147 // Supporting Utility Functions
149 static uint16_t GetRRType(const char *s
)
151 if (!strcasecmp(s
, "A" )) return(kDNSServiceType_A
);
152 else if (!strcasecmp(s
, "NS" )) return(kDNSServiceType_NS
);
153 else if (!strcasecmp(s
, "MD" )) return(kDNSServiceType_MD
);
154 else if (!strcasecmp(s
, "MF" )) return(kDNSServiceType_MF
);
155 else if (!strcasecmp(s
, "CNAME" )) return(kDNSServiceType_CNAME
);
156 else if (!strcasecmp(s
, "SOA" )) return(kDNSServiceType_SOA
);
157 else if (!strcasecmp(s
, "MB" )) return(kDNSServiceType_MB
);
158 else if (!strcasecmp(s
, "MG" )) return(kDNSServiceType_MG
);
159 else if (!strcasecmp(s
, "MR" )) return(kDNSServiceType_MR
);
160 else if (!strcasecmp(s
, "NULL" )) return(kDNSServiceType_NULL
);
161 else if (!strcasecmp(s
, "WKS" )) return(kDNSServiceType_WKS
);
162 else if (!strcasecmp(s
, "PTR" )) return(kDNSServiceType_PTR
);
163 else if (!strcasecmp(s
, "HINFO" )) return(kDNSServiceType_HINFO
);
164 else if (!strcasecmp(s
, "MINFO" )) return(kDNSServiceType_MINFO
);
165 else if (!strcasecmp(s
, "MX" )) return(kDNSServiceType_MX
);
166 else if (!strcasecmp(s
, "TXT" )) return(kDNSServiceType_TXT
);
167 else if (!strcasecmp(s
, "RP" )) return(kDNSServiceType_RP
);
168 else if (!strcasecmp(s
, "AFSDB" )) return(kDNSServiceType_AFSDB
);
169 else if (!strcasecmp(s
, "X25" )) return(kDNSServiceType_X25
);
170 else if (!strcasecmp(s
, "ISDN" )) return(kDNSServiceType_ISDN
);
171 else if (!strcasecmp(s
, "RT" )) return(kDNSServiceType_RT
);
172 else if (!strcasecmp(s
, "NSAP" )) return(kDNSServiceType_NSAP
);
173 else if (!strcasecmp(s
, "NSAP_PTR")) return(kDNSServiceType_NSAP_PTR
);
174 else if (!strcasecmp(s
, "SIG" )) return(kDNSServiceType_SIG
);
175 else if (!strcasecmp(s
, "KEY" )) return(kDNSServiceType_KEY
);
176 else if (!strcasecmp(s
, "PX" )) return(kDNSServiceType_PX
);
177 else if (!strcasecmp(s
, "GPOS" )) return(kDNSServiceType_GPOS
);
178 else if (!strcasecmp(s
, "AAAA" )) return(kDNSServiceType_AAAA
);
179 else if (!strcasecmp(s
, "LOC" )) return(kDNSServiceType_LOC
);
180 else if (!strcasecmp(s
, "NXT" )) return(kDNSServiceType_NXT
);
181 else if (!strcasecmp(s
, "EID" )) return(kDNSServiceType_EID
);
182 else if (!strcasecmp(s
, "NIMLOC" )) return(kDNSServiceType_NIMLOC
);
183 else if (!strcasecmp(s
, "SRV" )) return(kDNSServiceType_SRV
);
184 else if (!strcasecmp(s
, "ATMA" )) return(kDNSServiceType_ATMA
);
185 else if (!strcasecmp(s
, "NAPTR" )) return(kDNSServiceType_NAPTR
);
186 else if (!strcasecmp(s
, "KX" )) return(kDNSServiceType_KX
);
187 else if (!strcasecmp(s
, "CERT" )) return(kDNSServiceType_CERT
);
188 else if (!strcasecmp(s
, "A6" )) return(kDNSServiceType_A6
);
189 else if (!strcasecmp(s
, "DNAME" )) return(kDNSServiceType_DNAME
);
190 else if (!strcasecmp(s
, "SINK" )) return(kDNSServiceType_SINK
);
191 else if (!strcasecmp(s
, "OPT" )) return(kDNSServiceType_OPT
);
192 else if (!strcasecmp(s
, "TKEY" )) return(kDNSServiceType_TKEY
);
193 else if (!strcasecmp(s
, "TSIG" )) return(kDNSServiceType_TSIG
);
194 else if (!strcasecmp(s
, "IXFR" )) return(kDNSServiceType_IXFR
);
195 else if (!strcasecmp(s
, "AXFR" )) return(kDNSServiceType_AXFR
);
196 else if (!strcasecmp(s
, "MAILB" )) return(kDNSServiceType_MAILB
);
197 else if (!strcasecmp(s
, "MAILA" )) return(kDNSServiceType_MAILA
);
198 else if (!strcasecmp(s
, "ANY" )) return(kDNSServiceType_ANY
);
199 else return(atoi(s
));
202 #if HAS_NAT_PMP_API | HAS_ADDRINFO_API
203 static DNSServiceProtocol
GetProtocol(const char *s
)
205 if (!strcasecmp(s
, "v4" )) return(kDNSServiceProtocol_IPv4
);
206 else if (!strcasecmp(s
, "v6" )) return(kDNSServiceProtocol_IPv6
);
207 else if (!strcasecmp(s
, "v4v6" )) return(kDNSServiceProtocol_IPv4
| kDNSServiceProtocol_IPv6
);
208 else if (!strcasecmp(s
, "v6v4" )) return(kDNSServiceProtocol_IPv4
| kDNSServiceProtocol_IPv6
);
209 else if (!strcasecmp(s
, "udp" )) return(kDNSServiceProtocol_UDP
);
210 else if (!strcasecmp(s
, "tcp" )) return(kDNSServiceProtocol_TCP
);
211 else if (!strcasecmp(s
, "udptcp" )) return(kDNSServiceProtocol_UDP
| kDNSServiceProtocol_TCP
);
212 else if (!strcasecmp(s
, "tcpudp" )) return(kDNSServiceProtocol_UDP
| kDNSServiceProtocol_TCP
);
213 else return(atoi(s
));
217 //*************************************************************************************************************
218 // Sample callback functions for each of the operation types
220 static void printtimestamp(void)
226 time_t uct
= time(NULL
);
227 tm
= *localtime(&uct
);
228 GetLocalTime(&sysTime
);
229 ms
= sysTime
.wMilliseconds
;
232 gettimeofday(&tv
, NULL
);
233 localtime_r((time_t*)&tv
.tv_sec
, &tm
);
234 ms
= tv
.tv_usec
/1000;
236 printf("%2d:%02d:%02d.%03d ", tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ms
);
239 #define DomainMsg(X) (((X) & kDNSServiceFlagsDefault) ? "(Default)" : \
240 ((X) & kDNSServiceFlagsAdd) ? "Added" : "Removed")
242 static const char *GetNextLabel(const char *cstr
, char label
[64])
245 while (*cstr
&& *cstr
!= '.') // While we have characters in the label...
251 if (isdigit(cstr
[-1]) && isdigit(cstr
[0]) && isdigit(cstr
[1]))
253 int v0
= cstr
[-1] - '0'; // then interpret as three-digit decimal
254 int v1
= cstr
[ 0] - '0';
255 int v2
= cstr
[ 1] - '0';
256 int val
= v0
* 100 + v1
* 10 + v2
;
257 if (val
<= 255) { c
= (char)val
; cstr
+= 2; } // If valid three-digit decimal value, use it
261 if (ptr
>= label
+64) return(NULL
);
263 if (*cstr
) cstr
++; // Skip over the trailing dot (if present)
268 static void DNSSD_API
enum_reply(DNSServiceRef sdref
, const DNSServiceFlags flags
, uint32_t ifIndex
,
269 DNSServiceErrorType errorCode
, const char *replyDomain
, void *context
)
271 DNSServiceFlags partialflags
= flags
& ~(kDNSServiceFlagsMoreComing
| kDNSServiceFlagsAdd
| kDNSServiceFlagsDefault
);
272 int labels
= 0, depth
= 0, i
, initial
= 0;
274 const char *label
[128];
276 (void)sdref
; // Unused
277 (void)ifIndex
; // Unused
278 (void)context
; // Unused
280 // 1. Print the header
281 if (num_printed
++ == 0) printf("Timestamp Recommended %s domain\n", operation
== 'E' ? "Registration" : "Browsing");
284 printf("Error code %d\n", errorCode
);
285 else if (!*replyDomain
)
286 printf("Error: No reply domain\n");
289 printf("%-10s", DomainMsg(flags
));
290 printf("%-8s", (flags
& kDNSServiceFlagsMoreComing
) ? "(More)" : "");
291 if (partialflags
) printf("Flags: %4X ", partialflags
);
294 // 2. Count the labels
297 label
[labels
++] = replyDomain
;
298 replyDomain
= GetNextLabel(replyDomain
, text
);
301 // 3. Decide if we're going to clump the last two or three labels (e.g. "apple.com", or "nicta.com.au")
302 if (labels
>= 3 && replyDomain
- label
[labels
-1] <= 3 && label
[labels
-1] - label
[labels
-2] <= 4) initial
= 3;
303 else if (labels
>= 2 && replyDomain
- label
[labels
-1] <= 4) initial
= 2;
307 // 4. Print the initial one-, two- or three-label clump
308 for (i
=0; i
<initial
; i
++)
310 GetNextLabel(label
[labels
+i
], text
);
311 if (i
>0) printf(".");
316 // 5. Print the remainder of the hierarchy
317 for (depth
=0; depth
<labels
; depth
++)
320 for (i
=0; i
<=depth
; i
++) printf("- ");
321 GetNextLabel(label
[labels
-1-depth
], text
);
322 printf("> %s\n", text
);
326 if (!(flags
& kDNSServiceFlagsMoreComing
)) fflush(stdout
);
329 static void DNSSD_API
browse_reply(DNSServiceRef sdref
, const DNSServiceFlags flags
, uint32_t ifIndex
, DNSServiceErrorType errorCode
,
330 const char *replyName
, const char *replyType
, const char *replyDomain
, void *context
)
332 char *op
= (flags
& kDNSServiceFlagsAdd
) ? "Add" : "Rmv";
333 (void)sdref
; // Unused
334 (void)context
; // Unused
335 if (num_printed
++ == 0) printf("Timestamp A/R Flags if %-25s %-25s %s\n", "Domain", "Service Type", "Instance Name");
337 if (errorCode
) printf("Error code %d\n", errorCode
);
338 else printf("%s%6X%3d %-25s %-25s %s\n", op
, flags
, ifIndex
, replyDomain
, replyType
, replyName
);
339 if (!(flags
& kDNSServiceFlagsMoreComing
)) fflush(stdout
);
341 // To test selective cancellation of operations of shared sockets,
342 // cancel the current operation when we've got a multiple of five results
343 //if (operation == 'S' && num_printed % 5 == 0) DNSServiceRefDeallocate(sdref);
346 static void ShowTXTRecord(uint16_t txtLen
, const unsigned char *txtRecord
)
348 const unsigned char *ptr
= txtRecord
;
349 const unsigned char *max
= txtRecord
+ txtLen
;
352 const unsigned char *const end
= ptr
+ 1 + ptr
[0];
353 if (end
> max
) { printf("<< invalid data >>"); break; }
354 if (++ptr
< end
) printf(" "); // As long as string is non-empty, begin with a space
357 // We'd like the output to be shell-friendly, so that it can be copied and pasted unchanged into a "dns-sd -R" command.
358 // However, this is trickier than it seems. Enclosing a string in double quotes doesn't necessarily make it
359 // shell-safe, because shells still expand variables like $foo even when they appear inside quoted strings.
360 // Enclosing a string in single quotes is better, but when using single quotes even backslash escapes are ignored,
361 // meaning there's simply no way to represent a single quote (or apostrophe) inside a single-quoted string.
362 // The only remaining solution is not to surround the string with quotes at all, but instead to use backslash
363 // escapes to encode spaces and all other known shell metacharacters.
364 // (If we've missed any known shell metacharacters, please let us know.)
365 // In addition, non-printing ascii codes (0-31) are displayed as \xHH, using a two-digit hex value.
366 // Because '\' is itself a shell metacharacter (the shell escape character), it has to be escaped as "\\" to survive
367 // the round-trip to the shell and back. This means that a single '\' is represented here as EIGHT backslashes:
368 // The C compiler eats half of them, resulting in four appearing in the output.
369 // The shell parses those four as a pair of "\\" sequences, passing two backslashes to the "dns-sd -R" command.
370 // The "dns-sd -R" command interprets this single "\\" pair as an escaped literal backslash. Sigh.
371 if (strchr(" &;`'\"|*?~<>^()[]{}$", *ptr
)) printf("\\");
372 if (*ptr
== '\\') printf("\\\\\\\\");
373 else if (*ptr
>= ' ' ) printf("%c", *ptr
);
374 else printf("\\\\x%02X", *ptr
);
380 static void DNSSD_API
resolve_reply(DNSServiceRef sdref
, const DNSServiceFlags flags
, uint32_t ifIndex
, DNSServiceErrorType errorCode
,
381 const char *fullname
, const char *hosttarget
, uint16_t opaqueport
, uint16_t txtLen
, const unsigned char *txtRecord
, void *context
)
383 union { uint16_t s
; u_char b
[2]; } port
= { opaqueport
};
384 uint16_t PortAsNumber
= ((uint16_t)port
.b
[0]) << 8 | port
.b
[1];
386 (void)sdref
; // Unused
387 (void)ifIndex
; // Unused
388 (void)context
; // Unused
391 if (errorCode
) printf("Error code %d\n", errorCode
);
394 printf("%s can be reached at %s:%u", fullname
, hosttarget
, PortAsNumber
);
395 if (flags
) printf(" Flags: %X", flags
);
396 // Don't show degenerate TXT records containing nothing but a single empty string
397 if (txtLen
> 1) { printf("\n"); ShowTXTRecord(txtLen
, txtRecord
); }
401 if (!(flags
& kDNSServiceFlagsMoreComing
)) fflush(stdout
);
404 static void myTimerCallBack(void)
406 DNSServiceErrorType err
= kDNSServiceErr_Unknown
;
414 case 0: printf("Adding Test HINFO record\n");
415 err
= DNSServiceAddRecord(client
, &record
, 0, kDNSServiceType_HINFO
, sizeof(myhinfoW
), &myhinfoW
[0], 0);
418 case 1: printf("Updating Test HINFO record\n");
419 err
= DNSServiceUpdateRecord(client
, record
, 0, sizeof(myhinfoX
), &myhinfoX
[0], 0);
422 case 2: printf("Removing Test HINFO record\n");
423 err
= DNSServiceRemoveRecord(client
, record
, 0);
432 if (updatetest
[1] != 'Z') updatetest
[1]++;
433 else updatetest
[1] = 'A';
434 updatetest
[0] = 3 - updatetest
[0];
435 updatetest
[2] = updatetest
[1];
437 printf("Updating Test TXT record to %c\n", updatetest
[1]);
438 err
= DNSServiceUpdateRecord(client
, NULL
, 0, 1+updatetest
[0], &updatetest
[0], 0);
444 printf("Adding big NULL record\n");
445 err
= DNSServiceAddRecord(client
, &record
, 0, kDNSServiceType_NULL
, sizeof(bigNULL
), &bigNULL
[0], 0);
446 if (err
) printf("Failed: %d\n", err
); else printf("Succeeded\n");
452 if (err
!= kDNSServiceErr_NoError
)
454 fprintf(stderr
, "DNSService add/update/remove failed %ld\n", (long int)err
);
459 static void DNSSD_API
reg_reply(DNSServiceRef sdref
, const DNSServiceFlags flags
, DNSServiceErrorType errorCode
,
460 const char *name
, const char *regtype
, const char *domain
, void *context
)
462 (void)sdref
; // Unused
463 (void)flags
; // Unused
464 (void)context
; // Unused
467 printf("Got a reply for service %s.%s%s: ", name
, regtype
, domain
);
469 if (errorCode
== kDNSServiceErr_NoError
)
471 if (flags
& kDNSServiceFlagsAdd
) printf("Name now registered and active\n");
472 else printf("Name registration removed\n");
473 if (operation
== 'A' || operation
== 'U' || operation
== 'N') timeOut
= 5;
475 else if (errorCode
== kDNSServiceErr_NameConflict
)
477 printf("Name in use, please choose another\n");
481 printf("Error %d\n", errorCode
);
483 if (!(flags
& kDNSServiceFlagsMoreComing
)) fflush(stdout
);
486 // Output the wire-format domainname pointed to by rd
487 static int snprintd(char *p
, int max
, const unsigned char **rd
)
489 const char *const buf
= p
;
490 const char *const end
= p
+ max
;
491 while (**rd
) { p
+= snprintf(p
, end
-p
, "%.*s.", **rd
, *rd
+1); *rd
+= 1 + **rd
; }
492 *rd
+= 1; // Advance over the final zero byte
496 static void DNSSD_API
qr_reply(DNSServiceRef sdref
, const DNSServiceFlags flags
, uint32_t ifIndex
, DNSServiceErrorType errorCode
,
497 const char *fullname
, uint16_t rrtype
, uint16_t rrclass
, uint16_t rdlen
, const void *rdata
, uint32_t ttl
, void *context
)
499 char *op
= (flags
& kDNSServiceFlagsAdd
) ? "Add" : "Rmv";
500 const unsigned char *rd
= rdata
;
501 const unsigned char *end
= (const unsigned char *) rdata
+ rdlen
;
502 char rdb
[1000] = "", *p
= rdb
;
505 (void)sdref
; // Unused
506 (void)flags
; // Unused
507 (void)ifIndex
; // Unused
509 (void)context
; // Unused
511 if (num_printed
++ == 0) printf("Timestamp A/R Flags if %-30s%4s%4s Rdata\n", "Name", "T", "C");
518 case kDNSServiceType_A
:
519 snprintf(rdb
, sizeof(rdb
), "%d.%d.%d.%d", rd
[0], rd
[1], rd
[2], rd
[3]);
522 case kDNSServiceType_NS
:
523 case kDNSServiceType_CNAME
:
524 case kDNSServiceType_PTR
:
525 case kDNSServiceType_DNAME
:
526 p
+= snprintd(p
, sizeof(rdb
), &rd
);
529 case kDNSServiceType_SOA
:
530 p
+= snprintd(p
, rdb
+ sizeof(rdb
) - p
, &rd
); // mname
531 p
+= snprintf(p
, rdb
+ sizeof(rdb
) - p
, " ");
532 p
+= snprintd(p
, rdb
+ sizeof(rdb
) - p
, &rd
); // rname
533 p
+= snprintf(p
, rdb
+ sizeof(rdb
) - p
, " Ser %d Ref %d Ret %d Exp %d Min %d",
534 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]));
537 case kDNSServiceType_AAAA
:
538 snprintf(rdb
, sizeof(rdb
), "%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X",
539 rd
[0x0], rd
[0x1], rd
[0x2], rd
[0x3], rd
[0x4], rd
[0x5], rd
[0x6], rd
[0x7],
540 rd
[0x8], rd
[0x9], rd
[0xA], rd
[0xB], rd
[0xC], rd
[0xD], rd
[0xE], rd
[0xF]);
543 case kDNSServiceType_SRV
:
544 p
+= snprintf(p
, rdb
+ sizeof(rdb
) - p
, "%d %d %d ", // priority, weight, port
545 ntohs(*(unsigned short*)rd
), ntohs(*(unsigned short*)(rd
+2)), ntohs(*(unsigned short*)(rd
+4)));
547 p
+= snprintd(p
, rdb
+ sizeof(rdb
) - p
, &rd
); // target host
550 default : snprintf(rdb
, sizeof(rdb
), "%d bytes%s", rdlen
, rdlen
? ":" : ""); unknowntype
= 1; break;
554 printf("%s%6X%3d %-30s%4d%4d %s", op
, flags
, ifIndex
, fullname
, rrtype
, rrclass
, rdb
);
555 if (unknowntype
) while (rd
< end
) printf(" %02X", *rd
++);
558 if (errorCode
== kDNSServiceErr_NoSuchRecord
) printf("No Such Record");
559 else printf("Error code %d", errorCode
);
563 if (operation
== 'C')
564 if (flags
& kDNSServiceFlagsAdd
)
565 DNSServiceReconfirmRecord(flags
, ifIndex
, fullname
, rrtype
, rrclass
, rdlen
, rdata
);
567 if (!(flags
& kDNSServiceFlagsMoreComing
)) fflush(stdout
);
571 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
)
573 (void)sdref
; // Unused
574 (void)context
; // Unused
575 (void)flags
; // Unused
577 if (num_printed
++ == 0) printf("Timestamp if %-20s %-15s %-15s %-15s %-6s\n", "External Address", "Protocol", "Internal Port", "External Port", "TTL");
579 if (errorCode
&& errorCode
!= kDNSServiceErr_DoubleNAT
) printf("Error code %d\n", errorCode
);
582 const unsigned char *digits
= (const unsigned char *)&publicAddress
;
585 snprintf(addr
, sizeof(addr
), "%d.%d.%d.%d", digits
[0], digits
[1], digits
[2], digits
[3]);
586 printf("%-4d %-20s %-15d %-15d %-15d %-6d%s\n", ifIndex
, addr
, protocol
, ntohs(privatePort
), ntohs(publicPort
), ttl
, errorCode
== kDNSServiceErr_DoubleNAT
? " Double NAT" : "");
593 static void DNSSD_API
addrinfo_reply(DNSServiceRef sdref
, DNSServiceFlags flags
, uint32_t interfaceIndex
, DNSServiceErrorType errorCode
, const char *hostname
, const struct sockaddr
*address
, uint32_t ttl
, void *context
)
595 char *op
= (flags
& kDNSServiceFlagsAdd
) ? "Add" : "Rmv";
600 if (num_printed
++ == 0) printf("Timestamp A/R Flags if %-25s %-44s %s\n", "Hostname", "Address", "TTL");
603 if (address
&& address
->sa_family
== AF_INET
)
605 const unsigned char *b
= (const unsigned char *) &((struct sockaddr_in
*)address
)->sin_addr
;
606 snprintf(addr
, sizeof(addr
), "%d.%d.%d.%d", b
[0], b
[1], b
[2], b
[3]);
608 else if (address
&& address
->sa_family
== AF_INET6
)
610 char if_name
[IFNAMSIZ
]; // Older Linux distributions don't define IF_NAMESIZE
611 const struct sockaddr_in6
*s6
= (const struct sockaddr_in6
*)address
;
612 const unsigned char *b
= (const unsigned char * )&s6
->sin6_addr
;
613 if (!if_indextoname(s6
->sin6_scope_id
, if_name
))
614 snprintf(if_name
, sizeof(if_name
), "<%d>", s6
->sin6_scope_id
);
615 snprintf(addr
, sizeof(addr
), "%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X%%%s",
616 b
[0x0], b
[0x1], b
[0x2], b
[0x3], b
[0x4], b
[0x5], b
[0x6], b
[0x7],
617 b
[0x8], b
[0x9], b
[0xA], b
[0xB], b
[0xC], b
[0xD], b
[0xE], b
[0xF], if_name
);
620 printf("%s%6X%3d %-25s %-44s %d", op
, flags
, interfaceIndex
, hostname
, addr
, ttl
);
623 if (errorCode
== kDNSServiceErr_NoSuchRecord
) printf(" No Such Record");
624 else printf(" Error code %d", errorCode
);
628 if (!(flags
& kDNSServiceFlagsMoreComing
)) fflush(stdout
);
632 //*************************************************************************************************************
633 // The main test function
635 static void HandleEvents(void)
637 int dns_sd_fd
= client
? DNSServiceRefSockFD(client
) : -1;
638 int dns_sd_fd2
= client_pa
? DNSServiceRefSockFD(client_pa
) : -1;
639 int nfds
= dns_sd_fd
+ 1;
644 if (dns_sd_fd2
> dns_sd_fd
) nfds
= dns_sd_fd2
+ 1;
648 // 1. Set up the fd_set as usual here.
649 // This example client has no file descriptors of its own,
650 // but a real application would call FD_SET to add them to the set here
653 // 2. Add the fd for our client(s) to the fd_set
654 if (client
) FD_SET(dns_sd_fd
, &readfds
);
655 if (client_pa
) FD_SET(dns_sd_fd2
, &readfds
);
657 // 3. Set up the timeout.
661 result
= select(nfds
, &readfds
, (fd_set
*)NULL
, (fd_set
*)NULL
, &tv
);
664 DNSServiceErrorType err
= kDNSServiceErr_NoError
;
665 if (client
&& FD_ISSET(dns_sd_fd
, &readfds
)) err
= DNSServiceProcessResult(client
);
666 else if (client_pa
&& FD_ISSET(dns_sd_fd2
, &readfds
)) err
= DNSServiceProcessResult(client_pa
);
667 if (err
) { fprintf(stderr
, "DNSServiceProcessResult returned %d\n", err
); stopNow
= 1; }
669 else if (result
== 0)
673 printf("select() returned %d errno %d %s\n", result
, errno
, strerror(errno
));
674 if (errno
!= EINTR
) stopNow
= 1;
679 static int getfirstoption(int argc
, char **argv
, const char *optstr
, int *pOptInd
)
680 // Return the recognized option in optstr and the option index of the next arg.
684 for (i
=1; i
< argc
; i
++)
686 if (argv
[i
][0] == '-' && &argv
[i
][1] &&
687 NULL
!= strchr(optstr
, argv
[i
][1]))
697 int o
= getopt(argc
, (char *const *)argv
, optstr
);
703 static void DNSSD_API
MyRegisterRecordCallback(DNSServiceRef service
, DNSRecordRef rec
, const DNSServiceFlags flags
,
704 DNSServiceErrorType errorCode
, void *context
)
706 char *name
= (char *)context
;
708 (void)service
; // Unused
710 (void)flags
; // Unused
713 printf("Got a reply for record %s: ", name
);
717 case kDNSServiceErr_NoError
: printf("Name now registered and active\n"); break;
718 case kDNSServiceErr_NameConflict
: printf("Name in use, please choose another\n"); exit(-1);
719 default: printf("Error %d\n", errorCode
); break;
721 if (!(flags
& kDNSServiceFlagsMoreComing
)) fflush(stdout
);
722 // DNSServiceRemoveRecord(service, rec, 0); to test record removal
725 static unsigned long getip(const char *const name
)
727 unsigned long ip
= 0;
728 struct addrinfo hints
;
729 struct addrinfo
*addrs
= NULL
;
731 memset(&hints
, 0, sizeof(hints
));
732 hints
.ai_family
= AF_INET
;
734 if (getaddrinfo(name
, NULL
, &hints
, &addrs
) == 0)
736 ip
= ((struct sockaddr_in
*) addrs
->ai_addr
)->sin_addr
.s_addr
;
747 static DNSServiceErrorType
RegisterProxyAddressRecord(DNSServiceRef sdref
, const char *host
, const char *ip
)
749 // Call getip() after the call DNSServiceCreateConnection().
750 // On the Win32 platform, WinSock must be initialized for getip() to succeed.
751 // Any DNSService* call will initialize WinSock for us, so we make sure
752 // DNSServiceCreateConnection() is called before getip() is.
753 unsigned long addr
= getip(ip
);
754 return(DNSServiceRegisterRecord(sdref
, &record
, kDNSServiceFlagsUnique
, opinterface
, host
,
755 kDNSServiceType_A
, kDNSServiceClass_IN
, sizeof(addr
), &addr
, 240, MyRegisterRecordCallback
, (void*)host
));
756 // Note, should probably add support for creating proxy AAAA records too, one day
759 #define HexVal(X) ( ((X) >= '0' && (X) <= '9') ? ((X) - '0' ) : \
760 ((X) >= 'A' && (X) <= 'F') ? ((X) - 'A' + 10) : \
761 ((X) >= 'a' && (X) <= 'f') ? ((X) - 'a' + 10) : 0)
763 #define HexPair(P) ((HexVal((P)[0]) << 4) | HexVal((P)[1]))
765 static DNSServiceErrorType
RegisterService(DNSServiceRef
*sdref
,
766 const char *nam
, const char *typ
, const char *dom
, const char *host
, const char *port
, int argc
, char **argv
)
768 DNSServiceFlags flags
= 0;
769 uint16_t PortAsNumber
= atoi(port
);
770 Opaque16 registerPort
= { { PortAsNumber
>> 8, PortAsNumber
& 0xFF } };
771 unsigned char txt
[2048] = "";
772 unsigned char *ptr
= txt
;
775 if (nam
[0] == '.' && nam
[1] == 0) nam
= ""; // We allow '.' on the command line as a synonym for empty string
776 if (dom
[0] == '.' && dom
[1] == 0) dom
= ""; // We allow '.' on the command line as a synonym for empty string
778 printf("Registering Service %s.%s%s%s", nam
[0] ? nam
: "<<Default>>", typ
, dom
[0] ? "." : "", dom
);
779 if (host
&& *host
) printf(" host %s", host
);
780 printf(" port %s", port
);
784 for (i
= 0; i
< argc
; i
++)
786 const char *p
= argv
[i
];
788 while (*p
&& *ptr
< 255 && ptr
+ 1 + *ptr
< txt
+sizeof(txt
))
790 if (p
[0] != '\\' || p
[1] == 0) { ptr
[++*ptr
] = *p
; p
+=1; }
791 else if (p
[1] == 'x' && isxdigit(p
[2]) && isxdigit(p
[3])) { ptr
[++*ptr
] = HexPair(p
+2); p
+=4; }
792 else { ptr
[++*ptr
] = p
[1]; p
+=2; }
797 ShowTXTRecord(ptr
-txt
, txt
);
801 //flags |= kDNSServiceFlagsAllowRemoteQuery;
802 //flags |= kDNSServiceFlagsNoAutoRename;
804 return(DNSServiceRegister(sdref
, flags
, opinterface
, nam
, typ
, dom
, host
, registerPort
.NotAnInteger
, (uint16_t) (ptr
-txt
), txt
, reg_reply
, NULL
));
807 #define TypeBufferSize 80
808 static char *gettype(char *buffer
, char *typ
)
810 if (!typ
|| !*typ
|| (typ
[0] == '.' && typ
[1] == 0)) typ
= "_http._tcp";
811 if (!strchr(typ
, '.')) { snprintf(buffer
, TypeBufferSize
, "%s._tcp", typ
); typ
= buffer
; }
815 int main(int argc
, char **argv
)
817 DNSServiceErrorType err
;
818 char buffer
[TypeBufferSize
], *typ
, *dom
;
821 // Extract the program name from argv[0], which by convention contains the path to this executable.
822 // Note that this is just a voluntary convention, not enforced by the kernel --
823 // the process calling exec() can pass bogus data in argv[0] if it chooses to.
824 const char *a0
= strrchr(argv
[0], kFilePathSep
) + 1;
825 if (a0
== (const char *)1) a0
= argv
[0];
827 if (sizeof(argv
) == 8) printf("Running in 64-bit mode\n");
829 // Test code for TXTRecord functions
830 //TXTRecordRef txtRecord;
831 //TXTRecordCreate(&txtRecord, 0, NULL);
832 //TXTRecordSetValue(&txtRecord, "aaa", 1, "b");
833 //printf("%d\n", TXTRecordContainsKey(TXTRecordGetLength(&txtRecord), TXTRecordGetBytesPtr(&txtRecord), "Aaa"));
835 if (argc
> 1 && !strcmp(argv
[1], "-lo"))
839 opinterface
= kDNSServiceInterfaceIndexLocalOnly
;
840 printf("Using LocalOnly\n");
843 if (argc
> 2 && !strcmp(argv
[1], "-i"))
845 opinterface
= if_nametoindex(argv
[2]);
846 if (!opinterface
) opinterface
= atoi(argv
[2]);
847 if (!opinterface
) { fprintf(stderr
, "Unknown interface %s\n", argv
[2]); goto Fail
; }
852 if (argc
< 2) goto Fail
; // Minimum command line is the command name and one argument
853 operation
= getfirstoption(argc
, argv
, "EFBLRPQCAUNTMISV"
861 if (operation
== -1) goto Fail
;
863 if (opinterface
) printf("Using interface %d\n", opinterface
);
867 case 'E': printf("Looking for recommended registration domains:\n");
868 err
= DNSServiceEnumerateDomains(&client
, kDNSServiceFlagsRegistrationDomains
, opinterface
, enum_reply
, NULL
);
871 case 'F': printf("Looking for recommended browsing domains:\n");
872 err
= DNSServiceEnumerateDomains(&client
, kDNSServiceFlagsBrowseDomains
, opinterface
, enum_reply
, NULL
);
873 //enum_reply(client, kDNSServiceFlagsAdd, 0, 0, "nicta.com.au.", NULL);
874 //enum_reply(client, kDNSServiceFlagsAdd, 0, 0, "bonjour.nicta.com.au.", NULL);
875 //enum_reply(client, kDNSServiceFlagsAdd, 0, 0, "ibm.com.", NULL);
876 //enum_reply(client, kDNSServiceFlagsAdd, 0, 0, "dns-sd.ibm.com.", NULL);
879 case 'B': typ
= (argc
< opi
+1) ? "" : argv
[opi
+0];
880 dom
= (argc
< opi
+2) ? "" : argv
[opi
+1]; // Missing domain argument is the same as empty string i.e. use system default(s)
881 typ
= gettype(buffer
, typ
);
882 if (dom
[0] == '.' && dom
[1] == 0) dom
[0] = 0; // We allow '.' on the command line as a synonym for empty string
883 printf("Browsing for %s%s%s\n", typ
, dom
[0] ? "." : "", dom
);
884 err
= DNSServiceBrowse(&client
, 0, opinterface
, typ
, dom
, browse_reply
, NULL
);
887 case 'L': if (argc
< opi
+2) goto Fail
;
888 typ
= (argc
< opi
+2) ? "" : argv
[opi
+1];
889 dom
= (argc
< opi
+3) ? "local" : argv
[opi
+2];
890 typ
= gettype(buffer
, typ
);
891 if (dom
[0] == '.' && dom
[1] == 0) dom
= "local"; // We allow '.' on the command line as a synonym for "local"
892 printf("Lookup %s.%s.%s\n", argv
[opi
+0], typ
, dom
);
893 err
= DNSServiceResolve(&client
, 0, opinterface
, argv
[opi
+0], typ
, dom
, (DNSServiceResolveReply
)resolve_reply
, NULL
);
896 case 'R': if (argc
< opi
+4) goto Fail
;
897 typ
= (argc
< opi
+2) ? "" : argv
[opi
+1];
898 dom
= (argc
< opi
+3) ? "" : argv
[opi
+2];
899 typ
= gettype(buffer
, typ
);
900 if (dom
[0] == '.' && dom
[1] == 0) dom
[0] = 0; // We allow '.' on the command line as a synonym for empty string
901 err
= RegisterService(&client
, argv
[opi
+0], typ
, dom
, NULL
, argv
[opi
+3], argc
-(opi
+4), argv
+(opi
+4));
904 case 'P': if (argc
< opi
+6) goto Fail
;
905 err
= DNSServiceCreateConnection(&client_pa
);
906 if (err
) { fprintf(stderr
, "DNSServiceCreateConnection returned %d\n", err
); return(err
); }
907 err
= RegisterProxyAddressRecord(client_pa
, argv
[opi
+4], argv
[opi
+5]);
908 //err = RegisterProxyAddressRecord(client_pa, "two", argv[opi+5]);
910 err
= RegisterService(&client
, argv
[opi
+0], argv
[opi
+1], argv
[opi
+2], argv
[opi
+4], argv
[opi
+3], argc
-(opi
+6), argv
+(opi
+6));
911 //DNSServiceRemoveRecord(client_pa, record, 0);
912 //DNSServiceRemoveRecord(client_pa, record, 0);
917 uint16_t rrtype
, rrclass
;
918 DNSServiceFlags flags
= kDNSServiceFlagsReturnIntermediates
;
919 if (argc
< opi
+1) goto Fail
;
920 rrtype
= (argc
<= opi
+1) ? kDNSServiceType_A
: GetRRType(argv
[opi
+1]);
921 rrclass
= (argc
<= opi
+2) ? kDNSServiceClass_IN
: atoi(argv
[opi
+2]);
922 if (rrtype
== kDNSServiceType_TXT
|| rrtype
== kDNSServiceType_PTR
) flags
|= kDNSServiceFlagsLongLivedQuery
;
923 err
= DNSServiceQueryRecord(&client
, flags
, opinterface
, argv
[opi
+0], rrtype
, rrclass
, qr_reply
, NULL
);
930 Opaque16 registerPort
= { { 0x12, 0x34 } };
931 static const char TXT
[] = "\xC" "First String" "\xD" "Second String" "\xC" "Third String";
932 printf("Registering Service Test._testupdate._tcp.local.\n");
933 err
= DNSServiceRegister(&client
, 0, opinterface
, "Test", "_testupdate._tcp.", "", NULL
, registerPort
.NotAnInteger
, sizeof(TXT
)-1, TXT
, reg_reply
, NULL
);
938 Opaque16 registerPort
= { { 0x23, 0x45 } };
941 for (i
=0; i
<sizeof(TXT
); i
++)
942 if ((i
& 0x1F) == 0) TXT
[i
] = 0x1F; else TXT
[i
] = 'A' + (i
>> 5);
943 printf("Registering Service Test._testlargetxt._tcp.local.\n");
944 err
= DNSServiceRegister(&client
, 0, opinterface
, "Test", "_testlargetxt._tcp.", "", NULL
, registerPort
.NotAnInteger
, sizeof(TXT
), TXT
, reg_reply
, NULL
);
949 pid_t pid
= getpid();
950 Opaque16 registerPort
= { { pid
>> 8, pid
& 0xFF } };
951 static const char TXT1
[] = "\xC" "First String" "\xD" "Second String" "\xC" "Third String";
952 static const char TXT2
[] = "\xD" "Fourth String" "\xC" "Fifth String" "\xC" "Sixth String";
953 printf("Registering Service Test._testdualtxt._tcp.local.\n");
954 err
= DNSServiceRegister(&client
, 0, opinterface
, "Test", "_testdualtxt._tcp.", "", NULL
, registerPort
.NotAnInteger
, sizeof(TXT1
)-1, TXT1
, reg_reply
, NULL
);
955 if (!err
) err
= DNSServiceAddRecord(client
, &record
, 0, kDNSServiceType_TXT
, sizeof(TXT2
)-1, TXT2
, 0);
960 pid_t pid
= getpid();
961 Opaque16 registerPort
= { { pid
>> 8, pid
& 0xFF } };
962 static const char TXT
[] = "\x09" "Test Data";
963 printf("Registering Service Test._testtxt._tcp.local.\n");
964 err
= DNSServiceRegister(&client
, 0, opinterface
, "Test", "_testtxt._tcp.", "", NULL
, registerPort
.NotAnInteger
, 0, NULL
, reg_reply
, NULL
);
965 if (!err
) err
= DNSServiceUpdateRecord(client
, NULL
, 0, sizeof(TXT
)-1, TXT
, 0);
971 if (argc
== opi
) // If no arguments, just fetch IP address
972 err
= DNSServiceNATPortMappingCreate(&client
, 0, 0, 0, 0, 0, 0, port_mapping_create_reply
, NULL
);
973 else if (argc
>= opi
+2 && atoi(argv
[opi
+0]) == 0)
975 DNSServiceProtocol prot
= GetProtocol(argv
[opi
+0]); // Must specify TCP or UDP
976 uint16_t IntPortAsNumber
= atoi(argv
[opi
+1]); // Must specify internal port
977 uint16_t ExtPortAsNumber
= (argc
< opi
+3) ? 0 : atoi(argv
[opi
+2]); // Optional desired external port
978 uint32_t ttl
= (argc
< opi
+4) ? 0 : atoi(argv
[opi
+3]); // Optional desired lease lifetime
979 Opaque16 intp
= { { IntPortAsNumber
>> 8, IntPortAsNumber
& 0xFF } };
980 Opaque16 extp
= { { ExtPortAsNumber
>> 8, ExtPortAsNumber
& 0xFF } };
981 err
= DNSServiceNATPortMappingCreate(&client
, 0, 0, prot
, intp
.NotAnInteger
, extp
.NotAnInteger
, ttl
, port_mapping_create_reply
, NULL
);
990 if (argc
!= opi
+2) goto Fail
;
991 else err
= DNSServiceGetAddrInfo(&client
, kDNSServiceFlagsReturnIntermediates
, opinterface
, GetProtocol(argv
[opi
+0]), argv
[opi
+1], addrinfo_reply
, NULL
);
997 Opaque16 registerPort
= { { 0x23, 0x45 } };
998 err
= DNSServiceCreateConnection(&client
);
999 if (err
) { fprintf(stderr
, "DNSServiceCreateConnection failed %ld\n", (long int)err
); return (-1); }
1002 err
= DNSServiceBrowse(&sc1
, kDNSServiceFlagsShareConnection
, opinterface
, "_http._tcp", "", browse_reply
, NULL
);
1003 if (err
) { fprintf(stderr
, "DNSServiceBrowse _http._tcp failed %ld\n", (long int)err
); return (-1); }
1006 err
= DNSServiceBrowse(&sc2
, kDNSServiceFlagsShareConnection
, opinterface
, "_ftp._tcp", "", browse_reply
, NULL
);
1007 if (err
) { fprintf(stderr
, "DNSServiceBrowse _ftp._tcp failed %ld\n", (long int)err
); return (-1); }
1010 err
= DNSServiceRegister(&sc3
, kDNSServiceFlagsShareConnection
, opinterface
, "kDNSServiceFlagsShareConnection",
1011 "_http._tcp", "local", NULL
, registerPort
.NotAnInteger
, 0, NULL
, reg_reply
, NULL
);
1012 if (err
) { fprintf(stderr
, "SharedConnection DNSServiceRegister failed %ld\n", (long int)err
); return (-1); }
1019 uint32_t size
= sizeof(v
);
1020 err
= DNSServiceGetProperty(kDNSServiceProperty_DaemonVersion
, &v
, &size
);
1021 if (err
) fprintf(stderr
, "DNSServiceGetProperty failed %ld\n", (long int)err
);
1022 else printf("Currently running daemon (system service) is version %d.%d\n", v
/ 10000, v
/ 100 % 100);
1029 if (!client
|| err
!= kDNSServiceErr_NoError
) { fprintf(stderr
, "DNSService call failed %ld\n", (long int)err
); return (-1); }
1032 // Be sure to deallocate the DNSServiceRef when you're finished
1033 if (client
) DNSServiceRefDeallocate(client
);
1034 if (client_pa
) DNSServiceRefDeallocate(client_pa
);
1038 fprintf(stderr
, "%s -E (Enumerate recommended registration domains)\n", a0
);
1039 fprintf(stderr
, "%s -F (Enumerate recommended browsing domains)\n", a0
);
1040 fprintf(stderr
, "%s -B <Type> <Domain> (Browse for services instances)\n", a0
);
1041 fprintf(stderr
, "%s -L <Name> <Type> <Domain> (Look up a service instance)\n", a0
);
1042 fprintf(stderr
, "%s -R <Name> <Type> <Domain> <Port> [<TXT>...] (Register a service)\n", a0
);
1043 fprintf(stderr
, "%s -P <Name> <Type> <Domain> <Port> <Host> <IP> [<TXT>...] (Proxy)\n", a0
);
1044 fprintf(stderr
, "%s -Q <FQDN> <rrtype> <rrclass> (Generic query for any record type)\n", a0
);
1045 fprintf(stderr
, "%s -C <FQDN> <rrtype> <rrclass> (Query; reconfirming each result)\n", a0
);
1047 fprintf(stderr
, "%s -X udp/tcp/udptcp <IntPort> <ExtPort> <TTL> (NAT Port Mapping)\n", a0
);
1049 #if HAS_ADDRINFO_API
1050 fprintf(stderr
, "%s -G v4/v6/v4v6 <Hostname> (Get address information for hostname)\n", a0
);
1052 fprintf(stderr
, "%s -A (Test Adding/Updating/Deleting a record)\n", a0
);
1053 fprintf(stderr
, "%s -U (Test updating a TXT record)\n", a0
);
1054 fprintf(stderr
, "%s -N (Test adding a large NULL record)\n", a0
);
1055 fprintf(stderr
, "%s -T (Test creating a large TXT record)\n", a0
);
1056 fprintf(stderr
, "%s -M (Test creating a registration with multiple TXT records)\n", a0
);
1057 fprintf(stderr
, "%s -I (Test registering and then immediately updating TXT record)\n", a0
);
1058 fprintf(stderr
, "%s -S (Test multiple operations on a shared socket)\n", a0
);
1059 fprintf(stderr
, "%s -V (Get version of currently running daemon / system service)\n", a0
);
1063 // Note: The C preprocessor stringify operator ('#') makes a string from its argument, without macro expansion
1064 // e.g. If "version" is #define'd to be "4", then STRINGIFY_AWE(version) will return the string "version", not "4"
1065 // To expand "version" to its value before making the string, use STRINGIFY(version) instead
1066 #define STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) #s
1067 #define STRINGIFY(s) STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s)
1069 // NOT static -- otherwise the compiler may optimize it out
1070 // The "@(#) " pattern is a special prefix the "what" command looks for
1071 const char VersionString_SCCS
[] = "@(#) dns-sd " STRINGIFY(mDNSResponderVersion
) " (" __DATE__
" " __TIME__
")";
1073 // If the process crashes, then this string will be magically included in the automatically-generated crash log
1074 const char *__crashreporter_info__
= VersionString_SCCS
+ 5;
1075 asm(".desc ___crashreporter_info__, 0x10");