Bonjour/MYBonjourRegistration.m
changeset 60 dd637bdd214e
parent 59 46c7844cb592
child 62 8713f2d6a4c5
     1.1 --- a/Bonjour/MYBonjourRegistration.m	Mon Jul 20 13:26:29 2009 -0700
     1.2 +++ b/Bonjour/MYBonjourRegistration.m	Mon Jul 20 14:50:49 2009 -0700
     1.3 @@ -19,6 +19,7 @@
     1.4  
     1.5  @interface MYBonjourRegistration ()
     1.6  @property BOOL registered;
     1.7 +- (void) _updateNullRecord;
     1.8  @end
     1.9  
    1.10  
    1.11 @@ -125,7 +126,8 @@
    1.12      NSData *txtData = nil;
    1.13      if (_txtRecord)
    1.14          txtData = [NSNetService dataFromTXTRecordDictionary: _txtRecord];
    1.15 -    return DNSServiceRegister(sdRefPtr,
    1.16 +    DNSServiceErrorType err;
    1.17 +    err = DNSServiceRegister(sdRefPtr,
    1.18                                flags,
    1.19                                0,
    1.20                                _name.UTF8String,         // _name is likely to be nil
    1.21 @@ -137,11 +139,20 @@
    1.22                                txtData.bytes,
    1.23                                &regCallback,
    1.24                                self);
    1.25 +    if (!err && _nullRecord)
    1.26 +        [self _updateNullRecord];
    1.27 +    return err;
    1.28  }
    1.29  
    1.30  
    1.31  - (void) cancel {
    1.32 +    if (_nullRecordReg && self.serviceRef) {
    1.33 +        DNSServiceRemoveRecord(self.serviceRef, _nullRecordReg, 0);
    1.34 +        _nullRecordReg = NULL;
    1.35 +    }
    1.36 +    
    1.37      [super cancel];
    1.38 +  
    1.39      if (_registered) {
    1.40          [[self class] priv_removeRegistration: self];
    1.41          self.registered = NO;
    1.42 @@ -269,6 +280,44 @@
    1.43      }
    1.44  }
    1.45  
    1.46 +
    1.47 +- (NSData*) nullRecord {
    1.48 +  return _nullRecord;
    1.49 +}
    1.50 +
    1.51 +- (void) setNullRecord: (NSData*)nullRecord {
    1.52 +    if (ifSetObj(&_nullRecord, nullRecord))
    1.53 +        if (self.serviceRef)
    1.54 +            [self _updateNullRecord];
    1.55 +}
    1.56 +
    1.57 +
    1.58 +- (void) _updateNullRecord {
    1.59 +    DNSServiceRef serviceRef = self.serviceRef;
    1.60 +    Assert(serviceRef);
    1.61 +    DNSServiceErrorType err = 0;
    1.62 +    if (!_nullRecord) {
    1.63 +        if (_nullRecordReg) {
    1.64 +            err = DNSServiceRemoveRecord(serviceRef, _nullRecordReg, 0);
    1.65 +            _nullRecordReg = NULL;
    1.66 +        }
    1.67 +    } else if (!_nullRecordReg) {
    1.68 +        err = DNSServiceAddRecord(serviceRef, &_nullRecordReg, 0,
    1.69 +                                  kDNSServiceType_NULL, 
    1.70 +                                  _nullRecord.length, _nullRecord.bytes, 
    1.71 +                                  0);
    1.72 +    } else {
    1.73 +        err = DNSServiceUpdateRecord(serviceRef, _nullRecordReg, 0,
    1.74 +                                     _nullRecord.length, _nullRecord.bytes, 
    1.75 +                                     0);
    1.76 +    }
    1.77 +    if (err)
    1.78 +        Warn(@"MYBonjourRegistration: Couldn't update NULL record, err=%i",err);
    1.79 +    else
    1.80 +        LogTo(DNS, @"MYBonjourRegistration: Set NULL record (%u bytes) %@",
    1.81 +              _nullRecord.length, _nullRecord);
    1.82 +}
    1.83 +
    1.84  @end
    1.85  
    1.86