Skip to content

Commit

Permalink
Fixed socket creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Frugghi committed Apr 20, 2017
1 parent 9d86e49 commit 25a0706
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
10 changes: 10 additions & 0 deletions Examples/Examples.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 40 additions & 15 deletions Examples/PTYExample/PTYExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
18221C5A1EA9080300C489F4 /* NMSSH.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18221C591EA9080300C489F4 /* NMSSH.framework */; };
18221C5B1EA9080300C489F4 /* NMSSH.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18221C591EA9080300C489F4 /* NMSSH.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
183E6A341B38510500727F97 /* Launch Screen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 183E6A331B38510500727F97 /* Launch Screen.xib */; };
18AE8FA918BBEDD000AE0FC3 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE8FA818BBEDD000AE0FC3 /* Foundation.framework */; };
18AE8FAB18BBEDD000AE0FC3 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE8FAA18BBEDD000AE0FC3 /* CoreGraphics.framework */; };
Expand All @@ -17,13 +19,27 @@
18AE8FBC18BBEDD100AE0FC3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18AE8FBA18BBEDD100AE0FC3 /* Main.storyboard */; };
18AE8FBF18BBEDD100AE0FC3 /* NMViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE8FBE18BBEDD100AE0FC3 /* NMViewController.m */; };
18AE8FC118BBEDD100AE0FC3 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18AE8FC018BBEDD100AE0FC3 /* Images.xcassets */; };
18AE8FDE18BBF0A100AE0FC3 /* NMSSH.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE8FDD18BBF0A100AE0FC3 /* NMSSH.framework */; };
18AE8FE018BBF0C100AE0FC3 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE8FDF18BBF0C100AE0FC3 /* libz.dylib */; };
18AE8FE218BBF0C700AE0FC3 /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE8FE118BBF0C700AE0FC3 /* CFNetwork.framework */; };
18AE8FE518BBF11B00AE0FC3 /* NMTerminalViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE8FE418BBF11B00AE0FC3 /* NMTerminalViewController.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
18221C5C1EA9080300C489F4 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
18221C5B1EA9080300C489F4 /* NMSSH.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
18221C591EA9080300C489F4 /* NMSSH.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = NMSSH.framework; sourceTree = BUILT_PRODUCTS_DIR; };
183E6A331B38510500727F97 /* Launch Screen.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = "Launch Screen.xib"; sourceTree = "<group>"; };
18AE8FA518BBEDD000AE0FC3 /* PTYExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PTYExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
18AE8FA818BBEDD000AE0FC3 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand All @@ -40,7 +56,6 @@
18AE8FBE18BBEDD100AE0FC3 /* NMViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NMViewController.m; sourceTree = "<group>"; };
18AE8FC018BBEDD100AE0FC3 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
18AE8FC718BBEDD100AE0FC3 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
18AE8FDD18BBF0A100AE0FC3 /* NMSSH.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NMSSH.framework; path = ../../Products/NMSSH.framework; sourceTree = "<group>"; };
18AE8FDF18BBF0C100AE0FC3 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = usr/lib/libz.dylib; sourceTree = SDKROOT; };
18AE8FE118BBF0C700AE0FC3 /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; };
18AE8FE318BBF11B00AE0FC3 /* NMTerminalViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NMTerminalViewController.h; sourceTree = "<group>"; };
Expand All @@ -54,7 +69,7 @@
files = (
18AE8FE218BBF0C700AE0FC3 /* CFNetwork.framework in Frameworks */,
18AE8FE018BBF0C100AE0FC3 /* libz.dylib in Frameworks */,
18AE8FDE18BBF0A100AE0FC3 /* NMSSH.framework in Frameworks */,
18221C5A1EA9080300C489F4 /* NMSSH.framework in Frameworks */,
18AE8FAB18BBEDD000AE0FC3 /* CoreGraphics.framework in Frameworks */,
18AE8FAD18BBEDD000AE0FC3 /* UIKit.framework in Frameworks */,
18AE8FA918BBEDD000AE0FC3 /* Foundation.framework in Frameworks */,
Expand Down Expand Up @@ -84,9 +99,9 @@
18AE8FA718BBEDD000AE0FC3 /* Frameworks */ = {
isa = PBXGroup;
children = (
18221C591EA9080300C489F4 /* NMSSH.framework */,
18AE8FDF18BBF0C100AE0FC3 /* libz.dylib */,
18AE8FE118BBF0C700AE0FC3 /* CFNetwork.framework */,
18AE8FDD18BBF0A100AE0FC3 /* NMSSH.framework */,
18AE8FA818BBEDD000AE0FC3 /* Foundation.framework */,
18AE8FAA18BBEDD000AE0FC3 /* CoreGraphics.framework */,
18AE8FAC18BBEDD000AE0FC3 /* UIKit.framework */,
Expand Down Expand Up @@ -133,6 +148,7 @@
18AE8FA118BBEDD000AE0FC3 /* Sources */,
18AE8FA218BBEDD000AE0FC3 /* Frameworks */,
18AE8FA318BBEDD000AE0FC3 /* Resources */,
18221C5C1EA9080300C489F4 /* Embed Frameworks */,
);
buildRules = (
);
Expand All @@ -150,7 +166,7 @@
isa = PBXProject;
attributes = {
CLASSPREFIX = NM;
LastUpgradeCheck = 0510;
LastUpgradeCheck = 0830;
ORGANIZATIONNAME = "Nine Muses";
};
buildConfigurationList = 18AE8FA018BBEDD000AE0FC3 /* Build configuration list for PBXProject "PTYExample" */;
Expand Down Expand Up @@ -232,13 +248,19 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand All @@ -251,7 +273,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
};
Expand All @@ -270,20 +292,25 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
};
Expand All @@ -294,13 +321,12 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)/../../Products\"",
);
DEVELOPMENT_TEAM = "";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "PTYExample/PTYExample-Prefix.pch";
INFOPLIST_FILE = "PTYExample/PTYExample-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "se.ninemuses.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -311,13 +337,12 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)/../../Products\"",
);
DEVELOPMENT_TEAM = "";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "PTYExample/PTYExample-Prefix.pch";
INFOPLIST_FILE = "PTYExample/PTYExample-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "se.ninemuses.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand Down
2 changes: 1 addition & 1 deletion Examples/PTYExample/PTYExample/PTYExample-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>se.ninemuses.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
2 changes: 1 addition & 1 deletion NMSSH-iOS.xcodeproj/xcshareddata/xcschemes/NMSSH.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0830"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
18 changes: 7 additions & 11 deletions NMSSH/NMSSHSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ - (BOOL)connectWithTimeout:(NSNumber *)timeout {
NSInteger port = [self.port integerValue];
NSArray *addresses = [self hostIPAddresses];
CFSocketError error = 1;
struct sockaddr_storage *address = NULL;

CFDataRef address = NULL;
SInt32 addressFamily;

while (addresses && ++index < [addresses count] && error) {
Expand All @@ -233,27 +232,22 @@ - (BOOL)connectWithTimeout:(NSNumber *)timeout {
[addressData getBytes:&address4 length:sizeof(address4)];
address4.sin_port = htons(port);

address = (struct sockaddr_storage *)(&address4);
address->ss_len = sizeof(address4);

char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(address4.sin_addr), str, INET_ADDRSTRLEN);
ipAddress = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
addressFamily = AF_INET;
address = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&address4, sizeof(address4));
} // IPv6
else if([addressData length] == sizeof(struct sockaddr_in6)) {
struct sockaddr_in6 address6;
[addressData getBytes:&address6 length:sizeof(address6)];
address6.sin6_port = htons(port);

address = (struct sockaddr_storage *)(&address6);
address->ss_len = sizeof(address6);

char str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &(address6.sin6_addr), str, INET6_ADDRSTRLEN);
ipAddress = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
addressFamily = AF_INET6;

address = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&address6, sizeof(address6));
}
else {
NMSSHLogVerbose(@"Unknown address, it's not IPv4 or IPv6!");
Expand All @@ -264,19 +258,21 @@ - (BOOL)connectWithTimeout:(NSNumber *)timeout {
_socket = CFSocketCreate(kCFAllocatorDefault, addressFamily, SOCK_STREAM, IPPROTO_IP, kCFSocketNoCallBack, NULL, NULL);
if (!_socket) {
NMSSHLogError(@"Error creating the socket");
CFRelease(address);
return NO;
}

// Set NOSIGPIPE
int set = 1;
if (setsockopt(CFSocketGetNative(_socket), SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(set)) != 0) {
NMSSHLogError(@"Error setting socket option");
CFRelease(address);
[self disconnect];
return NO;
}


error = CFSocketConnectToAddress(_socket, (__bridge CFDataRef)[NSData dataWithBytes:address length:address->ss_len], [timeout doubleValue]);
error = CFSocketConnectToAddress(_socket, address, [timeout doubleValue]);
CFRelease(address);

if (error) {
NMSSHLogVerbose(@"Socket connection to %@ on port %ld failed with reason %li, trying next address...", ipAddress, (long)port, error);
Expand Down

0 comments on commit 25a0706

Please sign in to comment.