| jens@27 |      1 | //
 | 
| jens@27 |      2 | //  MYDNSService.h
 | 
| jens@27 |      3 | //  MYNetwork
 | 
| jens@27 |      4 | //
 | 
| jens@27 |      5 | //  Created by Jens Alfke on 4/23/09.
 | 
| jens@27 |      6 | //  Copyright 2009 Jens Alfke. All rights reserved.
 | 
| jens@27 |      7 | //
 | 
| jens@27 |      8 | 
 | 
| jens@27 |      9 | #import <Foundation/Foundation.h>
 | 
| jens@27 |     10 | #import <CoreFoundation/CFSocket.h>
 | 
| jens@31 |     11 | @class MYDNSConnection;
 | 
| jens@27 |     12 | 
 | 
| jens@27 |     13 | 
 | 
| jens@27 |     14 | /** Abstract superclass for services based on DNSServiceRefs, such as MYPortMapper. */
 | 
| jens@27 |     15 | @interface MYDNSService : NSObject
 | 
| jens@27 |     16 | {
 | 
| jens@27 |     17 |     @private
 | 
| jens@31 |     18 |     BOOL _usePrivateConnection;
 | 
| jens@31 |     19 |     MYDNSConnection *_connection;
 | 
| jens@27 |     20 |     struct _DNSServiceRef_t *_serviceRef;
 | 
| jens@27 |     21 |     CFSocketRef _socket;
 | 
| jens@27 |     22 |     CFRunLoopSourceRef _socketSource;
 | 
| jens@27 |     23 |     SInt32 _error;
 | 
| jens@31 |     24 |     BOOL _continuous, _gotResponse;
 | 
| jens@27 |     25 | }
 | 
| jens@27 |     26 | 
 | 
| jens@28 |     27 | /** If NO (the default), the service will stop after it gets a result.
 | 
| jens@28 |     28 |     If YES, it will continue to run until stopped. */
 | 
| jens@28 |     29 | @property BOOL continuous;
 | 
| jens@28 |     30 | 
 | 
| jens@27 |     31 | /** Starts the service.
 | 
| jens@27 |     32 |     Returns immediately; you can find out when the service actually starts (or fails to)
 | 
| jens@27 |     33 |     by observing the isOpen and error properties.
 | 
| jens@27 |     34 |     It's very unlikely that this call itself will fail (return NO). If it does, it
 | 
| jens@27 |     35 |     probably means that the mDNSResponder process isn't working. */
 | 
| jens@28 |     36 | - (BOOL) start;
 | 
| jens@27 |     37 | 
 | 
| jens@28 |     38 | /** Stops the service. */
 | 
| jens@28 |     39 | - (void) stop;
 | 
| jens@27 |     40 | 
 | 
| jens@27 |     41 | 
 | 
| jens@27 |     42 | /** The error status, a DNSServiceErrorType enum; nonzero if something went wrong. 
 | 
| jens@27 |     43 |     This property is KV observable. */
 | 
| jens@28 |     44 | @property int32_t error;
 | 
| jens@27 |     45 | 
 | 
| jens@31 |     46 | 
 | 
| jens@31 |     47 | /** Utility to construct a service's full name. */
 | 
| jens@31 |     48 | + (NSString*) fullNameOfService: (NSString*)serviceName
 | 
| jens@31 |     49 |                          ofType: (NSString*)type
 | 
| jens@31 |     50 |                        inDomain: (NSString*)domain;
 | 
| jens@31 |     51 | 
 | 
| jens@31 |     52 | 
 | 
| jens@32 |     53 | /** @name Protected
 | 
| jens@32 |     54 |  *  Methods for use only by subclasses
 | 
| jens@32 |     55 |  */
 | 
| jens@32 |     56 | //@{
 | 
| jens@31 |     57 | 
 | 
