1.1 --- a/Bonjour/MYBonjourRegistration.m Wed Jul 01 14:14:32 2009 -0700
1.2 +++ b/Bonjour/MYBonjourRegistration.m Mon Jul 20 13:26:29 2009 -0700
1.3 @@ -165,6 +165,65 @@
1.4 }
1.5
1.6
1.7 +static int compareData (id data1, id data2, void *context) {
1.8 + size_t length1 = [data1 length], length2 = [data2 length];
1.9 + int result = memcmp([data1 bytes], [data2 bytes], MIN(length1,length2));
1.10 + if (result==0) {
1.11 + if (length1>length2)
1.12 + result = 1;
1.13 + else if (length1<length2)
1.14 + result = -1;
1.15 + }
1.16 + return result;
1.17 +}
1.18 +
1.19 ++ (NSData*) canonicalFormOfTXTRecordDictionary: (NSDictionary*)txtDict
1.20 +{
1.21 + if (!txtDict)
1.22 + return nil;
1.23 +
1.24 + // First convert keys and values to NSData:
1.25 + NSMutableDictionary *dataDict = $mdict();
1.26 + for (NSString *key in txtDict) {
1.27 + if (![key hasPrefix: @"("]) { // ignore parenthesized keys
1.28 + if (![key isKindOfClass: [NSString class]]) {
1.29 + Warn(@"TXT dictionary cannot have %@ as key", [key class]);
1.30 + return nil;
1.31 + }
1.32 + NSData *keyData = [key dataUsingEncoding: NSUTF8StringEncoding];
1.33 + if (keyData.length > 255) {
1.34 + Warn(@"TXT dictionary key too long: %@", key);
1.35 + return nil;
1.36 + }
1.37 + id value = [txtDict objectForKey: key];
1.38 + if (![value isKindOfClass: [NSData class]]) {
1.39 + value = [[value description] dataUsingEncoding: NSUTF8StringEncoding];
1.40 + }
1.41 + if ([value length] > 255) {
1.42 + Warn(@"TXT dictionary value too long: %@", value);
1.43 + return nil;
1.44 + }
1.45 + [dataDict setObject: value forKey: keyData];
1.46 + }
1.47 + }
1.48 +
1.49 + // Add key/value pairs, sorted by increasing key:
1.50 + NSMutableData *canonical = [NSMutableData dataWithCapacity: 1000];
1.51 + for (NSData *key in [[dataDict allKeys] sortedArrayUsingFunction: compareData context: NULL]) {
1.52 + // Append key prefixed with length:
1.53 + UInt8 length = [key length];
1.54 + [canonical appendBytes: &length length: sizeof(length)];
1.55 + [canonical appendData: key];
1.56 + // Append value prefixed with length:
1.57 + NSData *value = [dataDict objectForKey: key];
1.58 + length = [value length];
1.59 + [canonical appendBytes: &length length: sizeof(length)];
1.60 + [canonical appendData: value];
1.61 + }
1.62 + return canonical;
1.63 +}
1.64 +
1.65 +
1.66 - (void) updateTxtRecord {
1.67 [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(updateTxtRecord) object: nil];
1.68 if (self.serviceRef) {
1.69 @@ -179,7 +238,7 @@
1.70 if (err)
1.71 Warn(@"%@ failed to update TXT (err=%i)", self,err);
1.72 else
1.73 - LogTo(Bonjour,@"%@ updated TXT to %@", self,data);
1.74 + LogTo(Bonjour,@"%@ updated TXT to %u bytes: %@", self,data.length,data);
1.75 }
1.76 }
1.77