Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bonjour services through bluetooth too #108

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions Core/DNSSDRegistration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
File: DNSSDRegistration.h

Contains: Uses the low-level DNS-SD API to manage a Bonjour service registration.

Written by: DTS

Copyright: Copyright (c) 2011 Apple Inc. All Rights Reserved.

Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of
these terms. If you do not agree with these terms, please do
not use, install, modify or redistribute this Apple software.

In consideration of your agreement to abide by the following
terms, and subject to these terms, Apple grants you a personal,
non-exclusive license, under Apple's copyrights in this
original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or
without modifications, in source and/or binary forms; provided
that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the
following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks
or logos of Apple Inc. may be used to endorse or promote
products derived from the Apple Software without specific prior
written permission from Apple. Except as expressly stated in
this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or
by other works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis.
APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING
THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY
OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

*/

#import <Foundation/Foundation.h>

// forward declarations

@protocol DNSSDRegistrationDelegate;

#pragma mark * DNSSDRegistration

// DNSSDRegistration represents a service that you can register on the network.

@interface DNSSDRegistration : NSObject

- (id)initWithDomain:(NSString *)domain type:(NSString *)type name:(NSString *)name port:(NSUInteger)port;
// domain and name can be nil or the empty string to get default behaviour.
//
// type must be of the form "_foo._tcp." or "_foo._udp." (possibly without the
// trailing dot, see below).
//
// port must be in the range 1..65535.
//
// domain and type should include the trailing dot; if they don't, one is added
// and that change is reflected in the domain and type properties.
//
// domain and type must not include a leading dot.

// properties that are set up by the init method

@property (copy, readonly ) NSString * domain;
@property (copy, readonly ) NSString * type;
@property (copy, readonly ) NSString * name;
@property (assign, readonly ) NSUInteger port;

// properties that you can change any time

@property (assign, readwrite) id<DNSSDRegistrationDelegate> delegate;

- (void)start;
// Starts the registration process. Does nothing if the registration is currently started.

- (void)stop;
// Stops a registration, deregistering the service from the network. Does nothing if the
// registration is not started.

// properties that are set up once the registration is in place

@property (copy, readonly ) NSString * registeredDomain;
@property (copy, readonly ) NSString * registeredName;

@end

@protocol DNSSDRegistrationDelegate <NSObject>

// All delegate methods are called on the main thread.

@optional

- (void)dnssdRegistrationWillRegister:(DNSSDRegistration *)sender;
// Called before the registration process starts.

- (void)dnssdRegistrationDidRegister:(DNSSDRegistration *)sender;
// Called when the service is successfully registered. At this point
// registeredName and registeredDomain are valid.

- (void)dnssdRegistration:(DNSSDRegistration *)sender didNotRegister:(NSError *)error;
// Called when the service can't be registered. The registration will be stopped
// immediately after this delegate method returns.

- (void)dnssdRegistrationDidStop:(DNSSDRegistration *)sender;
// Called when the registration stops (except if you call -stop on it).

@end
265 changes: 265 additions & 0 deletions Core/DNSSDRegistration.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
/*
File: DNSSDRegistration.m

Contains: Uses the low-level DNS-SD API to manage a Bonjour service registration.

Written by: DTS

Copyright: Copyright (c) 2011 Apple Inc. All Rights Reserved.

Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of
these terms. If you do not agree with these terms, please do
not use, install, modify or redistribute this Apple software.

In consideration of your agreement to abide by the following
terms, and subject to these terms, Apple grants you a personal,
non-exclusive license, under Apple's copyrights in this
original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or
without modifications, in source and/or binary forms; provided
that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the
following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks
or logos of Apple Inc. may be used to endorse or promote
products derived from the Apple Software without specific prior
written permission from Apple. Except as expressly stated in
this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or
by other works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis.
APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING
THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY
OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

*/

#import "DNSSDRegistration.h"
#import <dns_sd.h>

#pragma mark * DNSSDRegistration

@interface DNSSDRegistration ()

// read-write versions of public properties

@property (copy, readwrite) NSString * registeredDomain;
@property (copy, readwrite) NSString * registeredName;

// private properties

@property (assign, readwrite) DNSServiceRef sdRef;

// forward declarations

- (void)stopWithError:(NSError *)error notify:(BOOL)notify;

@end

@implementation DNSSDRegistration

@synthesize domain = domain_;
@synthesize type = type_;
@synthesize name = name_;
@synthesize port = port_;

@synthesize delegate = delegate_;

@synthesize registeredDomain = registeredDomain_;
@synthesize registeredName = registeredName_;

@synthesize sdRef = sdRef_;

- (id)initWithDomain:(NSString *)domain type:(NSString *)type name:(NSString *)name port:(NSUInteger)port
// See comment in header.
{
// domain may be nil or empty
assert( ! [domain hasPrefix:@"."] );
assert(type != nil);
assert([type length] != 0);
assert( ! [type hasPrefix:@"."] );
assert(port > 0);
assert(port < 65536);

self = [super init];
if (self != nil) {
if ( ([domain length] != 0) && ! [domain hasSuffix:@"."] ) {
domain = [domain stringByAppendingString:@"."];
}
if ( ! [type hasSuffix:@"."] ) {
type = [type stringByAppendingString:@"."];
}
self->domain_ = [domain copy];
self->type_ = [type copy];
self->name_ = [name copy];
self->port_ = port;
}
return self;
}

