mirror of
https://github.com/thearcanum/WIFSS
synced 2025-07-23 04:00:37 +02:00
Add thread Client + Mutex + Correct disconnection of Client
This commit is contained in:
@@ -12,9 +12,6 @@
|
||||
#include <ctype.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include <com.h>
|
||||
#include <str.h>
|
||||
|
||||
|
||||
#define BUFFER 256
|
||||
|
||||
@@ -22,28 +19,42 @@
|
||||
#define false 0
|
||||
typedef char bool;
|
||||
|
||||
//BOOL
|
||||
//bool _sComOn_;
|
||||
bool _tunnelOpened_;
|
||||
|
||||
//FCT
|
||||
#include <com.h>
|
||||
#include <str.h>
|
||||
|
||||
|
||||
//DATA_MUTEXED
|
||||
typedef struct
|
||||
{
|
||||
int sock;
|
||||
bool keepGoing;
|
||||
bool tunnelOpened;
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
} MUTEX;
|
||||
|
||||
|
||||
//START_STOP
|
||||
bool init(struct sockaddr_in*, int*);
|
||||
void disconnect(int);
|
||||
|
||||
void handle_command(const char*, int, bool*);
|
||||
void communication(int, bool*);
|
||||
//COMMUNICATION
|
||||
void handle_command(const char*, MUTEX*);
|
||||
void communication(MUTEX*);
|
||||
|
||||
//TRANSFER
|
||||
int upload(const char*, int);
|
||||
int download(const char*, int);
|
||||
|
||||
void startunnel(int, int);
|
||||
void acceptunnel(int, int);
|
||||
//TUNNEL
|
||||
void startunnel(MUTEX*, int);
|
||||
void acceptunnel(MUTEX*, int);
|
||||
|
||||
//THREAD
|
||||
void* scom(void*);
|
||||
void* clientCommand(void*);
|
||||
|
||||
//STRUCT
|
||||
//THREAD_COMMUNICATION
|
||||
void* serverCommunication(void*);
|
||||
void* clientCommunication(void*);
|
||||
|
||||
|
||||
#endif
|
||||
|
@@ -7,8 +7,8 @@
|
||||
#include <string.h>
|
||||
|
||||
|
||||
int str_beginwith(const char*, const char*);
|
||||
int str_validation(const char*, short int);
|
||||
bool str_beginwith(const char*, const char*);
|
||||
bool str_validation(const char*, short int);
|
||||
void str_lowerCase(char*);
|
||||
|
||||
|
||||
|
@@ -9,15 +9,23 @@ int main(void)
|
||||
|
||||
pthread_t sthread, cthread;
|
||||
|
||||
|
||||
if(init(&SERVER, &sock) == true)
|
||||
{
|
||||
pthread_create(&sthread, NULL, &scom, (void*)&sock);
|
||||
{
|
||||
MUTEX data;
|
||||
data.mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
|
||||
data.sock = sock;
|
||||
data.keepGoing = true;
|
||||
data.tunnelOpened = false;
|
||||
|
||||
pthread_create(&sthread, NULL, &serverCommunication, (void*)&data);
|
||||
pthread_create(&cthread, NULL, &clientCommunication, (void*)&data);
|
||||
|
||||
pthread_create(&cthread, NULL, &clientCommand, (void*)&sock);
|
||||
pthread_join(cthread, NULL);
|
||||
|
||||
pthread_mutex_destroy(&(data.mutex));
|
||||
}
|
||||
|
||||
pthread_join(sthread, NULL);
|
||||
pthread_join(cthread, NULL);
|
||||
|
||||
disconnect(sock);
|
||||
|
||||
|
@@ -1,96 +1,109 @@
|
||||
#include <client.h>
|
||||
|
||||
void* scom(void *data)
|
||||
void* serverCommunication(void *param)
|
||||
{
|
||||
int _sock;
|
||||
int _result;
|
||||
char _buff[BUFFER] = {0};
|
||||
|
||||
_sock = *((int*)data);
|
||||
MUTEX data = *((MUTEX*)param);
|
||||
|
||||
while(1)
|
||||
{
|
||||
memset(_buff, 0, BUFFER);
|
||||
_result = recv(_sock, _buff, BUFFER, false);
|
||||
pthread_mutex_lock(&(data.mutex));
|
||||
|
||||
if(_result <= 0)
|
||||
memset(_buff, 0, BUFFER);
|
||||
|
||||
_result = recv(data.sock, _buff, BUFFER, false);
|
||||
if(_result <= 0 || !data.keepGoing)
|
||||
{
|
||||
data.keepGoing = false;
|
||||
pthread_mutex_unlock(&(data.mutex));
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
{
|
||||
/* printf("\n\n[sthread] Received from server: %s\n", _buff); */
|
||||
|
||||
if(str_beginwith(_buff, UPLOAD) && str_validation(_buff, ARGUPL))
|
||||
{
|
||||
char _path[BUFFER] = {0};
|
||||
sscanf(_buff, "upload %s", _path);
|
||||
printf("\n\n[sthread] Server is asking us to upload: %s\n", _path);
|
||||
upload(_path, _sock);
|
||||
printf("\n\n[sthread] Server is asking us to upload: %s\n\n:|", _path);
|
||||
upload(_path, data.sock);
|
||||
}
|
||||
|
||||
else if(str_beginwith(_buff, ASKTUNNEL) && str_validation(_buff, ARGTUN))
|
||||
{
|
||||
if(!_tunnelOpened_)
|
||||
if(!data.tunnelOpened)
|
||||
{
|
||||
int _clientAsking = 0;
|
||||
|
||||
sscanf(_buff, "asktunnel %d", &_clientAsking);
|
||||
|
||||
acceptunnel(_sock, _clientAsking);
|
||||
acceptunnel(&data, _clientAsking);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\n\n[sthread] Someone is asking you for a tunnel but you're already tunneled\n");
|
||||
printf("\n\n[sthread] Someone is asking you for a tunnel but you're already tunneled.\n\n:|");
|
||||
}
|
||||
}
|
||||
|
||||
else if(!strcmp(_buff, DISCONNECT))
|
||||
{
|
||||
printf("\n\n[sthread] Server is demanding the Client disconnection. Stopping now.\n");
|
||||
data.keepGoing = false;
|
||||
pthread_mutex_unlock(&(data.mutex));
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
printf("\n\n[sthread] Command unknown received from Server.\n");
|
||||
printf("\n\n[sthread] Command unknown received from Server.\n\n:|");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* clientCommand(void *data)
|
||||
{
|
||||
bool _keepGoing = true;
|
||||
|
||||
int _sock;
|
||||
|
||||
_sock = *((int*)data);
|
||||
|
||||
while(_keepGoing)
|
||||
{
|
||||
communication(_sock, &_keepGoing);
|
||||
pthread_mutex_unlock(&(data.mutex));
|
||||
}
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
void handle_command(const char *command, int _sock, bool *keepGoing)
|
||||
void* clientCommunication(void *param)
|
||||
{
|
||||
MUTEX data = *((MUTEX*)param);
|
||||
|
||||
while(1)
|
||||
{
|
||||
pthread_mutex_lock(&(data.mutex));
|
||||
|
||||
if(!data.keepGoing)
|
||||
{
|
||||
pthread_mutex_unlock(&(data.mutex));
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
communication(&data);
|
||||
|
||||
pthread_mutex_unlock(&(data.mutex));
|
||||
}
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
void handle_command(const char *command, MUTEX *data)
|
||||
{
|
||||
if(!strcmp(command, QUIT) || !strcmp(command, EXIT) || !strcmp(command, STOP))
|
||||
{
|
||||
if(!_tunnelOpened_)
|
||||
if(!data->tunnelOpened)
|
||||
{
|
||||
printf("\n\nLet's close connection with Server...\n");
|
||||
*keepGoing = false;
|
||||
data->keepGoing = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\n\nLet's close tunnel with the other Client...\n");
|
||||
_tunnelOpened_ = false;
|
||||
data->tunnelOpened = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,22 +111,22 @@ void handle_command(const char *command, int _sock, bool *keepGoing)
|
||||
{
|
||||
char _path[32] = {0};
|
||||
|
||||
send(_sock, command, BUFFER, false);
|
||||
send(data->sock, command, BUFFER, false);
|
||||
|
||||
sscanf(command, "download %s", _path);
|
||||
|
||||
download(_path, _sock);
|
||||
download(_path, data->sock);
|
||||
}
|
||||
|
||||
else if(str_beginwith(command, TUNNEL) && str_validation(command, ARGTUN))
|
||||
{
|
||||
if(!_tunnelOpened_)
|
||||
if(!data->tunnelOpened)
|
||||
{
|
||||
int _idClient = 0;
|
||||
|
||||
sscanf(command, "tunnel %d", &_idClient);
|
||||
|
||||
startunnel(_sock, _idClient);
|
||||
startunnel(data, _idClient);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -123,14 +136,14 @@ void handle_command(const char *command, int _sock, bool *keepGoing)
|
||||
|
||||
else if(str_beginwith(command, SEND))
|
||||
{
|
||||
send(_sock, command, BUFFER, false);
|
||||
send(data->sock, command, BUFFER, false);
|
||||
}
|
||||
|
||||
else if(str_beginwith(command, SENDP))
|
||||
{
|
||||
if(!_tunnelOpened_)
|
||||
if(!data->tunnelOpened)
|
||||
{
|
||||
send(_sock, command, BUFFER, false);
|
||||
send(data->sock, command, BUFFER, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -154,18 +167,18 @@ void handle_command(const char *command, int _sock, bool *keepGoing)
|
||||
}
|
||||
}
|
||||
|
||||
void communication(int sock, bool *booleen)
|
||||
void communication(MUTEX *data)
|
||||
{
|
||||
static char _buff[BUFFER];
|
||||
|
||||
memset(_buff, 0, BUFFER);
|
||||
|
||||
if(!_tunnelOpened_)
|
||||
|
||||
if(!data->tunnelOpened)
|
||||
{
|
||||
printf("|: ");
|
||||
gets(_buff);
|
||||
str_lowerCase(_buff);
|
||||
handle_command(_buff, sock, booleen);
|
||||
handle_command(_buff, data);
|
||||
}
|
||||
|
||||
else
|
||||
@@ -173,6 +186,6 @@ void communication(int sock, bool *booleen)
|
||||
printf("[Tunnel] |: ");
|
||||
gets(_buff);
|
||||
str_lowerCase(_buff);
|
||||
handle_command(_buff, sock, NULL);
|
||||
handle_command(_buff, data);
|
||||
}
|
||||
}
|
||||
|
@@ -65,8 +65,6 @@ bool init(struct sockaddr_in *SERVER, int *sock)
|
||||
|
||||
} while(_result < 0 && (!strcmp(_buff, "yes") || !strcmp(_buff, "y")));
|
||||
|
||||
_tunnelOpened_ = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -1,22 +1,22 @@
|
||||
#include <client.h>
|
||||
|
||||
int str_beginwith(const char *w, const char *s)
|
||||
bool str_beginwith(const char *w, const char *s)
|
||||
{
|
||||
while(*s)
|
||||
{
|
||||
if(*s != *w)
|
||||
{
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
w++;
|
||||
s++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
int str_validation(const char *str, short int nbArgs)
|
||||
bool str_validation(const char *str, short int nbArgs)
|
||||
{
|
||||
short int _i, _arg;
|
||||
|
||||
@@ -31,16 +31,16 @@ int str_validation(const char *str, short int nbArgs)
|
||||
if(_arg > nbArgs)
|
||||
{
|
||||
printf("Too many arguments for this command.\n\n");
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
else if(_arg < nbArgs)
|
||||
{
|
||||
printf("Too few arguments for this command.\n\n");
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
void str_lowerCase(char *buff)
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#include <client.h>
|
||||
|
||||
void startunnel(int sock, int idClient)
|
||||
void startunnel(MUTEX *data, int idClient)
|
||||
{
|
||||
char _buff[BUFFER] = {0};
|
||||
|
||||
@@ -8,29 +8,29 @@ void startunnel(int sock, int idClient)
|
||||
|
||||
sprintf(_buff, "%s %d", ASKTUNNEL, idClient);
|
||||
|
||||
send(sock, _buff, BUFFER, false);
|
||||
send(data->sock, _buff, BUFFER, false);
|
||||
|
||||
memset(_buff, 0, BUFFER);
|
||||
|
||||
recv(sock, _buff, BUFFER, false);
|
||||
recv(data->sock, _buff, BUFFER, false);
|
||||
|
||||
|
||||
if(!strcmp(_buff, OKFORTUN))
|
||||
{
|
||||
_tunnelOpened_ = true;
|
||||
while(_tunnelOpened_)
|
||||
data->tunnelOpened = true;
|
||||
while(data->tunnelOpened)
|
||||
{
|
||||
communication(sock, NULL);
|
||||
communication(data);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_tunnelOpened_ = false;
|
||||
data->tunnelOpened = false;
|
||||
printf("Can't open a tunnel with %d Client.\n", idClient);
|
||||
}
|
||||
}
|
||||
|
||||
void acceptunnel(int sock, int clientAsking)
|
||||
void acceptunnel(MUTEX *data, int clientAsking)
|
||||
{
|
||||
char _buff[BUFFER] = {0};
|
||||
|
||||
@@ -44,19 +44,19 @@ void acceptunnel(int sock, int clientAsking)
|
||||
{
|
||||
memset(_buff, 0, BUFFER);
|
||||
sprintf(_buff, "%s", OKFORTUN);
|
||||
send(sock, _buff, BUFFER, false);
|
||||
send(data->sock, _buff, BUFFER, false);
|
||||
|
||||
_tunnelOpened_ = true;
|
||||
while(_tunnelOpened_)
|
||||
data->tunnelOpened = true;
|
||||
while(data->tunnelOpened)
|
||||
{
|
||||
communication(sock, NULL);
|
||||
communication(data);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(_buff, 0, BUFFER);
|
||||
sprintf(_buff, "%s", KOFORTUN);
|
||||
send(sock, _buff, BUFFER, false);
|
||||
_tunnelOpened_ = false;
|
||||
send(data->sock, _buff, BUFFER, false);
|
||||
data->tunnelOpened = false;
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,3 @@
|
||||
- Port display bug (Server)
|
||||
- Recv problem (Server)
|
||||
(- Server force Clients deconnection ?)
|
||||
|
||||
- Download / Upload Threads (Client)
|
||||
- Recv problem due to Thread (Server)
|
||||
(- Server force Clients deconnection ?)
|
Reference in New Issue
Block a user