1
0
mirror of https://github.com/thearcanum/WIFSS synced 2025-06-12 08:00:12 +02:00

Let's fall back on a simple chat service

--> WIFSS architecture was not fitting at all with
what we expected from it, let's just improve the first chat solution
This commit is contained in:
HorlogeSkynet
2017-05-24 23:34:07 +02:00
parent 08ed6fa620
commit 3917d0316a
14 changed files with 7 additions and 823 deletions

@ -11,7 +11,7 @@ all: $(EXEC)
# Client
WClient: ccommands.o ccore.o cclient.o cfiles.o cprotocols.o
WClient: ccommands.o ccore.o cclient.o
$(MAKE) str.o
$(CC) -o $@ $^ str.o $(LDFLAGS)
$(MAKE) clean
@ -22,10 +22,6 @@ ccore.o: client/core/*
$(CC) -o $@ -c client/core/core.c $(CFLAGS)
cclient.o: client/core/*
$(CC) -o $@ -c client/core/client.c $(CFLAGS)
cfiles.o: client/files/*
$(CC) -o $@ -c client/files/files.c $(CFLAGS)
cprotocols.o: client/protocols/*
$(CC) -o $@ -c client/protocols/protocols.c $(CFLAGS)
# Server

@ -1,5 +1,4 @@
# WIFSS
_WIFSS Is not a File Sharing System_
## How to use it
@ -18,16 +17,9 @@ Run with:
`who`
`send <message>`
`whisper <idClient> <message>`
`list`
`rename <file> <newFileName>`
`remove <file>`
`ispresent <idClient> <file>`
`asklist <idClient>`
`exit`
`logout`
`clear`
`download <idClient> <file>`
`checkfolder`
### List of commands currently available for Server
@ -37,7 +29,6 @@ Run with:
`whisper <idClient> <message>`
`disconnect <idClient> ['-1' = all]`
`exit`
`stop`
`clear`
### Additional notes

@ -5,7 +5,6 @@ void* server_communication(void *param)
{
int16_t result;
char buffer[BUFFER];
bool newCursor = true;
THREADS *threads = (THREADS*)param;
@ -24,46 +23,12 @@ void* server_communication(void *param)
else
{
if(str_beginwith(buffer, UPLOAD))
{
upload(buffer);
}
else if(!strcmp(buffer, ASKLIST) && check_download_folder())
{
char temp[BUFFER] = "";
list_files(temp);
send(g_core_variables.server_sock, temp, BUFFER, false);
}
else if(str_beginwith(buffer, ISPRESENT) && check_download_folder())
{
is_present(buffer);
}
else
{
/* We received from server a non-null string, let's print it */
if(strcmp(buffer, ""))
{
printf("\n\n[sthread] %s\n\n", buffer);
}
else
{
newCursor = false;
}
}
if(newCursor)
/* We received from server a non-null string, let's print it */
if(strcmp(buffer, ""))
{
printf("\n\n[sthread] %s\n\n", buffer);
command_cursor();
}
else
{
newCursor = true;
}
}
}
}
@ -96,11 +61,6 @@ void* client_communication(void *param)
pthread_exit(NULL);
}
else if(command_validation((const char* const*)args, nbArgs, DOWNLOAD, ARGDOWNLOAD))
{
download(buffer);
}
else if(str_beginwith(buffer, SEND))
{
send(g_core_variables.server_sock, buffer, BUFFER, false);
@ -111,31 +71,6 @@ void* client_communication(void *param)
send(g_core_variables.server_sock, buffer, BUFFER, false);
}
else if(command_validation((const char* const*)args, nbArgs, ISPRESENT, ARGISPRESENT))
{
send(g_core_variables.server_sock, buffer, BUFFER, false);
}
else if(command_validation((const char* const*)args, nbArgs, ASKLIST, ARGASKLIST))
{
ask_list(buffer);
}
else if(command_validation((const char* const*)args, nbArgs, REMOVE, ARGREMOVE))
{
remove_file(buffer);
}
else if(command_validation((const char* const*)args, nbArgs, RENAME, ARGRENAME))
{
rename_file(buffer);
}
else if(command_validation((const char* const*)args, nbArgs, LIST, 1))
{
list_files(NULL);
}
else if(command_validation((const char* const*)args, nbArgs, WHO, 1))
{
char temp[BUFFER] = WHO;
@ -147,15 +82,6 @@ void* client_communication(void *param)
clear_console();
}
else if(command_validation((const char* const*)args, nbArgs, CHECKFOLDER, 1))
{
if(!check_download_folder())
{
pthread_cancel(*(threads->sthread));
pthread_exit(NULL);
}
}
else if(command_validation((const char* const*)args, nbArgs, HELP, 1))
{
static const char *const helpMenu[32] =
@ -164,16 +90,9 @@ void* client_communication(void *param)
"who",
"send <message>",
"whisper <idClient> <message>",
"list",
"rename <file> <newFileName>",
"remove <file>",
"ispresent <idClient> <file>",
"asklist <idClient>",
"exit",
"logout",
"clear",
"download <idClient> <file>",
"checkfolder",
"\n"
};

