2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
26 Revision 1.7 2003/08/20 07:06:34 bradley
27 Update to APSL 2.0. Updated change history to match other mDNSResponder files.
29 Revision 1.6 2003/08/20 06:50:55 bradley
30 Updated to latest internal version of the Rendezvous for Windows code: Re-did everything to support
31 the latest DNSServices APIs (proxies, record updates, etc.); Added support for testing the platform
32 neutral DNSServices-based emulation layer for the Mac OS X DNSServiceDiscovery API.
36 #if( defined( _MSC_VER ) )
37 #pragma warning( disable:4068 ) // Disable "unknown pragma" warning for "pragma unused".
38 #pragma warning( disable:4127 ) // Disable "conditional expression is constant" warning for debug macros.
39 #pragma warning( disable:4311 ) // Disable "type cast : pointer truncation from void *const to int".
41 // No stdint.h with Visual C++ so emulate it here.
43 typedef signed char int8_t; // C99 stdint.h not supported in VC++/VS.NET yet.
44 typedef unsigned char uint8_t; // C99 stdint.h not supported in VC++/VS.NET yet.
45 typedef signed short int16_t; // C99 stdint.h not supported in VC++/VS.NET yet.
46 typedef unsigned short uint16_t; // C99 stdint.h not supported in VC++/VS.NET yet.
47 typedef signed long int32_t; // C99 stdint.h not supported in VC++/VS.NET yet.
48 typedef unsigned long uint32_t; // C99 stdint.h not supported in VC++/VS.NET yet.
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
64 #include <CoreServices/CoreServices.h>
66 #define WIN32_LEAN_AND_MEAN
72 #include "DNSServices.h"
73 #include "DNSServiceDiscovery.h"
75 //===========================================================================================================================
77 //===========================================================================================================================
80 #define require_action_string( X, LABEL, ACTION, STR ) \
85 fprintf( stderr, "%s\n", ( STR ) ); \
91 #define require_string( X, LABEL, STR ) \
96 fprintf( stderr, "%s\n", ( STR ) ); \
102 #define require_noerr_string( ERR, LABEL, STR ) \
107 fprintf( stderr, "%s (%ld)\n", ( STR ), ( ERR ) ); \
113 //===========================================================================================================================
115 //===========================================================================================================================
117 int main( int argc
, char* argv
[] );
118 static void Usage( void );
119 static int ProcessArgs( int argc
, char* argv
[] );
120 static DNSStatus
ProcessPreset( int inPreset
);
123 static void SigIntHandler( int inSignalNumber
);
126 #if( defined( WINVER ) )
127 static BOOL WINAPI
ConsoleControlHandler( DWORD inControlEvent
);
130 static void BrowserCallBack( void *inContext
, DNSBrowserRef inRef
, DNSStatus inStatusCode
, const DNSBrowserEvent
*inEvent
);
131 static void ResolverCallBack( void *inContext
, DNSResolverRef inRef
, DNSStatus inStatusCode
, const DNSResolverEvent
*inEvent
);
134 RegistrationCallBack(
136 DNSRegistrationRef inRef
,
137 DNSStatus inStatusCode
,
138 const DNSRegistrationEvent
* inEvent
);
141 HostRegistrationCallBack(
143 DNSHostRegistrationRef inRef
,
144 DNSStatus inStatusCode
,
148 EmulatedBrowserCallBack(
149 DNSServiceBrowserReplyResultType inResult
,
152 const char * inDomain
,
153 DNSServiceDiscoveryReplyFlags inFlags
,
157 EmulatedDomainEnumerationCallBack(
158 DNSServiceDomainEnumerationReplyResultType inResult
,
159 const char * inDomain
,
160 DNSServiceDiscoveryReplyFlags inFlags
,
164 EmulatedResolverCallBack(
165 struct sockaddr
* inInterfaceAddr
,
166 struct sockaddr
* inAddr
,
167 const char * inTextRecord
,
168 DNSServiceDiscoveryReplyFlags inFlags
,
171 static void EmulatedRegistrationCallBack( DNSServiceRegistrationReplyErrorType inResult
, void *inContext
);
173 static char * IPv4ToString( DNSOpaque32 inIP
, char *outString
);
175 //===========================================================================================================================
177 //===========================================================================================================================
179 #if( defined( WINVER ) )
180 static volatile int gQuit
= 0;
183 static int gPrintTXTRecords
= 1;
187 typedef struct PresetData PresetData
;
195 #pragma mark == Presets ==
198 static const PresetData gPresets
[] =
200 /* 01 */ { 2, { "rendezvous", "-bbd" } },
201 /* 02 */ { 4, { "rendezvous", "-bs", "_airport._tcp", "local." } },
202 /* 03 */ { 4, { "rendezvous", "-bs", "_xserveraid._tcp", "local." } },
203 /* 04 */ { 3, { "rendezvous", "-rdb", "apple.com" } },
204 /* 05 */ { 7, { "rendezvous", "-rs", "My Fake AirPort", "_airport._tcp", "local.", "1234", "My Fake Info" } },
205 /* 06 */ { 7, { "rendezvous", "-rs", "My Fake Xserve RAID", "_xserveraid._tcp", "local.", "1234", "My Fake Info" } },
206 /* 07 */ { 7, { "rendezvous", "-rs", "My Fake Web Server", "_http._tcp", "local.", "8080", "index.html" } },
207 /* 08 */ { 9, { "rendezvous", "-rps", "www.apple.com", "17.254.0.91", "Apple Web Server", "_http._tcp", "local.", "80", "index.html" } },
210 const int gPresetsCount
= sizeof( gPresets
) / sizeof( gPresets
[ 0 ] );
216 //===========================================================================================================================
218 //===========================================================================================================================
220 int main( int argc
, char* argv
[] )
224 // Set up DNS Services and install a Console Control Handler to handle things like control-c signals.
226 err
= DNSServicesInitialize( kDNSFlagAdvertise
, 0 );
227 require_noerr_string( err
, exit
, "could not initialize Rendezvous" );
230 signal( SIGINT
, SigIntHandler
);
233 #if( defined( WINVER ) )
234 SetConsoleCtrlHandler( ConsoleControlHandler
, TRUE
);
237 ProcessArgs( argc
, argv
);
240 DNSServicesFinalize();
244 //===========================================================================================================================
246 //===========================================================================================================================
248 static void Usage( void )
250 fprintf( stderr
, "\n" );
251 fprintf( stderr
, "rendezvous - Rendezvous Tool 1.0d1\n" );
252 fprintf( stderr
, "\n" );
253 fprintf( stderr
, " -bbd 'b'rowse for 'b'rowsing 'd'omains\n" );
254 fprintf( stderr
, " -brd 'b'rowse for 'r'egistration 'd'omains\n" );
255 fprintf( stderr
, " -bs <type> <domain> 'b'rowse for 's'ervices\n" );
256 fprintf( stderr
, " -lsi <name> <type> <domain> 'l'ookup 's'ervice 'i'nstance\n" );
257 fprintf( stderr
, " -rdb[d] <domain> 'r'egister 'd'omain for 'b'rowsing ['d'efault]\n" );
258 fprintf( stderr
, " -rdr[d] <domain> 'r'egister 'd'omain for 'r'egistration ['d'efault]\n" );
259 fprintf( stderr
, " -rs <name> <type> <domain> <port> <txt> 'r'egister 's'ervice\n" );
260 fprintf( stderr
, " -rps <host> <ip> <name> <type> <domain> <port> <txt> 'r'egister 'p'roxy 's'ervice\n" );
261 fprintf( stderr
, " -rnss <name> <type> <domain> 'r'egister 'n'o 's'uch 's'ervice\n" );
263 fprintf( stderr
, " -ebs <type> <domain> 'e'mulated 'b'rowse for 's'ervices\n" );
264 fprintf( stderr
, " -ebd <registration/browse> 'e'mulated 'b'rowse for 'd'omains\n" );
265 fprintf( stderr
, " -elsi <name> <type> <domain> 'e'mulated 'l'ookup 's'ervice 'i'nstance\n" );
266 fprintf( stderr
, " -ers <name> <type> <domain> <port> <txt> 'e'mulated 'r'egister 's'ervice\n" );
268 fprintf( stderr
, " -h[elp] 'h'elp\n" );
269 fprintf( stderr
, "\n" );
271 fprintf( stderr
, " -1 Preset 1 (browse for browsing domains) rendezvous -bbd\n" );
272 fprintf( stderr
, " -2 Preset 2 (browse for AirPort) rendezvous -bs \"_airport._tcp\" \"local.\"\n" );
273 fprintf( stderr
, " -3 Preset 3 (browse for Xserve RAID) rendezvous -bs \"_xserveraid._tcp\" \"local.\"\n" );
274 fprintf( stderr
, " -4 Preset 4 (register apple.com domain) rendezvous -rdb \"apple.com\"\n" );
275 fprintf( stderr
, " -5 Preset 5 (register fake AirPort) rendezvous -rs \"My Fake AirPort\" \"_airport._tcp\" \"local.\" 1234 \"My Fake Info\"\n" );
276 fprintf( stderr
, " -6 Preset 6 (register fake Xserve RAID) rendezvous -rs \"My Fake Xserve RAID\" \"_xserveraid._tcp\" \"local.\" 1234 \"My Fake Info\"\n" );
277 fprintf( stderr
, " -7 Preset 7 (register fake web server) rendezvous -rs \"My Fake Web Server\" \"_http._tcp\" \"local.\" 8080 \"index.html\"\n" );
278 fprintf( stderr
, "\n" );
281 //===========================================================================================================================
283 //===========================================================================================================================
285 static int ProcessArgs( int argc
, char* argv
[] )
295 DNSBrowserRef browser
;
296 DNSResolverFlags resolverFlags
;
297 DNSDomainRegistrationType domainType
;
302 DNSNetworkAddress addr
;
303 dns_service_discovery_ref emulatedRef
;
305 // Parse the command line arguments (ignore first argument since it's just the program name).
307 require_action_string( argc
>= 2, exit
, err
= kDNSBadParamErr
, "no arguments specified" );
309 for( i
= 1; i
< argc
; ++i
)
311 if( strcmp( argv
[ i
], "-bbd" ) == 0 )
313 // 'b'rowse for 'b'rowsing 'd'omains
315 fprintf( stdout
, "browsing for browsing domains\n" );
317 err
= DNSBrowserCreate( 0, BrowserCallBack
, NULL
, &browser
);
318 require_noerr_string( err
, exit
, "create browser failed" );
320 err
= DNSBrowserStartDomainSearch( browser
, 0 );
321 require_noerr_string( err
, exit
, "start domain search failed" );
323 else if( strcmp( argv
[ i
], "-brd" ) == 0 )
325 // 'b'rowse for 'r'egistration 'd'omains
327 fprintf( stdout
, "browsing for registration domains\n" );
329 err
= DNSBrowserCreate( 0, BrowserCallBack
, NULL
, &browser
);
330 require_noerr_string( err
, exit
, "create browser failed" );
332 err
= DNSBrowserStartDomainSearch( browser
, kDNSBrowserFlagRegistrationDomainsOnly
);
333 require_noerr_string( err
, exit
, "start domain search failed" );
335 else if( strcmp( argv
[ i
], "-bs" ) == 0 )
337 // 'b'rowse for 's'ervices <type> <domain>
339 require_action_string( argc
> ( i
+ 2 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
343 if( ( domain
[ 0 ] == '\0' ) || ( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) ) )
347 fprintf( stdout
, "browsing for \"%s.%s\"\n", type
, domain
);
349 err
= DNSBrowserCreate( 0, BrowserCallBack
, NULL
, &browser
);
350 require_noerr_string( err
, exit
, "create browser failed" );
352 err
= DNSBrowserStartServiceSearch( browser
, kDNSBrowserFlagAutoResolve
, type
, domain
);
353 require_noerr_string( err
, exit
, "start service search failed" );
355 else if( strcmp( argv
[ i
], "-lsi" ) == 0 )
357 // 'l'ookup 's'ervice 'i'nstance <name> <type> <domain>
359 require_action_string( argc
> ( i
+ 3 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
364 if( ( domain
[ 0 ] == '\0' ) || ( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) ) )
368 fprintf( stdout
, "resolving \"%s.%s.%s\"\n", name
, type
, domain
);
370 resolverFlags
= kDNSResolverFlagOnlyIfUnique
|
371 kDNSResolverFlagAutoReleaseByName
;
372 err
= DNSResolverCreate( resolverFlags
, name
, type
, domain
, ResolverCallBack
, 0, NULL
, NULL
);
373 require_noerr_string( err
, exit
, "create resolver failed" );
375 else if( ( strcmp( argv
[ i
], "-rdb" ) == 0 ) || ( strcmp( argv
[ i
], "-rdbd" ) == 0 ) )
377 // 'r'egister 'd'omain for 'b'rowsing ['d'efault] <domain>
379 require_action_string( argc
> ( i
+ 1 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
380 if( strcmp( argv
[ i
], "-rdb" ) == 0 )
382 domainType
= kDNSDomainRegistrationTypeBrowse
;
387 domainType
= kDNSDomainRegistrationTypeBrowseDefault
;
392 if( ( domain
[ 0 ] == '\0' ) || ( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) ) )
396 fprintf( stdout
, "registering \"%s\" as %sbrowse domain\n", domain
, label
);
398 err
= DNSDomainRegistrationCreate( 0, domain
, domainType
, NULL
);
399 require_noerr_string( err
, exit
, "create domain registration failed" );
401 else if( ( strcmp( argv
[ i
], "-rdr" ) == 0 ) || ( strcmp( argv
[ i
], "-rdrd" ) == 0 ) )
403 // 'r'egister 'd'omain for 'r'egistration ['d'efault] <domain>
405 require_action_string( argc
> ( i
+ 1 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
406 if( strcmp( argv
[ i
], "-rdr" ) == 0 )
408 domainType
= kDNSDomainRegistrationTypeRegistration
;
413 domainType
= kDNSDomainRegistrationTypeRegistrationDefault
;
418 if( ( domain
[ 0 ] == '\0' ) || ( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) ) )
422 fprintf( stdout
, "registering \"%s\" as %sregistration domain\n", domain
, label
);
424 err
= DNSDomainRegistrationCreate( 0, domain
, domainType
, NULL
);
425 require_noerr_string( err
, exit
, "create domain registration failed" );
427 else if( strcmp( argv
[ i
], "-rs" ) == 0 )
429 // 'r'egister 's'ervice <name> <type> <domain> <port> <txt>
431 require_action_string( argc
> ( i
+ 5 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
435 domain
= argv
[ i
++ ];
436 port
= atoi( argv
[ i
++ ] );
438 textSize
= strlen( text
);
439 if( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) )
443 fprintf( stdout
, "registering service \"%s.%s.%s\" port %d text \"%s\"\n", name
, type
, domain
, port
, text
);
445 err
= DNSRegistrationCreate( 0, name
, type
, domain
, (DNSPort
) port
, text
, (DNSCount
) textSize
, NULL
, NULL
,
446 RegistrationCallBack
, NULL
, NULL
);
447 require_noerr_string( err
, exit
, "create registration failed" );
449 else if( strcmp( argv
[ i
], "-rps" ) == 0 )
451 DNSHostRegistrationFlags hostFlags
;
453 // 'r'egister 'p'roxy 's'ervice <name> <type> <domain> <port> <txt>
455 require_action_string( argc
> ( i
+ 7 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
461 domain
= argv
[ i
++ ];
462 port
= atoi( argv
[ i
++ ] );
464 textSize
= strlen( text
);
465 if( ( domain
[ 0 ] == '\0' ) || ( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) ) )
470 sscanf( ip
, "%u.%u.%u.%u", &b
[ 0 ], &b
[ 1 ], &b
[ 2 ], &b
[ 3 ] );
471 addr
.addressType
= kDNSNetworkAddressTypeIPv4
;
472 addr
.u
.ipv4
.addr
.v32
= (DNSUInt32
)( ( b
[ 0 ] << 24 ) | ( b
[ 1 ] << 16 ) | ( b
[ 2 ] << 8 ) | ( b
[ 3 ] << 0 ) );
474 fprintf( stdout
, "registering proxy service \"%s.%s.%s\" port %d text \"%s\"\n", name
, type
, domain
, port
, text
);
476 hostFlags
= kDNSHostRegistrationFlagOnlyIfNotFound
| kDNSHostRegistrationFlagAutoRenameOnConflict
;
477 err
= DNSHostRegistrationCreate( hostFlags
, host
, domain
, &addr
, NULL
,
478 HostRegistrationCallBack
, NULL
, NULL
);
479 require_noerr_string( err
, exit
, "create host registration failed" );
481 err
= DNSRegistrationCreate( 0, name
, type
, domain
, (DNSPort
) port
, text
, (DNSCount
) textSize
, host
, NULL
,
482 RegistrationCallBack
, NULL
, NULL
);
483 require_noerr_string( err
, exit
, "create registration failed" );
485 else if( strcmp( argv
[ i
], "-rnss" ) == 0 )
487 // 'r'egister 'n'o 's'uch 's'ervice <name> <type> <domain>
489 require_action_string( argc
> ( i
+ 3 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
493 domain
= argv
[ i
++ ];
494 if( ( domain
[ 0 ] == '\0' ) || ( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) ) )
498 fprintf( stdout
, "registering no-such-service \"%s.%s.%s\"\n", name
, type
, domain
);
500 err
= DNSNoSuchServiceRegistrationCreate( 0, name
, type
, domain
, NULL
, RegistrationCallBack
, NULL
, NULL
);
501 require_noerr_string( err
, exit
, "create no-such-service registration failed" );
503 else if( strcmp( argv
[ i
], "-ebs" ) == 0 )
505 // 'e'mulated 'b'rowse for 's'ervices <type> <domain>
507 require_action_string( argc
> ( i
+ 2 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
511 if( ( domain
[ 0 ] == '\0' ) || ( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) ) )
515 fprintf( stdout
, "emulated browsing for \"%s.%s\"\n", type
, domain
);
517 emulatedRef
= DNSServiceBrowserCreate( type
, domain
, EmulatedBrowserCallBack
, NULL
);
518 require_action_string( emulatedRef
, exit
, err
= kDNSUnknownErr
, "create emulated browser failed" );
520 else if( strcmp( argv
[ i
], "-ebd" ) == 0 )
522 int registrationOnly
;
524 // 'e'mulated 'b'rowse for 'd'omains <registration/browse>
526 require_action_string( argc
> ( i
+ 1 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
529 if( strcmp( type
, "registration" ) == 0 )
531 registrationOnly
= 1;
533 else if( strcmp( type
, "browse" ) == 0 )
535 registrationOnly
= 0;
539 require_action_string( 0, exit
, err
= kDNSBadParamErr
, "invalid browse type" );
541 fprintf( stdout
, "emulated browsing for %s domains\n", type
);
543 emulatedRef
= DNSServiceDomainEnumerationCreate( registrationOnly
, EmulatedDomainEnumerationCallBack
, NULL
);
544 require_action_string( emulatedRef
, exit
, err
= kDNSUnknownErr
, "create emulated domain browser failed" );
546 else if( strcmp( argv
[ i
], "-elsi" ) == 0 )
548 // 'e'mulated 'l'ookup 's'ervice 'i'nstance <name> <type> <domain>
550 require_action_string( argc
> ( i
+ 3 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
555 if( ( domain
[ 0 ] == '\0' ) || ( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) ) )
559 fprintf( stdout
, "emulated resolving \"%s.%s.%s\"\n", name
, type
, domain
);
561 emulatedRef
= DNSServiceResolverResolve( name
, type
, domain
, EmulatedResolverCallBack
, NULL
);
562 require_action_string( emulatedRef
, exit
, err
= kDNSUnknownErr
, "create emulated resolver failed" );
564 else if( strcmp( argv
[ i
], "-ers" ) == 0 )
566 // 'e'mulated 'r'egister 's'ervice <name> <type> <domain> <port> <txt>
568 require_action_string( argc
> ( i
+ 5 ), exit
, err
= kDNSBadParamErr
, "missing arguments" );
572 domain
= argv
[ i
++ ];
573 port
= atoi( argv
[ i
++ ] );
575 textSize
= strlen( text
);
576 if( ( domain
[ 0 ] == '.' ) && ( domain
[ 1 ] == '\0' ) )
580 fprintf( stdout
, "registering service \"%s.%s.%s\" port %d text \"%s\"\n", name
, type
, domain
, port
, text
);
582 emulatedRef
= DNSServiceRegistrationCreate( name
, type
, domain
, (uint16_t) port
, text
,
583 EmulatedRegistrationCallBack
, NULL
);
584 require_action_string( emulatedRef
, exit
, err
= kDNSUnknownErr
, "create emulated registration failed" );
586 else if( ( argv
[ i
][ 0 ] == '-' ) && isdigit( argv
[ i
][ 1 ] ) )
590 ProcessPreset( atoi( &argv
[ i
][ 1 ] ) );
594 else if( strcmp( argv
[ i
], "-q" ) == 0 )
596 // Quiet (no text records)
598 gPrintTXTRecords
= 0;
600 else if( ( strcmp( argv
[ i
], "-help" ) == 0 ) || ( strcmp( argv
[ i
], "-h" ) == 0 ) )
610 // Unknown parameter.
612 require_action_string( 0, exit
, err
= kDNSBadParamErr
, "unknown parameter" );
617 // Run until control-C'd.
623 #if( defined( WINVER ) )
640 //===========================================================================================================================
642 //===========================================================================================================================
644 static DNSStatus
ProcessPreset( int inPreset
)
648 require_action_string( ( inPreset
> 0 ) && ( inPreset
<= gPresetsCount
), exit
, err
= kDNSBadParamErr
, "invalid preset" );
650 err
= ProcessArgs( gPresets
[ inPreset
- 1 ].argc
, (char **) gPresets
[ inPreset
- 1 ].argv
);
657 //===========================================================================================================================
659 //===========================================================================================================================
661 static void SigIntHandler( int inSignalNumber
)
663 DNS_UNUSED( inSignalNumber
);
665 signal( SIGINT
, SIG_DFL
);
666 CFRunLoopStop( CFRunLoopGetCurrent() );
670 #if( defined( WINVER ) )
671 //===========================================================================================================================
672 // ConsoleControlHandler
673 //===========================================================================================================================
675 static BOOL WINAPI
ConsoleControlHandler( DWORD inControlEvent
)
680 switch( inControlEvent
)
683 case CTRL_BREAK_EVENT
:
684 case CTRL_CLOSE_EVENT
:
685 case CTRL_LOGOFF_EVENT
:
686 case CTRL_SHUTDOWN_EVENT
:
698 //===========================================================================================================================
700 //===========================================================================================================================
702 static void BrowserCallBack( void *inContext
, DNSBrowserRef inRef
, DNSStatus inStatusCode
, const DNSBrowserEvent
*inEvent
)
707 DNS_UNUSED( inContext
);
709 DNS_UNUSED( inStatusCode
);
711 switch( inEvent
->type
)
713 case kDNSBrowserEventTypeRelease
:
716 case kDNSBrowserEventTypeAddDomain
:
717 fprintf( stdout
, "domain \"%s\" added on interface 0x%08X (%s)\n",
718 inEvent
->data
.addDomain
.domain
,
719 (int) inEvent
->data
.addDomain
.interfaceID
,
720 IPv4ToString( inEvent
->data
.addDomain
.interfaceIP
.u
.ipv4
.addr
, ifIP
) );
723 case kDNSBrowserEventTypeAddDefaultDomain
:
724 fprintf( stdout
, "default domain \"%s\" added on interface 0x%08X (%s)\n",
725 inEvent
->data
.addDefaultDomain
.domain
,
726 (int) inEvent
->data
.addDefaultDomain
.interfaceID
,
727 IPv4ToString( inEvent
->data
.addDefaultDomain
.interfaceIP
.u
.ipv4
.addr
, ifIP
) );
730 case kDNSBrowserEventTypeRemoveDomain
:
731 fprintf( stdout
, "domain \"%s\" removed on interface 0x%08X (%s)\n",
732 inEvent
->data
.removeDomain
.domain
,
733 (int) inEvent
->data
.removeDomain
.interfaceID
,
734 IPv4ToString( inEvent
->data
.removeDomain
.interfaceIP
.u
.ipv4
.addr
, ifIP
) );
737 case kDNSBrowserEventTypeAddService
:
738 fprintf( stdout
, "service \"%s.%s%s\" added on interface 0x%08X (%s)\n",
739 inEvent
->data
.addService
.name
,
740 inEvent
->data
.addService
.type
,
741 inEvent
->data
.addService
.domain
,
742 (int) inEvent
->data
.addService
.interfaceID
,
743 IPv4ToString( inEvent
->data
.addService
.interfaceIP
.u
.ipv4
.addr
, ifIP
) );
746 case kDNSBrowserEventTypeRemoveService
:
747 fprintf( stdout
, "service \"%s.%s%s\" removed on interface 0x%08X (%s)\n",
748 inEvent
->data
.removeService
.name
,
749 inEvent
->data
.removeService
.type
,
750 inEvent
->data
.removeService
.domain
,
751 (int) inEvent
->data
.removeService
.interfaceID
,
752 IPv4ToString( inEvent
->data
.removeService
.interfaceIP
.u
.ipv4
.addr
, ifIP
) );
755 case kDNSBrowserEventTypeResolved
:
761 fprintf( stdout
, "resolved \"%s.%s%s\" to %s:%u on interface 0x%08X (%s)%s\n",
762 inEvent
->data
.resolved
->name
,
763 inEvent
->data
.resolved
->type
,
764 inEvent
->data
.resolved
->domain
,
765 IPv4ToString( inEvent
->data
.resolved
->address
.u
.ipv4
.addr
, ip
),
766 ( inEvent
->data
.resolved
->address
.u
.ipv4
.port
.v8
[ 0 ] << 8 ) |
767 inEvent
->data
.resolved
->address
.u
.ipv4
.port
.v8
[ 1 ],
768 (int) inEvent
->data
.resolved
->interfaceID
,
769 IPv4ToString( inEvent
->data
.resolved
->interfaceIP
.u
.ipv4
.addr
, ifIP
),
770 ( inEvent
->data
.resolved
->textRecordRawSize
> 0 ) ? " with text:" : "" );
772 p
= (const uint8_t *) inEvent
->data
.resolved
->textRecordRaw
;
773 end
= p
+ inEvent
->data
.resolved
->textRecordRawSize
;
776 if( gPrintTXTRecords
)
783 if( ( p
+ size
) > end
)
785 fprintf( stdout
, "\n### MALFORMED TXT RECORD (length byte too big for record)\n\n" );
788 fprintf( stdout
, "%5d (%3d bytes): \"%.*s\"\n", i
, size
, size
, p
);
792 fprintf( stdout
, "\n" );
802 //===========================================================================================================================
804 //===========================================================================================================================
806 static void ResolverCallBack( void *inContext
, DNSResolverRef inRef
, DNSStatus inStatusCode
, const DNSResolverEvent
*inEvent
)
811 DNS_UNUSED( inContext
);
813 DNS_UNUSED( inStatusCode
);
815 switch( inEvent
->type
)
817 case kDNSResolverEventTypeResolved
:
823 fprintf( stdout
, "resolved \"%s.%s%s\" to %s:%u on interface 0x%08X (%s)%s\n",
824 inEvent
->data
.resolved
.name
,
825 inEvent
->data
.resolved
.type
,
826 inEvent
->data
.resolved
.domain
,
827 IPv4ToString( inEvent
->data
.resolved
.address
.u
.ipv4
.addr
, ip
),
828 ( inEvent
->data
.resolved
.address
.u
.ipv4
.port
.v8
[ 0 ] << 8 ) |
829 inEvent
->data
.resolved
.address
.u
.ipv4
.port
.v8
[ 1 ],
830 (int) inEvent
->data
.resolved
.interfaceID
,
831 IPv4ToString( inEvent
->data
.resolved
.interfaceIP
.u
.ipv4
.addr
, ifIP
),
832 ( inEvent
->data
.resolved
.textRecordRawSize
> 0 ) ? " with text:" : "" );
834 p
= (const uint8_t *) inEvent
->data
.resolved
.textRecordRaw
;
835 end
= p
+ inEvent
->data
.resolved
.textRecordRawSize
;
838 if( gPrintTXTRecords
)
845 if( ( p
+ size
) > end
)
847 fprintf( stdout
, "\n### MALFORMED TXT RECORD (length byte too big for record)\n\n" );
850 fprintf( stdout
, "%5d (%3d bytes): \"%.*s\"\n", i
, size
, size
, p
);
854 fprintf( stdout
, "\n" );
859 case kDNSResolverEventTypeRelease
:
867 //===========================================================================================================================
868 // RegistrationCallBack
869 //===========================================================================================================================
872 RegistrationCallBack(
874 DNSRegistrationRef inRef
,
875 DNSStatus inStatusCode
,
876 const DNSRegistrationEvent
* inEvent
)
878 DNS_UNUSED( inContext
);
880 DNS_UNUSED( inStatusCode
);
882 switch( inEvent
->type
)
884 case kDNSRegistrationEventTypeRelease
:
887 case kDNSRegistrationEventTypeRegistered
:
888 fprintf( stdout
, "name registered and active\n" );
891 case kDNSRegistrationEventTypeNameCollision
:
892 fprintf( stdout
, "name in use, please choose another name\n" );
900 //===========================================================================================================================
901 // HostRegistrationCallBack
902 //===========================================================================================================================
905 HostRegistrationCallBack(
907 DNSHostRegistrationRef inRef
,
908 DNSStatus inStatusCode
,
911 DNS_UNUSED( inContext
);
913 DNS_UNUSED( inData
);
915 if( inStatusCode
== kDNSNoErr
)
917 fprintf( stdout
, "host name registered and active\n" );
919 else if( inStatusCode
== kDNSNameConflictErr
)
921 fprintf( stdout
, "host name in use, please choose another name\n" );
925 fprintf( stdout
, "unknown host registration status (%ld)\n", inStatusCode
);
929 //===========================================================================================================================
930 // EmulatedBrowserCallBack
931 //===========================================================================================================================
934 EmulatedBrowserCallBack(
935 DNSServiceBrowserReplyResultType inResult
,
938 const char * inDomain
,
939 DNSServiceDiscoveryReplyFlags inFlags
,
942 DNS_UNUSED( inFlags
);
943 DNS_UNUSED( inContext
);
945 if( inResult
== DNSServiceBrowserReplyAddInstance
)
947 fprintf( stdout
, "\"%s.%s%s\" service added emulated\n", inName
, inType
, inDomain
);
949 else if( inResult
== DNSServiceBrowserReplyRemoveInstance
)
951 fprintf( stdout
, "\"%s.%s%s\" service removed emulated\n", inName
, inType
, inDomain
);
955 fprintf( stdout
, "### unknown emulated browser callback result (%d)\n", inResult
);
959 //===========================================================================================================================
960 // EmulatedDomainEnumerationCallBack
961 //===========================================================================================================================
964 EmulatedDomainEnumerationCallBack(
965 DNSServiceDomainEnumerationReplyResultType inResult
,
966 const char * inDomain
,
967 DNSServiceDiscoveryReplyFlags inFlags
,
970 DNS_UNUSED( inFlags
);
971 DNS_UNUSED( inContext
);
973 if( inResult
== DNSServiceDomainEnumerationReplyAddDomain
)
975 fprintf( stdout
, "\"%s\" domain added emulated\n", inDomain
);
977 else if( inResult
== DNSServiceDomainEnumerationReplyAddDomainDefault
)
979 fprintf( stdout
, "\"%s\" default domain added emulated\n", inDomain
);
981 else if( inResult
== DNSServiceDomainEnumerationReplyRemoveDomain
)
983 fprintf( stdout
, "\"%s\" domain removed emulated\n", inDomain
);
987 fprintf( stdout
, "### unknown emulated domain enumeration callback result (%d)\n", inResult
);
991 //===========================================================================================================================
992 // EmulatedResolverCallBack
993 //===========================================================================================================================
996 EmulatedResolverCallBack(
997 struct sockaddr
* inInterfaceAddr
,
998 struct sockaddr
* inAddr
,
999 const char * inTextRecord
,
1000 DNSServiceDiscoveryReplyFlags inFlags
,
1003 struct sockaddr_in
* ifSin4
;
1004 struct sockaddr_in
* sin4
;
1008 DNS_UNUSED( inFlags
);
1009 DNS_UNUSED( inContext
);
1011 ifSin4
= (struct sockaddr_in
*) inInterfaceAddr
;
1012 sin4
= (struct sockaddr_in
*) inAddr
;
1014 fprintf( stdout
, "service resolved to %s:%d on interface %s with text \"%s\"\n",
1015 IPv4ToString( *( (DNSOpaque32
*) &sin4
->sin_addr
.s_addr
), ip
),
1016 ntohs( sin4
->sin_port
),
1017 IPv4ToString( *( (DNSOpaque32
*) &ifSin4
->sin_addr
.s_addr
), ifIP
),
1018 inTextRecord
? inTextRecord
: "" );
1021 //===========================================================================================================================
1022 // EmulatedResolverCallBack
1023 //===========================================================================================================================
1025 static void EmulatedRegistrationCallBack( DNSServiceRegistrationReplyErrorType inResult
, void *inContext
)
1027 DNS_UNUSED( inContext
);
1029 if( inResult
== kDNSServiceDiscoveryNoError
)
1031 fprintf( stdout
, "service name registered successfully\n" );
1035 fprintf( stdout
, "service registration failed( %d)\n", inResult
);
1039 //===========================================================================================================================
1041 //===========================================================================================================================
1043 static char * IPv4ToString( DNSOpaque32 inIP
, char *outString
)
1045 sprintf( outString
, "%u.%u.%u.%u", inIP
.v8
[ 0 ], inIP
.v8
[ 1 ], inIP
.v8
[ 2 ], inIP
.v8
[ 3 ] );
1046 return( outString
);