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