@ -7,25 +7,14 @@
#define HELP "help"
#define EXIT "exit"
#define SEND "send"
#define LIST "list"
#define CLEAR "clear"
#define LOGOUT "logout"
#define REMOVE "remove"
#define RENAME "rename"
#define UPLOAD "upload"
#define ASKLIST "asklist"
#define WHISPER "whisper"
#define DOWNLOAD "download"
#define ISPRESENT "ispresent"
#define DISCONNECT "disconnect"
#define CHECKFOLDER "checkfolder"
#define ARGDOWNLOAD 3
#define ARGWHISPER 3
#define ARGISPRESENT 3
#define ARGASKLIST 2
#define ARGRENAME 3
#define ARGREMOVE 2
typedef struct

@ -5,10 +5,6 @@
#include "../../commons/commons.h"
#include "../../commons/str/str.h"
#include "../commands/commands.h"
#include "../files/files.h"
#include "../protocols/protocols.h"
#define PATHWORKINGDIR "/Downloads/WIFSS/"
#define LOCALHOST "localhost"
#define ADDRLOCAL "127.0.0.1"
@ -20,7 +16,6 @@ typedef struct
int16_t server_sock;
int16_t server_port;
int8_t client_id;
char *working_dir;
} core_variables_t;

@ -5,15 +5,10 @@ bool start_client(struct sockaddr_in *server)
{
clear_console();
printf("\n\033[32m[WIFSS] Starting Client...\033[0m\n");
printf("\n\033[32m[WIFSS] Starting Client...\033[0m\n\n");
init_global_variables();
if(!set_work_dir() || !check_download_folder())
{
return false;
}
char address[BUFFER];
char *args[BUFFER];
uint16_t nbArgs;
@ -123,9 +118,6 @@ void stop_client(void)
printf("\n\n[WIFSS] Socket successfully closed.\n\n");
}
}
free(g_core_variables.working_dir);
g_core_variables.working_dir = NULL;
printf("[WIFSS] Client is shutting down for now.\n");
@ -144,5 +136,4 @@ void init_global_variables(void)
g_core_variables.server_sock = -1;
g_core_variables.server_port = -1;
g_core_variables.client_id = -1;
g_core_variables.working_dir = NULL;
}

