#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFXPCBridge.h>
#include <dns_sd.h>
#include <UserEventAgentInterface.h>
#include <stdio.h>
#include <stdlib.h>
#include <asl.h>
#include <xpc/xpc.h>
#pragma mark -
#pragma mark Types
#pragma mark -
static const char* sPluginIdentifier = "com.apple.bonjour.events";
static const CFStringRef sServiceNameKey = CFSTR("ServiceName");
static const CFStringRef sServiceTypeKey = CFSTR("ServiceType");
static const CFStringRef sServiceDomainKey = CFSTR("ServiceDomain");
static const CFStringRef sOnServiceAddKey = CFSTR("OnServiceAdd");
static const CFStringRef sOnServiceRemoveKey = CFSTR("OnServiceRemove");
static const CFStringRef sLaunchdTokenKey = CFSTR("LaunchdToken");
static const CFStringRef sLaunchdDictKey = CFSTR("LaunchdDict");
typedef struct {
UserEventAgentInterfaceStruct* _UserEventAgentInterface;
CFUUIDRef _factoryID;
UInt32 _refCount;
void* _pluginContext;
CFMutableDictionaryRef _tokenToBrowserMap; CFMutableDictionaryRef _browsers; CFMutableDictionaryRef _onAddEvents; CFMutableDictionaryRef _onRemoveEvents; } BonjourUserEventsPlugin;
typedef struct {
CFIndex refCount;
DNSServiceRef browserRef;
} NetBrowserInfo;
#pragma mark -
#pragma mark Prototypes
#pragma mark -
static HRESULT QueryInterface(void *myInstance, REFIID iid, LPVOID *ppv);
static ULONG AddRef(void* instance);
static ULONG Release(void* instance);
static BonjourUserEventsPlugin* Alloc(CFUUIDRef factoryID);
static void Dealloc(BonjourUserEventsPlugin* plugin);
void * UserEventAgentFactory(CFAllocatorRef allocator, CFUUIDRef typeID);
static void Install(void* instance);
static void ManageEventsCallback(
UserEventAgentLaunchdAction action,
CFNumberRef token,
CFTypeRef eventMatchDict,
void * vContext);
void AddEventToPlugin(BonjourUserEventsPlugin* plugin, CFNumberRef launchdToken, CFDictionaryRef eventParameters);
void RemoveEventFromPlugin(BonjourUserEventsPlugin* plugin, CFNumberRef launchToken);
NetBrowserInfo* CreateBrowser(BonjourUserEventsPlugin* plugin, CFStringRef type, CFStringRef domain);
NetBrowserInfo* BrowserForSDRef(BonjourUserEventsPlugin* plugin, DNSServiceRef sdRef);
void AddEventDictionary(CFDictionaryRef eventDict, CFMutableDictionaryRef allEventsDictionary, NetBrowserInfo* key);
void RemoveEventFromArray(CFMutableArrayRef array, CFNumberRef launchdToken);
void ServiceBrowserCallback (DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char* serviceName, const char* regtype, const char* replyDomain, void* context);
void HandleTemporaryEventsForService(BonjourUserEventsPlugin* plugin, NetBrowserInfo* browser, CFStringRef serviceName, CFMutableDictionaryRef eventsDictionary);
const char* CStringFromCFString(CFStringRef string);
NetBrowserInfo* NetBrowserInfoCreate(CFStringRef serviceType, CFStringRef domain, void* context);
const void* NetBrowserInfoRetain(CFAllocatorRef allocator, const void* info);
void NetBrowserInfoRelease(CFAllocatorRef allocator, const void* info);
Boolean NetBrowserInfoEqual(const void *value1, const void *value2);
CFHashCode NetBrowserInfoHash(const void *value);
CFStringRef NetBrowserInfoCopyDescription(const void *value);
static const CFDictionaryKeyCallBacks kNetBrowserInfoDictionaryKeyCallbacks = {
0,
NetBrowserInfoRetain,
NetBrowserInfoRelease,
NetBrowserInfoCopyDescription,
NetBrowserInfoEqual,
NetBrowserInfoHash
};
static const CFDictionaryValueCallBacks kNetBrowserInfoDictionaryValueCallbacks = {
0,
NetBrowserInfoRetain,
NetBrowserInfoRelease,
NetBrowserInfoCopyDescription,
NetBrowserInfoEqual
};
static UserEventAgentInterfaceStruct UserEventAgentInterfaceFtbl = {
NULL, QueryInterface, AddRef, Release, Install };
#pragma mark -
#pragma mark COM Management
#pragma mark -
static HRESULT QueryInterface(void *myInstance, REFIID iid, LPVOID *ppv)
{
CFUUIDRef interfaceID = CFUUIDCreateFromUUIDBytes(NULL, iid);
if(CFEqual(interfaceID, kUserEventAgentInterfaceID))
{
((BonjourUserEventsPlugin *) myInstance)->_UserEventAgentInterface->AddRef(myInstance);
*ppv = myInstance;
CFRelease(interfaceID);
return S_OK;
}
else if(CFEqual(interfaceID, IUnknownUUID))
{
((BonjourUserEventsPlugin *) myInstance)->_UserEventAgentInterface->AddRef(myInstance);
*ppv = myInstance;
CFRelease(interfaceID);
return S_OK;
}
else {
*ppv = NULL;
CFRelease(interfaceID);
return E_NOINTERFACE;
}
}
static ULONG AddRef(void* instance)
{
BonjourUserEventsPlugin* plugin = (BonjourUserEventsPlugin*)instance;
return ++plugin->_refCount;
}
static ULONG Release(void* instance)
{
BonjourUserEventsPlugin* plugin = (BonjourUserEventsPlugin*)instance;
if (plugin->_refCount != 0)
--plugin->_refCount;
if (plugin->_refCount == 0)
{
Dealloc(instance);
return 0;
}
return plugin->_refCount;
}
static BonjourUserEventsPlugin* Alloc(CFUUIDRef factoryID)
{
BonjourUserEventsPlugin* plugin = malloc(sizeof(BonjourUserEventsPlugin));
plugin->_UserEventAgentInterface = &UserEventAgentInterfaceFtbl;
plugin->_pluginContext = NULL;
if (factoryID)
{
plugin->_factoryID = (CFUUIDRef)CFRetain(factoryID);
CFPlugInAddInstanceForFactory(factoryID);
}
plugin->_refCount = 1;
plugin->_tokenToBrowserMap = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kNetBrowserInfoDictionaryValueCallbacks);
plugin->_browsers = CFDictionaryCreateMutable(NULL, 0, &kNetBrowserInfoDictionaryKeyCallbacks, &kCFTypeDictionaryValueCallBacks);
plugin->_onAddEvents = CFDictionaryCreateMutable(NULL, 0, &kNetBrowserInfoDictionaryKeyCallbacks, &kCFTypeDictionaryValueCallBacks);
plugin->_onRemoveEvents = CFDictionaryCreateMutable(NULL, 0, &kNetBrowserInfoDictionaryKeyCallbacks, &kCFTypeDictionaryValueCallBacks);
return plugin;
}
static void Dealloc(BonjourUserEventsPlugin* plugin)
{
CFUUIDRef factoryID = plugin->_factoryID;
if (factoryID)
{
CFPlugInRemoveInstanceForFactory(factoryID);
CFRelease(factoryID);
}
if (plugin->_tokenToBrowserMap)
CFRelease(plugin->_tokenToBrowserMap);
if (plugin->_browsers)
CFRelease(plugin->_browsers);
if (plugin->_onAddEvents)
CFRelease(plugin->_onAddEvents);
if (plugin->_onRemoveEvents)
CFRelease(plugin->_onRemoveEvents);
free(plugin);
}
void * UserEventAgentFactory(CFAllocatorRef allocator, CFUUIDRef typeID)
{
(void)allocator;
BonjourUserEventsPlugin * result = NULL;
if (typeID && CFEqual(typeID, kUserEventAgentTypeID)) {
result = Alloc(kUserEventAgentFactoryID);
}
return (void *)result;
}
#pragma mark -
#pragma mark Plugin Management
#pragma mark -
static void Install(void *instance)
{
BonjourUserEventsPlugin* plugin = (BonjourUserEventsPlugin*)instance;
plugin->_pluginContext = UserEventAgentRegisterForLaunchEvents(sPluginIdentifier, &ManageEventsCallback, plugin);
if (!plugin->_pluginContext)
{
fprintf(stderr, "%s:%s failed to register for launch events.\n", sPluginIdentifier, __FUNCTION__);
return;
}
}
static void ManageEventsCallback(UserEventAgentLaunchdAction action, CFNumberRef token, CFTypeRef eventMatchDict, void* vContext)
{
if (action == kUserEventAgentLaunchdAdd)
{
if (!eventMatchDict)
{
fprintf(stderr, "%s:%s empty dictionary\n", sPluginIdentifier, __FUNCTION__);
return;
}
if (CFGetTypeID(eventMatchDict) != CFDictionaryGetTypeID())
{
fprintf(stderr, "%s:%s given non-dict for event dictionary, action %d\n", sPluginIdentifier, __FUNCTION__, action);
return;
}
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s calling AddEventToPlugin", sPluginIdentifier, __FUNCTION__);
AddEventToPlugin((BonjourUserEventsPlugin*)vContext, token, (CFDictionaryRef)eventMatchDict);
}
else if (action == kUserEventAgentLaunchdRemove)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s calling RemoveEventToPlugin", sPluginIdentifier, __FUNCTION__);
RemoveEventFromPlugin((BonjourUserEventsPlugin*)vContext, token);
}
else
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s unknown callback event\n", sPluginIdentifier, __FUNCTION__);
}
}
#pragma mark -
#pragma mark Plugin Guts
#pragma mark -
void AddEventToPlugin(BonjourUserEventsPlugin* plugin, CFNumberRef launchdToken, CFDictionaryRef eventParameters)
{
CFStringRef domain = CFDictionaryGetValue(eventParameters, sServiceDomainKey);
CFStringRef type = CFDictionaryGetValue(eventParameters, sServiceTypeKey);
CFStringRef name = CFDictionaryGetValue(eventParameters, sServiceNameKey);
CFBooleanRef cfOnAdd = CFDictionaryGetValue(eventParameters, sOnServiceAddKey);
CFBooleanRef cfOnRemove = CFDictionaryGetValue(eventParameters, sOnServiceRemoveKey);
Boolean onAdd = false;
Boolean onRemove = false;
if (cfOnAdd && CFGetTypeID(cfOnAdd) == CFBooleanGetTypeID() && CFBooleanGetValue(cfOnAdd))
onAdd = true;
if (cfOnRemove && CFGetTypeID(cfOnRemove) == CFBooleanGetTypeID() && CFBooleanGetValue(cfOnRemove))
onRemove = true;
if (!type || CFGetTypeID(type) != CFStringGetTypeID())
{
fprintf(stderr, "%s:%s: a LaunchEvent is missing a service type.\n", sPluginIdentifier, __FUNCTION__);
return;
}
if (!onAdd && !onRemove)
{
fprintf(stderr, "%s:%s a LaunchEvent is missing both onAdd and onRemove events\n", sPluginIdentifier, __FUNCTION__);
return;
}
if (!domain)
{
domain = CFSTR("local");
}
else if (CFGetTypeID(domain) != CFStringGetTypeID() ) {
fprintf(stderr, "%s:%s a LaunchEvent has a domain that is not a string.\n", sPluginIdentifier, __FUNCTION__);
return;
}
if (name && CFGetTypeID(name) != CFStringGetTypeID())
{
fprintf(stderr, "%s:%s a LaunchEvent has a domain that is not a string.\n", sPluginIdentifier, __FUNCTION__);
return;
}
NetBrowserInfo* browser = CreateBrowser(plugin, type, domain);
if (!browser)
{
fprintf(stderr, "%s:%s cannot create browser\n", sPluginIdentifier, __FUNCTION__);
return;
}
CFMutableDictionaryRef eventDictionary = CFDictionaryCreateMutable(NULL, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(eventDictionary, sLaunchdTokenKey, launchdToken);
CFDictionarySetValue(eventDictionary, sLaunchdDictKey, eventParameters);
if (name)
CFDictionarySetValue(eventDictionary, sServiceNameKey, name);
if (onAdd)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Adding browser to AddEvents", sPluginIdentifier, __FUNCTION__);
AddEventDictionary(eventDictionary, plugin->_onAddEvents, browser);
}
if (onRemove)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Adding browser to RemoveEvents", sPluginIdentifier, __FUNCTION__);
AddEventDictionary(eventDictionary, plugin->_onRemoveEvents, browser);
}
CFDictionarySetValue(plugin->_tokenToBrowserMap, launchdToken, browser);
CFRelease(eventDictionary);
}
void RemoveEventFromPlugin(BonjourUserEventsPlugin* plugin, CFNumberRef launchdToken)
{
NetBrowserInfo* browser = (NetBrowserInfo*)CFDictionaryGetValue(plugin->_tokenToBrowserMap, launchdToken);
Boolean othersUsingBrowser = false;
if (!browser)
{
long long value = 0;
CFNumberGetValue(launchdToken, kCFNumberLongLongType, &value);
fprintf(stderr, "%s:%s Launchd asked us to remove a token we did not register! ==Token:%lld== \n", sPluginIdentifier, __FUNCTION__, value);
return;
}
CFMutableArrayRef onAddEvents = (CFMutableArrayRef)CFDictionaryGetValue(plugin->_onAddEvents, browser);
CFMutableArrayRef onRemoveEvents = (CFMutableArrayRef)CFDictionaryGetValue(plugin->_onRemoveEvents, browser);
if (onAddEvents)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Calling RemoveEventFromArray for OnAddEvents", sPluginIdentifier, __FUNCTION__);
RemoveEventFromArray(onAddEvents, launchdToken);
if (CFArrayGetCount(onAddEvents) == 0)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Removing the browser from AddEvents", sPluginIdentifier, __FUNCTION__);
CFDictionaryRemoveValue(plugin->_onAddEvents, browser);
}
}
if (onRemoveEvents)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Calling RemoveEventFromArray for OnRemoveEvents", sPluginIdentifier, __FUNCTION__);
RemoveEventFromArray(onRemoveEvents, launchdToken);
if (CFArrayGetCount(onRemoveEvents) == 0)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Removing the browser from RemoveEvents", sPluginIdentifier, __FUNCTION__);
CFDictionaryRemoveValue(plugin->_onRemoveEvents, browser);
}
}
CFDictionaryRemoveValue(plugin->_tokenToBrowserMap, launchdToken);
CFIndex i;
CFIndex count = CFDictionaryGetCount(plugin->_tokenToBrowserMap);
NetBrowserInfo** browsers = malloc(count * sizeof(NetBrowserInfo*));
CFDictionaryGetKeysAndValues(plugin->_tokenToBrowserMap, NULL, (const void**)browsers);
for (i = 0; i < count; ++i)
{
if (NetBrowserInfoEqual(browsers[i], browser))
{
othersUsingBrowser = true;
break;
}
}
if (!othersUsingBrowser)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Removing browser %p from _browsers", sPluginIdentifier, __FUNCTION__, browser);
CFDictionaryRemoveValue(plugin->_browsers, browser); }
else
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Decrementing browsers %p count", sPluginIdentifier, __FUNCTION__, browser);
NetBrowserInfoRelease(NULL, browser);
}
free(browsers);
}
NetBrowserInfo* CreateBrowser(BonjourUserEventsPlugin* plugin, CFStringRef type, CFStringRef domain)
{
CFIndex i;
CFIndex count = CFDictionaryGetCount(plugin->_browsers);
NetBrowserInfo* browser = NULL;
CFDictionaryRef* dicts = malloc(count * sizeof(CFDictionaryRef));
NetBrowserInfo** browsers = malloc(count * sizeof(NetBrowserInfo*));
CFDictionaryGetKeysAndValues(plugin->_browsers, (const void**)browsers, (const void**)dicts);
for (i = 0; i < count; ++i)
{
CFDictionaryRef browserDict = dicts[i];
CFStringRef browserType = CFDictionaryGetValue(browserDict, sServiceTypeKey);
CFStringRef browserDomain = CFDictionaryGetValue(browserDict, sServiceDomainKey);
if ((CFStringCompare(browserType, type, kCFCompareCaseInsensitive) == kCFCompareEqualTo) &&
(CFStringCompare(browserDomain, domain, kCFCompareCaseInsensitive) == kCFCompareEqualTo))
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: found a duplicate browser\n", sPluginIdentifier, __FUNCTION__);
browser = browsers[i];
NetBrowserInfoRetain(NULL, browser);
break;
}
}
if (!browser)
{
browser = NetBrowserInfoCreate(type, domain, plugin);
if (!browser)
{
fprintf(stderr, "%s:%s failed to search for %s.%s", sPluginIdentifier, __FUNCTION__, CStringFromCFString(type), CStringFromCFString(domain));
free(dicts);
free(browsers);
return NULL;
}
CFMutableDictionaryRef browserDict = CFDictionaryCreateMutable(NULL, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(browserDict, sServiceTypeKey, type);
CFDictionarySetValue(browserDict, sServiceDomainKey, domain);
CFDictionarySetValue(plugin->_browsers, browser, browserDict);
NetBrowserInfoRelease(NULL, browser);
CFRelease(browserDict);
}
free(dicts);
free(browsers);
return browser;
}
NetBrowserInfo* BrowserForSDRef(BonjourUserEventsPlugin* plugin, DNSServiceRef sdRef)
{
CFIndex i;
CFIndex count = CFDictionaryGetCount(plugin->_browsers);
NetBrowserInfo* browser = NULL;
NetBrowserInfo** browsers = malloc(count * sizeof(NetBrowserInfo*));
CFDictionaryGetKeysAndValues(plugin->_browsers, (const void**)browsers, NULL);
for (i = 0; i < count; ++i)
{
NetBrowserInfo* currentBrowser = browsers[i];
if (currentBrowser->browserRef == sdRef)
{
browser = currentBrowser;
break;
}
}
free(browsers);
return browser;
}
void AddEventDictionary(CFDictionaryRef eventDict, CFMutableDictionaryRef allEventsDictionary, NetBrowserInfo* key)
{
CFMutableArrayRef eventsForBrowser = (CFMutableArrayRef)CFDictionaryGetValue(allEventsDictionary, key);
if (!eventsForBrowser) {
eventsForBrowser = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
CFDictionarySetValue(allEventsDictionary, key, eventsForBrowser);
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s creating a new array", sPluginIdentifier, __FUNCTION__);
}
else
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s Incrementing refcount", sPluginIdentifier, __FUNCTION__);
CFRetain(eventsForBrowser);
}
CFArrayAppendValue(eventsForBrowser, eventDict);
CFRelease(eventsForBrowser);
}
void RemoveEventFromArray(CFMutableArrayRef array, CFNumberRef launchdToken)
{
CFIndex i;
CFIndex count = CFArrayGetCount(array);
for (i = 0; i < count; )
{
CFDictionaryRef eventDict = CFArrayGetValueAtIndex(array, i);
CFNumberRef token = CFDictionaryGetValue(eventDict, sLaunchdTokenKey);
if (CFEqual(token, launchdToken)) {
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s found token", sPluginIdentifier, __FUNCTION__);
CFArrayRemoveValueAtIndex(array, i); break; }
else
{
++i; }
}
if (i == count) asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s did not find token", sPluginIdentifier, __FUNCTION__);
}
#pragma mark -
#pragma mark Net Service Browser Stuff
#pragma mark -
void ServiceBrowserCallback (DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* serviceName,
const char* regtype,
const char* replyDomain,
void* context )
{
(void)interfaceIndex;
(void)regtype;
(void)replyDomain;
BonjourUserEventsPlugin* plugin = (BonjourUserEventsPlugin*)context;
NetBrowserInfo* browser = BrowserForSDRef(plugin, sdRef);
if (!browser) {
fprintf(stderr, "%s:%s ServiceBrowserCallback: missing browser\n", sPluginIdentifier, __FUNCTION__);
return;
}
if (errorCode != kDNSServiceErr_NoError)
{
fprintf(stderr, "%s:%s ServiceBrowserCallback: errcode set %d\n", sPluginIdentifier, __FUNCTION__, errorCode);
return;
}
CFStringRef cfServiceName = CFStringCreateWithCString(NULL, serviceName, kCFStringEncodingUTF8);
if (flags & kDNSServiceFlagsAdd)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s calling HandleTemporaryEventsForService Add\n", sPluginIdentifier, __FUNCTION__);
HandleTemporaryEventsForService(plugin, browser, cfServiceName, plugin->_onAddEvents);
}
else
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s calling HandleTemporaryEventsForService Remove\n", sPluginIdentifier, __FUNCTION__);
HandleTemporaryEventsForService(plugin, browser, cfServiceName, plugin->_onRemoveEvents);
}
CFRelease(cfServiceName);
}
void HandleTemporaryEventsForService(BonjourUserEventsPlugin* plugin, NetBrowserInfo* browser, CFStringRef serviceName, CFMutableDictionaryRef eventsDictionary)
{
CFArrayRef events = (CFArrayRef)CFDictionaryGetValue(eventsDictionary, browser); CFIndex i;
CFIndex count;
if (!events) return;
count = CFArrayGetCount(events);
for (i = 0; i < count; ++i)
{
CFDictionaryRef eventDict = (CFDictionaryRef)CFArrayGetValueAtIndex(events, i);
CFStringRef eventServiceName = (CFStringRef)CFDictionaryGetValue(eventDict, sServiceNameKey);
CFNumberRef token = (CFNumberRef) CFDictionaryGetValue(eventDict, sLaunchdTokenKey);
CFDictionaryRef dict = (CFDictionaryRef) CFDictionaryGetValue(eventDict, sLaunchdDictKey);
if (!eventServiceName || CFEqual(serviceName, eventServiceName))
{
uint64_t tokenUint64;
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s HandleTemporaryEventsForService signal\n", sPluginIdentifier, __FUNCTION__);
CFNumberGetValue(token, kCFNumberLongLongType, &tokenUint64);
xpc_object_t jobRequest = _CFXPCCreateXPCObjectFromCFObject(dict);
UserEventAgentFireEvent(plugin->_pluginContext, tokenUint64, jobRequest);
xpc_release(jobRequest);
}
}
}
#pragma mark -
#pragma mark Convenience
#pragma mark -
const char* CStringFromCFString(CFStringRef string)
{
const char* defaultString = "??????";
const char* cstring;
if (!string)
return defaultString;
cstring = CFStringGetCStringPtr(string, kCFStringEncodingUTF8);
return (cstring) ? cstring : defaultString;
}
#pragma mark -
#pragma mark NetBrowserInfo "Object"
#pragma mark -
NetBrowserInfo* NetBrowserInfoCreate(CFStringRef serviceType, CFStringRef domain, void* context)
{
NetBrowserInfo* outObj = NULL;
DNSServiceRef browserRef = NULL;
char* cServiceType = NULL;
char* cDomain = NULL;
Boolean success = true;
CFIndex serviceSize = CFStringGetMaximumSizeForEncoding(CFStringGetLength(serviceType), kCFStringEncodingUTF8);
cServiceType = calloc(serviceSize, 1);
success = CFStringGetCString(serviceType, cServiceType, serviceSize, kCFStringEncodingUTF8);
if (domain)
{
CFIndex domainSize = CFStringGetMaximumSizeForEncoding(CFStringGetLength(domain), kCFStringEncodingUTF8);
cDomain = calloc(serviceSize, 1);
success = success && CFStringGetCString(domain, cDomain, domainSize, kCFStringEncodingUTF8);
}
if (!success)
{
fprintf(stderr, "%s:%s LaunchEvent has badly encoded service type or domain.\n", sPluginIdentifier, __FUNCTION__);
free(cServiceType);
if (cDomain)
free(cDomain);
return NULL;
}
DNSServiceErrorType err = DNSServiceBrowse(&browserRef, 0, 0, cServiceType, cDomain, ServiceBrowserCallback, context);
if (err != kDNSServiceErr_NoError)
{
fprintf(stderr, "%s:%s Failed to create browser for %s, %s\n", sPluginIdentifier, __FUNCTION__, cServiceType, cDomain);
free(cServiceType);
if (cDomain)
free(cDomain);
return NULL;
}
DNSServiceSetDispatchQueue(browserRef, dispatch_get_main_queue());
outObj = malloc(sizeof(NetBrowserInfo));
outObj->refCount = 1;
outObj->browserRef = browserRef;
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: created new object %p", sPluginIdentifier, __FUNCTION__, outObj);
free(cServiceType);
if (cDomain)
free(cDomain);
return outObj;
}
const void* NetBrowserInfoRetain(CFAllocatorRef allocator, const void* info)
{
(void)allocator;
NetBrowserInfo* obj = (NetBrowserInfo*)info;
if (!obj)
return NULL;
++obj->refCount;
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Incremented ref count on %p, count %d", sPluginIdentifier, __FUNCTION__, obj->browserRef, (int)obj->refCount);
return obj;
}
void NetBrowserInfoRelease(CFAllocatorRef allocator, const void* info)
{
(void)allocator;
NetBrowserInfo* obj = (NetBrowserInfo*)info;
if (!obj)
return;
if (obj->refCount == 1)
{
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: DNSServiceRefDeallocate %p", sPluginIdentifier, __FUNCTION__, obj->browserRef);
DNSServiceRefDeallocate(obj->browserRef);
free(obj);
}
else
{
--obj->refCount;
asl_log(NULL, NULL, ASL_LEVEL_INFO, "%s:%s: Decremented ref count on %p, count %d", sPluginIdentifier, __FUNCTION__, obj->browserRef, (int)obj->refCount);
}
}
Boolean NetBrowserInfoEqual(const void *value1, const void *value2)
{
NetBrowserInfo* obj1 = (NetBrowserInfo*)value1;
NetBrowserInfo* obj2 = (NetBrowserInfo*)value2;
if (obj1->browserRef == obj2->browserRef)
return true;
return false;
}
CFHashCode NetBrowserInfoHash(const void *value)
{
return (CFHashCode)((NetBrowserInfo*)value)->browserRef;
}
CFStringRef NetBrowserInfoCopyDescription(const void *value)
{
(void)value;
return CFStringCreateWithCString(NULL, "NetBrowserInfo: No useful description", kCFStringEncodingUTF8);
}