146 lines
3.5 KiB
C
146 lines
3.5 KiB
C
|
#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[]){
|
||
|
|
||
|
testServer();
|
||
|
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
static void* testServer(){
|
||
|
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;
|
||
|
|
||
|
/* [0] On lance @DROP_SERVER
|
||
|
==========================================================*/
|
||
|
serverPort = malloc(4*sizeof(char));
|
||
|
strcpy(serverPort, "4444");
|
||
|
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);
|
||
|
MANAGE_RESPONSE(&USER_SOCKET, &ftp_response);
|
||
|
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)
|
||
|
============================================================================*/
|
||
|
if( WAIT_CLIENT(&USER_SOCKET, BUFFER) == -1 ) break;
|
||
|
xPrint("C->P: %s\n", BUFFER);
|
||
|
|
||
|
if( DEBUGMOD ) printf("Recu: %d\n", nbReceived);
|
||
|
if( DEBUGMOD ) printf("C->P: %s\n", BUFFER);
|
||
|
|
||
|
|
||
|
/* [4] Redirection vers le serveur FTP
|
||
|
============================================================================*/
|
||
|
CLIENT_SEND(&FTP_SOCKET, BUFFER, &ftp_response);
|
||
|
// printf("U22: %d\n", USER_SOCKET);
|
||
|
xPrint("P->F: %s\n\n", BUFFER);
|
||
|
xPrint("F->P: %s\n", ftp_response);
|
||
|
|
||
|
|
||
|
/* [5] On analyse et renvoie la réponse à l'utilisateur
|
||
|
============================================================================*/
|
||
|
// on analyse
|
||
|
MANAGE_RESPONSE(&USER_SOCKET, &ftp_response);
|
||
|
|
||
|
// on réponds au client
|
||
|
if( swrite(&USER_SOCKET, ftp_response) == -1 ) break;
|
||
|
printf("P->C: %s\n", ftp_response);
|
||
|
|
||
|
|
||
|
|
||
|
/* [6] On vide les buffers
|
||
|
============================================================================*/
|
||
|
memset(BUFFER, '\0', sizeof(BUFFER)); // on vide le buffer
|
||
|
ftp_response = NULL;
|
||
|
}
|
||
|
|
||
|
close(USER_SOCKET);
|
||
|
close(FTP_SOCKET);
|
||
|
close(LISTENSOCK);
|
||
|
strcpy(remoteAssignedPort, "\0");
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
* DROP_SERVER
|
||
|
* INIT_CLIENT
|
||
|
*
|
||
|
* while(){
|
||
|
* accept();
|
||
|
* client_send()
|
||
|
* }
|
||
|
*
|
||
|
* EXIT_CLIENT
|
||
|
* HALT_SERVER
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*/
|