@ -1,259 +0,0 @@
#include "files.h"
bool set_work_dir(void)
{
g_core_variables.working_dir = calloc(strlen(getenv("HOME")) + strlen(PATHWORKINGDIR), sizeof(*g_core_variables.working_dir));
if(g_core_variables.working_dir == NULL)
{
printf("\n\033[31m[WIFSS] Error: Can\'t allocate memory for the environment variable \"WORKING_DIR\".\033[0m\n\n");
return false;
}
strcpy(g_core_variables.working_dir, getenv("HOME"));
strcat(g_core_variables.working_dir, PATHWORKINGDIR);
return true;
}
bool check_download_folder()
{
char buffer[strlen(g_core_variables.working_dir) + sysconf(_PC_NAME_MAX)];
strcpy(buffer, g_core_variables.working_dir);
DIR *directory = opendir(buffer);
if(directory == NULL)
{
printf("\n\033[31m[WIFSS] Error: Target directory doesn\'t exist, we attempt to create it.\033[0m\n\n");
mkdir(buffer, S_IRWXU);
directory = opendir(buffer);
if(directory == NULL)
{
printf("\n\033[31m[WIFSS] Error: Target directory couldn\'t be created.\033[0m\n\n");
return false;
}
else
{
printf("\n\033[32m[WIFSS] Target directory: \"%s\" successfully created.\033[0m\n\n", buffer);
}
}
uint8_t count = 0;
struct stat file_stat;
struct dirent *ep;
while((ep = readdir(directory)))
{
/* All the content, bar 'current_dir' and 'parent_dir' */
if(strcmp(ep->d_name, ".") && strcmp(ep->d_name, ".."))
{
if((long)strlen(ep->d_name) > sysconf(_PC_NAME_MAX))
{
printf("\n\033[31m[WIFSS] Error: One of your file (\"%s\") in your working directory has more than %ld characters in its name. Consider renaming it.\033[0m\n\n", ep->d_name, sysconf(_PC_NAME_MAX));
return false;
}
strcpy(buffer, g_core_variables.working_dir);
strcat(buffer, ep->d_name);
if(stat(buffer, &file_stat) == -1)
{
printf("\n\033[31m[WIFSS] Error: A problem occurred while reading information about one of your working directory content (\"%s\").\033[0m\n\n", buffer);
return false;
}
else
{
if(S_ISDIR(file_stat.st_mode))
{
printf("\n\033[31m[WIFSS] Error: One of your working directory content is a sub-directory (\"%s\"). Please consider removing it.\033[0m\n\n", ep->d_name);
return false;
}
}
count++;
}
}
closedir(directory);
if(count > MAXFILEDIR)
{
printf("\n\033[31m[WIFSS] Error: You've got more than %d files in: \"%s\" directory. Clean this up please.\033[0m\n\n", MAXFILEDIR, buffer);
return false;
}
else
{
printf("\n\033[32m[WIFSS] Your directory is clear and you\'re able to start downloading or uploading some files.\033[0m\n\n");
return true;
}
}
void is_present(const char *command)
{
char buff[BUFFER];
char fileName[sysconf(_PC_NAME_MAX)];
char destFile[strlen(g_core_variables.working_dir) + sysconf(_PC_NAME_MAX)];
strncpy(fileName, getSecondArgsGroup(command), sysconf(_PC_NAME_MAX));
strcpy(destFile, g_core_variables.working_dir);
strcat(destFile, fileName);
if(!access(destFile, F_OK))
{
strcpy(buff, PRESENT);
}
else
{
strcpy(buff, FAIL);
}
send(g_core_variables.server_sock, buff, BUFFER, false);
}
void remove_file(const char *command)
{
char fileName[sysconf(_PC_NAME_MAX)];
char destFile[strlen(g_core_variables.working_dir) + sysconf(_PC_NAME_MAX)];
strncpy(fileName, getSecondArgsGroup(command), sysconf(_PC_NAME_MAX));
strcpy(destFile, g_core_variables.working_dir);
strcat(destFile, fileName);
if(remove(destFile) == -1)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be removed as well.\033[0m\n\n", fileName);
}
else
{
printf("\n\033[32m[WIFSS] \"%s\" has been removed.\033[0m\n\n", fileName);
}
}
void rename_file(const char *command)
{
char destFile[strlen(g_core_variables.working_dir) + sysconf(_PC_NAME_MAX)];
char newDestFile[strlen(g_core_variables.working_dir) + sysconf(_PC_NAME_MAX)];
const char *const tempGrp2 = getSecondArgsGroup(command);
const char *const tempGrp3 = getThirdArgsGroup(command);
strcpy(destFile, g_core_variables.working_dir);
strncat(destFile, tempGrp2, strlen(tempGrp2) - strlen(tempGrp3) - 1);
strcpy(newDestFile, g_core_variables.working_dir);
strncat(newDestFile, tempGrp3, sysconf(_PC_NAME_MAX));
if(rename(destFile, newDestFile) == -1)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be renamed as well.\033[0m\n\n", destFile);
}
else
{
printf("\n\033[32m[WIFSS] \"%s\" has been renamed as \"%s\".\033[0m\n\n", destFile, newDestFile);
}
}
void close_file(FILE *file, const char *fileName)
{
if(fclose(file) == EOF)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be closed as well.\033[0m\n\n", fileName);
}
}
void list_files(char *buffer)
{
DIR *directory = opendir(g_core_variables.working_dir);
if(directory != NULL)
{
/* Let's pre-format the buffer for the server */
if(buffer != NULL)
{
strcpy(buffer, "list: ");
}
struct dirent *ep;
while((ep = readdir(directory)))
{
/* All the content, bar 'current_dir' and 'parent_dir' */
if(strcmp(ep->d_name, ".") && strcmp(ep->d_name, ".."))
{
/* If the function is called with a buffer: non-verbose listing */
if(buffer != NULL)
{
strcat(buffer, ep->d_name);
strcat(buffer, "/");
}
else
{
printf("\t%s\n", ep->d_name);
}
}
}
printf("\n");
if(closedir(directory) != 0)
{
printf("\n\033[31m[WIFSS] Error: Couldn't close the target directory: \"%s\".\033[0m\n\n", g_core_variables.working_dir);
}
}
else
{
printf("\n\033[31m[WIFSS] Error: Couldn't open the target directory: \"%s\".\033[0m\n\n", g_core_variables.working_dir);
}
}
void ask_list(const char *command)
{
char buff[BUFFER] = "";
send(g_core_variables.server_sock, command, BUFFER, false);
recv(g_core_variables.server_sock, buff, BUFFER, false);
if(str_beginwith(buff, LIST))
{
char temp[BUFFER] = "";
strncat(temp, getSecondArgsGroup(buff), BUFFER);
printf("\n\n[WIFSS] File list of client asked:\n\t");
for(uint16_t i = 0; temp[i] != '\0'; i++)
{
if(temp[i] == '/')
{
printf("\n\t");
continue;
}
else
{
printf("%c", temp[i]);
}
}
printf("\n");
}
else
{
printf("\n\n\033[33m[WIFSS] Error: List of client asked could not be retrieved as well.\033[0m\n\n");
}
}

