215 lines
4.7 KiB
C
215 lines
4.7 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* testClient(char* host, char* port);
|
|
static void* testServer();
|
|
static void* testClientFTP();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************/
|
|
/* CORPS DU PROG */
|
|
/*****************/
|
|
int main(int argc, char* argv[]){
|
|
|
|
/* client */
|
|
if( argc > 2 && strcmp(argv[1],"c") == 0 )
|
|
testClient("localhost", argv[2]);
|
|
|
|
/* server */
|
|
else if( argc > 1 && strcmp(argv[1], "s") == 0 )
|
|
testServer();
|
|
|
|
/* clientFTP */
|
|
else if( argc > 1 && strcmp(argv[1], "f") == 0 )
|
|
testClientFTP();
|
|
|
|
/* client + server (multithread) */
|
|
else if( argc > 1 && strcmp(argv[1], "both") == 0 ){
|
|
|
|
pthread_create(&userServer, NULL, &testServer, (void*) NULL);
|
|
|
|
// on attends d'avoir le port
|
|
while( strlen(remoteAssignedPort) == 0 );
|
|
|
|
// on lance le client en boucle
|
|
while( strlen(remoteAssignedPort) != 0 )
|
|
testClient("localhost", remoteAssignedPort);
|
|
|
|
pthread_join(userServer, (void*) NULL);
|
|
pthread_exit((void*) userServer);
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void* testClientFTP(){
|
|
CLIENT_FTPREQUEST(FTP_HOST, FTP_PORT);
|
|
return;
|
|
|
|
// char request[maxBuffLen];
|
|
// char* response;
|
|
|
|
// while( 1 ){
|
|
// // on vide les buffers
|
|
// memset(&request, '\0', sizeof(request));
|
|
// memset(&response, '\0', sizeof(response));
|
|
|
|
// printf("> ");
|
|
// read_stdin(request, maxBuffLen);
|
|
// // on supprime le dernier retour chariot
|
|
// // request[strlen(request)-1] == '\0';
|
|
|
|
|
|
|
|
// CLIENT_REQUEST(FTP_HOST, FTP_PORT, request, &response);
|
|
|
|
// if( strlen(response) > 0 )
|
|
// printf("%s\n", response);
|
|
|
|
// response = NULL;
|
|
// }
|
|
|
|
// return;
|
|
}
|
|
|
|
|
|
static void* testClient(char* host, char* port){
|
|
char request[maxBuffLen];
|
|
char* response;
|
|
|
|
// on vide les buffers
|
|
memset(&request, '\0', sizeof(request));
|
|
memset(&response, '\0', sizeof(response));
|
|
|
|
printf("> ");
|
|
read_stdin(request, maxBuffLen);
|
|
// on supprime le dernier retour chariot
|
|
// request[strlen(request)-1] == '\0';
|
|
|
|
|
|
|
|
// CLIENT_REQUEST(remoteHost, remotePort, "GET / HTTP/1.0\r\n\r\n", &response);
|
|
CLIENT_REQUEST(host, port, request, &response);
|
|
|
|
if( strlen(response) > 0 )
|
|
printf("%s\n", response);
|
|
|
|
response = NULL;
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
|
|
static void* testServer(){
|
|
int SOCKET; // contiendra la connection en cours
|
|
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
|
|
|
|
|
|
/* [0] On lance @DROP_SERVER
|
|
==========================================================*/
|
|
DROP_SERVER(remoteHost, &serverPort, &LISTENSOCK);
|
|
|
|
printf("Port: %s\n", serverPort);
|
|
strcpy(remoteAssignedPort, serverPort);// PORT GLOBAL
|
|
|
|
while( userServerState ){
|
|
|
|
/* [1] Attente d'une demande de connection, puis création d'une socket
|
|
============================================================================*/
|
|
SOCKET = accept(LISTENSOCK, (struct sockaddr *) &clientInfo, &len);
|
|
|
|
if( DEBUGMOD ) printf("SOCKET: %d\n", SOCKET);
|
|
|
|
/* [2] On récupère les données reçues
|
|
============================================================================*/
|
|
nbReceived = read(SOCKET, BUFFER, sizeof(BUFFER)-1);
|
|
|
|
if( DEBUGMOD ) printf("Recu: %d\n", nbReceived);
|
|
if( DEBUGMOD ) printf("Message: %s\n", BUFFER);
|
|
|
|
/* [3] Redirection vers le serveur FTP
|
|
============================================================================*/
|
|
// on redirige la réponse vers le serveur FTP distant
|
|
char* ftp_response;
|
|
CLIENT_REQUEST(FTP_HOST, FTP_PORT, BUFFER, &ftp_response);
|
|
// printf("===FTP===\n%s\n", ftp_response);
|
|
|
|
/* [4] On analyse et renvoie la réponse à l'utilisateur
|
|
============================================================================*/
|
|
// formatBuffer(ftp_response);
|
|
// MANAGE_RESPONSE(&SOCKET, ftp_response);
|
|
|
|
// on réponds au client
|
|
formatBuffer(ftp_response);
|
|
write(SOCKET, ftp_response, strlen(ftp_response)-1);
|
|
|
|
|
|
/* [5] On ferme la SOCKET et vide les buffers
|
|
============================================================================*/
|
|
close(SOCKET);
|
|
memset(&BUFFER, '\0', sizeof(BUFFER)); // on vide le buffer
|
|
ftp_response = NULL;
|
|
|
|
|
|
}
|
|
|
|
close(LISTENSOCK);
|
|
strcpy(remoteAssignedPort, "\0");
|
|
}
|
|
|
|
|
|
/*
|
|
* DROP_SERVER
|
|
* INIT_CLIENT
|
|
*
|
|
* while(){
|
|
* accept();
|
|
* send
|
|
* }
|
|
*
|
|
* EXIT_CLIENT
|
|
* HALT_SERVER
|
|
*
|
|
*
|
|
*
|
|
*
|
|
*
|
|
*
|
|
*
|
|
*
|
|
*/ |