- (void)dealloc
{
if (self->sdRef_ != NULL) {
DNSServiceRefDeallocate(self->sdRef_);
}
}

- (void)didRegisterWithDomain:(NSString *)domain name:(NSString *)name
// Called when DNS-SD tells us that a registration has been added.
{
// On the Mac this routine can get called multiple times, once for the "local." domain and again
// for wide-area domains. As a matter of policy we ignore everything except the "local."
// domain. The "local." domain is really the flagship domain here; that's what our clients
// care about. If it works but a wide-area registration fails, we don't want to error out.
// Conversely, if a wide-area registration succeeds but the "local." domain fails, that
// is a good reason to fail totally.

assert([domain caseInsensitiveCompare:@"local"] != NSOrderedSame); // DNS-SD always gives us the trailing dot; complain otherwise.

if ( [domain caseInsensitiveCompare:@"local."] == NSOrderedSame ) {
self.registeredDomain = domain;
self.registeredName = name;
if ([self.delegate respondsToSelector:@selector(dnssdRegistrationDidRegister:)]) {
[self.delegate dnssdRegistrationDidRegister:self];
}
}
}

- (void)didUnregisterWithDomain:(NSString *)domain name:(NSString *)name
// Called when DNS-SD tells us that a registration has been removed.
{
#pragma unused(name)

// The registration can be removed in the following cases:
//
// A. When you register with the default name (that is, passing the empty string
// to the "name" parameter of DNSServiceRegister) /and/ you specify the 'no rename'
// flags (kDNSServiceFlagsNoAutoRename) /and/ the computer name changes.
//
// B. When you successfully register in a domain and then the domain becomes unavailable
// (for example, if you turn off Back to My Mac after registering).
//
// Case B we ignore based on the same policy outlined in -didRegisterWithDomain:name:.
//
// Case A is interesting and we handle it here.

assert([domain caseInsensitiveCompare:@"local"] != NSOrderedSame); // DNS-SD always gives us the trailing dot; complain otherwise.

if ( [domain caseInsensitiveCompare:@"local."] == NSOrderedSame ) {
[self stopWithError:[NSError errorWithDomain:NSNetServicesErrorDomain code:kDNSServiceErr_NameConflict userInfo:nil] notify:YES];
}
}

static void RegisterReplyCallback(
DNSServiceRef sdRef,
DNSServiceFlags flags,
DNSServiceErrorType errorCode,
const char * name,
const char * regtype,
const char * domain,
void * context
)
// Called by DNS-SD when something happens with the registered service.
{
DNSSDRegistration * obj;
#pragma unused(flags)
#pragma unused(regtype)

assert([NSThread isMainThread]); // because sdRef dispatches to the main queue

obj = (__bridge DNSSDRegistration *) context;
assert([obj isKindOfClass:[DNSSDRegistration class]]);
assert(sdRef == obj->sdRef_);
#pragma unused(sdRef)

if (errorCode == kDNSServiceErr_NoError) {
assert([[NSString stringWithUTF8String:regtype] caseInsensitiveCompare:obj.type] == NSOrderedSame);
if (flags & kDNSServiceFlagsAdd) {
[obj didRegisterWithDomain:[NSString stringWithUTF8String:domain] name:[NSString stringWithUTF8String:name]];
} else {
[obj didUnregisterWithDomain:[NSString stringWithUTF8String:domain] name:[NSString stringWithUTF8String:name]];
}
} else {
[obj stopWithError:[NSError errorWithDomain:NSNetServicesErrorDomain code:errorCode userInfo:nil] notify:YES];
}
}

- (void)start
// See comment in header.
{
DNSServiceErrorType errorCode;

if (self.sdRef == NULL) {
NSString * name;
NSString * domain;

domain = self.domain;
if (domain == nil) {
domain = @"";
}
name = self.name;
if (name == nil) {
name = @"";
}

errorCode = DNSServiceRegister(&self->sdRef_, kDNSServiceFlagsIncludeP2P, kDNSServiceInterfaceIndexAny, [name UTF8String], [self.type UTF8String], [domain UTF8String], NULL, htons(self.port), 0, NULL, RegisterReplyCallback, (__bridge void *)(self));
if (errorCode == kDNSServiceErr_NoError) {
errorCode = DNSServiceSetDispatchQueue(self.sdRef, dispatch_get_main_queue());
}
if (errorCode == kDNSServiceErr_NoError) {
if ([self.delegate respondsToSelector:@selector(dnssdRegistrationWillRegister:)]) {
[self.delegate dnssdRegistrationWillRegister:self];
}
} else {
[self stopWithError:[NSError errorWithDomain:NSNetServicesErrorDomain code:errorCode userInfo:nil] notify:YES];
}
}
}

- (void)stopWithError:(NSError *)error notify:(BOOL)notify
// An internal bottleneck for shutting down the object.
{
if (notify) {
if (error != nil) {
if ([self.delegate respondsToSelector:@selector(dnssdRegistration:didNotRegister:)]) {
[self.delegate dnssdRegistration:self didNotRegister:error];
}
}
}
self.registeredDomain = nil;
self.registeredName = nil;
if (self.sdRef != NULL) {
DNSServiceRefDeallocate(self.sdRef);
self.sdRef = NULL;
}
if (notify) {
if ([self.delegate respondsToSelector:@selector(dnssdRegistrationDidStop:)]) {
[self.delegate dnssdRegistrationDidStop:self];
}
}
}

- (void)stop
// See comment in header.
{
[self stopWithError:nil notify:NO];
}

@end
Loading