Changeset 94

Show
Ignore:
Timestamp:
12/28/05 16:46:03 (3 years ago)
Author:
mrowe
Message:

Clean up formatting and minor logic changes in SSHTunnel.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Libs/SSHTunnel.m

    r77 r94  
    5050- (BOOL)setTunnelHost:(NSString *)host withPort:(int)port andUser:(NSString *)user 
    5151{ 
    52         if(open) 
    53         { 
    54                 return NO; 
    55         } 
    56          
    57         [tunnelHost autorelease]; 
    58         tunnelHost = [[NSString stringWithString:host] retain]; 
     52        if (open) 
     53                return NO; 
     54         
     55        NSString *oldHost = tunnelHost; 
     56        tunnelHost = [host copy]; 
     57        [oldHost release]; 
    5958        tunnelPort = port; 
    60         [tunnelUser autorelease]; 
    61         tunnelUser = [[NSString stringWithString:user] retain]; 
     59         
     60        NSString *oldUser = tunnelUser; 
     61        tunnelUser = [user copy]; 
     62        [oldUser release]; 
    6263 
    6364        return YES; 
     
    6768- (BOOL)setCompression:(BOOL)theBool 
    6869{ 
    69         if(open) 
    70         { 
    71                 return NO; 
    72         } 
     70        if (open) 
     71                return NO; 
    7372 
    7473        compression = theBool; 
    75  
    7674        return YES; 
    7775} 
     
    8078- (BOOL)setRemoteAccess:(BOOL)theBool 
    8179{ 
    82         if(open) 
    83         { 
    84                 return NO; 
    85         } 
     80        if (open) 
     81                return NO; 
    8682         
    8783        remoteAccess = theBool; 
    88          
    8984        return YES; 
    9085} 
     
    9388- (BOOL)addLocalPortForwardWithPort:(int)lport remoteHost:(NSString *)rhost remotePort:(int)rport 
    9489{ 
    95         if((open) || (lport < 1) || (lport > 65535) || (rport < 1) || (rport > 65535)) 
    96         { 
    97                 return NO; 
    98         } 
     90        if (open || lport < 1 || lport > 65535 || rport < 1 || rport > 65535) 
     91                return NO; 
    9992 
    10093        [localPortForwards addObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:lport], rhost, [NSNumber numberWithInt:rport], nil]]; 
    101  
    10294        return YES; 
    10395} 
     
    10698- (BOOL)addRemotePortForwardWithPort:(int)rport localHost:(NSString *)lhost localPort:(int)lport 
    10799{ 
    108         if((open) || (lport < 1) || (lport > 65535) || (rport < 1) || (rport > 65535)) 
    109         { 
    110                 return NO; 
    111         } 
     100        if (open || lport < 1 || lport > 65535 || rport < 1 || rport > 65535) 
     101                return NO; 
    112102 
    113103        [remotePortForwards addObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:rport], lhost, [NSNumber numberWithInt:lport], nil]]; 
    114  
    115104        return YES; 
    116105} 
     
    119108- (BOOL)addDynamicPortForwardWithPort:(int)lport 
    120109{ 
    121         if((open) || (lport < 1) || (lport > 65535)) 
    122         { 
    123                 return NO; 
    124         } 
     110        if (open || lport < 1 || lport > 65535) 
     111                return NO; 
    125112         
    126113        [dynamicPortForwards addObject:[NSNumber numberWithInt:lport]]; 
    127          
    128114        return YES; 
    129115} 
     
    132118- (NSString *)getOutput 
    133119{ 
    134         return [[[NSString alloc] initWithData:[[thePipe fileHandleForReading] readDataToEndOfFile] encoding:NSASCIIStringEncoding] autorelease]; 
     120        return [[[NSString alloc] initWithData:[[thePipe fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding] autorelease]; 
    135121} 
    136122 
     
    139125{ 
    140126 
    141         if((!theSelector) || (!theObject)) 
    142         { 
     127        if (!theSelector || !theObject) 
    143128                return; 
    144         } 
    145129         
    146130        closeSelector = theSelector; 
    147131        closeObject = theObject; 
    148132         
    149         if(theInfo) 
    150         { 
     133        if (theInfo) 
    151134                closeInfo = theInfo; 
    152         } 
    153          
    154135        else 
    155         { 
    156136                closeInfo = nil; 
    157         } 
    158137} 
    159138 
     
    166145- (BOOL)openTunnel 
    167146{ 
    168         int i; 
    169         NSMutableArray *arguments; 
    170  
    171         /* If the PID is > 1, the tunnel should be open. */ 
    172         if([self isOpen]) 
    173         { 
    174                 return NO; 
    175         } 
    176  
    177         if((!tunnelHost) || ([tunnelHost isEqualToString:@""])) 
    178         { 
    179                 return NO; 
    180         } 
     147 
     148        if ([self isOpen]) 
     149                return NO; 
     150 
     151        if (!tunnelHost || [tunnelHost isEqualToString:@""]) 
     152                return NO; 
    181153 
    182154        open = YES; 
     
    195167        NSString *toolPath; 
    196168        NSString *sshPathString = [[NSUserDefaults standardUserDefaults] stringForKey:SSHToolsPathString]; 
    197         if (floor(NSAppKitVersionNumber) != NSAppKitVersionNumber10_3) { 
     169        if (floor(NSAppKitVersionNumber) != NSAppKitVersionNumber10_3) 
    198170                // It's not Panther (i.e. either Jaguar or Tiger) 
    199171                toolPath = [sshPathString stringByAppendingPathComponent:@"ssh"]; 
    200         } else { 
    201                if([dynamicPortForwards count] > 0 && [[sshPathString stringByStandardizingPath] isEqualToString:@"/usr/bin"]) { 
    202                        /* Time to use our internal build */ 
    203                        toolPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ssh"]; 
    204                } else { 
    205                        toolPath = [sshPathString stringByAppendingPathComponent:@"ssh"]; 
    206                 } 
    207         } 
    208         arguments = [NSMutableArray arrayWithObject:toolPath]; 
    209          
    210         for(i=0; i < [localPortForwards count]; i++
    211         { 
    212                 [arguments addObject:[NSString stringWithFormat:@"-L%d:%@:%d", [[[localPortForwards objectAtIndex:i] objectAtIndex:0] intValue],  
    213                                                                                                [[localPortForwards objectAtIndex:i] objectAtIndex:1], 
    214                                                                                                [[[localPortForwards objectAtIndex:i] objectAtIndex:2] intValue]] 
     172        else if([dynamicPortForwards count] > 0 && [[sshPathString stringByStandardizingPath] isEqualToString:@"/usr/bin"]) 
     173         
     174                /* Time to use our internal build */ 
     175                toolPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ssh"]; 
     176        else 
     177                toolPath = [sshPathString stringByAppendingPathComponent:@"ssh"]; 
     178 
     179        NSMutableArray *arguments = [NSMutableArray arrayWithObject:toolPath]; 
     180        NSEnumerator *e = [localPortForwards objectEnumerator]; 
     181        NSArray *portForward; 
     182        while (portForward = [e nextObject]
     183        { 
     184                [arguments addObject:[NSString stringWithFormat:@"-L%d:%@:%d", [[portForward objectAtIndex:0] intValue],  
     185                                                                               [portForward objectAtIndex:1], 
     186                                                                               [[portForward objectAtIndex:2] intValue]] 
    215187                        ]; 
    216188        } 
    217189         
    218         for(i=0; i < [remotePortForwards count]; i++) 
    219         { 
    220                 [arguments addObject:[NSString stringWithFormat:@"-R%d:%@:%d", [[[remotePortForwards objectAtIndex:i] objectAtIndex:0] intValue],  
    221                                                                         [[remotePortForwards objectAtIndex:i] objectAtIndex:1], 
    222                                                                         [[[remotePortForwards objectAtIndex:i] objectAtIndex:2] intValue]] 
     190        e = [remotePortForwards objectEnumerator]; 
     191        while (portForward = [e nextObject]) 
     192        { 
     193                [arguments addObject:[NSString stringWithFormat:@"-R%d:%@:%d", [[portForward objectAtIndex:0] intValue],  
     194                                                                               [portForward objectAtIndex:1], 
     195                                                                               [[portForward objectAtIndex:2] intValue]] 
    223196                        ]; 
    224197        } 
    225198         
    226199        // No dynamic ports under Jaguar 
    227         if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_2) { 
    228                 for(i=0; i < [dynamicPortForwards count]; i++) 
     200        if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_2) 
     201        { 
     202                e = [dynamicPortForwards objectEnumerator]; 
     203                NSNumber *dynamicPortForward; 
     204                while (dynamicPortForward = [e nextObject]) 
    229205                { 
    230206                        [arguments addObject:@"-D"]; 
    231207 
    232208                        /* A bit awkward, but it makes sure a number is given. */ 
    233                         [arguments addObject:[NSString stringWithFormat:@"%d", [[dynamicPortForwards objectAtIndex:i] intValue]]]; 
     209                        [arguments addObject:[NSString stringWithFormat:@"%d", [dynamicPortForward intValue]]]; 
    234210                } 
    235211        } 
    236212 
    237         if((tunnelPort > 0) && (tunnelPort < 65535)
    238         { 
    239                        [arguments addObject:@"-p"]; 
    240                        [arguments addObject:[NSString stringWithFormat:@"%d", tunnelPort]]; 
     213        if (tunnelPort > 0 && tunnelPort < 65535
     214        { 
     215                [arguments addObject:@"-p"]; 
     216                [arguments addObject:[NSString stringWithFormat:@"%d", tunnelPort]]; 
    241217        } 
    242218 
     
    245221        [arguments addObject:@"-x"]; 
    246222 
    247         if(compression) 
    248         { 
     223        if (compression) 
    249224                [arguments addObject:@"-C"]; 
    250         } 
    251          
    252         if(remoteAccess) 
    253         { 
     225         
     226        if (remoteAccess) 
    254227                [arguments addObject:@"-g"]; 
    255         } 
    256228         
    257229        [arguments addObject:@"-o"]; 
    258230        [arguments addObject:@"PreferredAuthentications=hostbased,publickey,password,keyboard-interactive"]; 
    259231         
    260         if((tunnelUser) && (![tunnelUser isEqualToString:@""])) 
    261         { 
    262                         [arguments addObject:[NSString stringWithFormat:@"%@@%@", tunnelUser, tunnelHost]]; 
    263         } 
     232        if (tunnelUser && ![tunnelUser isEqualToString:@""]) 
     233                [arguments addObject:[NSString stringWithFormat:@"%@@%@", tunnelUser, tunnelHost]]; 
    264234 
    265235        else 
    266         { 
    267                         [arguments addObject:[NSString stringWithFormat:@"%@", tunnelHost]]; 
    268         } 
     236                [arguments addObject:[NSString stringWithFormat:@"%@", tunnelHost]]; 
    269237                 
    270238        [tunnel setArguments:arguments]; 
     
    279247                             withValue:[[NSUserDefaults standardUserDefaults] stringForKey:SocketPathString]]; 
    280248 
    281         if((closeSelector) && (closeObject)) { 
     249        if (closeSelector && closeObject) 
    282250                [tunnel handleTerminateWithSelector:closeSelector toObject:closeObject withInfo:closeInfo]; 
    283         } 
    284251         
    285252        thePipe = [[NSPipe alloc] init]; 
     
    288255         
    289256        /* Launch ssh. */ 
    290         if([tunnel launch] == NO) { 
    291                 return NO; 
    292         } 
     257        if (![tunnel launch]) 
     258                return NO; 
    293259         
    294260        open = YES; 
    295  
    296261        return YES; 
    297262} 
     
    299264- (void)closeTunnel 
    300265{        
    301         if((open) && (tunnel)) { 
     266        if (open && tunnel) 
    302267                [tunnel terminate]; 
    303         } 
    304268 
    305269        [tunnel release];