Skip to content

Commit

Permalink
Merge and minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
danieloskarsson committed Jan 4, 2012
2 parents c3d8a56 + 9ccec88 commit b57fd25
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 438 deletions.
2 changes: 1 addition & 1 deletion Classes/SSHWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

}

-(int) connectToHost:(NSString *)host port:(int)port user:(NSString *)user password:(NSString *)password;
-(int) connectToHost:(NSString *)host port:(int)port user:(NSString *)username password:(NSString *)password;
-(int) closeConnection;
-(NSString*)executeCommand:(NSString*)command;
-(void) setPortForwardFromPort:(unsigned int)localPort toHost:(NSString*)remoteHost onPort:(unsigned int)remotePort;
Expand Down
44 changes: 41 additions & 3 deletions Classes/SSHWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,46 @@ void keyboard_interactive(const char *name, int name_len, const char *instr, int

@implementation SSHWrapper

-(int) connectToHost:(NSString *)host port:(int)port user:(NSString *)user password:(NSString *)password {
- (NSString*) resolveHost:(NSString*) hostname {
Boolean result;
NSArray *addresses;
NSString *resolvedHost = nil;
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostname);
if (hostRef) {
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
if (result == TRUE) {
addresses = (NSArray*)CFHostGetAddressing(hostRef, &result);
}
}
if (result == TRUE) {
NSMutableArray *tempDNS = [[NSMutableArray alloc] init];
for(int i = 0; i < CFArrayGetCount((CFArrayRef)addresses); i++){
struct sockaddr_in* remoteAddr;
CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex((CFArrayRef)addresses, i);
remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData);

if(remoteAddr != NULL){
// Extract the ip address
//const char *strIP41 = inet_ntoa(remoteAddr->sin_addr);
NSString *strDNS =[NSString stringWithCString:inet_ntoa(remoteAddr->sin_addr) encoding:NSASCIIStringEncoding];
NSLog(@"RESOLVED %d:<%@>", i, strDNS);
[tempDNS addObject:strDNS];

if (resolvedHost == nil) resolvedHost = [strDNS retain];
}
}
} else {
NSLog(@"Not resolved");
}

return [resolvedHost autorelease];
}

- (int) connectToHost:(NSString *)host port:(int)port user:(NSString *)username password:(NSString *)password {
host = [self resolveHost:host];

const char* hostChar = [host cStringUsingEncoding:NSUTF8StringEncoding];
const char* userChar = [user cStringUsingEncoding:NSUTF8StringEncoding];
const char* userChar = [username cStringUsingEncoding:NSUTF8StringEncoding];
const char* passwordChar = [password cStringUsingEncoding:NSUTF8StringEncoding];

char *userAuthList;
Expand Down Expand Up @@ -360,9 +397,10 @@ -(NSString *)executeCommand:(NSString *)command {
do
{
char buffer[0x2000];
rc1 = libssh2_channel_read( channel, buffer, sizeof(buffer) );
rc1 = libssh2_channel_read( channel, buffer, sizeof(buffer) -1 );
if( rc1 > 0 )
{
buffer[rc1] = '\0';
result = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding];
}
}
Expand Down
13 changes: 7 additions & 6 deletions Classes/libssh2_for_iOSAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@
#import <UIKit/UIKit.h>
#import "SSHWrapper.h"

@interface libssh2_for_iOSAppDelegate : NSObject <UIApplicationDelegate> {
@interface libssh2_for_iOSAppDelegate : NSObject <UIApplicationDelegate, UIGestureRecognizerDelegate> {
UIWindow *window;

IBOutlet UITextField *textField;
IBOutlet UITextField *ipField;
IBOutlet UITextField *hostField;
IBOutlet UITextField *userField;
IBOutlet UITextField *passwordField;
IBOutlet UITextView *textView;

IBOutlet UIButton *executeButton;
IBOutlet UIButton *portForwardButton;
SSHWrapper *sshPortForwardWrapper;

IBOutlet UIWebView *webview;
IBOutlet UIButton *portForwardButton;
IBOutlet UIButton *webviewButton;

IBOutlet UIWebView *webview;

SSHWrapper *sshPortForwardWrapper;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
Expand Down
35 changes: 25 additions & 10 deletions Classes/libssh2_for_iOSAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// limitations under the License.

#import "libssh2_for_iOSAppDelegate.h"

#import "SSHWrapper.h"

@implementation libssh2_for_iOSAppDelegate
Expand All @@ -32,11 +31,23 @@ - (void)webViewDidFinishLoad:(UIWebView *)_webView {

#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self.window addGestureRecognizer:tapRecognizer];
[self.window makeKeyAndVisible];
return YES;
}

- (void)handleTap:(UITapGestureRecognizer *)sender {
//if (sender.state == UIGestureRecognizerStateEnded) {}
[textField resignFirstResponder];
[hostField resignFirstResponder];
[userField resignFirstResponder];
[passwordField resignFirstResponder];
}

- (IBAction)go:(id)sender {
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
Expand All @@ -54,7 +65,7 @@ - (IBAction)go:(id)sender {

- (IBAction)portForward:(id)sender {
[textField resignFirstResponder];
[ipField resignFirstResponder];
[hostField resignFirstResponder];
[userField resignFirstResponder];
[passwordField resignFirstResponder];

Expand All @@ -78,7 +89,7 @@ - (IBAction)portForward:(id)sender {
dispatch_async(queue, ^{
if (webview.hidden == NO) {
sshPortForwardWrapper = [[SSHWrapper alloc] init];
[sshPortForwardWrapper connectToHost:ipField.text port:22 user:userField.text password:passwordField.text];
[sshPortForwardWrapper connectToHost:hostField.text port:22 user:userField.text password:passwordField.text];
[sshPortForwardWrapper setPortForwardFromPort:localPort toHost:remoteIp onPort:remotePort];
} else {
[sshPortForwardWrapper closeConnection];
Expand All @@ -90,23 +101,31 @@ - (IBAction)portForward:(id)sender {

- (IBAction)executeCommand:(id)sender {
[textField resignFirstResponder];
[ipField resignFirstResponder];
[hostField resignFirstResponder];
[userField resignFirstResponder];
[passwordField resignFirstResponder];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
SSHWrapper *sshWrapper = [[SSHWrapper alloc] init];
[sshWrapper connectToHost:ipField.text port:22 user:userField.text password:passwordField.text];
[sshWrapper connectToHost:hostField.text port:22 user:userField.text password:passwordField.text];

textView.text = [sshWrapper executeCommand:textField.text];
[sshWrapper closeConnection];
[sshWrapper release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}












- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Expand Down Expand Up @@ -144,10 +163,6 @@ - (void)applicationWillTerminate:(UIApplication *)application {
*/
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
Expand Down
Loading

0 comments on commit b57fd25

Please sign in to comment.