Fixed two CF memory leaks. (Fixes issue #5)
5 // Created by Jens Alfke on 4/24/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
9 #import "MYBonjourQuery.h"
10 #import "MYBonjourService.h"
13 #import "ExceptionUtils.h"
17 static NSString* kRecordTypeNames[] = {
19 @"A", // = 1, /* Host address. */
20 @"NS", // = 2, /* Authoritative server. */
21 @"MD", // = 3, /* Mail destination. */
22 @"MF", // = 4, /* Mail forwarder. */
23 @"CNAME", // = 5, /* Canonical name. */
24 @"SOA", // = 6, /* Start of authority zone. */
25 @"MB", // = 7, /* Mailbox domain name. */
26 @"MG", // = 8, /* Mail group member. */
27 @"MR", // = 9, /* Mail rename name. */
28 @"NULL", // = 10, /* Null resource record. */
29 @"WKS", // = 11, /* Well known service. */
30 @"PTR", // = 12, /* Domain name pointer. */
31 @"HINFO", // = 13, /* Host information. */
32 @"MINFO", // = 14, /* Mailbox information. */
33 @"MX", // = 15, /* Mail routing information. */
34 @"TXT" // = 16, /* One or more text strings (NOT "zero or more..."). */
35 // this isn't a complete list; it just includes the most common ones.
36 // For the full list, see the "kDNSServiceType_..." constants in <dns_sd.h>.
39 @interface MYBonjourQuery ()
40 @property (copy) NSData *recordData;
44 @implementation MYBonjourQuery
47 - (id) initWithBonjourService: (MYBonjourService*)service recordType: (uint16_t)recordType;
51 _bonjourService = service;
52 _recordType = recordType;
59 [_recordData release];
64 - (NSString*) description
67 if (_recordType <= 16)
68 typeName = kRecordTypeNames[_recordType];
70 typeName = $sprintf(@"%u", _recordType);
71 return $sprintf(@"%@[%@ /%@]", self.class, _bonjourService.name, typeName);
75 @synthesize recordData=_recordData;
78 - (void) priv_gotRecordBytes: (const void *)rdata
79 length: (uint16_t)rdlen
80 type: (uint16_t)rrtype
82 flags: (DNSServiceFlags)flags
84 NSData *data = [NSData dataWithBytes: rdata length: rdlen];
85 if (!$equal(data,_recordData)) {
86 if (data.length <= 16)
87 LogTo(Bonjour,@"%@ = %@", self, data);
89 LogTo(Bonjour,@"%@ = %@...", self, [data subdataWithRange: NSMakeRange(0,16)]);
90 self.recordData = data;
92 [_bonjourService queryDidUpdate: self];
96 static void queryCallback( DNSServiceRef DNSServiceRef,
97 DNSServiceFlags flags,
98 uint32_t interfaceIndex,
99 DNSServiceErrorType errorCode,
100 const char *fullname,
109 //LogTo(Bonjour, @"queryCallback for %@ (err=%i)", context,errorCode);
111 [(MYBonjourQuery*)context setError: errorCode];
113 [(MYBonjourQuery*)context priv_gotRecordBytes: rdata
118 }catchAndReport(@"MYBonjourResolver query callback");
122 - (DNSServiceRef) createServiceRef {
123 DNSServiceRef serviceRef = NULL;
124 const char *fullName = _bonjourService.fullName.UTF8String;
126 self.error = DNSServiceQueryRecord(&serviceRef, 0,
127 _bonjourService.interfaceIndex,
129 _recordType, kDNSServiceClass_IN,
130 &queryCallback, self);