| jens@32 |     58 | /** Normally, all DNSService objects use a shared IPC connection to the mDNSResponder daemon.
 | 
| jens@32 |     59 |     If an instance wants to use its own connection instead, it can set this property to YES before
 | 
| jens@32 |     60 |     it starts. If it does so, it must NOT set the kDNSServiceFlagsShareConnection flag when creating
 | 
| jens@32 |     61 |     its underlying DNSService.
 | 
| jens@32 |     62 |     This functionality is only provided because MYBonjourRegistration needs it -- there's a bug
 | 
| jens@32 |     63 |     that prevents DNSServiceUpdateRecord from working with a shared connection. */
 | 
| jens@31 |     64 | @property BOOL usePrivateConnection;
 | 
| jens@31 |     65 | 
 | 
| jens@27 |     66 | /** Subclass must implement this abstract method to create a new DNSServiceRef.
 | 
| jens@27 |     67 |     This method is called by -open.
 | 
| jens@31 |     68 |     The implementation MUST pass the given sdRefPtr directly to the DNSService function
 | 
| jens@31 |     69 |     that creates the new ref, without setting it to NULL first.
 | 
| jens@32 |     70 |     It MUST also set the kDNSServiceFlagsShareConnection flag, unless it's already set the
 | 
| jens@32 |     71 |     usePrivateConnection property. */
 | 
| jens@31 |     72 | - (int32_t/*DNSServiceErrorType*/) createServiceRef: (struct _DNSServiceRef_t**)sdRefPtr;
 | 
| jens@31 |     73 | 
 | 
| jens@31 |     74 | /** Subclass's callback must call this method after doing its own work.
 | 
| jens@31 |     75 |     This method will update the error state, and will stop the service if it's not set to be
 | 
| jens@31 |     76 |     continuous. */
 | 
| jens@31 |     77 | - (void) gotResponse: (int32_t/*DNSServiceErrorType*/)errorCode;
 | 
| jens@27 |     78 | 
 | 
| jens@32 |     79 | /** The underlying DNSServiceRef. This will be NULL except while the service is running. */
 | 
| jens@28 |     80 | @property (readonly) struct _DNSServiceRef_t* serviceRef;
 | 
| jens@28 |     81 | 
 | 
| jens@28 |     82 | /** Same as -stop, but does not clear the error property.
 | 
| jens@28 |     83 |     (The stop method actually calls this first.) */
 | 
| jens@28 |     84 | - (void) cancel;
 | 
| jens@28 |     85 | 
 | 
| jens@28 |     86 | /** Block until a message is received from the daemon.
 | 
| jens@28 |     87 |     This will cause the service's callback (defined by the subclass) to be invoked.
 | 
| jens@28 |     88 |     @return  YES if a message is received, NO on error (or if the service isn't started) */
 | 
| jens@28 |     89 | - (BOOL) waitForReply;
 | 
| jens@27 |     90 | 
 | 
| jens@32 |     91 | //@}
 | 
| jens@31 |     92 | 
 | 
| jens@27 |     93 | @end
 | 
| jens@31 |     94 | 
 | 
| jens@31 |     95 | 
 | 
| jens@31 |     96 | 
 | 
| jens@31 |     97 | 
 | 
| jens@31 |     98 | @interface MYDNSConnection : NSObject
 | 
| jens@31 |     99 | {
 | 
| jens@31 |    100 |     struct _DNSServiceRef_t* _connectionRef;
 | 
| jens@31 |    101 |     CFSocketRef _socket;
 | 
| jens@31 |    102 |     CFRunLoopSourceRef _runLoopSource;
 | 
| jens@31 |    103 | }
 | 
| jens@31 |    104 | 
 | 
| jens@31 |    105 | + (MYDNSConnection*) sharedConnection;
 | 
| jens@31 |    106 | - (id) initWithServiceRef: (struct _DNSServiceRef_t *)serviceRef;
 | 
| jens@31 |    107 | @property (readonly) struct _DNSServiceRef_t* connectionRef;
 | 
| jens@31 |    108 | - (BOOL) processResult;
 | 
| jens@31 |    109 | - (void) close;
 | 
| jens@31 |    110 | 
 | 
| jens@31 |    111 | @end
 |