1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2009 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #pragma warning(disable:4995)
28 #include "DNSSDService.h"
30 #include "DNSSDEventManager.h"
32 #include "DNSSDRecord.h"
34 #include "TXTRecord.h"
36 #include "StringServices.h"
38 #include <DebugServices.h>
44 #define WM_SOCKET (WM_APP + 100)
54 BOOL
CDNSSDService::m_registeredWindowClass
= FALSE
;
56 HWND
CDNSSDService::m_hiddenWindow
= NULL
;
58 CDNSSDService::SocketMap
CDNSSDService::m_socketMap
;
64 HRESULT
CDNSSDService::FinalConstruct()
68 DNSServiceErrorType err
= 0;
76 err
= DNSServiceCreateConnection( &m_primary
);
78 require_action( !err
, exit
, hr
= E_FAIL
);
82 if ( !m_hiddenWindow
)
86 TCHAR windowClassName
[ 256 ];
90 StringCchPrintf( windowClassName
, sizeof( windowClassName
) / sizeof( TCHAR
), TEXT( "Bonjour Hidden Window %d" ), GetProcessId( NULL
) );
94 if ( !m_registeredWindowClass
)
106 wc
.lpfnWndProc
= WndProc
;
118 wc
.hbrBackground
= NULL
;
120 wc
.lpszMenuName
= NULL
;
122 wc
.lpszClassName
= windowClassName
;
126 atom
= RegisterClass(&wc
);
128 require_action( atom
!= NULL
, exit
, hr
= E_FAIL
);
132 m_registeredWindowClass
= TRUE
;
138 m_hiddenWindow
= CreateWindow( windowClassName
, windowClassName
, WS_OVERLAPPED
, 0, 0, 0, 0, NULL
, NULL
, GetModuleHandle( NULL
), NULL
);
140 require_action( m_hiddenWindow
!= NULL
, exit
, hr
= E_FAIL
);
146 err
= WSAAsyncSelect( DNSServiceRefSockFD( m_primary
), m_hiddenWindow
, WM_SOCKET
, FD_READ
);
148 require_action( !err
, exit
, hr
= E_FAIL
);
152 m_socketMap
[ DNSServiceRefSockFD( m_primary
) ] = this;
168 void CDNSSDService::FinalRelease()
172 dlog( kDebugLevelTrace
, "FinalRelease()\n" );
182 STDMETHODIMP
CDNSSDService::EnumerateDomains(DNSSDFlags flags
, ULONG ifIndex
, IDNSSDEventManager
*eventManager
, IDNSSDService
**service
)
186 CComObject
<CDNSSDService
> * object
= NULL
;
188 DNSServiceRef subord
= NULL
;
190 DNSServiceErrorType err
= 0;
210 object
= new CComObject
<CDNSSDService
>();
224 require_action( object
!= NULL
, exit
, err
= kDNSServiceErr_NoMemory
);
232 err
= DNSServiceEnumerateDomains( &subord
, flags
| kDNSServiceFlagsShareConnection
, ifIndex
, ( DNSServiceDomainEnumReply
) &DomainEnumReply
, object
);
234 require_noerr( err
, exit
);
238 object
->SetPrimaryRef( m_primary
);
240 object
->SetSubordRef( subord
);
242 object
->SetEventManager( eventManager
);
272 STDMETHODIMP
CDNSSDService::Browse(DNSSDFlags flags
, ULONG ifIndex
, BSTR regtype
, BSTR domain
, IDNSSDEventManager
* eventManager
, IDNSSDService
** service
)
276 CComObject
<CDNSSDService
> * object
= NULL
;
278 std::string regtypeUTF8
;
280 std::string domainUTF8
;
282 DNSServiceRef subord
= NULL
;
284 DNSServiceErrorType err
= 0;
302 // Convert BSTR params to utf8
304 ok
= BSTRToUTF8( regtype
, regtypeUTF8
);
306 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
308 ok
= BSTRToUTF8( domain
, domainUTF8
);
310 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
318 object
= new CComObject
<CDNSSDService
>();
332 require_action( object
!= NULL
, exit
, err
= kDNSServiceErr_NoMemory
);
340 err
= DNSServiceBrowse( &subord
, flags
| kDNSServiceFlagsShareConnection
, ifIndex
, regtypeUTF8
.c_str(), ( domainUTF8
.size() > 0 ) ? domainUTF8
.c_str() : NULL
, ( DNSServiceBrowseReply
) &BrowseReply
, object
);
342 require_noerr( err
, exit
);
346 object
->SetPrimaryRef( m_primary
);
348 object
->SetSubordRef( subord
);
350 object
->SetEventManager( eventManager
);
380 STDMETHODIMP
CDNSSDService::Resolve(DNSSDFlags flags
, ULONG ifIndex
, BSTR serviceName
, BSTR regType
, BSTR domain
, IDNSSDEventManager
* eventManager
, IDNSSDService
** service
)
384 CComObject
<CDNSSDService
> * object
= NULL
;
386 std::string serviceNameUTF8
;
388 std::string regTypeUTF8
;
390 std::string domainUTF8
;
392 DNSServiceRef subord
= NULL
;
394 DNSServiceErrorType err
= 0;
412 // Convert BSTR params to utf8
414 ok
= BSTRToUTF8( serviceName
, serviceNameUTF8
);
416 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
418 ok
= BSTRToUTF8( regType
, regTypeUTF8
);
420 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
422 ok
= BSTRToUTF8( domain
, domainUTF8
);
424 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
432 object
= new CComObject
<CDNSSDService
>();
446 require_action( object
!= NULL
, exit
, err
= kDNSServiceErr_NoMemory
);
454 err
= DNSServiceResolve( &subord
, flags
| kDNSServiceFlagsShareConnection
, ifIndex
, serviceNameUTF8
.c_str(), regTypeUTF8
.c_str(), domainUTF8
.c_str(), ( DNSServiceResolveReply
) &ResolveReply
, object
);
456 require_noerr( err
, exit
);
460 object
->SetPrimaryRef( m_primary
);
462 object
->SetSubordRef( subord
);
464 object
->SetEventManager( eventManager
);
494 STDMETHODIMP
CDNSSDService::Register(DNSSDFlags flags
, ULONG ifIndex
, BSTR serviceName
, BSTR regType
, BSTR domain
, BSTR host
, USHORT port
, ITXTRecord
*record
, IDNSSDEventManager
*eventManager
, IDNSSDService
**service
)
498 CComObject
<CDNSSDService
> * object
= NULL
;
500 std::string serviceNameUTF8
;
502 std::string regTypeUTF8
;
504 std::string domainUTF8
;
506 std::string hostUTF8
;
508 const void * txtRecord
= NULL
;
512 DNSServiceRef subord
= NULL
;
514 DNSServiceErrorType err
= 0;
532 // Convert BSTR params to utf8
534 ok
= BSTRToUTF8( serviceName
, serviceNameUTF8
);
536 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
538 ok
= BSTRToUTF8( regType
, regTypeUTF8
);
540 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
542 ok
= BSTRToUTF8( domain
, domainUTF8
);
544 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
546 ok
= BSTRToUTF8( host
, hostUTF8
);
548 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
556 object
= new CComObject
<CDNSSDService
>();
570 require_action( object
!= NULL
, exit
, err
= kDNSServiceErr_NoMemory
);
580 CComObject
< CTXTRecord
> * realTXTRecord
;
584 realTXTRecord
= ( CComObject
< CTXTRecord
>* ) record
;
588 txtRecord
= realTXTRecord
->GetBytes();
590 txtLen
= realTXTRecord
->GetLen();
598 err
= DNSServiceRegister( &subord
, flags
| kDNSServiceFlagsShareConnection
, ifIndex
, serviceNameUTF8
.c_str(), regTypeUTF8
.c_str(), ( domainUTF8
.size() > 0 ) ? domainUTF8
.c_str() : NULL
, hostUTF8
.c_str(), htons( port
), txtLen
, txtRecord
, ( DNSServiceRegisterReply
) &RegisterReply
, object
);
600 require_noerr( err
, exit
);
604 object
->SetPrimaryRef( m_primary
);
606 object
->SetSubordRef( subord
);
608 object
->SetEventManager( eventManager
);
638 STDMETHODIMP
CDNSSDService::QueryRecord(DNSSDFlags flags
, ULONG ifIndex
, BSTR fullname
, DNSSDRRType rrtype
, DNSSDRRClass rrclass
, IDNSSDEventManager
*eventManager
, IDNSSDService
**service
)
642 CComObject
<CDNSSDService
> * object
= NULL
;
644 DNSServiceRef subord
= NULL
;
646 std::string fullNameUTF8
;
648 DNSServiceErrorType err
= 0;
666 // Convert BSTR params to utf8
668 ok
= BSTRToUTF8( fullname
, fullNameUTF8
);
670 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
678 object
= new CComObject
<CDNSSDService
>();
692 require_action( object
!= NULL
, exit
, err
= kDNSServiceErr_NoMemory
);
700 err
= DNSServiceQueryRecord( &subord
, flags
| kDNSServiceFlagsShareConnection
, ifIndex
, fullNameUTF8
.c_str(), ( uint16_t ) rrtype
, ( uint16_t ) rrclass
, ( DNSServiceQueryRecordReply
) &QueryRecordReply
, object
);
702 require_noerr( err
, exit
);
706 object
->SetPrimaryRef( m_primary
);
708 object
->SetSubordRef( subord
);
710 object
->SetEventManager( eventManager
);
740 STDMETHODIMP
CDNSSDService::RegisterRecord(DNSSDFlags flags
, ULONG ifIndex
, BSTR fullName
, DNSSDRRType rrtype
, DNSSDRRClass rrclass
, VARIANT rdata
, ULONG ttl
, IDNSSDEventManager
* eventManager
, IDNSSDRecord
** record
)
744 CComObject
<CDNSSDRecord
> * object
= NULL
;
746 DNSRecordRef rref
= NULL
;
748 std::string fullNameUTF8
;
750 std::vector
< BYTE
> byteArray
;
752 const void * byteArrayPtr
= NULL
;
754 DNSServiceErrorType err
= 0;
772 // Convert BSTR params to utf8
774 ok
= BSTRToUTF8( fullName
, fullNameUTF8
);
776 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
780 // Convert the VARIANT
782 ok
= VariantToByteArray( &rdata
, byteArray
);
784 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
792 object
= new CComObject
<CDNSSDRecord
>();
806 require_action( object
!= NULL
, exit
, err
= kDNSServiceErr_NoMemory
);
812 err
= DNSServiceRegisterRecord( m_primary
, &rref
, flags
, ifIndex
, fullNameUTF8
.c_str(), rrtype
, rrclass
, ( uint16_t ) byteArray
.size(), byteArray
.size() > 0 ? &byteArray
[ 0 ] : NULL
, ttl
, &RegisterRecordReply
, object
);
814 require_noerr( err
, exit
);
818 object
->SetServiceObject( this );
820 object
->SetRecordRef( rref
);
822 this->SetEventManager( eventManager
);
852 STDMETHODIMP
CDNSSDService::AddRecord(DNSSDFlags flags
, DNSSDRRType rrtype
, VARIANT rdata
, ULONG ttl
, IDNSSDRecord
** record
)
856 CComObject
<CDNSSDRecord
> * object
= NULL
;
858 DNSRecordRef rref
= NULL
;
860 std::vector
< BYTE
> byteArray
;
862 const void * byteArrayPtr
= NULL
;
864 DNSServiceErrorType err
= 0;
882 // Convert the VARIANT
884 ok
= VariantToByteArray( &rdata
, byteArray
);
886 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
894 object
= new CComObject
<CDNSSDRecord
>();
908 require_action( object
!= NULL
, exit
, err
= kDNSServiceErr_NoMemory
);
914 err
= DNSServiceAddRecord( m_primary
, &rref
, flags
, rrtype
, ( uint16_t ) byteArray
.size(), byteArray
.size() > 0 ? &byteArray
[ 0 ] : NULL
, ttl
);
916 require_noerr( err
, exit
);
920 object
->SetServiceObject( this );
922 object
->SetRecordRef( rref
);
950 STDMETHODIMP
CDNSSDService::ReconfirmRecord(DNSSDFlags flags
, ULONG ifIndex
, BSTR fullName
, DNSSDRRType rrtype
, DNSSDRRClass rrclass
, VARIANT rdata
)
954 std::string fullNameUTF8
;
956 std::vector
< BYTE
> byteArray
;
958 const void * byteArrayPtr
= NULL
;
960 DNSServiceErrorType err
= 0;
968 // Convert BSTR params to utf8
970 ok
= BSTRToUTF8( fullName
, fullNameUTF8
);
972 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
976 // Convert the VARIANT
978 ok
= VariantToByteArray( &rdata
, byteArray
);
980 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
984 err
= DNSServiceReconfirmRecord( flags
, ifIndex
, fullNameUTF8
.c_str(), rrtype
, rrclass
, ( uint16_t ) byteArray
.size(), byteArray
.size() > 0 ? &byteArray
[ 0 ] : NULL
);
986 require_noerr( err
, exit
);
1002 STDMETHODIMP
CDNSSDService::GetProperty(BSTR prop
, VARIANT
* value
)
1006 std::string propUTF8
;
1008 std::vector
< BYTE
> byteArray
;
1010 SAFEARRAY
* psa
= NULL
;
1012 BYTE
* pData
= NULL
;
1016 DNSServiceErrorType err
= 0;
1022 // Convert BSTR params to utf8
1024 ok
= BSTRToUTF8( prop
, propUTF8
);
1026 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
1030 // Setup the byte array
1032 require_action( V_VT( value
) == ( VT_ARRAY
|VT_UI1
), exit
, err
= kDNSServiceErr_Unknown
);
1034 psa
= V_ARRAY( value
);
1036 require_action( psa
, exit
, err
= kDNSServiceErr_Unknown
);
1038 require_action( SafeArrayGetDim( psa
) == 1, exit
, err
= kDNSServiceErr_Unknown
);
1040 byteArray
.reserve( psa
->rgsabound
[0].cElements
);
1042 byteArray
.assign( byteArray
.capacity(), 0 );
1044 elems
= ( uint32_t ) byteArray
.capacity();
1048 // Call the function and package the return value in the Variant
1050 err
= DNSServiceGetProperty( propUTF8
.c_str(), &byteArray
[ 0 ], &elems
);
1052 require_noerr( err
, exit
);
1054 ok
= ByteArrayToVariant( &byteArray
[ 0 ], elems
, value
);
1056 require_action( ok
, exit
, err
= kDNSSDError_Unknown
);
1068 SafeArrayUnaccessData( psa
);
1082 STDMETHODIMP
CDNSSDService::GetAddrInfo(DNSSDFlags flags
, ULONG ifIndex
, DNSSDAddressFamily addressFamily
, BSTR hostName
, IDNSSDEventManager
*eventManager
, IDNSSDService
**service
)
1086 CComObject
<CDNSSDService
> * object
= NULL
;
1088 DNSServiceRef subord
= NULL
;
1090 std::string hostNameUTF8
;
1092 DNSServiceErrorType err
= 0;
1110 // Convert BSTR params to utf8
1112 ok
= BSTRToUTF8( hostName
, hostNameUTF8
);
1114 require_action( ok
, exit
, err
= kDNSServiceErr_BadParam
);
1122 object
= new CComObject
<CDNSSDService
>();
1136 require_action( object
!= NULL
, exit
, err
= kDNSServiceErr_NoMemory
);
1144 err
= DNSServiceGetAddrInfo( &subord
, flags
| kDNSServiceFlagsShareConnection
, ifIndex
, addressFamily
, hostNameUTF8
.c_str(), ( DNSServiceGetAddrInfoReply
) &GetAddrInfoReply
, object
);
1146 require_noerr( err
, exit
);
1150 object
->SetPrimaryRef( m_primary
);
1152 object
->SetSubordRef( subord
);
1154 object
->SetEventManager( eventManager
);
1166 if ( err
&& object
)
1184 STDMETHODIMP
CDNSSDService::NATPortMappingCreate(DNSSDFlags flags
, ULONG ifIndex
, DNSSDAddressFamily addressFamily
, DNSSDProtocol protocol
, USHORT internalPort
, USHORT externalPort
, ULONG ttl
, IDNSSDEventManager
*eventManager
, IDNSSDService
**service
)
1188 CComObject
<CDNSSDService
> * object
= NULL
;
1190 DNSServiceRef subord
= NULL
;
1192 DNSServiceProtocol prot
= 0;
1194 DNSServiceErrorType err
= 0;
1214 object
= new CComObject
<CDNSSDService
>();
1228 require_action( object
!= NULL
, exit
, err
= kDNSServiceErr_NoMemory
);
1234 prot
= ( addressFamily
| protocol
);
1240 err
= DNSServiceNATPortMappingCreate( &subord
, flags
| kDNSServiceFlagsShareConnection
, ifIndex
, prot
, htons( internalPort
), htons( externalPort
), ttl
, ( DNSServiceNATPortMappingReply
) &NATPortMappingReply
, object
);
1242 require_noerr( err
, exit
);
1246 object
->SetPrimaryRef( m_primary
);
1248 object
->SetSubordRef( subord
);
1250 object
->SetEventManager( eventManager
);
1262 if ( err
&& object
)
1280 STDMETHODIMP
CDNSSDService::Stop(void)
1292 dlog( kDebugLevelTrace
, "Stop()\n" );
1296 if ( m_isPrimary
&& m_primary
)
1300 SocketMap::iterator it
;
1304 if ( m_hiddenWindow
)
1308 WSAAsyncSelect( DNSServiceRefSockFD( m_primary
), m_hiddenWindow
, 0, 0 );
1314 it
= m_socketMap
.find( DNSServiceRefSockFD( m_primary
) );
1318 if ( it
!= m_socketMap
.end() )
1322 m_socketMap
.erase( it
);
1328 DNSServiceRefDeallocate( m_primary
);
1334 else if ( m_subord
)
1338 DNSServiceRefDeallocate( m_subord
);
1346 if ( m_eventManager
!= NULL
)
1350 m_eventManager
->Release();
1352 m_eventManager
= NULL
;
1369 CDNSSDService::DomainEnumReply
1371 DNSServiceRef sdRef
,
1372 DNSServiceFlags flags
,
1374 DNSServiceErrorType errorCode
,
1375 const char *replyDomainUTF8
,
1381 CComObject
<CDNSSDService
> * service
= NULL
;
1383 CDNSSDEventManager
* eventManager
= NULL
;
1389 service
= ( CComObject
< CDNSSDService
>* ) context
;
1391 require_action( service
, exit
, err
= kDNSServiceErr_Unknown
);
1395 if ( service
->ShouldHandleReply( errorCode
, eventManager
) )
1399 CComBSTR replyDomain
;
1405 ok
= UTF8ToBSTR( replyDomainUTF8
, replyDomain
);
1407 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1411 if ( flags
& kDNSServiceFlagsAdd
)
1415 eventManager
->Fire_DomainFound( service
, ( DNSSDFlags
) flags
, ifIndex
, replyDomain
);
1423 eventManager
->Fire_DomainLost( service
, ( DNSSDFlags
) flags
, ifIndex
, replyDomain
);
1444 CDNSSDService::BrowseReply
1446 DNSServiceRef sdRef
,
1447 DNSServiceFlags flags
,
1449 DNSServiceErrorType errorCode
,
1450 const char *serviceNameUTF8
,
1451 const char *regTypeUTF8
,
1452 const char *replyDomainUTF8
,
1458 CComObject
<CDNSSDService
> * service
= NULL
;
1460 CDNSSDEventManager
* eventManager
= NULL
;
1466 service
= ( CComObject
< CDNSSDService
>* ) context
;
1468 require_action( service
, exit
, err
= kDNSServiceErr_Unknown
);
1472 if ( service
->ShouldHandleReply( errorCode
, eventManager
) )
1476 CComBSTR serviceName
;
1480 CComBSTR replyDomain
;
1484 UTF8ToBSTR( serviceNameUTF8
, serviceName
);
1486 UTF8ToBSTR( regTypeUTF8
, regType
);
1488 UTF8ToBSTR( replyDomainUTF8
, replyDomain
);
1492 if ( flags
& kDNSServiceFlagsAdd
)
1496 eventManager
->Fire_ServiceFound( service
, ( DNSSDFlags
) flags
, ifIndex
, serviceName
, regType
, replyDomain
);
1504 eventManager
->Fire_ServiceLost( service
, ( DNSSDFlags
) flags
, ifIndex
, serviceName
, regType
, replyDomain
);
1526 CDNSSDService::ResolveReply
1529 DNSServiceRef sdRef
,
1530 DNSServiceFlags flags
,
1532 DNSServiceErrorType errorCode
,
1533 const char *fullNameUTF8
,
1534 const char *hostNameUTF8
,
1537 const unsigned char *txtRecord
,
1544 CComObject
<CDNSSDService
> * service
= NULL
;
1546 CDNSSDEventManager
* eventManager
= NULL
;
1552 service
= ( CComObject
< CDNSSDService
>* ) context
;
1554 require_action( service
, exit
, err
= kDNSServiceErr_Unknown
);
1558 if ( service
->ShouldHandleReply( errorCode
, eventManager
) )
1568 CComBSTR replyDomain
;
1570 CComObject
< CTXTRecord
>* record
;
1576 ok
= UTF8ToBSTR( fullNameUTF8
, fullName
);
1578 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1580 ok
= UTF8ToBSTR( hostNameUTF8
, hostName
);
1582 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1590 record
= new CComObject
<CTXTRecord
>();
1604 require_action( record
, exit
, err
= kDNSServiceErr_NoMemory
);
1614 record
->SetBytes( txtRecord
, txtLen
);
1620 eventManager
->Fire_ServiceResolved( service
, ( DNSSDFlags
) flags
, ifIndex
, fullName
, hostName
, ntohs( port
), record
);
1639 CDNSSDService::RegisterReply
1641 DNSServiceRef sdRef
,
1642 DNSServiceFlags flags
,
1643 DNSServiceErrorType errorCode
,
1644 const char *serviceNameUTF8
,
1645 const char *regTypeUTF8
,
1646 const char *domainUTF8
,
1652 CComObject
<CDNSSDService
> * service
= NULL
;
1654 CDNSSDEventManager
* eventManager
= NULL
;
1660 service
= ( CComObject
< CDNSSDService
>* ) context
;
1662 require_action( service
, exit
, err
= kDNSServiceErr_Unknown
);
1666 if ( service
->ShouldHandleReply( errorCode
, eventManager
) )
1670 CComBSTR serviceName
;
1680 ok
= UTF8ToBSTR( serviceNameUTF8
, serviceName
);
1682 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1684 ok
= UTF8ToBSTR( regTypeUTF8
, regType
);
1686 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1688 ok
= UTF8ToBSTR( domainUTF8
, domain
);
1690 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1694 eventManager
->Fire_ServiceRegistered( service
, ( DNSSDFlags
) flags
, serviceName
, regType
, domain
);
1713 CDNSSDService::QueryRecordReply
1715 DNSServiceRef sdRef
,
1716 DNSServiceFlags flags
,
1718 DNSServiceErrorType errorCode
,
1719 const char *fullNameUTF8
,
1730 CComObject
<CDNSSDService
> * service
= NULL
;
1732 CDNSSDEventManager
* eventManager
= NULL
;
1738 service
= ( CComObject
< CDNSSDService
>* ) context
;
1740 require_action( service
, exit
, err
= kDNSServiceErr_Unknown
);
1744 if ( service
->ShouldHandleReply( errorCode
, eventManager
) )
1756 ok
= UTF8ToBSTR( fullNameUTF8
, fullName
);
1758 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1760 ok
= ByteArrayToVariant( rdata
, rdlen
, &var
);
1762 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1766 eventManager
->Fire_QueryRecordAnswered( service
, ( DNSSDFlags
) flags
, ifIndex
, fullName
, ( DNSSDRRType
) rrtype
, ( DNSSDRRClass
) rrclass
, var
, ttl
);
1785 CDNSSDService::GetAddrInfoReply
1787 DNSServiceRef sdRef
,
1788 DNSServiceFlags flags
,
1790 DNSServiceErrorType errorCode
,
1791 const char *hostNameUTF8
,
1792 const struct sockaddr
*rawAddress
,
1799 CComObject
<CDNSSDService
> * service
= NULL
;
1801 CDNSSDEventManager
* eventManager
= NULL
;
1807 service
= ( CComObject
< CDNSSDService
>* ) context
;
1809 require_action( service
, exit
, err
= kDNSServiceErr_Unknown
);
1813 if ( service
->ShouldHandleReply( errorCode
, eventManager
) )
1821 DNSSDAddressFamily addressFamily
;
1823 char addressUTF8
[INET6_ADDRSTRLEN
];
1825 DWORD addressLen
= sizeof( addressUTF8
);
1833 ok
= UTF8ToBSTR( hostNameUTF8
, hostName
);
1835 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1839 switch ( rawAddress
->sa_family
)
1847 addressFamily
= kDNSSDAddressFamily_IPv4
;
1849 sockaddrLen
= sizeof( sockaddr_in
);
1861 addressFamily
= kDNSSDAddressFamily_IPv6
;
1863 sockaddrLen
= sizeof( sockaddr_in6
);
1873 err
= WSAAddressToStringA( ( LPSOCKADDR
) rawAddress
, sockaddrLen
, NULL
, addressUTF8
, &addressLen
);
1875 require_noerr( err
, exit
);
1877 ok
= UTF8ToBSTR( addressUTF8
, address
);
1879 require_action( ok
, exit
, err
= kDNSServiceErr_Unknown
);
1883 eventManager
->Fire_AddressFound( service
, ( DNSSDFlags
) flags
, ifIndex
, hostName
, addressFamily
, address
, ttl
);
1902 CDNSSDService::NATPortMappingReply
1904 DNSServiceRef sdRef
,
1905 DNSServiceFlags flags
,
1907 DNSServiceErrorType errorCode
,
1908 uint32_t externalAddress
, /* four byte IPv4 address in network byte order */
1909 DNSServiceProtocol protocol
,
1910 uint16_t internalPort
,
1911 uint16_t externalPort
, /* may be different than the requested port */
1912 uint32_t ttl
, /* may be different than the requested ttl */
1918 CComObject
<CDNSSDService
> * service
= NULL
;
1920 CDNSSDEventManager
* eventManager
= NULL
;
1926 service
= ( CComObject
< CDNSSDService
>* ) context
;
1928 require_action( service
, exit
, err
= kDNSServiceErr_Unknown
);
1932 if ( service
->ShouldHandleReply( errorCode
, eventManager
) )
1936 eventManager
->Fire_MappingCreated( service
, ( DNSSDFlags
) flags
, ifIndex
, externalAddress
, ( DNSSDAddressFamily
) ( protocol
& 0x8 ), ( DNSSDProtocol
) ( protocol
& 0x80 ), ntohs( internalPort
), ntohs( externalPort
), ttl
);
1955 CDNSSDService::RegisterRecordReply
1957 DNSServiceRef sdRef
,
1958 DNSRecordRef RecordRef
,
1959 DNSServiceFlags flags
,
1960 DNSServiceErrorType errorCode
,
1966 CComObject
<CDNSSDRecord
> * record
= NULL
;
1968 CDNSSDService
* service
= NULL
;
1970 CDNSSDEventManager
* eventManager
= NULL
;
1976 record
= ( CComObject
< CDNSSDRecord
>* ) context
;
1978 require_action( record
, exit
, err
= kDNSServiceErr_Unknown
);
1980 service
= record
->GetServiceObject();
1982 require_action( service
, exit
, err
= kDNSServiceErr_Unknown
);
1986 if ( service
->ShouldHandleReply( errorCode
, eventManager
) )
1990 eventManager
->Fire_RecordRegistered( record
, ( DNSSDFlags
) flags
);
2010 CDNSSDService::ShouldHandleReply( DNSServiceErrorType errorCode
, CDNSSDEventManager
*& eventManager
)
2018 if ( !this->Stopped() )
2022 eventManager
= this->GetEventManager();
2024 require_action( eventManager
, exit
, ok
= FALSE
);
2040 eventManager
->Fire_OperationFailed( this, ( DNSSDError
) errorCode
);
2062 CDNSSDService::WndProc( HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
2066 if ( msg
== WM_SOCKET
)
2070 SocketMap::iterator it
;
2074 it
= m_socketMap
.find( ( SOCKET
) wParam
);
2076 check( it
!= m_socketMap
.end() );
2080 if ( it
!= m_socketMap
.end() )
2084 DNSServiceProcessResult( it
->second
->m_primary
);
2092 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);;