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 Contains: mDNS platform plugin for VxWorks.
25 Copyright: Copyright (C) 2002-2003 Apple Computer, Inc., All Rights Reserved.
27 Change History (most recent first):
29 $Log: mDNSVxWorks.c,v $
30 Revision 1.7.2.2 2004/04/09 17:57:31 cheshire
31 Make sure to set the TxAndRx field so that duplicate suppression works correctly
33 Revision 1.7.2.1 2004/04/03 21:31:20 bradley
34 Integrated changes from TOT to remove legacy port 53 support.
36 Revision 1.8 2003/10/28 10:08:27 bradley
37 Removed legacy port 53 support as it is no longer needed.
39 Revision 1.7 2003/08/20 05:58:54 bradley
40 Removed dependence on modified mDNSCore: define structures/prototypes locally.
42 Revision 1.6 2003/08/18 23:19:05 cheshire
43 <rdar://problem/3382647> mDNSResponder divide by zero in mDNSPlatformTimeNow()
45 Revision 1.5 2003/08/15 00:05:04 bradley
46 Updated to use name/InterfaceID from new AuthRecord resrec field. Added output of new record sizes.
48 Revision 1.4 2003/08/14 02:19:55 cheshire
49 <rdar://problem/3375491> Split generic ResourceRecord type into two separate types: AuthRecord and CacheRecord
51 Revision 1.3 2003/08/12 19:56:27 cheshire
54 Revision 1.2 2003/08/05 23:58:34 cheshire
55 Update code to compile with the new mDNSCoreReceive() function that requires a TTL
56 Right now this platform layer just reports 255 instead of returning the real value -- we should fix this
58 Revision 1.1 2003/08/02 10:06:48 bradley
59 mDNS platform plugin for VxWorks.
62 Notes for non-Apple platforms:
64 TARGET_NON_APPLE should be defined to 1 to avoid relying on Apple-only header files, macros, or functions.
68 - Add support for IPv6 (needs VxWorks IPv6 support).
71 // Set up the debug library to use the default category (see DebugServicesLite.h for details).
73 #if( !TARGET_NON_APPLE )
74 #define DEBUG_USE_DEFAULT_CATEGORY 1
83 #include <sys/types.h>
84 #include <arpa/inet.h>
86 #include <netinet/if_ether.h>
87 #include <netinet/in.h>
88 #include <netinet/ip.h>
89 #include <sys/ioctl.h>
90 #include <sys/socket.h>
97 #include "selectLib.h"
106 #if( !TARGET_NON_APPLE )
107 #include "ACP/ACPUtilities.h"
108 #include "Support/DebugServicesLite.h"
109 #include "Support/MiscUtilities.h"
112 #include "mDNSClientAPI.h"
113 #include "mDNSPlatformFunctions.h"
115 #include "mDNSVxWorks.h"
118 #pragma mark == Preprocessor ==
121 //===========================================================================================================================
123 //===========================================================================================================================
125 #if( !TARGET_NON_APPLE )
126 debug_log_new_default_category( mdns
);
130 #pragma mark == Constants ==
133 //===========================================================================================================================
135 //===========================================================================================================================
137 #define DEBUG_NAME "[mDNS] "
139 #define kMDNSDefaultName "My-Device"
141 #define kMDNSTaskName "tMDNS"
142 #define kMDNSTaskPriority 102
143 #define kMDNSTaskStackSize 49152
145 #define kMDNSPipeName "/pipe/mDNS"
146 #define kMDNSPipeMessageQueueSize 32
147 #define kMDNSPipeMessageSize 1
149 #define kInvalidSocketRef -1
151 typedef uint8_t MDNSPipeCommandCode
;
154 kMDNSPipeCommandCodeInvalid
= 0,
155 kMDNSPipeCommandCodeReschedule
= 1,
156 kMDNSPipeCommandCodeReconfigure
= 2,
157 kMDNSPipeCommandCodeQuit
= 3
161 #pragma mark == Structures ==
164 //===========================================================================================================================
166 //===========================================================================================================================
168 typedef int MDNSSocketRef
;
170 struct MDNSInterfaceItem
172 MDNSInterfaceItem
* next
;
174 MDNSSocketRef multicastSocketRef
;
175 MDNSSocketRef sendingSocketRef
;
176 NetworkInterfaceInfo hostSet
;
177 mDNSBool hostRegistered
;
179 int sendMulticastCounter
;
180 int sendUnicastCounter
;
181 int sendErrorCounter
;
184 int recvErrorCounter
;
189 #pragma mark == Macros ==
192 //===========================================================================================================================
194 //===========================================================================================================================
196 #if( TARGET_NON_APPLE )
198 // Do-nothing versions of the debugging macros for non-Apple platforms.
200 #define check(assertion)
201 #define check_string( assertion, cstring )
202 #define check_noerr(err)
203 #define check_noerr_string( error, cstring )
204 #define check_errno( assertion, errno_value )
205 #define debug_string( cstring )
206 #define require( assertion, label ) do { if( !(assertion) ) goto label; } while(0)
207 #define require_string( assertion, label, string ) require(assertion, label)
208 #define require_quiet( assertion, label ) require( assertion, label )
209 #define require_noerr( error, label ) do { if( (error) != 0 ) goto label; } while(0)
210 #define require_noerr_quiet( assertion, label ) require_noerr( assertion, label )
211 #define require_noerr_action( error, label, action ) do { if( (error) != 0 ) { {action;}; goto label; } } while(0)
212 #define require_noerr_action_quiet( assertion, label, action ) require_noerr_action( assertion, label, action )
213 #define require_action( assertion, label, action ) do { if( !(assertion) ) { {action;}; goto label; } } while(0)
214 #define require_action_quiet( assertion, label, action ) require_action( assertion, label, action )
215 #define require_action_string( assertion, label, action, cstring ) do { if( !(assertion) ) { {action;}; goto label; } } while(0)
216 #define require_errno( assertion, errno_value, label ) do { if( !(assertion) ) goto label; } while(0)
217 #define require_errno_action( assertion, errno_value, label, action ) do { if( !(assertion) ) { {action;}; goto label; } } while(0)
219 #define dlog( ARGS... )
221 #define DEBUG_UNUSED( X ) (void)( X )
225 #pragma mark == Prototypes ==
228 //===========================================================================================================================
230 //===========================================================================================================================
232 // ifIndexToIfp is in net/if.c, but not exported by net/if.h so provide it here.
234 extern struct ifnet
* ifIndexToIfp(int ifIndex
);
236 // Platform Internals
238 mDNSlocal
void SetupNames( mDNS
* const inMDNS
);
239 mDNSlocal mStatus
SetupInterfaceList( mDNS
* const inMDNS
);
240 mDNSlocal mStatus
TearDownInterfaceList( mDNS
* const inMDNS
);
241 mDNSlocal mStatus
SetupInterface( mDNS
* const inMDNS
, const struct ifaddrs
*inAddr
, MDNSInterfaceItem
**outItem
);
242 mDNSlocal mStatus
TearDownInterface( mDNS
* const inMDNS
, MDNSInterfaceItem
*inItem
);
246 const struct ifaddrs
* inAddr
,
248 MDNSSocketRef
* outSocketRef
);
252 mDNSlocal mStatus
SetupCommandPipe( mDNS
* const inMDNS
);
253 mDNSlocal mStatus
TearDownCommandPipe( mDNS
* const inMDNS
);
254 mDNSlocal mStatus
SendCommand( const mDNS
* const inMDNS
, MDNSPipeCommandCode inCommandCode
);
255 mDNSlocal mStatus
ProcessCommand( mDNS
* const inMDNS
);
256 mDNSlocal
void ProcessCommandReconfigure( mDNS
*inMDNS
);
260 mDNSlocal mStatus
SetupTask( mDNS
* const inMDNS
);
261 mDNSlocal mStatus
TearDownTask( mDNS
* const inMDNS
);
262 mDNSlocal
void Task( mDNS
*inMDNS
);
263 mDNSlocal mStatus
TaskInit( mDNS
*inMDNS
);
264 mDNSlocal
void TaskSetupReadSet( mDNS
*inMDNS
, fd_set
*outReadSet
, int *outMaxSocket
);
265 mDNSlocal
void TaskSetupTimeout( mDNSs32 inNextTaskTime
, struct timeval
*outTimeout
);
266 mDNSlocal
void TaskProcessPacket( mDNS
*inMDNS
, MDNSInterfaceItem
*inItem
, MDNSSocketRef inSocketRef
);
270 #if( TARGET_NON_APPLE )
271 mDNSlocal
void GenerateUniqueHostName( char *outName
, long *ioSeed
);
272 mDNSlocal
void GenerateUniqueDNSName( char *outName
, long *ioSeed
);
275 // Platform Accessors
281 typedef struct mDNSPlatformInterfaceInfo mDNSPlatformInterfaceInfo
;
282 struct mDNSPlatformInterfaceInfo
288 mDNSexport mStatus
mDNSPlatformInterfaceNameToID( mDNS
* const inMDNS
, const char *inName
, mDNSInterfaceID
*outID
);
289 mDNSexport mStatus
mDNSPlatformInterfaceIDToInfo( mDNS
* const inMDNS
, mDNSInterfaceID inID
, mDNSPlatformInterfaceInfo
*outInfo
);
296 #pragma mark == Globals ==
299 //===========================================================================================================================
301 //===========================================================================================================================
303 mDNSlocal mDNS
* gMDNSPtr
= NULL
;
304 mDNSlocal mDNS_PlatformSupport gMDNSPlatformSupport
;
305 mDNSlocal mDNSs32 gMDNSTicksToMicrosecondsMultiplier
= 0;
309 mDNSs32 mDNSPlatformOneSecond
;
313 #pragma mark == Public APIs ==
316 //===========================================================================================================================
318 //===========================================================================================================================
320 void mDNSReconfigure( void )
322 // Send a "reconfigure" command to the MDNS task.
326 SendCommand( gMDNSPtr
, kMDNSPipeCommandCodeReconfigure
);
332 #pragma mark == Platform Support ==
335 //===========================================================================================================================
337 //===========================================================================================================================
339 mStatus
mDNSPlatformInit( mDNS
* const inMDNS
)
343 dlog( kDebugLevelInfo
, DEBUG_NAME
"platform init\n" );
345 // Initialize variables.
347 memset( &gMDNSPlatformSupport
, 0, sizeof( gMDNSPlatformSupport
) );
348 inMDNS
->p
= &gMDNSPlatformSupport
;
349 inMDNS
->p
->commandPipe
= ERROR
;
350 inMDNS
->p
->task
= ERROR
;
351 inMDNS
->p
->rescheduled
= 1; // Default to rescheduled until fully initialized.
352 mDNSPlatformOneSecond
= sysClkRateGet();
353 gMDNSTicksToMicrosecondsMultiplier
= ( 1000000L / mDNSPlatformOneSecond
);
355 // Allocate semaphores.
357 inMDNS
->p
->lockID
= semMCreate( SEM_Q_FIFO
);
358 require_action( inMDNS
->p
->lockID
, exit
, err
= mStatus_NoMemoryErr
);
360 inMDNS
->p
->readyEvent
= semBCreate( SEM_Q_FIFO
, SEM_EMPTY
);
361 require_action( inMDNS
->p
->readyEvent
, exit
, err
= mStatus_NoMemoryErr
);
363 inMDNS
->p
->quitEvent
= semBCreate( SEM_Q_FIFO
, SEM_EMPTY
);
364 require_action( inMDNS
->p
->quitEvent
, exit
, err
= mStatus_NoMemoryErr
);
368 // Set up the task and wait for it to initialize. Initialization is done from the task instead of here to avoid
369 // stack space issues. Some of the initialization may require a larger stack than the current task supports.
371 err
= SetupTask( inMDNS
);
372 require_noerr( err
, exit
);
374 err
= semTake( inMDNS
->p
->readyEvent
, WAIT_FOREVER
);
375 require_noerr( err
, exit
);
376 err
= inMDNS
->p
->taskInitErr
;
377 require_noerr( err
, exit
);
379 mDNSCoreInitComplete( inMDNS
, err
);
384 mDNSPlatformClose( inMDNS
);
386 dlog( kDebugLevelInfo
, DEBUG_NAME
"platform init done (err=%ld)\n", err
);
390 //===========================================================================================================================
392 //===========================================================================================================================
394 void mDNSPlatformClose( mDNS
* const inMDNS
)
398 dlog( kDebugLevelInfo
, DEBUG_NAME
"platform close\n" );
401 // Tear everything down.
403 err
= TearDownTask( inMDNS
);
406 err
= TearDownInterfaceList( inMDNS
);
409 err
= TearDownCommandPipe( inMDNS
);
414 // Release semaphores.
416 if( inMDNS
->p
->quitEvent
)
418 semDelete( inMDNS
->p
->quitEvent
);
419 inMDNS
->p
->quitEvent
= 0;
421 if( inMDNS
->p
->readyEvent
)
423 semDelete( inMDNS
->p
->readyEvent
);
424 inMDNS
->p
->readyEvent
= 0;
426 if( inMDNS
->p
->lockID
)
428 semDelete( inMDNS
->p
->lockID
);
429 inMDNS
->p
->lockID
= 0;
432 dlog( kDebugLevelInfo
, DEBUG_NAME
"platform close done\n" );
435 //===========================================================================================================================
436 // mDNSPlatformSendUDP
437 //===========================================================================================================================
441 const mDNS
* const inMDNS
,
442 const DNSMessage
* const inMsg
,
443 const mDNSu8
* const inMsgEnd
,
444 mDNSInterfaceID inInterfaceID
,
445 mDNSIPPort inSrcPort
,
446 const mDNSAddr
* inDstIP
,
447 mDNSIPPort inDstPort
)
450 MDNSInterfaceItem
* item
;
451 struct sockaddr_in addr
;
454 DEBUG_UNUSED( inSrcPort
);
456 dlog( kDebugLevelChatty
, DEBUG_NAME
"platform send UDP\n" );
463 check( inInterfaceID
);
465 if( inDstIP
->type
!= mDNSAddrType_IPv4
)
467 err
= mStatus_BadParamErr
;
472 // Make sure the InterfaceID is valid.
474 for( item
= inMDNS
->p
->interfaceList
; item
; item
= item
->next
)
476 if( item
== (MDNSInterfaceItem
*) inInterfaceID
)
481 require_action( item
, exit
, err
= mStatus_NoSuchNameErr
);
486 item
= (MDNSInterfaceItem
*) inInterfaceID
;
487 check( item
->sendingSocketRef
!= kInvalidSocketRef
);
489 memset( &addr
, 0, sizeof( addr
) );
490 addr
.sin_family
= AF_INET
;
491 addr
.sin_port
= inDstPort
.NotAnInteger
;
492 addr
.sin_addr
.s_addr
= inDstIP
->ip
.v4
.NotAnInteger
;
494 n
= inMsgEnd
- ( (const mDNSu8
* const) inMsg
);
495 n
= sendto( item
->sendingSocketRef
, (char *) inMsg
, n
, 0, (struct sockaddr
*) &addr
, sizeof( addr
) );
496 check_errno( n
, errno
);
498 item
->sendErrorCounter
+= ( n
< 0 );
499 item
->sendMulticastCounter
+= ( inDstPort
.NotAnInteger
== MulticastDNSPort
.NotAnInteger
);
500 item
->sendUnicastCounter
+= ( inDstPort
.NotAnInteger
!= MulticastDNSPort
.NotAnInteger
);
502 dlog( kDebugLevelChatty
, DEBUG_NAME
"sent (to=%u.%u.%u.%u:%hu)\n",
503 inDstIP
->ip
.v4
.b
[ 0 ], inDstIP
->ip
.v4
.b
[ 1 ], inDstIP
->ip
.v4
.b
[ 2 ], inDstIP
->ip
.v4
.b
[ 3 ],
504 htons( inDstPort
.NotAnInteger
) );
505 err
= mStatus_NoError
;
508 dlog( kDebugLevelChatty
, DEBUG_NAME
"platform send UDP done\n" );
512 //===========================================================================================================================
514 //===========================================================================================================================
516 void mDNSPlatformLock( const mDNS
* const inMDNS
)
518 check( inMDNS
->p
->lockID
);
520 if( inMDNS
->p
->lockID
)
522 #if( TARGET_NON_APPLE )
523 semTake( inMDNS
->p
->lockID
, WAIT_FOREVER
);
525 semTakeDeadlockDetect( inMDNS
->p
->lockID
, WAIT_FOREVER
);
530 //===========================================================================================================================
531 // mDNSPlatformUnlock
532 //===========================================================================================================================
534 void mDNSPlatformUnlock( const mDNS
* const inMDNS
)
538 check( inMDNS
->p
->lockID
);
539 check_string( inMDNS
->p
->task
!= ERROR
, "mDNS task not started" );
541 // When an API routine is called, "m->NextScheduledEvent" is reset to "timenow" before calling mDNSPlatformUnlock()
542 // Since our main mDNS_Execute() loop is on a different thread, we need to wake up that thread to:
543 // (a) handle immediate work (if any) resulting from this API call
544 // (b) calculate the next sleep time between now and the next interesting event
546 if( ( mDNSPlatformTimeNow() - inMDNS
->NextScheduledEvent
) >= 0 )
548 // We only need to send the reschedule event when called from a task other than the mDNS task since if we are
549 // called from mDNS task, we'll loop back and call mDNS_Execute. This avoids filling up the command queue.
551 if( ( inMDNS
->p
->rescheduled
++ == 0 ) && ( taskIdSelf() != inMDNS
->p
->task
) )
553 SendCommand( inMDNS
, kMDNSPipeCommandCodeReschedule
);
557 if( inMDNS
->p
->lockID
)
559 semGive( inMDNS
->p
->lockID
);
563 //===========================================================================================================================
564 // mDNSPlatformStrLen
565 //===========================================================================================================================
567 mDNSu32
mDNSPlatformStrLen( const void *inSrc
)
571 return( (mDNSu32
) strlen( (const char *) inSrc
) );
574 //===========================================================================================================================
575 // mDNSPlatformStrCopy
576 //===========================================================================================================================
578 void mDNSPlatformStrCopy( const void *inSrc
, void *inDst
)
583 strcpy( (char *) inDst
, (const char*) inSrc
);
586 //===========================================================================================================================
587 // mDNSPlatformMemCopy
588 //===========================================================================================================================
590 void mDNSPlatformMemCopy( const void *inSrc
, void *inDst
, mDNSu32 inSize
)
595 memcpy( inDst
, inSrc
, inSize
);
598 //===========================================================================================================================
599 // mDNSPlatformMemSame
600 //===========================================================================================================================
602 mDNSBool
mDNSPlatformMemSame( const void *inSrc
, const void *inDst
, mDNSu32 inSize
)
607 return( memcmp( inSrc
, inDst
, inSize
) == 0 );
610 //===========================================================================================================================
611 // mDNSPlatformMemZero
612 //===========================================================================================================================
614 void mDNSPlatformMemZero( void *inDst
, mDNSu32 inSize
)
618 memset( inDst
, 0, inSize
);
621 //===========================================================================================================================
622 // mDNSPlatformMemAllocate
623 //===========================================================================================================================
625 mDNSexport
void * mDNSPlatformMemAllocate( mDNSu32 inSize
)
631 mem
= malloc( inSize
);
637 //===========================================================================================================================
638 // mDNSPlatformMemFree
639 //===========================================================================================================================
641 mDNSexport
void mDNSPlatformMemFree( void *inMem
)
648 //===========================================================================================================================
649 // mDNSPlatformTimeInit
650 //===========================================================================================================================
652 mDNSexport mStatus
mDNSPlatformTimeInit( mDNSs32
*outTimeNow
)
656 // No special setup is required on VxWorks -- we just use tickGet().
658 *outTimeNow
= mDNSPlatformTimeNow();
659 return( mStatus_NoError
);
662 //===========================================================================================================================
663 // mDNSPlatformTimeNow
664 //===========================================================================================================================
666 mDNSs32
mDNSPlatformTimeNow( void )
668 return( (mDNSs32
) tickGet() );
671 //===========================================================================================================================
672 // mDNSPlatformInterfaceNameToID
673 //===========================================================================================================================
675 mStatus
mDNSPlatformInterfaceNameToID( mDNS
* const inMDNS
, const char *inName
, mDNSInterfaceID
*outID
)
678 MDNSInterfaceItem
* ifd
;
684 // Search for an interface with the specified name,
686 for( ifd
= inMDNS
->p
->interfaceList
; ifd
; ifd
= ifd
->next
)
688 if( strcmp( ifd
->name
, inName
) == 0 )
695 err
= mStatus_NoSuchNameErr
;
703 *outID
= (mDNSInterfaceID
) ifd
;
705 err
= mStatus_NoError
;
711 //===========================================================================================================================
712 // mDNSPlatformInterfaceIDToInfo
713 //===========================================================================================================================
715 mStatus
mDNSPlatformInterfaceIDToInfo( mDNS
* const inMDNS
, mDNSInterfaceID inID
, mDNSPlatformInterfaceInfo
*outInfo
)
718 MDNSInterfaceItem
* ifd
;
724 // Search for an interface with the specified ID,
726 for( ifd
= inMDNS
->p
->interfaceList
; ifd
; ifd
= ifd
->next
)
728 if( ifd
== (MDNSInterfaceItem
*) inID
)
735 err
= mStatus_NoSuchNameErr
;
741 outInfo
->name
= ifd
->name
;
742 outInfo
->ip
= ifd
->hostSet
.ip
;
743 err
= mStatus_NoError
;
749 //===========================================================================================================================
751 //===========================================================================================================================
753 #if( MDNS_DEBUGMSGS )
754 mDNSexport
void debugf_( const char *format
, ... )
760 va_start( args
, format
);
761 length
= mDNS_vsnprintf( buffer
, sizeof( buffer
), format
, args
);
764 dlog( kDebugLevelInfo
, "%s\n", buffer
);
768 //===========================================================================================================================
770 //===========================================================================================================================
772 #if( MDNS_DEBUGMSGS > 1 )
773 mDNSexport
void verbosedebugf_( const char *format
, ... )
779 va_start( args
, format
);
780 length
= mDNS_vsnprintf( buffer
, sizeof( buffer
), format
, args
);
783 dlog( kDebugLevelVerbose
, "%s\n", buffer
);
787 //===========================================================================================================================
789 //===========================================================================================================================
791 void LogMsg( const char *inFormat
, ... )
797 va_start( args
, inFormat
);
798 length
= mDNS_vsnprintf( buffer
, sizeof( buffer
), inFormat
, args
);
801 dlog( kDebugLevelWarning
, "%s\n", buffer
);
806 #pragma mark == Platform Internals ==
809 //===========================================================================================================================
811 //===========================================================================================================================
813 mDNSlocal
void SetupNames( mDNS
* const inMDNS
)
815 char tempCString
[ 128 ];
816 mDNSu8 tempPString
[ 128 ];
819 // Set up the host name.
821 tempCString
[ 0 ] = '\0';
822 GenerateUniqueHostName( tempCString
, NULL
);
823 check( tempCString
[ 0 ] != '\0' );
824 if( tempCString
[ 0 ] == '\0' )
826 // No name so use the default.
828 strcpy( tempCString
, kMDNSDefaultName
);
830 inMDNS
->nicelabel
.c
[ 0 ] = strlen( tempCString
);
831 memcpy( &inMDNS
->nicelabel
.c
[ 1 ], tempCString
, inMDNS
->nicelabel
.c
[ 0 ] );
832 check( inMDNS
->nicelabel
.c
[ 0 ] > 0 );
834 // Set up the DNS name.
836 tempCString
[ 0 ] = '\0';
837 GenerateUniqueDNSName( tempCString
, NULL
);
838 if( tempCString
[ 0 ] != '\0' )
840 tempPString
[ 0 ] = strlen( tempCString
);
841 memcpy( &tempPString
[ 1 ], tempCString
, tempPString
[ 0 ] );
842 namePtr
= tempPString
;
846 // No DNS name so use the host name.
848 namePtr
= inMDNS
->nicelabel
.c
;
850 ConvertUTF8PstringToRFC1034HostLabel( namePtr
, &inMDNS
->hostlabel
);
851 if( inMDNS
->hostlabel
.c
[ 0 ] == 0 )
853 // Nice name has no characters that are representable as an RFC 1034 name (e.g. Japanese) so use the default.
855 MakeDomainLabelFromLiteralString( &inMDNS
->hostlabel
, kMDNSDefaultName
);
857 check( inMDNS
->hostlabel
.c
[ 0 ] > 0 );
859 mDNS_GenerateFQDN( inMDNS
);
861 dlog( kDebugLevelInfo
, DEBUG_NAME
"nice name \"%.*s\"\n", inMDNS
->nicelabel
.c
[ 0 ], &inMDNS
->nicelabel
.c
[ 1 ] );
862 dlog( kDebugLevelInfo
, DEBUG_NAME
"host name \"%.*s\"\n", inMDNS
->hostlabel
.c
[ 0 ], &inMDNS
->hostlabel
.c
[ 1 ] );
865 //===========================================================================================================================
866 // SetupInterfaceList
867 //===========================================================================================================================
869 mDNSlocal mStatus
SetupInterfaceList( mDNS
* const inMDNS
)
872 struct ifaddrs
* addrs
;
876 MDNSInterfaceItem
** next
;
877 MDNSInterfaceItem
* item
;
881 dlog( kDebugLevelVerbose
, DEBUG_NAME
"setting up interface list\n" );
884 // Tear down any existing interfaces that may be set up.
886 TearDownInterfaceList( inMDNS
);
887 inMDNS
->p
->interfaceList
= NULL
;
888 next
= &inMDNS
->p
->interfaceList
;
890 // Set up each interface that is active, multicast-capable, and not the loopback interface or point-to-point.
892 flagMask
= IFF_UP
| IFF_MULTICAST
| IFF_LOOPBACK
| IFF_POINTOPOINT
;
893 flagTest
= IFF_UP
| IFF_MULTICAST
;
895 err
= getifaddrs( &addrs
);
896 require_noerr( err
, exit
);
898 for( p
= addrs
; p
; p
= p
->ifa_next
)
900 if( ( p
->ifa_flags
& flagMask
) == flagTest
)
902 err
= SetupInterface( inMDNS
, p
, &item
);
903 require_noerr( err
, exit
);
909 err
= mStatus_NoError
;
914 freeifaddrs( addrs
);
918 TearDownInterfaceList( inMDNS
);
920 dlog( kDebugLevelVerbose
, DEBUG_NAME
"setting up interface list done (err=%ld)\n", err
);
924 //===========================================================================================================================
925 // TearDownInterfaceList
926 //===========================================================================================================================
928 mDNSlocal mStatus
TearDownInterfaceList( mDNS
* const inMDNS
)
930 dlog( kDebugLevelVerbose
, DEBUG_NAME
"tearing down interface list\n" );
933 // Tear down all the interfaces.
935 while( inMDNS
->p
->interfaceList
)
937 MDNSInterfaceItem
* item
;
939 item
= inMDNS
->p
->interfaceList
;
940 inMDNS
->p
->interfaceList
= item
->next
;
942 TearDownInterface( inMDNS
, item
);
945 dlog( kDebugLevelVerbose
, DEBUG_NAME
"tearing down interface list done\n" );
946 return( mStatus_NoError
);
949 //===========================================================================================================================
951 //===========================================================================================================================
953 mDNSlocal mStatus
SetupInterface( mDNS
* const inMDNS
, const struct ifaddrs
*inAddr
, MDNSInterfaceItem
**outItem
)
956 MDNSInterfaceItem
* item
;
957 MDNSSocketRef socketRef
;
958 const struct sockaddr_in
* ipv4
;
960 dlog( kDebugLevelVerbose
, DEBUG_NAME
"setting up interface (name=%s)\n", inAddr
->ifa_name
);
963 check( inAddr
->ifa_addr
);
964 ipv4
= (const struct sockaddr_in
*) inAddr
->ifa_addr
;
967 // Allocate memory for the info item.
969 item
= (MDNSInterfaceItem
*) calloc( 1, sizeof( *item
) );
970 require_action( item
, exit
, err
= mStatus_NoMemoryErr
);
971 strcpy( item
->name
, inAddr
->ifa_name
);
972 item
->multicastSocketRef
= kInvalidSocketRef
;
973 item
->sendingSocketRef
= kInvalidSocketRef
;
975 // Set up the multicast DNS (port 5353) socket for this interface.
977 err
= SetupSocket( inMDNS
, inAddr
, MulticastDNSPort
, &socketRef
);
978 require_noerr( err
, exit
);
979 item
->multicastSocketRef
= socketRef
;
981 // Set up the sending socket for this interface.
983 err
= SetupSocket( inMDNS
, inAddr
, zeroIPPort
, &socketRef
);
984 require_noerr( err
, exit
);
985 item
->sendingSocketRef
= socketRef
;
987 // Register this interface with mDNS.
989 item
->hostSet
.InterfaceID
= (mDNSInterfaceID
) item
;
990 item
->hostSet
.ip
.type
= mDNSAddrType_IPv4
;
991 item
->hostSet
.ip
.ip
.v4
.NotAnInteger
= ipv4
->sin_addr
.s_addr
;
992 item
->hostSet
.Advertise
= inMDNS
->AdvertiseLocalAddresses
;
993 item
->hostSet
.TxAndRx
= mDNStrue
;
995 err
= mDNS_RegisterInterface( inMDNS
, &item
->hostSet
);
996 require_noerr( err
, exit
);
997 item
->hostRegistered
= mDNStrue
;
999 dlog( kDebugLevelInfo
, DEBUG_NAME
"Registered IP address: %u.%u.%u.%u\n",
1000 item
->hostSet
.ip
.ip
.v4
.b
[ 0 ], item
->hostSet
.ip
.ip
.v4
.b
[ 1 ],
1001 item
->hostSet
.ip
.ip
.v4
.b
[ 2 ], item
->hostSet
.ip
.ip
.v4
.b
[ 3 ] );
1011 TearDownInterface( inMDNS
, item
);
1013 dlog( kDebugLevelVerbose
, DEBUG_NAME
"setting up interface done (name=%s, err=%ld)\n", inAddr
->ifa_name
, err
);
1017 //===========================================================================================================================
1018 // TearDownInterface
1019 //===========================================================================================================================
1021 mDNSlocal mStatus
TearDownInterface( mDNS
* const inMDNS
, MDNSInterfaceItem
*inItem
)
1023 MDNSSocketRef socketRef
;
1028 // Deregister this interface with mDNS.
1030 dlog( kDebugLevelInfo
, DEBUG_NAME
"Deregistering IP address: %u.%u.%u.%u\n",
1031 inItem
->hostSet
.ip
.ip
.v4
.b
[ 0 ], inItem
->hostSet
.ip
.ip
.v4
.b
[ 1 ],
1032 inItem
->hostSet
.ip
.ip
.v4
.b
[ 2 ], inItem
->hostSet
.ip
.ip
.v4
.b
[ 3 ] );
1034 if( inItem
->hostRegistered
)
1036 inItem
->hostRegistered
= mDNSfalse
;
1037 mDNS_DeregisterInterface( inMDNS
, &inItem
->hostSet
);
1040 // Close the multicast socket.
1042 socketRef
= inItem
->multicastSocketRef
;
1043 inItem
->multicastSocketRef
= kInvalidSocketRef
;
1044 if( socketRef
!= kInvalidSocketRef
)
1046 dlog( kDebugLevelVerbose
, DEBUG_NAME
"tearing down multicast socket %d\n", socketRef
);
1050 // Close the sending socket.
1052 socketRef
= inItem
->sendingSocketRef
;
1053 inItem
->sendingSocketRef
= kInvalidSocketRef
;
1054 if( socketRef
!= kInvalidSocketRef
)
1056 dlog( kDebugLevelVerbose
, DEBUG_NAME
"tearing down sending socket %d\n", socketRef
);
1060 // Free the memory used by the interface info.
1063 return( mStatus_NoError
);
1066 //===========================================================================================================================
1068 //===========================================================================================================================
1072 mDNS
* const inMDNS
,
1073 const struct ifaddrs
* inAddr
,
1075 MDNSSocketRef
* outSocketRef
)
1078 MDNSSocketRef socketRef
;
1080 unsigned char optionByte
;
1081 struct ip_mreq mreq
;
1082 const struct sockaddr_in
* ipv4
;
1083 struct sockaddr_in addr
;
1086 dlog( kDebugLevelVerbose
, DEBUG_NAME
"setting up socket done\n" );
1089 check( inAddr
->ifa_addr
);
1090 ipv4
= (const struct sockaddr_in
*) inAddr
->ifa_addr
;
1091 check( outSocketRef
);
1093 // Set up a UDP socket for multicast DNS.
1095 socketRef
= socket( AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
1096 require_errno_action( socketRef
, errno
, exit
, err
= mStatus_UnknownErr
);
1098 // A port of zero means this socket is for sending and should be set up for sending. Otherwise, it is for receiving
1099 // and should be set up for receiving. The reason for separate sending vs receiving sockets is to workaround problems
1100 // with VxWorks IP stack when using dynamic IP configuration such as DHCP (problems binding to wildcard IP when the
1101 // IP address later changes). Since we have to bind the Multicast DNS address to workaround these issues we have to
1102 // use a separate sending socket since it is illegal to send a packet with a multicast source address (RFC 1122).
1104 if( inPort
.NotAnInteger
!= zeroIPPort
.NotAnInteger
)
1106 // Turn on reuse port option so multiple servers can listen for Multicast DNS packets.
1109 err
= setsockopt( socketRef
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &option
, sizeof( option
) );
1110 check_errno( err
, errno
);
1112 // Join the all-DNS multicast group so we receive Multicast DNS packets.
1114 ip
.NotAnInteger
= ipv4
->sin_addr
.s_addr
;
1115 mreq
.imr_multiaddr
.s_addr
= AllDNSLinkGroup
.NotAnInteger
;
1116 mreq
.imr_interface
.s_addr
= ip
.NotAnInteger
;
1117 err
= setsockopt( socketRef
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, (char *) &mreq
, sizeof( mreq
) );
1118 check_errno( err
, errno
);
1120 // Bind to the multicast DNS address and port 5353.
1122 memset( &addr
, 0, sizeof( addr
) );
1123 addr
.sin_family
= AF_INET
;
1124 addr
.sin_port
= inPort
.NotAnInteger
;
1125 addr
.sin_addr
.s_addr
= AllDNSLinkGroup
.NotAnInteger
;
1126 err
= bind( socketRef
, (struct sockaddr
*) &addr
, sizeof( addr
) );
1127 check_errno( err
, errno
);
1129 dlog( kDebugLevelVerbose
, DEBUG_NAME
"setting up socket done (%s, %u.%u.%u.%u:%u, %d)\n",
1130 inAddr
->ifa_name
, ip
.b
[ 0 ], ip
.b
[ 1 ], ip
.b
[ 2 ], ip
.b
[ 3 ], ntohs( inPort
.NotAnInteger
), socketRef
);
1134 // Bind to the interface address and multicast DNS port.
1136 ip
.NotAnInteger
= ipv4
->sin_addr
.s_addr
;
1137 memset( &addr
, 0, sizeof( addr
) );
1138 addr
.sin_family
= AF_INET
;
1139 addr
.sin_port
= MulticastDNSPort
.NotAnInteger
;
1140 addr
.sin_addr
.s_addr
= ip
.NotAnInteger
;
1141 err
= bind( socketRef
, (struct sockaddr
*) &addr
, sizeof( addr
) );
1142 check_errno( err
, errno
);
1144 // Direct multicast packets to the specified interface.
1146 addr
.sin_addr
.s_addr
= ip
.NotAnInteger
;
1147 err
= setsockopt( socketRef
, IPPROTO_IP
, IP_MULTICAST_IF
, (char *) &addr
.sin_addr
, sizeof( addr
.sin_addr
) );
1148 check_errno( err
, errno
);
1150 // Set the TTL of outgoing unicast packets to 255 (helps against spoofing).
1153 err
= setsockopt( socketRef
, IPPROTO_IP
, IP_TTL
, (char *) &option
, sizeof( option
) );
1154 check_errno( err
, errno
);
1156 // Set the TTL of outgoing multicast packets to 255 (helps against spoofing).
1159 err
= setsockopt( socketRef
, IPPROTO_IP
, IP_MULTICAST_TTL
, (char *) &optionByte
, sizeof( optionByte
) );
1160 check_errno( err
, errno
);
1162 // WARNING: Setting this option causes unicast responses to be routed to the wrong interface so they are
1163 // WARNING: disabled. These options were only hints to improve 802.11 performance (and not implemented) anyway.
1166 // Mark packets as high-throughput/low-delay (i.e. lowest reliability) to maximize 802.11 multicast rate.
1168 option
= IPTOS_LOWDELAY
| IPTOS_THROUGHPUT
;
1169 err
= setsockopt( socketRef
, IPPROTO_IP
, IP_TOS
, (char *) &option
, sizeof( option
) );
1170 check_errno( err
, errno
);
1173 dlog( kDebugLevelVerbose
, DEBUG_NAME
"setting up sending socket done (%s, %u.%u.%u.%u, %d)\n",
1174 inAddr
->ifa_name
, ip
.b
[ 0 ], ip
.b
[ 1 ], ip
.b
[ 2 ], ip
.b
[ 3 ], socketRef
);
1179 *outSocketRef
= socketRef
;
1180 socketRef
= kInvalidSocketRef
;
1181 err
= mStatus_NoError
;
1184 if( socketRef
!= kInvalidSocketRef
)
1193 #pragma mark == Commands ==
1196 //===========================================================================================================================
1198 //===========================================================================================================================
1200 mDNSlocal mStatus
SetupCommandPipe( mDNS
* const inMDNS
)
1204 // Clean up any leftover command pipe.
1206 TearDownCommandPipe( inMDNS
);
1208 // Create the pipe device and open it.
1210 pipeDevCreate( kMDNSPipeName
, kMDNSPipeMessageQueueSize
, kMDNSPipeMessageSize
);
1212 inMDNS
->p
->commandPipe
= open( kMDNSPipeName
, O_RDWR
, 0 );
1213 require_errno_action( inMDNS
->p
->commandPipe
, errno
, exit
, err
= mStatus_UnsupportedErr
);
1215 err
= mStatus_NoError
;
1221 //===========================================================================================================================
1222 // TearDownCommandPipe
1223 //===========================================================================================================================
1225 mDNSlocal mStatus
TearDownCommandPipe( mDNS
* const inMDNS
)
1227 if( inMDNS
->p
->commandPipe
!= ERROR
)
1229 close( inMDNS
->p
->commandPipe
);
1230 inMDNS
->p
->commandPipe
= ERROR
;
1232 return( mStatus_NoError
);
1235 //===========================================================================================================================
1237 //===========================================================================================================================
1239 mDNSlocal mStatus
SendCommand( const mDNS
* const inMDNS
, MDNSPipeCommandCode inCommandCode
)
1243 require_action( inMDNS
->p
->commandPipe
!= ERROR
, exit
, err
= mStatus_NotInitializedErr
);
1245 err
= write( inMDNS
->p
->commandPipe
, &inCommandCode
, sizeof( inCommandCode
) );
1246 require_errno( err
, errno
, exit
);
1248 err
= mStatus_NoError
;
1254 //===========================================================================================================================
1256 //===========================================================================================================================
1258 mDNSlocal mStatus
ProcessCommand( mDNS
* const inMDNS
)
1261 MDNSPipeCommandCode commandCode
;
1263 require_action( inMDNS
->p
->commandPipe
!= ERROR
, exit
, err
= mStatus_NotInitializedErr
);
1265 // Read the command code from the pipe and dispatch it.
1267 err
= read( inMDNS
->p
->commandPipe
, &commandCode
, sizeof( commandCode
) );
1268 require_errno( err
, errno
, exit
);
1270 switch( commandCode
)
1272 case kMDNSPipeCommandCodeReschedule
:
1274 // Reschedule event. Do nothing here, but this will cause mDNS_Execute to run before waiting again.
1276 dlog( kDebugLevelChatty
, DEBUG_NAME
"reschedule\n" );
1279 case kMDNSPipeCommandCodeReconfigure
:
1280 ProcessCommandReconfigure( inMDNS
);
1283 case kMDNSPipeCommandCodeQuit
:
1285 // Quit requested. Set quit flag and bump the config ID to let the thread exit normally.
1287 dlog( kDebugLevelVerbose
, DEBUG_NAME
"processing pipe quit command\n" );
1288 inMDNS
->p
->quit
= mDNStrue
;
1289 ++inMDNS
->p
->configID
;
1293 dlog( kDebugLevelError
, DEBUG_NAME
"unknown pipe command code (code=0x%08X)\n", commandCode
);
1294 err
= mStatus_BadParamErr
;
1298 err
= mStatus_NoError
;
1304 //===========================================================================================================================
1305 // ProcessCommandReconfigure
1306 //===========================================================================================================================
1308 mDNSlocal
void ProcessCommandReconfigure( mDNS
*inMDNS
)
1312 dlog( kDebugLevelVerbose
, DEBUG_NAME
"processing pipe reconfigure command\n" );
1314 // Tear down the existing interfaces and set up new ones using the new IP info.
1316 mDNSPlatformLock( inMDNS
);
1318 err
= TearDownInterfaceList( inMDNS
);
1321 err
= SetupInterfaceList( inMDNS
);
1324 mDNSPlatformUnlock( inMDNS
);
1326 // Inform clients of the change.
1328 if( inMDNS
->MainCallback
)
1330 inMDNS
->MainCallback( inMDNS
, mStatus_ConfigChanged
);
1333 // Force mDNS to update.
1335 mDNSCoreMachineSleep( inMDNS
, mDNSfalse
);
1337 // Bump the config ID so the main processing loop detects the configuration change.
1339 ++inMDNS
->p
->configID
;
1344 #pragma mark == Threads ==
1347 //===========================================================================================================================
1349 //===========================================================================================================================
1351 mDNSlocal mStatus
SetupTask( mDNS
* const inMDNS
)
1356 dlog( kDebugLevelVerbose
, DEBUG_NAME
"setting up thread\n" );
1359 // Create our main thread. Note: The task will save off its ID in the globals. We cannot do it here because the
1360 // task invokes code that needs it and the task may begin execution before taskSpawn returns the task ID.
1361 // This also means code in this thread context cannot rely on the task ID until the task has fully initialized.
1363 task
= taskSpawn( kMDNSTaskName
, kMDNSTaskPriority
, 0, kMDNSTaskStackSize
, (FUNCPTR
) Task
,
1364 (int) inMDNS
, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
1365 require_action( task
!= ERROR
, exit
, err
= mStatus_NoMemoryErr
);
1367 err
= mStatus_NoError
;
1370 dlog( kDebugLevelVerbose
, DEBUG_NAME
"setting up thread done (err=%ld, id=%d)\n", err
, task
);
1374 //===========================================================================================================================
1376 //===========================================================================================================================
1378 mDNSlocal mStatus
TearDownTask( mDNS
* const inMDNS
)
1382 dlog( kDebugLevelVerbose
, DEBUG_NAME
"tearing down thread\n" );
1385 // Send a quit command to cause the thread to exit.
1387 SendCommand( inMDNS
, kMDNSPipeCommandCodeQuit
);
1389 // Wait for the thread to signal it has exited. Timeout in 10 seconds to handle a hung thread.
1391 if( inMDNS
->p
->quitEvent
)
1393 err
= semTake( inMDNS
->p
->quitEvent
, sysClkRateGet() * 10 );
1396 err
= mStatus_NoError
;
1398 dlog( kDebugLevelVerbose
, DEBUG_NAME
"tearing down thread done (err=%ld)\n", err
);
1402 //===========================================================================================================================
1404 //===========================================================================================================================
1406 mDNSlocal
void Task( mDNS
*inMDNS
)
1410 MDNSInterfaceItem
* item
;
1413 struct timeval timeout
;
1415 dlog( kDebugLevelVerbose
, DEBUG_NAME
"task starting\n" );
1418 // Set up everything up.
1420 err
= TaskInit( inMDNS
);
1421 require_noerr( err
, exit
);
1423 // Main Processing Loop.
1425 while( !inMDNS
->p
->quit
)
1427 // Set up the read set here to avoid the overhead of setting it up each iteration of the main processing loop.
1428 // If the configuration changes, the server ID will be bumped, causing this code to set up the read set again.
1430 TaskSetupReadSet( inMDNS
, &allReadSet
, &maxSocket
);
1431 configID
= inMDNS
->p
->configID
;
1432 dlog( kDebugLevelVerbose
, DEBUG_NAME
"task starting processing loop (configID=%ld)\n", configID
);
1434 while( configID
== inMDNS
->p
->configID
)
1436 mDNSs32 nextTaskTime
;
1440 // Give the mDNS core a chance to do its work. Reset the rescheduled flag before calling mDNS_Execute
1441 // so anything that needs processing during or after causes a re-schedule to wake up the thread. The
1442 // reschedule flag is set to 1 after processing a waking up to prevent redundant reschedules while
1443 // processing packets. This introduces a window for a race condition because the thread wake-up and
1444 // reschedule set are not atomic, but this would be benign. Even if the reschedule flag is "corrupted"
1445 // like this, it would only result in a redundant reschedule since it will loop back to mDNS_Execute.
1447 inMDNS
->p
->rescheduled
= 0;
1448 nextTaskTime
= mDNS_Execute( inMDNS
);
1449 TaskSetupTimeout( nextTaskTime
, &timeout
);
1451 // Wait until something occurs (e.g. command, incoming packet, or timeout).
1453 readSet
= allReadSet
;
1454 n
= select( maxSocket
+ 1, &readSet
, NULL
, NULL
, &timeout
);
1455 inMDNS
->p
->rescheduled
= 1;
1456 check_errno( n
, errno
);
1457 dlog( kDebugLevelChatty
- 1, DEBUG_NAME
"task select result = %d\n", n
);
1460 // Next task timeout occurred. Loop back up to give mDNS core a chance to work.
1462 dlog( kDebugLevelChatty
, DEBUG_NAME
"next task timeout occurred (%ld)\n", mDNSPlatformTimeNow() );
1466 // Scan the read set to determine if any sockets have something pending and process them.
1469 for( item
= inMDNS
->p
->interfaceList
; item
; item
= item
->next
)
1471 if( FD_ISSET( item
->multicastSocketRef
, &readSet
) )
1473 TaskProcessPacket( inMDNS
, item
, item
->multicastSocketRef
);
1478 // Check for a pending command and process it.
1480 if( FD_ISSET( inMDNS
->p
->commandPipe
, &readSet
) )
1482 ProcessCommand( inMDNS
);
1490 // Signal we've quit.
1492 check( inMDNS
->p
->quitEvent
);
1493 semGive( inMDNS
->p
->quitEvent
);
1495 dlog( kDebugLevelInfo
, DEBUG_NAME
"task ended\n" );
1498 //===========================================================================================================================
1500 //===========================================================================================================================
1502 mDNSlocal mStatus
TaskInit( mDNS
*inMDNS
)
1506 dlog( kDebugLevelVerbose
, DEBUG_NAME
"task init\n" );
1507 check( inMDNS
->p
->readyEvent
);
1509 inMDNS
->p
->task
= taskIdSelf();
1511 err
= SetupCommandPipe( inMDNS
);
1512 require_noerr( err
, exit
);
1514 SetupNames( inMDNS
);
1516 err
= SetupInterfaceList( inMDNS
);
1517 require_noerr( err
, exit
);
1520 // Signal the "ready" semaphore to indicate the task initialization code has completed (success or not).
1522 inMDNS
->p
->taskInitErr
= err
;
1523 semGive( inMDNS
->p
->readyEvent
);
1525 dlog( kDebugLevelVerbose
, DEBUG_NAME
"task init done (err=%ld)\n", err
);
1529 //===========================================================================================================================
1531 //===========================================================================================================================
1533 mDNSlocal
void TaskSetupReadSet( mDNS
*inMDNS
, fd_set
*outReadSet
, int *outMaxSocket
)
1535 MDNSInterfaceItem
* item
;
1538 dlog( kDebugLevelVerbose
, DEBUG_NAME
"task setting up read set\n" );
1540 check( outReadSet
);
1541 check( outMaxSocket
);
1543 // Initialize the read set. Default the max socket to -1 so "maxSocket + 1" (as needed by select) is zero. This
1544 // should never happen since we should always have at least one interface, but it's just to be safe.
1546 FD_ZERO( outReadSet
);
1549 // Add all the receiving sockets to the read set.
1551 for( item
= inMDNS
->p
->interfaceList
; item
; item
= item
->next
)
1553 FD_SET( item
->multicastSocketRef
, outReadSet
);
1554 if( item
->multicastSocketRef
> maxSocket
)
1556 maxSocket
= item
->multicastSocketRef
;
1560 // Add the command pipe to the read set.
1562 FD_SET( inMDNS
->p
->commandPipe
, outReadSet
);
1563 if( inMDNS
->p
->commandPipe
> maxSocket
)
1565 maxSocket
= inMDNS
->p
->commandPipe
;
1567 check( maxSocket
> 0 );
1568 *outMaxSocket
= maxSocket
;
1570 dlog( kDebugLevelVerbose
, DEBUG_NAME
"task setting up read set done (maxSocket=%d)\n", maxSocket
);
1573 //===========================================================================================================================
1575 //===========================================================================================================================
1577 mDNSlocal
void TaskSetupTimeout( mDNSs32 inNextTaskTime
, struct timeval
*outTimeout
)
1581 // Calculate how long to wait before performing idle processing.
1583 delta
= inNextTaskTime
- mDNSPlatformTimeNow();
1586 // The next task time is now or in the past. Set the timeout to fire immediately.
1588 outTimeout
->tv_sec
= 0;
1589 outTimeout
->tv_usec
= 0;
1593 // Calculate the seconds and microseconds until the timeout should occur. Add one to the ticks remainder
1594 // before multiplying to account for integer rounding error and avoid firing the timeout too early.
1596 outTimeout
->tv_sec
= delta
/ mDNSPlatformOneSecond
;
1597 outTimeout
->tv_usec
= ( ( delta
% mDNSPlatformOneSecond
) + 1 ) * gMDNSTicksToMicrosecondsMultiplier
;
1599 // Check if the microseconds is more than 1 second. If so, bump the seconds instead.
1601 if( outTimeout
->tv_usec
>= 1000000L )
1603 outTimeout
->tv_sec
+= 1;
1604 outTimeout
->tv_usec
= 0;
1608 dlog( kDebugLevelChatty
, DEBUG_NAME
"next task in %ld:%ld seconds (%ld)\n",
1609 outTimeout
->tv_sec
, outTimeout
->tv_usec
, inNextTaskTime
);
1611 //===========================================================================================================================
1612 // TaskProcessPacket
1613 //===========================================================================================================================
1615 mDNSlocal
void TaskProcessPacket( mDNS
*inMDNS
, MDNSInterfaceItem
*inItem
, MDNSSocketRef inSocketRef
)
1619 struct sockaddr_in addr
;
1621 mDNSu8
* packetEndPtr
;
1627 // Receive the packet.
1629 addrSize
= sizeof( addr
);
1630 n
= recvfrom( inSocketRef
, (char *) &packet
, sizeof( packet
), 0, (struct sockaddr
*) &addr
, &addrSize
);
1634 // Set up the src/dst/interface info.
1636 srcAddr
.type
= mDNSAddrType_IPv4
;
1637 srcAddr
.ip
.v4
.NotAnInteger
= addr
.sin_addr
.s_addr
;
1638 srcPort
.NotAnInteger
= addr
.sin_port
;
1639 dstAddr
.type
= mDNSAddrType_IPv4
;
1640 dstAddr
.ip
.v4
= AllDNSLinkGroup
;
1641 dstPort
= MulticastDNSPort
;
1643 dlog( kDebugLevelChatty
, DEBUG_NAME
"packet received\n" );
1644 dlog( kDebugLevelChatty
, DEBUG_NAME
" size = %d\n", n
);
1645 dlog( kDebugLevelChatty
, DEBUG_NAME
" src = %u.%u.%u.%u:%hu\n",
1646 srcAddr
.ip
.v4
.b
[ 0 ], srcAddr
.ip
.v4
.b
[ 1 ], srcAddr
.ip
.v4
.b
[ 2 ], srcAddr
.ip
.v4
.b
[ 3 ],
1647 ntohs( srcPort
.NotAnInteger
) );
1648 dlog( kDebugLevelChatty
, DEBUG_NAME
" dst = %u.%u.%u.%u:%hu\n",
1649 dstAddr
.ip
.v4
.b
[ 0 ], dstAddr
.ip
.v4
.b
[ 1 ], dstAddr
.ip
.v4
.b
[ 2 ], dstAddr
.ip
.v4
.b
[ 3 ],
1650 ntohs( dstPort
.NotAnInteger
) );
1651 dlog( kDebugLevelChatty
, DEBUG_NAME
" interface = 0x%08X\n", (int) inItem
->hostSet
.InterfaceID
);
1652 dlog( kDebugLevelChatty
, DEBUG_NAME
"--\n" );
1654 // Dispatch the packet to mDNS.
1656 packetEndPtr
= ( (mDNSu8
*) &packet
) + n
;
1657 mDNSCoreReceive( inMDNS
, &packet
, packetEndPtr
, &srcAddr
, srcPort
, &dstAddr
, dstPort
, inItem
->hostSet
.InterfaceID
, 255 );
1662 inItem
->recvCounter
+= 1;
1663 inItem
->recvErrorCounter
+= ( n
< 0 );
1668 #pragma mark == Utilities ==
1671 #if( TARGET_NON_APPLE )
1672 //===========================================================================================================================
1673 // GenerateUniqueHostName
1675 // Non-Apple platform stub routine to generate a unique name for the device. Should be implemented to return a unique name.
1676 //===========================================================================================================================
1678 mDNSlocal
void GenerateUniqueHostName( char *outName
, long *ioSeed
)
1680 DEBUG_UNUSED( ioSeed
);
1682 // $$$ Non-Apple Platforms: Fill in appropriate name for device.
1684 mDNSPlatformStrCopy( kMDNSDefaultName
, outName
);
1687 //===========================================================================================================================
1688 // GenerateUniqueDNSName
1690 // Non-Apple platform stub routine to generate a unique RFC 1034-compatible DNS name for the device. Should be
1691 // implemented to return a unique name.
1692 //===========================================================================================================================
1694 mDNSlocal
void GenerateUniqueDNSName( char *outName
, long *ioSeed
)
1696 DEBUG_UNUSED( ioSeed
);
1698 // $$$ Non-Apple Platforms: Fill in appropriate DNS name for device.
1700 mDNSPlatformStrCopy( kMDNSDefaultName
, outName
);
1708 //===========================================================================================================================
1710 //===========================================================================================================================
1712 int getifaddrs( struct ifaddrs
**outAddrs
)
1715 struct ifaddrs
* head
;
1716 struct ifaddrs
** next
;
1717 struct ifaddrs
* ifa
;
1720 char ipString
[ INET_ADDR_LEN
];
1729 ifp
= ifIndexToIfp( i
);
1736 // Allocate and initialize the ifaddrs structure and attach it to the linked list.
1738 ifa
= (struct ifaddrs
*) calloc( 1, sizeof( struct ifaddrs
) );
1739 require_action( ifa
, exit
, err
= ENOMEM
);
1742 next
= &ifa
->ifa_next
;
1746 ifa
->ifa_name
= (char *) malloc( 16 );
1747 require_action( ifa
->ifa_name
, exit
, err
= ENOMEM
);
1749 n
= sprintf( ifa
->ifa_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
1750 require_action( n
< 16, exit
, err
= ENOBUFS
);
1752 // Fetch the address.
1754 ifa
->ifa_addr
= (struct sockaddr
*) calloc( 1, sizeof( struct sockaddr_in
) );
1755 require_action( ifa
->ifa_addr
, exit
, err
= ENOMEM
);
1757 ipString
[ 0 ] = '\0';
1758 #if( TARGET_NON_APPLE )
1759 err
= ifAddrGet( ifa
->ifa_name
, ipString
);
1760 require_noerr( err
, exit
);
1762 err
= ifAddrGetNonAlias( ifa
->ifa_name
, ipString
);
1763 require_noerr( err
, exit
);
1766 err
= sock_pton( ipString
, AF_INET
, ifa
->ifa_addr
, 0, NULL
);
1767 require_noerr( err
, exit
);
1771 ifa
->ifa_flags
= ifp
->if_flags
;
1786 freeifaddrs( head
);
1791 //===========================================================================================================================
1793 //===========================================================================================================================
1795 void freeifaddrs( struct ifaddrs
*inAddrs
)
1800 // Free each piece of the structure. Set to null after freeing to handle macro-aliased fields.
1802 for( p
= inAddrs
; p
; p
= q
)
1808 free( p
->ifa_name
);
1813 free( p
->ifa_addr
);
1816 if( p
->ifa_netmask
)
1818 free( p
->ifa_netmask
);
1819 p
->ifa_netmask
= NULL
;
1821 if( p
->ifa_dstaddr
)
1823 free( p
->ifa_dstaddr
);
1824 p
->ifa_dstaddr
= NULL
;
1828 free( p
->ifa_data
);
1835 //===========================================================================================================================
1837 //===========================================================================================================================
1839 int sock_pton( const char *inString
, int inFamily
, void *outAddr
, size_t inAddrSize
, size_t *outAddrSize
)
1843 if( inFamily
== AF_INET
)
1845 struct sockaddr_in
* ipv4
;
1847 if( inAddrSize
== 0 )
1849 inAddrSize
= sizeof( struct sockaddr_in
);
1851 if( inAddrSize
< sizeof( struct sockaddr_in
) )
1857 ipv4
= (struct sockaddr_in
*) outAddr
;
1858 err
= inet_aton( (char *) inString
, &ipv4
->sin_addr
);
1861 ipv4
->sin_family
= AF_INET
;
1864 *outAddrSize
= sizeof( struct sockaddr_in
);
1868 #if( defined( AF_INET6 ) )
1869 else if( inFamily
== AF_INET6
) // $$$ TO DO: Add IPv6 support.
1885 //===========================================================================================================================
1887 //===========================================================================================================================
1889 char * sock_ntop( const void *inAddr
, size_t inAddrSize
, char *inBuffer
, size_t inBufferSize
)
1891 const struct sockaddr
* addr
;
1893 addr
= (const struct sockaddr
*) inAddr
;
1894 if( addr
->sa_family
== AF_INET
)
1896 struct sockaddr_in
* ipv4
;
1898 if( inAddrSize
== 0 )
1900 inAddrSize
= sizeof( struct sockaddr_in
);
1902 if( inAddrSize
< sizeof( struct sockaddr_in
) )
1908 if( inBufferSize
< 16 )
1915 ipv4
= (struct sockaddr_in
*) addr
;
1916 inet_ntoa_b( ipv4
->sin_addr
, inBuffer
);
1918 #if( defined( AF_INET6 ) )
1919 else if( addr
->sa_family
== AF_INET6
) // $$$ TO DO: Add IPv6 support.
1921 errno
= EAFNOSUPPORT
;
1928 errno
= EAFNOSUPPORT
;
1939 #pragma mark == Debugging ==
1944 void mDNSShow( BOOL inShowRecords
);
1945 void mDNSShowRecords( void );
1946 void mDNSShowTXT( const void *inTXT
, size_t inTXTSize
);
1948 //===========================================================================================================================
1950 //===========================================================================================================================
1952 void mDNSShow( BOOL inShowRecords
)
1954 MDNSInterfaceItem
* item
;
1960 printf( "### mDNS not initialized\n" );
1966 printf( "\n-- mDNS globals --\n" );
1967 printf( " sizeof( mDNS ) = %d\n", (int) sizeof( mDNS
) );
1968 printf( " sizeof( ResourceRecord ) = %d\n", (int) sizeof( ResourceRecord
) );
1969 printf( " sizeof( AuthRecord ) = %d\n", (int) sizeof( AuthRecord
) );
1970 printf( " sizeof( CacheRecord ) = %d\n", (int) sizeof( CacheRecord
) );
1971 printf( " gMDNSPtr = 0x%08lX\n", (unsigned long) gMDNSPtr
);
1972 printf( " gMDNSTicksToMicrosecondsMultiplier = %ld\n", gMDNSTicksToMicrosecondsMultiplier
);
1973 printf( " lockID = 0x%08lX\n", (unsigned long) gMDNSPtr
->p
->lockID
);
1974 printf( " readyEvent = 0x%08lX\n", (unsigned long) gMDNSPtr
->p
->readyEvent
);
1975 printf( " taskInitErr = %ld\n", gMDNSPtr
->p
->taskInitErr
);
1976 printf( " quitEvent = 0x%08lX\n", (unsigned long) gMDNSPtr
->p
->quitEvent
);
1977 printf( " commandPipe = %d\n", gMDNSPtr
->p
->commandPipe
);
1978 printf( " task = 0x%08lX\n", (unsigned long) gMDNSPtr
->p
->task
);
1979 printf( " quit = %d\n", gMDNSPtr
->p
->quit
);
1980 printf( " configID = %ld\n", gMDNSPtr
->p
->configID
);
1981 printf( " rescheduled = %d\n", gMDNSPtr
->p
->rescheduled
);
1982 printf( " nicelabel = \"%.*s\"\n", gMDNSPtr
->nicelabel
.c
[ 0 ], (char *) &gMDNSPtr
->nicelabel
.c
[ 1 ] );
1983 printf( " hostLabel = \"%.*s\"\n", gMDNSPtr
->hostlabel
.c
[ 0 ], (char *) &gMDNSPtr
->hostlabel
.c
[ 1 ] );
1988 printf( "\n-- mDNS interfaces --\n" );
1990 for( item
= gMDNSPtr
->p
->interfaceList
; item
; item
= item
->next
)
1992 printf( " -- interface %u --\n", n
);
1993 printf( " name = \"%s\"\n", item
->name
);
1994 printf( " multicastSocketRef = %d\n", item
->multicastSocketRef
);
1995 printf( " sendingSocketRef = %d\n", item
->sendingSocketRef
);
1996 ip
= item
->hostSet
.ip
;
1997 printf( " hostSet.ip = %u.%u.%u.%u\n", ip
.ip
.v4
.b
[ 0 ], ip
.ip
.v4
.b
[ 1 ],
1998 ip
.ip
.v4
.b
[ 2 ], ip
.ip
.v4
.b
[ 3 ] );
1999 printf( " hostSet.advertise = %s\n", item
->hostSet
.Advertise
? "YES" : "NO" );
2000 printf( " hostRegistered = %s\n", item
->hostRegistered
? "YES" : "NO" );
2002 printf( " sendMulticastCounter = %d\n", item
->sendMulticastCounter
);
2003 printf( " sendUnicastCounter = %d\n", item
->sendUnicastCounter
);
2004 printf( " sendErrorCounter = %d\n", item
->sendErrorCounter
);
2005 printf( " recvCounter = %d\n", item
->recvCounter
);
2006 printf( " recvErrorCounter = %d\n", item
->recvErrorCounter
);
2007 printf( " recvLoopCounter = %d\n", item
->recvLoopCounter
);
2020 //===========================================================================================================================
2022 //===========================================================================================================================
2024 void mDNSShowRecords( void )
2026 MDNSInterfaceItem
* item
;
2028 AuthRecord
* record
;
2029 char name
[ MAX_ESCAPED_DOMAIN_NAME
];
2031 printf( "\n-- mDNS resource records --\n" );
2033 for( record
= gMDNSPtr
->ResourceRecords
; record
; record
= record
->next
)
2035 item
= (MDNSInterfaceItem
*) record
->resrec
.InterfaceID
;
2036 ConvertDomainNameToCString( &record
->resrec
.name
, name
);
2037 printf( " -- record %d --\n", n
);
2038 printf( " interface = 0x%08X (%s)\n", (int) item
, item
? item
->name
: "<any>" );
2039 printf( " name = \"%s\"\n", name
);
2046 //===========================================================================================================================
2048 //===========================================================================================================================
2050 void mDNSShowTXT( const void *inTXT
, size_t inTXTSize
)
2057 printf( "\nTXT record (%u bytes):\n\n", inTXTSize
);
2059 p
= (const mDNSu8
*) inTXT
;
2060 end
= p
+ inTXTSize
;
2066 if( ( p
+ size
) > end
)
2068 printf( "\n### MALFORMED TXT RECORD (length byte too big for record)\n\n" );
2071 printf( "%2d (%3d bytes): \"%.*s\"\n", i
, size
, size
, p
);