Plugins/Bonjour/libezv/Private Classes/AWEzvContactManager.m
author Evan Schoenberg
Sat Oct 31 12:31:33 2009 -0500 (2009-10-31)
changeset 2706 13898cc883cd
parent 1572 b2faf3d4b0d4
child 2709 38de51f6428e
permissions -rw-r--r--
If Bonjour disconnects but the user opens a contact window in the moment before the contact disappears from the list, it wass possible to attempt to open with a nil avInstanceName. Furthermore, if Bonjour disconnects, attempting to send a message to a still-open Bonjour chat could try to reference avInstanceName which would previously be nil after disconnect.

There's no reason to be so aggressive with having avInstanceName only be set while logged in; it's a single string, and it doesn't generally change. We'll create it in init and destroy it in dealloc for AWEzvContactManager.

Fixes #11799 fully.
     1 /*
     2  * Project:     Libezv
     3  * File:        AWEzvContactManager.m
     4  *
     5  * Version:     1.0
     6  * Author:      Andrew Wellington <proton[at]wiretapped.net>
     7  *
     8  * License:
     9  * Copyright (C) 2004-2005 Andrew Wellington.
    10  * All rights reserved.
    11  * 
    12  * Redistribution and use in source and binary forms, with or without
    13  * modification, are permitted provided that the following conditions are met:
    14  * 
    15  * 1. Redistributions of source code must retain the above copyright notice,
    16  * this list of conditions and the following disclaimer.
    17  * 2. Redistributions in binary form must reproduce the above copyright notice,
    18  * this list of conditions and the following disclaimer in the documentation
    19  * and/or other materials provided with the distribution.
    20  * 
    21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
    22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
    24  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    30  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31  */
    32 
    33 #import "AWEzvContactManager.h"
    34 #import "AWEzvContactPrivate.h"
    35 #import "AWEzvXMLStream.h"
    36 
    37 @implementation AWEzvContactManager
    38 
    39 - (id)initWithClient:(AWEzv *)newClient 
    40 {
    41     if ((self = [super init])) {
    42 		contacts = [[NSMutableDictionary alloc] init];
    43 		client = newClient;
    44 		isConnected = NO;
    45 		
    46 		/* find username and computer name */
    47 		CFStringRef consoleUser = SCDynamicStoreCopyConsoleUser(NULL, NULL, NULL);
    48 		CFStringRef computerName = SCDynamicStoreCopyLocalHostName(NULL);
    49 		if (!computerName) {
    50 			/* computerName can return NULL if the computer name is not set or an error occurs */
    51 			CFUUIDRef	uuid;
    52 			
    53 			uuid = CFUUIDCreate(NULL);
    54 			computerName = CFUUIDCreateString(NULL, uuid);
    55 			CFRelease(uuid);		
    56 		}
    57 		avInstanceName = [[NSString alloc] initWithFormat:@"%@@%@",
    58 						  (consoleUser ? (NSString *)consoleUser : @""),
    59 						  (computerName ? (NSString *)computerName : @"")];
    60 		if (consoleUser) CFRelease(consoleUser);
    61 		if (computerName) CFRelease(computerName);		
    62 	}
    63 
    64     return self;
    65 }
    66 
    67 - (AWEzvContact *)contactForIdentifier:(NSString *)uniqueID {
    68     AWEzvContact *contact = [contacts objectForKey:uniqueID];
    69     /* try a case insensitive search if not found */
    70     if (!contact) {
    71 		for (contact in [contacts allValues]) {
    72 			if ([contact.uniqueID caseInsensitiveCompare:uniqueID] == NSOrderedSame)
    73 				break;
    74 		}
    75 	}
    76     return contact;
    77 }
    78 
    79 - (void)closeConnections {
    80 	for (AWEzvContact *contact in [contacts allValues]) {
    81 		if (contact.stream)
    82 			[contact.stream endConnection];
    83 	}
    84 }
    85 
    86 @synthesize client;
    87 
    88 - (void)dealloc {
    89 	/* AWEzvContactManagerListener adds an observer; remove it */
    90 	[[NSNotificationCenter defaultCenter] removeObserver:self];
    91 
    92 	[userAnnounceData release]; userAnnounceData = nil;
    93 	[avInstanceName release]; avInstanceName = nil;
    94 
    95 	[super dealloc];
    96 }
    97 
    98 - (NSString *)myInstanceName {
    99 	NSAssert(NO, @"AWEzvContactManager -myInstanceName: This should not be reached");
   100 	return nil;
   101 }
   102 
   103 @end