2015-12-14 20:48:19 +00:00
|
|
|
#include "proxy_ftp.h"
|
|
|
|
|
|
|
|
// DECLARATIONS
|
|
|
|
static pthread_t userServer;
|
|
|
|
static short unsigned int userServerState = 1;
|
|
|
|
static char remoteAssignedPort[maxPortLen] = {'\0'};
|
|
|
|
|
|
|
|
/* headers */
|
|
|
|
static void* testServer();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************/
|
|
|
|
/* CORPS DU PROG */
|
|
|
|
/*****************/
|
|
|
|
int main(int argc, char* argv[]){
|
2015-12-14 23:02:56 +00:00
|
|
|
if( argc > 1 )
|
|
|
|
testServer(argv[1]);
|
|
|
|
else
|
|
|
|
testServer("");
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-14 23:02:56 +00:00
|
|
|
static void* testServer(char* localPort){
|
2015-12-14 20:48:19 +00:00
|
|
|
int USER_SOCKET; // contiendra le BUS DE COMMANDE utilisateur
|
|
|
|
int FTP_SOCKET; // contiendra le BUS DE COMMANDE FTP
|
|
|
|
int DUSER_SOCKET; // contiendra le BUS DE DONNES utilisateur
|
|
|
|
int DFTP_SOCKET; // contiendra le BUS DE DONNEES FTP
|
|
|
|
|
|
|
|
char BUFFER[maxBuffLen]; // contiendra le BUFFER
|
|
|
|
struct sockaddr_storage clientInfo; // contiendra les infos client
|
|
|
|
char repeat; // sert à sortir de la boucle
|
|
|
|
int nbReceived, nbSend; // contiendra les nb reçu && envoyé
|
|
|
|
socklen_t len = sizeof(struct sockaddr_storage);
|
|
|
|
|
|
|
|
// retour de @DROP_SERVER
|
|
|
|
char* serverPort; // contiendra le port
|
|
|
|
int LISTENSOCK; // contiendra la socket d'écoute
|
|
|
|
char* ftp_response;
|
|
|
|
|
2015-12-14 21:00:05 +00:00
|
|
|
/* [0] On démarre le SERVEUR + le CLIENT
|
2015-12-14 20:48:19 +00:00
|
|
|
==========================================================*/
|
2015-12-14 23:02:56 +00:00
|
|
|
serverPort = malloc(maxPortLen);
|
|
|
|
|
|
|
|
if( strlen(localPort) != 0 )
|
|
|
|
strcpy(serverPort, localPort);
|
|
|
|
|
|
|
|
|
2015-12-14 20:48:19 +00:00
|
|
|
DROP_SERVER(remoteHost, &serverPort, &LISTENSOCK);
|
|
|
|
CONNECT_CLIENT(FTP_HOST, FTP_PORT, &FTP_SOCKET);
|
|
|
|
|
|
|
|
printf("Port: %s\n", serverPort);
|
|
|
|
strcpy(remoteAssignedPort, serverPort);// PORT GLOBAL
|
|
|
|
|
|
|
|
/* [1] Attente d'une demande de connection, puis création d'une socket
|
|
|
|
============================================================================*/
|
|
|
|
USER_SOCKET = accept(LISTENSOCK, (struct sockaddr *) &clientInfo, &len);
|
|
|
|
|
|
|
|
/* [2] On envoie la séquence d'initialisation
|
|
|
|
============================================================================*/
|
|
|
|
strcpy(BUFFER, "");
|
|
|
|
CLIENT_SEND(&FTP_SOCKET, BUFFER, &ftp_response);
|
2015-12-14 23:02:56 +00:00
|
|
|
MANAGE_RESPONSE(&ftp_response, &DUSER_SOCKET, &DFTP_SOCKET);
|
2015-12-14 20:48:19 +00:00
|
|
|
swrite(&USER_SOCKET, ftp_response);
|
|
|
|
|
|
|
|
xPrint("P->F: %s\n\n", BUFFER);
|
|
|
|
xPrint("F->P: %s\n", ftp_response);
|
|
|
|
|
|
|
|
/* BOUCLE DE COMMANDES */
|
|
|
|
while( USER_SOCKET != -1 && FTP_SOCKET != -1 ){
|
|
|
|
|
|
|
|
/* [3] On récupère les données reçues (+attente)
|
|
|
|
============================================================================*/
|
2015-12-14 23:02:56 +00:00
|
|
|
if( WAIT_SOCKET_UPDATE(&USER_SOCKET, BUFFER) == -1 ) break;
|
2015-12-14 20:48:19 +00:00
|
|
|
xPrint("C->P: %s\n", BUFFER);
|
|
|
|
|
|
|
|
if( DEBUGMOD ) printf("Recu: %d\n", nbReceived);
|
|
|
|
if( DEBUGMOD ) printf("C->P: %s\n", BUFFER);
|
|
|
|
|
|
|
|
|
2015-12-14 21:00:05 +00:00
|
|
|
/* [4] Traitement de la requête
|
|
|
|
============================================================================*/
|
2015-12-14 23:02:56 +00:00
|
|
|
MANAGE_REQUEST(BUFFER, &DUSER_SOCKET, &DFTP_SOCKET);
|
2015-12-14 21:00:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [5] Redirection vers le serveur FTP
|
2015-12-14 20:48:19 +00:00
|
|
|
============================================================================*/
|
|
|
|
xPrint("P->F: %s\n\n", BUFFER);
|
2015-12-14 23:02:56 +00:00
|
|
|
CLIENT_SEND(&FTP_SOCKET, BUFFER, &ftp_response);
|
2015-12-14 20:48:19 +00:00
|
|
|
xPrint("F->P: %s\n", ftp_response);
|
|
|
|
|
|
|
|
|
2015-12-14 21:00:05 +00:00
|
|
|
/* [6] Traitement de la réponse (FTP)
|
2015-12-14 20:48:19 +00:00
|
|
|
============================================================================*/
|
2015-12-14 23:02:56 +00:00
|
|
|
MANAGE_RESPONSE(&ftp_response, &DUSER_SOCKET, &DFTP_SOCKET);
|
2015-12-14 20:48:19 +00:00
|
|
|
|
2015-12-14 21:00:05 +00:00
|
|
|
|
|
|
|
/* [7] Redirection vers le CLIENT
|
|
|
|
============================================================================*/
|
2015-12-14 20:48:19 +00:00
|
|
|
if( swrite(&USER_SOCKET, ftp_response) == -1 ) break;
|
|
|
|
printf("P->C: %s\n", ftp_response);
|
|
|
|
|
|
|
|
|
2015-12-14 21:00:05 +00:00
|
|
|
/* [8] On vide les buffers
|
2015-12-14 20:48:19 +00:00
|
|
|
============================================================================*/
|
|
|
|
memset(BUFFER, '\0', sizeof(BUFFER)); // on vide le buffer
|
|
|
|
ftp_response = NULL;
|
|
|
|
}
|
|
|
|
|
2015-12-14 21:00:05 +00:00
|
|
|
/* [9] Fermeture des connections (SOCKETS)
|
|
|
|
============================================================================*/
|
2015-12-14 20:48:19 +00:00
|
|
|
close(USER_SOCKET);
|
|
|
|
close(FTP_SOCKET);
|
|
|
|
close(LISTENSOCK);
|
|
|
|
strcpy(remoteAssignedPort, "\0");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DROP_SERVER
|
|
|
|
* INIT_CLIENT
|
|
|
|
*
|
|
|
|
* while(){
|
|
|
|
* accept();
|
|
|
|
* client_send()
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* EXIT_CLIENT
|
|
|
|
* HALT_SERVER
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|