@ -1,20 +0,0 @@
#ifndef __FILES__
#define __FILES__
#include "../core/client.h"
#define MAXFILEDIR 32
bool set_work_dir(void);
bool check_download_folder(void);
void list_files(char*);
void remove_file(const char*);
void rename_file(const char*);
void close_file(FILE*, const char*);
void is_present(const char*);
void ask_list(const char*);
#endif

@ -1,203 +0,0 @@
#include "protocols.h"
void download(const char *command)
{
char fileName[sysconf(_PC_NAME_MAX)];
char destFile[strlen(g_core_variables.working_dir) + sysconf(_PC_NAME_MAX)];
char temp[strlen(g_core_variables.working_dir) + sysconf(_PC_NAME_MAX) + strlen(DOTPART)];
int8_t foo = -1;
sscanf(command, "download %" SCNd8 " %[^\n]", &foo, fileName);
strcpy(destFile, g_core_variables.working_dir);
strcat(destFile, fileName); /* destFile = "$HOME/Downloads/WIFSS/fileName" */
strcpy(temp, destFile);
strcat(temp, DOTPART); /* We append ".part" at the end of the file name during download */
FILE *file = NULL;
if(!access(temp, F_OK))
{
/* Continue interrupted download of last time ? */
}
if(access(destFile, F_OK))
{
file = fopen(destFile, "wb");
}
if(file == NULL)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" cannot be created. Already exists ?\033[0m\n\n", fileName);
return;
}
else
{
if(rename(destFile, temp) == -1)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be renamed as well.\033[0m\n\n", fileName);
close_file(file, fileName);
return;
}
}
char buff[BUFFER] = "";
struct sockaddr_in serverDL;
int sockDL;
int res;
send(g_core_variables.server_port, command, BUFFER, false); /* Wake up the server with the command */
/* Ok, now we'll need a new socket to download this file through the server */
serverDL.sin_port = htons(g_core_variables.server_port + g_core_variables.client_id + 1);
serverDL.sin_addr.s_addr = inet_addr(g_core_variables.server_addr);
sockDL = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
res = connect(sockDL, (struct sockaddr*)&serverDL, sizeof(serverDL));
if(res < 0)
{
printf("\n\033[31m[WIFSS] Error: could not establish a new connection the server.\033[0m\n\n");
return;
}
/* Waiting for ACK... */
recv(sockDL, buff, BUFFER, false);
if(!strcmp(buff, FAIL))
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be uploaded by remote [Client %" SCNu8 "].\033[0m\n", fileName, foo);
close_file(file, fileName);
if(remove(destFile) == -1)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be removed as well.\033[0m\n", fileName);
}
return;
}
else
{
int32_t fsize = 0;
sscanf(buff, "size: %" SCNd32, &fsize);
printf("\n\033[32m[WIFSS] Reception of \"%s\" (%" SCNd32 "bytes) started in \"%s\" !\033[0m\n\n", fileName, fsize, getenv("WORKDIR"));
}
while(1)
{
do
{
res = recv(sockDL, buff, BUFFER, false);
if(res <= 0)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be downloaded completely.\033[0m\n", fileName);
close_file(file, fileName);
return;
}
} while(res != BUFFER);
if(!strcmp(buff, FINISHED))
{
printf("\n\033[32m[WIFSS] Download finished !\033[0m\n\n");
break;
}
else
{
fwrite(buff, sizeof(char), BUFFER, file);
fseek(file, SEEK_CUR, SEEK_CUR + BUFFER);
memset(buff, 0, BUFFER);
}
}
if(rename(temp, destFile) == -1)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be renamed as well.\033[0m\n\n", fileName);
}
close_file(file, fileName);
}
void upload(const char *command)
{
char buff[BUFFER] = "";
char fileName[sysconf(_PC_NAME_MAX)];
char destFile[strlen(g_core_variables.working_dir) + sysconf(_PC_NAME_MAX)];
sscanf(command, "upload %[^\n]", fileName);
printf("\n\n[sthread] [Server] is asking us to upload: \"%s\". Trying to upload it...\n", fileName);
strcpy(destFile, g_core_variables.working_dir); /* destFile = "$HOME/Downloads/WIFSS/" */
strcat(destFile, fileName); /* destFile = "$HOME/Downloads/WIFSS/fileName" */
FILE *file = NULL;
if(!access(destFile, F_OK))
{
file = fopen(destFile, "rb");
}
struct sockaddr_in serverDL;
/* Ok, now we'll need a new socket to upload this file through the server */
serverDL.sin_port = htons(g_core_variables.server_port + g_core_variables.client_id + 1);
serverDL.sin_addr.s_addr = inet_addr(g_core_variables.server_addr);
int8_t sockDL = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
int16_t res = connect(sockDL, (struct sockaddr*)&serverDL, sizeof(serverDL));
if(res == -1)
{
printf("\n\033[31m[WIFSS] Error: Could not establish a new connection the server.\033[0m\n\n");
return;
}
if(file == NULL)
{
printf("\n\033[31m[WIFSS] \"%s\" asked is unreachable.\033[0m\n", fileName);
sprintf(buff, "%s", FAIL);
send(sockDL, buff, BUFFER, false);
}
else // À refaire complètement...
{
strcpy(buff, "");
fseek(file, 0, SEEK_END);
int32_t fsize = ftell(file);
fseek(file, 0, SEEK_SET);
sprintf(buff, "size: %" SCNd32, fsize);
send(sockDL, buff, BUFFER, false);
printf("\n[WIFSS] Sending: \"%s\" (%" SCNd32 " bytes).\n", fileName, fsize);
recv(sockDL, buff, BUFFER, false); /* Receive "OK", cue-role */
while(ftell(file) != SEEK_END)
{
strcpy(buff, "");
fread(buff, sizeof(char), BUFFER, file);
do
{
res = send(sockDL, buff, BUFFER, false);
if(res <= 0)
{
printf("\n\n[WIFSS] File could not be uploaded completely.\n");
break;
}
} while(res != BUFFER);
}
strcpy(buff, "");
sprintf(buff, "%s", FINISHED);
send(sockDL, buff, BUFFER, false);
close_file(file, fileName);
}
}

