1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2009, Apple Computer, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 static int g_defaultErrorCode
= kDNSServiceErr_ServiceNotRunning
;
32 static DLLStub g_glueLayer
;
35 // ------------------------------------------
36 // DLLStub implementation
37 // ------------------------------------------
38 DLLStub
* DLLStub::m_instance
;
42 m_library( LoadLibrary( TEXT( "dnssd.dll" ) ) )
50 if ( m_library
!= NULL
)
52 FreeLibrary( m_library
);
61 DLLStub::GetProcAddress( FARPROC
* func
, LPCSTR lpProcName
)
63 if ( m_instance
&& m_instance
->m_library
)
65 // Only call ::GetProcAddress if *func is NULL. This allows
66 // the calling code to cache the funcptr value, and we get
67 // some performance benefit.
71 *func
= ::GetProcAddress( m_instance
->m_library
, lpProcName
);
79 return ( *func
!= NULL
);
83 dnssd_sock_t DNSSD_API
84 DNSServiceRefSockFD(DNSServiceRef sdRef
)
86 typedef int (DNSSD_API
* Func
)(DNSServiceRef sdRef
);
87 static Func func
= NULL
;
88 int ret
= INVALID_SOCKET
;
90 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
99 DNSServiceErrorType DNSSD_API
100 DNSServiceProcessResult(DNSServiceRef sdRef
)
102 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef sdRef
);
103 static Func func
= NULL
;
104 DNSServiceErrorType ret
= g_defaultErrorCode
;
106 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
116 DNSServiceRefDeallocate(DNSServiceRef sdRef
)
118 typedef void (DNSSD_API
* Func
)(DNSServiceRef sdRef
);
119 static Func func
= NULL
;
121 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
128 DNSServiceErrorType DNSSD_API
129 DNSServiceEnumerateDomains
131 DNSServiceRef
*sdRef
,
132 DNSServiceFlags flags
,
133 uint32_t interfaceIndex
,
134 DNSServiceDomainEnumReply callBack
,
138 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
*, DNSServiceFlags
, uint32_t, DNSServiceDomainEnumReply
, void* );
139 static Func func
= NULL
;
140 DNSServiceErrorType ret
= g_defaultErrorCode
;
142 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
144 ret
= func( sdRef
, flags
, interfaceIndex
, callBack
, context
);
151 DNSServiceErrorType DNSSD_API
154 DNSServiceRef
*sdRef
,
155 DNSServiceFlags flags
,
156 uint32_t interfaceIndex
,
163 const void *txtRecord
,
164 DNSServiceRegisterReply callBack
,
168 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
*, DNSServiceFlags
, uint32_t, const char*, const char*, const char*, const char*, uint16_t, uint16_t, const void*, DNSServiceRegisterReply
, void* );
169 static Func func
= NULL
;
170 DNSServiceErrorType ret
= g_defaultErrorCode
;
172 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
174 ret
= func( sdRef
, flags
, interfaceIndex
, name
, regtype
, domain
, host
, port
, txtLen
, txtRecord
, callBack
, context
);
181 DNSServiceErrorType DNSSD_API
185 DNSRecordRef
*RecordRef
,
186 DNSServiceFlags flags
,
193 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
, DNSRecordRef
*, DNSServiceFlags
, uint16_t, uint16_t, const void*, uint32_t );
194 static Func func
= NULL
;
195 DNSServiceErrorType ret
= g_defaultErrorCode
;
197 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
199 ret
= func( sdRef
, RecordRef
, flags
, rrtype
, rdlen
, rdata
, ttl
);
206 DNSServiceErrorType DNSSD_API
207 DNSServiceUpdateRecord
210 DNSRecordRef RecordRef
, /* may be NULL */
211 DNSServiceFlags flags
,
217 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
, DNSRecordRef
, DNSServiceFlags
, uint16_t, const void*, uint32_t );
218 static Func func
= NULL
;
219 DNSServiceErrorType ret
= g_defaultErrorCode
;
221 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
223 ret
= func( sdRef
, RecordRef
, flags
, rdlen
, rdata
, ttl
);
230 DNSServiceErrorType DNSSD_API
231 DNSServiceRemoveRecord
234 DNSRecordRef RecordRef
,
235 DNSServiceFlags flags
238 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
, DNSRecordRef
, DNSServiceFlags
);
239 static Func func
= NULL
;
240 DNSServiceErrorType ret
= g_defaultErrorCode
;
242 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
244 ret
= func( sdRef
, RecordRef
, flags
);
251 DNSServiceErrorType DNSSD_API
254 DNSServiceRef
*sdRef
,
255 DNSServiceFlags flags
,
256 uint32_t interfaceIndex
,
258 const char *domain
, /* may be NULL */
259 DNSServiceBrowseReply callBack
,
260 void *context
/* may be NULL */
263 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
*, DNSServiceFlags
, uint32_t, const char*, const char*, DNSServiceBrowseReply
, void* );
264 static Func func
= NULL
;
265 DNSServiceErrorType ret
= g_defaultErrorCode
;
267 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
269 ret
= func( sdRef
, flags
, interfaceIndex
, regtype
, domain
, callBack
, context
);
276 DNSServiceErrorType DNSSD_API
279 DNSServiceRef
*sdRef
,
280 DNSServiceFlags flags
,
281 uint32_t interfaceIndex
,
285 DNSServiceResolveReply callBack
,
286 void *context
/* may be NULL */
289 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
*, DNSServiceFlags
, uint32_t, const char*, const char*, const char*, DNSServiceResolveReply
, void* );
290 static Func func
= NULL
;
291 DNSServiceErrorType ret
= g_defaultErrorCode
;
293 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
295 ret
= func( sdRef
, flags
, interfaceIndex
, name
, regtype
, domain
, callBack
, context
);
302 DNSServiceErrorType DNSSD_API
303 DNSServiceConstructFullName
306 const char *service
, /* may be NULL */
311 typedef DNSServiceErrorType (DNSSD_API
* Func
)( char*, const char*, const char*, const char* );
312 static Func func
= NULL
;
313 DNSServiceErrorType ret
= g_defaultErrorCode
;
315 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
317 ret
= func( fullName
, service
, regtype
, domain
);
324 DNSServiceErrorType DNSSD_API
325 DNSServiceCreateConnection(DNSServiceRef
*sdRef
)
327 typedef DNSServiceErrorType (DNSSD_API
* Func
)( DNSServiceRef
* );
328 static Func func
= NULL
;
329 DNSServiceErrorType ret
= g_defaultErrorCode
;
331 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
340 DNSServiceErrorType DNSSD_API
341 DNSServiceRegisterRecord
344 DNSRecordRef
*RecordRef
,
345 DNSServiceFlags flags
,
346 uint32_t interfaceIndex
,
347 const char *fullname
,
353 DNSServiceRegisterRecordReply callBack
,
354 void *context
/* may be NULL */
357 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
, DNSRecordRef
*, DNSServiceFlags
, uint32_t, const char*, uint16_t, uint16_t, uint16_t, const void*, uint16_t, DNSServiceRegisterRecordReply
, void* );
358 static Func func
= NULL
;
359 DNSServiceErrorType ret
= g_defaultErrorCode
;
361 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
363 ret
= func( sdRef
, RecordRef
, flags
, interfaceIndex
, fullname
, rrtype
, rrclass
, rdlen
, rdata
, ttl
, callBack
, context
);
370 DNSServiceErrorType DNSSD_API
371 DNSServiceQueryRecord
373 DNSServiceRef
*sdRef
,
374 DNSServiceFlags flags
,
375 uint32_t interfaceIndex
,
376 const char *fullname
,
379 DNSServiceQueryRecordReply callBack
,
380 void *context
/* may be NULL */
383 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
*, DNSServiceFlags
, uint32_t, const char*, uint16_t, uint16_t, DNSServiceQueryRecordReply
, void* );
384 static Func func
= NULL
;
385 DNSServiceErrorType ret
= g_defaultErrorCode
;
387 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
389 ret
= func( sdRef
, flags
, interfaceIndex
, fullname
, rrtype
, rrclass
, callBack
, context
);
396 DNSServiceErrorType DNSSD_API
397 DNSServiceReconfirmRecord
399 DNSServiceFlags flags
,
400 uint32_t interfaceIndex
,
401 const char *fullname
,
408 typedef DNSServiceErrorType (DNSSD_API
* Func
)( DNSServiceFlags
, uint32_t, const char*, uint16_t, uint16_t, uint16_t, const void* );
409 static Func func
= NULL
;
410 DNSServiceErrorType ret
= g_defaultErrorCode
;
412 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
414 ret
= func( flags
, interfaceIndex
, fullname
, rrtype
, rrclass
, rdlen
, rdata
);
421 DNSServiceErrorType DNSSD_API
422 DNSServiceNATPortMappingCreate
424 DNSServiceRef
*sdRef
,
425 DNSServiceFlags flags
,
426 uint32_t interfaceIndex
,
427 DNSServiceProtocol protocol
, /* TCP and/or UDP */
428 uint16_t internalPort
, /* network byte order */
429 uint16_t externalPort
, /* network byte order */
430 uint32_t ttl
, /* time to live in seconds */
431 DNSServiceNATPortMappingReply callBack
,
432 void *context
/* may be NULL */
435 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
*, DNSServiceFlags
, uint32_t, DNSServiceProtocol
, uint16_t, uint16_t, uint16_t, DNSServiceNATPortMappingReply
, void* );
436 static Func func
= NULL
;
437 DNSServiceErrorType ret
= g_defaultErrorCode
;
439 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
441 ret
= func( sdRef
, flags
, interfaceIndex
, protocol
, internalPort
, externalPort
, ttl
, callBack
, context
);
448 DNSServiceErrorType DNSSD_API
449 DNSServiceGetAddrInfo
451 DNSServiceRef
*sdRef
,
452 DNSServiceFlags flags
,
453 uint32_t interfaceIndex
,
454 DNSServiceProtocol protocol
,
455 const char *hostname
,
456 DNSServiceGetAddrInfoReply callBack
,
457 void *context
/* may be NULL */
460 typedef DNSServiceErrorType (DNSSD_API
* Func
)(DNSServiceRef
*, DNSServiceFlags
, uint32_t, DNSServiceProtocol
, const char*, DNSServiceGetAddrInfoReply
, void* );
461 static Func func
= NULL
;
462 DNSServiceErrorType ret
= g_defaultErrorCode
;
464 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
466 ret
= func( sdRef
, flags
, interfaceIndex
, protocol
, hostname
, callBack
, context
);
473 DNSServiceErrorType DNSSD_API
474 DNSServiceGetProperty
476 const char *property
, /* Requested property (i.e. kDNSServiceProperty_DaemonVersion) */
477 void *result
, /* Pointer to place to store result */
478 uint32_t *size
/* size of result location */
481 typedef DNSServiceErrorType (DNSSD_API
* Func
)( const char*, void*, uint32_t* );
482 static Func func
= NULL
;
483 DNSServiceErrorType ret
= g_defaultErrorCode
;
485 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
487 ret
= func( property
, result
, size
);
497 TXTRecordRef
*txtRecord
,
502 typedef void (DNSSD_API
* Func
)( TXTRecordRef
*, uint16_t, void* );
503 static Func func
= NULL
;
505 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
507 func( txtRecord
, bufferLen
, buffer
);
515 TXTRecordRef
*txtRecord
518 typedef void (DNSSD_API
* Func
)( TXTRecordRef
* );
519 static Func func
= NULL
;
521 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
528 DNSServiceErrorType DNSSD_API
531 TXTRecordRef
*txtRecord
,
533 uint8_t valueSize
, /* may be zero */
534 const void *value
/* may be NULL */
537 typedef DNSServiceErrorType (DNSSD_API
* Func
)( TXTRecordRef
*, const char*, uint8_t, const void* );
538 static Func func
= NULL
;
539 DNSServiceErrorType ret
= g_defaultErrorCode
;
541 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
543 ret
= func( txtRecord
, key
, valueSize
, value
);
550 DNSServiceErrorType DNSSD_API
553 TXTRecordRef
*txtRecord
,
557 typedef DNSServiceErrorType (DNSSD_API
* Func
)( TXTRecordRef
*, const char* );
558 static Func func
= NULL
;
559 DNSServiceErrorType ret
= g_defaultErrorCode
;
561 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
563 ret
= func( txtRecord
, key
);
574 const void *txtRecord
,
578 typedef int (DNSSD_API
* Func
)( uint16_t, const void*, const char* );
579 static Func func
= NULL
;
582 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
584 ret
= func( txtLen
, txtRecord
, key
);
595 const void *txtRecord
598 typedef uint16_t (DNSSD_API
* Func
)( uint16_t, const void* );
599 static Func func
= NULL
;
602 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
604 ret
= func( txtLen
, txtRecord
);
614 const TXTRecordRef
*txtRecord
617 typedef uint16_t (DNSSD_API
* Func
)( const TXTRecordRef
* );
618 static Func func
= NULL
;
621 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
623 ret
= func( txtRecord
);
630 const void * DNSSD_API
633 const TXTRecordRef
*txtRecord
636 typedef const void* (DNSSD_API
* Func
)( const TXTRecordRef
* );
637 static Func func
= NULL
;
638 const void* ret
= NULL
;
640 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
642 ret
= func( txtRecord
);
649 const void * DNSSD_API
653 const void *txtRecord
,
658 typedef const void* (DNSSD_API
* Func
)( uint16_t, const void*, const char*, uint8_t* );
659 static Func func
= NULL
;
660 const void* ret
= NULL
;
662 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
664 ret
= func( txtLen
, txtRecord
, key
, valueLen
);
671 DNSServiceErrorType DNSSD_API
672 TXTRecordGetItemAtIndex
675 const void *txtRecord
,
683 typedef DNSServiceErrorType (DNSSD_API
* Func
)( uint16_t, const void*, uint16_t, uint16_t, char*, uint8_t*, const void** );
684 static Func func
= NULL
;
685 DNSServiceErrorType ret
= g_defaultErrorCode
;
687 if ( DLLStub::GetProcAddress( ( FARPROC
* ) &func
, __FUNCTION__
) )
689 ret
= func( txtLen
, txtRecord
, itemIndex
, keyBufLen
, key
, valueLen
, value
);