Permalink
Browse files

adding a method to know which interface will be used to reach the iphone

  • Loading branch information...
1 parent e390a7d commit b75e1a7ede1527ab7c89f2395cbc78d64aa618fe Jérôme Lebel committed Sep 26, 2010
Showing with 31 additions and 0 deletions.
  1. +31 −0 iProxyMacSetup/Classes/iProxyMacSetupAppDelegate.m
@@ -9,6 +9,7 @@
#import "iProxyMacSetupAppDelegate.h"
#define NETWORKSETUP_PATH @"/usr/sbin/networksetup"
+#define ROUTE_PATH @"/sbin/route"
@interface iProxyMacSetupAppDelegate ()
@@ -319,5 +320,35 @@ - (void)disableProxyForInterface:(NSString *)interface
}
}
+- (NSString *)_getInterfaceNameForIP:(NSString *)ip
+{
+ NSTask *task;
+ NSString *result = nil;
+ NSData *data;
+ NSFileHandle *outputFileHandle;
+
+ task = [self taskWithLaunchPath:ROUTE_PATH arguments:[NSArray arrayWithObjects:@"get", ip, nil]];
+ outputFileHandle = [[task standardOutput] fileHandleForReading];
+ [task launch];
+
+ data = [outputFileHandle readDataToEndOfFile];
+ NSString *allLines = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+ NSUInteger index = 0;
+ NSUInteger endOfData = 0;
+ NSUInteger endLine = 0;
+ while (index < [allLines length]) {
+ NSString *line;
+
+ [allLines getLineStart:&index end:&endLine contentsEnd:&endOfData forRange:NSMakeRange(index, 1)];
+ line = [[allLines substringWithRange:NSMakeRange(index, endOfData - index)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
+ if ([line hasPrefix:@"interface:"]) {
+ result = [[line substringFromIndex:[@"interface:" length]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
+ break;
+ }
+ index = endLine + 1;
+ };
+ return result;
+}
+
@end

0 comments on commit b75e1a7

Please sign in to comment.