@ -1,20 +0,0 @@
#ifndef __PROTOCOLS__
#define __PROTOCOLS__
#include "../core/client.h"
#define OK "ok"
#define FAIL "fail"
#define ENDT "ENDT"
#define PRESENT "present"
#define FINISHED "finished"
#define DOTPART ".part"
void upload(const char*);
void download(const char*);
#endif

@ -23,7 +23,7 @@ void* command_handler(void *foo)
continue;
}
if(command_validation((const char* const*)args, nbArgs, EXIT, 1) || command_validation((const char* const*)args, nbArgs, STOP, 1))
if(command_validation((const char* const*)args, nbArgs, EXIT, 1))
{
broadcast(SID, "[Server] is going to shutdown !");
pthread_exit(NULL);
@ -83,7 +83,6 @@ void* command_handler(void *foo)
"whisper <idClient> <message>",
"disconnect <idClient> ['-1' for all]",
"exit",
"stop",
"clear",
"\n"
};
@ -106,12 +105,7 @@ void* command_handler(void *foo)
void process_command(const char *command, const uint8_t sender_id)
{
if(str_beginwith(command, DOWNLOAD))
{
download(command, sender_id);
}
else if(str_beginwith(command, WHO))
if(str_beginwith(command, WHO))
{
who(sender_id);
}
@ -126,16 +120,6 @@ void process_command(const char *command, const uint8_t sender_id)
whisper(command, sender_id);
}
else if(str_beginwith(command, ASKLIST))
{
ask_list(command, sender_id);
}
else if(str_beginwith(command, ISPRESENT))
{
is_present(command, sender_id);
}
else
{
printf("\n\n[WIFSS] Unknown command from [Client %d]: \"%s\".\n\n", sender_id, command);

@ -4,29 +4,20 @@
#include "../core/server.h"
/* ACK */
#define OK "ok"
#define FAIL "fail"
#define ENDT "ENDT"
#define PRESENT "present"
#define FINISHED "finished"
/* CMD */
#define WHO "who"
#define HELP "help"
#define EXIT "exit"
#define STOP "stop"
#define SEND "send"
#define CLEAR "clear"
#define UPLOAD "upload"
#define ASKLIST "asklist"
#define WHISPER "whisper"
#define DOWNLOAD "download"
#define ISPRESENT "ispresent"
#define DISCONNECT "disconnect"
/* ARG */
#define ARGDOWNLOAD 2
#define ARGDISCONNECT 2

@ -1,141 +1,6 @@
#include "protocols.h"
void download(const char *command, const uint8_t sender_id)
{
char copy[BUFFER] = "";
char buffer[BUFFER] = "";
char filename[BUFFER] = "";
long int fsize = 0;
int8_t remote_id = -1;
int res_DL = 0, res_UL = 0;
struct sockaddr_in clientDL, clientUL;
int clientDL_sock, clientUL_sock;
unsigned int asize;
sscanf(command, "download %" SCNd8 "%[^\n]", &remote_id, filename);
printf("\n\n[Client %d] is asking the uploading of [Client %d]'s \"%s\".\n", sender_id, remote_id, filename);
if((remote_id >= MAX_CLIENTS) || (remote_id < 0) || (remote_id == sender_id) || (g_core_variables.clients[remote_id].sock <= 0))
{
memset(filename, 0, BUFFER);
sprintf(filename, "%s", "Error: Client wanted is invalid or not connected...");
send(g_core_variables.clients[sender_id].sock, filename, BUFFER, false);
return;
}
/* Let's create a new socket for the client who want to download */
clientDL_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
clientDL.sin_family = AF_INET;
clientDL.sin_addr.s_addr = INADDR_ANY;
clientDL.sin_port = htons(g_core_variables.server_port + sender_id + 1); /* We use this value because it's known by client as well */
res_DL = bind(g_core_variables.server_port + sender_id, (struct sockaddr*)&clientDL, sizeof(clientDL));
/* Now the same, but with the client who'll upload the file */
clientUL_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
clientUL.sin_family = AF_INET;
clientUL.sin_addr.s_addr = INADDR_ANY;
clientUL.sin_port = htons(g_core_variables.server_port + remote_id + 1);
res_UL = bind(g_core_variables.server_port + remote_id, (struct sockaddr*)&clientUL, sizeof(clientUL));
close(clientDL_sock);
close(clientUL_sock);
/* Let's test the bindings */
if(res_DL < 0 || res_UL < 0)
{
printf("\n\n\033[31m[WIFSS] Error during creation of the listening socket for one of the two clients (%d) & (%" SCNd8 ") [bind (%d) & (%d)].\033[0m\n", sender_id, remote_id, res_DL, res_UL);
memset(buffer, 0, BUFFER);
sprintf(buffer, "%s", FAIL);
send(g_core_variables.clients[sender_id].sock, buffer, BUFFER, false);
return;
}
listen(clientDL_sock, 1);
listen(clientUL_sock, 1);
asize = sizeof(struct sockaddr_in);
/* Now waiting for both connections ! */
res_DL = accept(clientDL_sock, (struct sockaddr*)&clientDL, &asize);
printf("\n\n[WIFSS] Downloading client paired !.\n");
close(clientDL_sock);
res_UL = accept(clientUL_sock, (struct sockaddr*)&clientUL, &asize);
printf("\n\n[WIFSS] Uploading client paired !.\n");
close(clientUL_sock);
/* We FINALLY could send a file ! */
sprintf(copy, "upload %s", filename);
send(g_core_variables.clients[remote_id].sock, copy, BUFFER, false);
/* Waiting for ACK... */
recv(res_UL, buffer, BUFFER, false);
if(!strcmp(buffer, FAIL))
{
printf("\n\n[WIFSS] A problem occurred with the file during its opening.\n");
memset(buffer, 0, BUFFER);
sprintf(buffer, "%s", FAIL);
send(res_DL, buffer, BUFFER, false);
return;
}
else
{
sscanf(buffer, "size: %ld", &fsize);
printf("\n\n[WIFSS] File size: %ld bytes.\n", fsize);
send(res_DL, buffer, BUFFER, false);
/* Waiting for ACK... */
memset(buffer, 0, BUFFER);
recv(res_DL, buffer, BUFFER, false);
/* Receive and Send "OK" (cue-role), from sender, to remote */
send(res_UL, buffer, BUFFER, false);
}
int res;
while(1)
{
memset(buffer, 0, BUFFER);
do
{
res = recv(res_UL, buffer, BUFFER, false);
if(res <= 0)
{
printf("\n[WIFSS] File could not be downloaded completely.\n\n");
return;
}
} while(res != BUFFER);
if(!strcmp(buffer, FINISHED))
{
break;
}
else
{
do
{
res = send(res_DL, buffer, BUFFER, false);
if(res <= 0)
{
printf("\n[WIFSS] File could not be uploaded completely.\n\n");
return;
}
} while(res != BUFFER);
}
}
}
void who(const int8_t sender)
{
char buffer[BUFFER] = "Id(s) of other client(s) currently connected: ";
@ -164,38 +29,6 @@ void who(const int8_t sender)
}
void ask_list(const char *command, const uint8_t sender_id)
{
char buff[BUFFER] = "";
int8_t remote_id = -1;
sscanf(command, "asklist %" SCNd8, &remote_id);
if(g_core_variables.clients[remote_id].status == TAKEN && sender_id != remote_id && remote_id >= 0 && remote_id < MAX_CLIENTS)
{
sprintf(buff, "%s", ASKLIST);
send(g_core_variables.clients[remote_id].sock, buff, BUFFER, false);
/* Waiting for file list... */
memset(buff, 0, BUFFER);
recv(g_core_variables.clients[remote_id].sock, buff, BUFFER, false);
send(g_core_variables.clients[sender_id].sock, buff, BUFFER, false);
printf("\n\n[Client %d] asked the file list of [Client %d].\n\n", sender_id, remote_id);
}
else
{
sprintf(buff, "%s", "Error: This client is not connected or its identifier is invalid.");
send(g_core_variables.clients[sender_id].sock, buff, BUFFER, false);
printf("\n\n[Client %d] asked the file list of [Client %d], but he is not connected.\n\n", sender_id, remote_id);
}
}
void is_present(const char *command, const uint8_t sender_id)
{
/* ... */
}
void message(const char *command, const uint8_t sender_id)
{
char copy[BUFFER] = "";

@ -5,10 +5,7 @@
#include "../core/server.h"
void download(const char*, const uint8_t);
void who(const int8_t);
void ask_list(const char*, const uint8_t);
void is_present(const char*, const uint8_t);
void message(const char*, const uint8_t);
void whisper(const char*, const uint8_t);
void broadcast(const uint8_t, const char*);