aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPSSHTunnel.m
diff options
context:
space:
mode:
authorMax <post@wickenrode.com>2015-05-01 02:08:48 +0200
committerMax <post@wickenrode.com>2015-05-01 02:08:48 +0200
commit80a2088f43bd3a901fb20d714318d4b5d3dc60a9 (patch)
tree1325fbf84467ac56789b9c10a947ef4772214736 /Source/SPSSHTunnel.m
parentace145be795f25dfa42080e459a63cc0c885a9fa (diff)
downloadsequelpro-80a2088f43bd3a901fb20d714318d4b5d3dc60a9.tar.gz
sequelpro-80a2088f43bd3a901fb20d714318d4b5d3dc60a9.tar.bz2
sequelpro-80a2088f43bd3a901fb20d714318d4b5d3dc60a9.zip
Significantly reduce risk of a race condition in a certain setup (see #2107)
* Replaced a run loop sleep with a thread sleep as it was on a background thread where a) a regular sleep is fine, b) the run loop sleep did not seem to have any effect * While we are at it, cut done some duplicate code * Removed a redundant if()
Diffstat (limited to 'Source/SPSSHTunnel.m')
-rw-r--r--Source/SPSSHTunnel.m56
1 files changed, 27 insertions, 29 deletions
diff --git a/Source/SPSSHTunnel.m b/Source/SPSSHTunnel.m
index 8e9ffb13..d2c999c4 100644
--- a/Source/SPSSHTunnel.m
+++ b/Source/SPSSHTunnel.m
@@ -40,6 +40,8 @@
#import <netinet/in.h>
#import <CommonCrypto/CommonDigest.h>
+static unsigned short getRandomPort();
+
@implementation SPSSHTunnel
@synthesize passwordPromptCancelled;
@@ -264,36 +266,10 @@
// If no local port has yet been chosen, choose one
if (!localPort) {
- int tempSocket;
- struct sockaddr_in tempSocketAddress;
- size_t addressLength = sizeof(tempSocketAddress);
- if((tempSocket = socket(AF_INET, SOCK_STREAM, 0)) > 0) {
- memset(&tempSocketAddress, 0, sizeof(tempSocketAddress));
- tempSocketAddress.sin_family = AF_INET;
- tempSocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
- tempSocketAddress.sin_port = 0;
- if (bind(tempSocket, (struct sockaddr *)&tempSocketAddress, (socklen_t)addressLength) >= 0) {
- if (getsockname(tempSocket, (struct sockaddr *)&tempSocketAddress, (uint32_t *)&addressLength) >= 0) {
- localPort = ntohs(tempSocketAddress.sin_port);
- }
- }
- close(tempSocket);
- }
+ localPort = getRandomPort();
if (useHostFallback) {
- if((tempSocket = socket(AF_INET, SOCK_STREAM, 0)) > 0) {
- memset(&tempSocketAddress, 0, sizeof(tempSocketAddress));
- tempSocketAddress.sin_family = AF_INET;
- tempSocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
- tempSocketAddress.sin_port = 0;
- if (bind(tempSocket, (struct sockaddr *)&tempSocketAddress, (socklen_t)addressLength) >= 0) {
- if (getsockname(tempSocket, (struct sockaddr *)&tempSocketAddress, (uint32_t *)&addressLength) >= 0) {
- localPortFallback = ntohs(tempSocketAddress.sin_port);
- }
- }
- close(tempSocket);
- }
-
+ localPortFallback = getRandomPort();
}
// Abort if no local free port could be allocated
@@ -748,7 +724,7 @@
{
delegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
- if (connectionState != SPMySQLProxyIdle) [self disconnect];
+ [self disconnect];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
SPClear(sshHost);
SPClear(sshLogin);
@@ -775,3 +751,25 @@
}
@end
+
+#pragma mark -
+
+unsigned short getRandomPort() {
+ int port = 0;
+ int tempSocket;
+ struct sockaddr_in tempSocketAddress;
+ size_t addressLength = sizeof(tempSocketAddress);
+ if((tempSocket = socket(AF_INET, SOCK_STREAM, 0)) > 0) {
+ memset(&tempSocketAddress, 0, sizeof(tempSocketAddress));
+ tempSocketAddress.sin_family = AF_INET;
+ tempSocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
+ tempSocketAddress.sin_port = 0;
+ if (bind(tempSocket, (struct sockaddr *)&tempSocketAddress, (socklen_t)addressLength) >= 0) {
+ if (getsockname(tempSocket, (struct sockaddr *)&tempSocketAddress, (uint32_t *)&addressLength) >= 0) {
+ port = ntohs(tempSocketAddress.sin_port);
+ }
+ }
+ close(tempSocket);
+ }
+ return port;
+}