Connection des SOCKETS du BUS DE DONNEES + Fermeture lors du code de succès + LIST à faire
This commit is contained in:
parent
85901e9119
commit
c534ef1682
|
@ -85,7 +85,7 @@ void CLIENT_REQUEST(char* serverHost, char* serverPort, char* pRequest, char** p
|
|||
// memset(BUFFER, '\0', sizeof(BUFFER)); // on vide le buffer
|
||||
int nbRecup = sread(&SOCKET, BUFFER);
|
||||
|
||||
*pAnswer = malloc( sizeof(BUFFER) );
|
||||
*pAnswer = malloc( maxBuffLen );
|
||||
strcpy(*pAnswer, BUFFER);
|
||||
|
||||
if( DEBUGMOD ) printf("nbReceived: %d\n", nbRecup);
|
||||
|
@ -183,9 +183,9 @@ void CLIENT_SEND(int* pSocket, char* pRequest, char** pAnswer){
|
|||
|
||||
/* [6] On lit la réponse
|
||||
=======================================================*/
|
||||
int nbRecup = sread(pSocket, BUFFER);
|
||||
int nbRecup = WAIT_SOCKET_UPDATE(pSocket, BUFFER);
|
||||
|
||||
*pAnswer = malloc( sizeof(BUFFER) );
|
||||
*pAnswer = malloc( maxBuffLen );
|
||||
strcpy(*pAnswer, BUFFER);
|
||||
|
||||
if( DEBUGMOD ) printf("nbReceived: %d\n", nbRecup);
|
||||
|
|
88
dep/server.c
88
dep/server.c
|
@ -112,7 +112,7 @@ void DROP_SERVER(char* serverHost, char** givenPort, int* listenSocket){
|
|||
/* [7] On envoie les données par référence
|
||||
=======================================================*/
|
||||
// port
|
||||
*givenPort = malloc( sizeof(infoPort) );
|
||||
*givenPort = malloc( maxPortLen );
|
||||
strcpy(*givenPort, infoPort);
|
||||
|
||||
// socket d'écoute
|
||||
|
@ -127,15 +127,8 @@ void DROP_SERVER(char* serverHost, char** givenPort, int* listenSocket){
|
|||
// CMD: CLIENT (SRV) PROXY (CLT) FTP
|
||||
// DTA: CLIENT (CLT) PROXY (CLT) FTP
|
||||
//
|
||||
//
|
||||
//
|
||||
// [1] Ecoute sur port p de localhost
|
||||
// [2-n] Si commande, envoi à FTP
|
||||
// Puis Reception de FTP, envoi à CLIENT
|
||||
|
||||
|
||||
void MANAGE_REQUEST(int* SOCKET, char* pRequest){
|
||||
if( DEBUGMOD ) printf("====== MANAGE_REQUEST(%d, %s) ======\n\n", *SOCKET, pRequest);
|
||||
void MANAGE_REQUEST(char* pRequest, int* DUSER_SOCKET, int* DFTP_SOCKET){
|
||||
if( DEBUGMOD ) printf("====== MANAGE_REQUEST(%s, %d, %d) ======\n\n", pRequest, *DUSER_SOCKET, *DFTP_SOCKET);
|
||||
|
||||
char answer[maxBuffLen]; // contiendra la réponse
|
||||
int nbSend; // contiendra le nombre de données envoyées
|
||||
|
@ -151,42 +144,46 @@ void MANAGE_REQUEST(int* SOCKET, char* pRequest){
|
|||
/* [1] On découpe la requête en 2 parties
|
||||
================================================*/
|
||||
splitFtpRequest(pRequest, rCommand, rContent);
|
||||
// strcpy(answer, strcat(rCommand, rContent) );
|
||||
|
||||
/* [2] Selection en fonction de @rCommand
|
||||
================================================*/
|
||||
|
||||
/* (1) Erreur de syntaxe
|
||||
/* (1) PORT a1,a2,a3,a4,p1,p2 => utilisateur (actif)
|
||||
--------------------------------------------*/
|
||||
if( strcmp(rCommand, "ERROR") == 0 )
|
||||
strcpy(answer, "666 Tu t'es chié!!\n");
|
||||
if( strcmp(rCommand, "PORT") == 0 ){
|
||||
int a1, a2, a3, a4 = 0;
|
||||
int p1, p2 = 0;
|
||||
|
||||
/* (2) Connection (username)
|
||||
--------------------------------------------*/
|
||||
else if( strcmp(rCommand, "USER") == 0 )
|
||||
strcpy(answer, "331 C'est donc ça ton blase!\n");
|
||||
char serverHost[maxHostLen];
|
||||
char serverPort[maxPortLen];
|
||||
char BUFFER[maxBuffLen];
|
||||
|
||||
sscanf(pRequest, "PORT %d,%d,%d,%d,%d,%d", &a1, &a2, &a3, &a4, &p1, &p2);
|
||||
// on récupère l'adresse en <char*>
|
||||
sprintf(serverHost, "%d.%d.%d.%d", a1, a2, a3, a4);
|
||||
// on récupère le port en <char*>
|
||||
sprintf(serverPort, "%d", p1*256+p2);
|
||||
|
||||
|
||||
/* (n) Commande inconnue
|
||||
--------------------------------------------*/
|
||||
else
|
||||
strcpy(answer, "??? Connè pô!\n");
|
||||
// on se connecte au client
|
||||
CONNECT_CLIENT(serverHost, serverPort, DUSER_SOCKET);
|
||||
|
||||
printf("CONNECTED TO CLIENT %s:%s\n", serverHost, serverPort);
|
||||
|
||||
// on envoie PASV car on veut être en mode passif entre le proxy et le serveur FTP
|
||||
strcpy(pRequest, "PASV\n");
|
||||
}
|
||||
// WAIT_SOCKET_UPDATE(DFTP_SOCKET, BUFFER);
|
||||
// xPrint("FTPLIST: %s\n", BUFFER);
|
||||
|
||||
/* [3] Envoi de la réponse
|
||||
================================================*/
|
||||
formatBuffer(answer);
|
||||
nbSend = write(*SOCKET, answer, strlen(answer));
|
||||
|
||||
/* [4] Fermeture de la SOCKET
|
||||
================================================*/
|
||||
// close(*SOCKET);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MANAGE_RESPONSE(char** pAnswer, int* DUSER_SOCKET, int* DFTP_SOCKET){
|
||||
if( DEBUGMOD ) printf("====== MANAGE_RESPONSE(%d, %s) ======\n\n", *SOCKET, *pAnswer);
|
||||
if( DEBUGMOD ) printf("====== MANAGE_RESPONSE(%s, %d, %d) ======\n\n", *pAnswer, *DUSER_SOCKET, *DFTP_SOCKET);
|
||||
|
||||
char response[maxBuffLen]; // contiendra la réponse
|
||||
char ftpCode[4]; // contiendra le code FTP (1ère partie)
|
||||
|
@ -239,6 +236,37 @@ void MANAGE_RESPONSE(char** pAnswer, int* DUSER_SOCKET, int* DFTP_SOCKET){
|
|||
strcpy(response, EXIT_MSG);
|
||||
|
||||
|
||||
/* (7) Mode passif => On lance les SOCKETS du BUS DE DONNEES
|
||||
--------------------------------------------*/
|
||||
else if( strcmp(ftpCode, "227") == 0 ){ // on lance la SOCKET FTP du BUS DE DONNEES
|
||||
strcpy(response, *pAnswer);
|
||||
int a1, a2, a3, a4 = 0;
|
||||
int p1, p2 = 0;
|
||||
|
||||
char serverHost[maxHostLen];
|
||||
char serverPort[maxPortLen];
|
||||
char BUFFER[maxBuffLen];
|
||||
|
||||
sscanf(*pAnswer, "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)", &a1, &a2, &a3, &a4, &p1, &p2);
|
||||
// on récupère l'adresse en <char*>
|
||||
sprintf(serverHost, "%d.%d.%d.%d", a1, a2, a3, a4);
|
||||
// on récupère le port en <char*>
|
||||
sprintf(serverPort, "%d", p1*256+p2);
|
||||
|
||||
// on se connecte au serveur FTP
|
||||
CONNECT_CLIENT(serverHost, serverPort, DFTP_SOCKET);
|
||||
|
||||
printf("CONNECTED TO FTP %s:%s\n", serverHost, serverPort);
|
||||
}
|
||||
|
||||
|
||||
/* (8) Fin de transfert de données
|
||||
--------------------------------------------*/
|
||||
else if( strcmp(ftpCode, "226") == 0 ){ // on ferme les SOCKETS du BUS DE DONNEES
|
||||
strcpy(response, *pAnswer);
|
||||
close(*DUSER_SOCKET);
|
||||
close(*DFTP_SOCKET);
|
||||
}
|
||||
|
||||
/* (n) Commande inconnue
|
||||
--------------------------------------------*/
|
||||
|
@ -255,7 +283,7 @@ void MANAGE_RESPONSE(char** pAnswer, int* DUSER_SOCKET, int* DFTP_SOCKET){
|
|||
|
||||
|
||||
|
||||
int WAIT_CLIENT(int* pSocket, char* pBuffer){
|
||||
int WAIT_SOCKET_UPDATE(int* pSocket, char* pBuffer){
|
||||
memset(pBuffer, 0, maxBuffLen); // on vide le buffer
|
||||
|
||||
int nbRead = 0;
|
||||
|
|
21
dep/server.h
21
dep/server.h
|
@ -10,13 +10,18 @@ void DROP_SERVER(char* serverHost, char** givenPort, int* listenSocket);
|
|||
|
||||
|
||||
|
||||
/* Gestion de la réponse au client
|
||||
/* Gestion de la requête du client
|
||||
*
|
||||
* @SOCKET<int*> Pointeur sur la SOCKET concernée
|
||||
* @pRequest<char*> Requête reçue et à traiter
|
||||
* @pBuffer<char*> Requête en question
|
||||
* @DUSER_SOCKET<int*> Pointeur sur la SOCKET du BUS DE DONNEES utilisateur
|
||||
* @DFTP_SOCKET<int*> Pointeur sur la SOCKET du BUS DE DONNEES FTP
|
||||
*
|
||||
* @history
|
||||
* [1] si "PORT a1,a2,a3,a4,p1,p2" on démarre la SOCKET CLIENT du BUS DE DONNEES
|
||||
* [2]
|
||||
* [3]
|
||||
*/
|
||||
void MANAGE_REQUEST(int* SOCKET, char* pRequest);
|
||||
void MANAGE_REQUEST(char* pRequest, int* DUSER_SOCKET, int* DFTP_SOCKET);
|
||||
|
||||
|
||||
|
||||
|
@ -27,11 +32,15 @@ void MANAGE_REQUEST(int* SOCKET, char* pRequest);
|
|||
* @DFTP_SOCKET<int*> Pointeur sur la SOCKET du BUS DE DONNEES FTP
|
||||
*
|
||||
* @history
|
||||
* [1] si commande sans transfert nécessaire, on modifie la réponse
|
||||
* [2] si on a besoin d'un transfert de données
|
||||
* [1] SI commande sans transfert nécessaire, on modifie la réponse
|
||||
|
||||
* [2] SI on a besoin d'un transfert de données
|
||||
* (1) On initialise les SOCKETS avec la valeur de PORT a,b,c,d,p1,p2
|
||||
* (2) a.b.c.d (adresse ip) + p1*256+p2 (port)
|
||||
* (3) UTILISATEUR (ACIF) PROXY (PASSIF) SRV_FTP
|
||||
*
|
||||
* [3] SI 227, on lance la SOCKET FTP du BUS DE DONNEES (mode passif)
|
||||
* [4] SI 226, on ferme les SOCKETS du BUS DE DONNEES
|
||||
*
|
||||
*/
|
||||
void MANAGE_RESPONSE(char** pAnswer, int* DUSER_SOCKET, int* DFTP_SOCKET);
|
||||
|
|
26
proxy_ftp.c
26
proxy_ftp.c
|
@ -21,8 +21,10 @@ static void* testServer();
|
|||
/* CORPS DU PROG */
|
||||
/*****************/
|
||||
int main(int argc, char* argv[]){
|
||||
|
||||
testServer();
|
||||
if( argc > 1 )
|
||||
testServer(argv[1]);
|
||||
else
|
||||
testServer("");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -39,7 +41,7 @@ int main(int argc, char* argv[]){
|
|||
|
||||
|
||||
|
||||
static void* testServer(){
|
||||
static void* testServer(char* localPort){
|
||||
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
|
||||
|
@ -58,8 +60,12 @@ static void* testServer(){
|
|||
|
||||
/* [0] On démarre le SERVEUR + le CLIENT
|
||||
==========================================================*/
|
||||
serverPort = malloc(4*sizeof(char));
|
||||
strcpy(serverPort, "4444");
|
||||
serverPort = malloc(maxPortLen);
|
||||
|
||||
if( strlen(localPort) != 0 )
|
||||
strcpy(serverPort, localPort);
|
||||
|
||||
|
||||
DROP_SERVER(remoteHost, &serverPort, &LISTENSOCK);
|
||||
CONNECT_CLIENT(FTP_HOST, FTP_PORT, &FTP_SOCKET);
|
||||
|
||||
|
@ -74,7 +80,7 @@ static void* testServer(){
|
|||
============================================================================*/
|
||||
strcpy(BUFFER, "");
|
||||
CLIENT_SEND(&FTP_SOCKET, BUFFER, &ftp_response);
|
||||
MANAGE_RESPONSE(&USER_SOCKET, &ftp_response);
|
||||
MANAGE_RESPONSE(&ftp_response, &DUSER_SOCKET, &DFTP_SOCKET);
|
||||
swrite(&USER_SOCKET, ftp_response);
|
||||
|
||||
xPrint("P->F: %s\n\n", BUFFER);
|
||||
|
@ -85,7 +91,7 @@ static void* testServer(){
|
|||
|
||||
/* [3] On récupère les données reçues (+attente)
|
||||
============================================================================*/
|
||||
if( WAIT_CLIENT(&USER_SOCKET, BUFFER) == -1 ) break;
|
||||
if( WAIT_SOCKET_UPDATE(&USER_SOCKET, BUFFER) == -1 ) break;
|
||||
xPrint("C->P: %s\n", BUFFER);
|
||||
|
||||
if( DEBUGMOD ) printf("Recu: %d\n", nbReceived);
|
||||
|
@ -94,19 +100,19 @@ static void* testServer(){
|
|||
|
||||
/* [4] Traitement de la requête
|
||||
============================================================================*/
|
||||
MANAGE_REQUEST(&BUFFER, &DUSER_SOCKET, &DFTP_SOCKET);
|
||||
MANAGE_REQUEST(BUFFER, &DUSER_SOCKET, &DFTP_SOCKET);
|
||||
|
||||
|
||||
/* [5] Redirection vers le serveur FTP
|
||||
============================================================================*/
|
||||
CLIENT_SEND(&FTP_SOCKET, BUFFER, &ftp_response);
|
||||
xPrint("P->F: %s\n\n", BUFFER);
|
||||
CLIENT_SEND(&FTP_SOCKET, BUFFER, &ftp_response);
|
||||
xPrint("F->P: %s\n", ftp_response);
|
||||
|
||||
|
||||
/* [6] Traitement de la réponse (FTP)
|
||||
============================================================================*/
|
||||
MANAGE_RESPONSE(&USER_SOCKET, &ftp_response, &DUSER_SOCKET, &DFTP_SOCKET);
|
||||
MANAGE_RESPONSE(&ftp_response, &DUSER_SOCKET, &DFTP_SOCKET);
|
||||
|
||||
|
||||
/* [7] Redirection vers le CLIENT
|
||||
|
|
Loading…
Reference in New Issue