Skip to content
This repository was archived by the owner on Dec 4, 2021. It is now read-only.

Commit 5acd937

Browse files
author
Chris Board
committed
Fix potential socket issue
We noticed that there was a possibility that the client socket could get overwritten so the socket processor thread would lose access to the client socket and cause a crash.
1 parent 0c66222 commit 5acd937

File tree

6 files changed

+77
-17
lines changed

6 files changed

+77
-17
lines changed

MySQLManager-TunnelPlugin_C++/LinuxSocket.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,16 @@ bool LinuxSocket::bindAndStartListening()
142142
@param clientAddr A memset initialised sockaddr_in structure where the client information will be stored when a new client connects
143143
@return SOCKET A socket descriptor of the client connection
144144
*/
145-
int LinuxSocket::acceptClientAndReturnSocket(sockaddr_in *clientAddr)
145+
int *LinuxSocket::acceptClientAndReturnSocket(sockaddr_in *clientAddr)
146146
{
147147
socklen_t clilen = sizeof(clientAddr);
148-
int clientSocket = accept(this->serverSocket, (struct sockaddr *)&clientAddr, &clilen);
149-
if (clientSocket < 0)
148+
//int clientSocket = accept(this->serverSocket, (struct sockaddr *)&clientAddr, &clilen);
149+
int *clientSocket = new int;
150+
*clientSocket = accept(this->serverSocket, (struct sockaddr *)&clientAddr, &clilen);
151+
if (*clientSocket < 0)
150152
{
151153
stringstream logstream;
152-
logstream << "Unable to accept client socket. Error: " << strerror(clientSocket);
154+
logstream << "Unable to accept client socket. Error: " << strerror(*clientSocket);
153155
throw SocketException(logstream.str().c_str());
154156
}
155157
return clientSocket;
@@ -246,6 +248,7 @@ void LinuxSocket::closeSocket(int *socket)
246248
if (*socket != -1)
247249
{
248250
int result = close(*socket);
251+
delete socket;
249252
if (result < 0)
250253
{
251254
stringstream logstream;

MySQLManager-TunnelPlugin_C++/LinuxSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LinuxSocket : public BaseSocket
2020
bool createSocket(int family, int socketType, int protocol, int port, int bufferLength, std::string ipAddress);
2121
bool bindAndStartListening();
2222
int returnSocket();
23-
int acceptClientAndReturnSocket(sockaddr_in *clientAddr);
23+
int *acceptClientAndReturnSocket(sockaddr_in *clientAddr);
2424
int sendToSocket(int *socket, std::string dataToSend);
2525
std::string receiveDataOnSocket(int * socket);
2626
void closeSocket();

MySQLManager-TunnelPlugin_C++/SocketListener.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ void SocketListener::socketListenerThread()
7979
sockaddr_in clientAddr;
8080
memset(&clientAddr, 0, sizeof(sockaddr_in));
8181
#ifdef _WIN32
82-
SOCKET clientSocket = socketManager.acceptClientAndReturnSocket(&clientAddr);
82+
SOCKET *clientSocket = socketManager.acceptClientAndReturnSocket(&clientAddr);
8383
#else
84-
int clientSocket = socketManager.acceptClientAndReturnSocket(&clientAddr);
84+
int *clientSocket = socketManager.acceptClientAndReturnSocket(&clientAddr);
8585
#endif //!_WIN32
8686

8787

@@ -90,15 +90,15 @@ void SocketListener::socketListenerThread()
9090
//1 extra client has connected.
9191
if (statusManager.getApplicationStatus() == StatusManager::ApplicationStatus::Stopping)
9292
{
93-
this->socketManager.closeSocket(&clientSocket);
93+
this->socketManager.closeSocket(clientSocket);
9494
this->socketManager.closeSocket();
9595
return;
9696
}
9797

9898
SocketListener::socketListenerMutex.lock();
9999
SocketProcessor socketProcessor(this->logger, &socketManager);
100100
//socketProcessor.processSocketData(&clientSocket);
101-
std::thread *socketProcessorThread = new thread(&SocketProcessor::processSocketData, &socketProcessor, &clientSocket);
101+
std::thread *socketProcessorThread = new thread(&SocketProcessor::processSocketData, &socketProcessor, clientSocket);
102102
processingThreadList.push_back(socketProcessorThread);
103103
this_thread::sleep_for(chrono::seconds(1));
104104
SocketListener::socketListenerMutex.unlock();

MySQLManager-TunnelPlugin_C++/WindowsSocket.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ bool WindowsSocket::bindAndStartListening(int backlog)
114114
@param clientAddr A memset initialised sockaddr_in structure where the client information will be stored when a new client connects
115115
@return SOCKET A socket descriptor of the client connection
116116
*/
117-
SOCKET WindowsSocket::acceptClientAndReturnSocket(sockaddr_in *clientAddr)
117+
SOCKET *WindowsSocket::acceptClientAndReturnSocket(sockaddr_in *clientAddr)
118118
{
119-
SOCKET clientSocket = INVALID_SOCKET;
119+
SOCKET *clientSocket = new SOCKET();
120+
*clientSocket = INVALID_SOCKET;
120121
//sockaddr_in clientAddr;
121122
socklen_t sin_size = sizeof(struct sockaddr_in);
122-
clientSocket = accept(this->serverSocket, (struct sockaddr*)clientAddr, &sin_size);
123+
*clientSocket = accept(this->serverSocket, (struct sockaddr*)clientAddr, &sin_size);
123124
return clientSocket;
124125
}
125126

@@ -292,13 +293,10 @@ void WindowsSocket::closeSocket(SOCKET *socket)
292293
if (*socket != -1)
293294
{
294295
int result = closesocket(*socket);
296+
delete socket;
295297
if (result < 0)
296298
{
297299
throw SocketException(this->getErrorStringFromErrorCode(result).c_str());
298300
}
299-
else
300-
{
301-
*socket = -1;
302-
}
303301
}
304302
}

MySQLManager-TunnelPlugin_C++/WindowsSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class WindowsSocket : public BaseSocket
2424
bool bindAndStartListening();
2525
bool bindAndStartListening(int backlog);
2626
SOCKET *returnSocket();
27-
SOCKET acceptClientAndReturnSocket(sockaddr_in *clientAddr);
27+
SOCKET *acceptClientAndReturnSocket(sockaddr_in *clientAddr);
2828
int sendToSocket(SOCKET *clientSocket, std::string dataToSend);
2929
std::string receiveDataOnSocket(SOCKET *socket);
3030
std::string getErrorStringFromErrorCode(int errorCode);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
First of all, thank you for your interest in Boardies MySQL Manager for Android and the PHP and SSH Tunnelling APIs. This is the source code for the SSH Tunnelling Plugin
2+
3+
This readme file will outline how to use the project but more information can be found on the GitHub page. If you need
4+
any help with anything, such as making changes to the code, installing the APIs on your own server, then please ask the question
5+
via GitHub or via the support portal support ticketing system at https://support.boardiesitsolutions.com.
6+
7+
We would love to hear your feedback and see what changes and enhancements the open source community wish to make, but please be gentle with me,
8+
this is my first C++ application coming from a C#/Java/PHP background :).
9+
10+
***Using the Source Code***
11+
The project consists of a Visual Studio Solution file and project file, so can be opened straight into Visual Studio - 2017 minimum.
12+
You can use a different IDE if you prefer when making changes, but when uploading the changes to GitHub, please ensure that your own IDE
13+
project files are not uploaded to GitHub and that the Visual Studio project files remain and are still intact.
14+
15+
Note that you might need to change the project properties to point to the correct location of the dependency libraries to
16+
match the location of where they are stored on your own development environment.
17+
18+
Note that there is also a makefile located in the route of the project source. This makefile is not used by Visual Studio but is instead the file
19+
that is used by the Linux compiler - more on that in a sec.
20+
21+
***Prerequsits***
22+
There are several dependencies that are required in order to compile the source code.
23+
Whether its on Linux or Windows, the following dependency libraries are required:
24+
- OpenSSL
25+
- Boost Library including:
26+
- Boost Filesystem
27+
- Boost System
28+
- Libssh2
29+
- RapidJson
30+
- C++11 Compiler
31+
How to compile and link to the libraries are outside the scope of this readme - follow the libraries documentation on how to compile
32+
33+
***Compiling for Windows***
34+
As mentioned Visual Studio can be used - minimum version of Visual Studio 2017 is required. You might need to change the project properties to match the location
35+
of where your dependency libraries are stored. You can use your own IDE but ensure your IDE files are not added to the project on GitHub.
36+
37+
The project has only been compiled on x64 bit Windows 10, there should be no reason that I can think of as to why it wouldn't work on 32 bit but as Boardies IT Solutions
38+
is just me, I don't have another x86 copy of Windows to be able to try it on. If you have/need this to run on a 32 bit operating system, and find that it does not work, then
39+
please feel free to make any necessary changes - but please ensure it doesn't break the x64 and Linux platform compatibility.
40+
41+
***Compiling for Linux***
42+
You need the following tools installed on Linux in order to build the source code
43+
- GCC
44+
- G++
45+
- Make
46+
- GDB (Not necessarily required, but might be useful if you need to debug)
47+
Ensure that any dependencies are installed for the above tools.
48+
The make file is used in order to compile the source code. Go into the root directory of where the .cpp and .h files are and run make clean to remove any already compiled object files
49+
followed by make. This will ensure everything is compiled from scratch, while just make changes to the code, you can just run make to compile what's changed instead of make clean followed
50+
by make.
51+
52+
***Notes About Making Changes***
53+
In order to push changes to GitHub, you must follow the points below otherwise the change might be rejected
54+
- Ensure that any changes do not break compatibility between the Windows and Linux platforms, if a change is made, that is only supported on 1 particular platform,
55+
unless it provides a big improvement or requirement for the platform it works on it will likely get rejected. All efforts must be made to ensure that compatibility
56+
between Linux and Windows is maintained.
57+
- Test the code for memory leaks, don't worry, if you miss one too much, its easily done, but please try to ensure memory leaks do not occur due to your code changes
58+
- If you're using your own IDE - if you're developing for Linux you have to, but please ensure that any specific IDE files or directories that are created are not pushed
59+
to GitHub.

0 commit comments

Comments
 (0)