2015-12-14 20:48:19 +00:00
|
|
|
#include "utility.h"
|
|
|
|
|
|
|
|
void splitFtpRequest(char* pRequest, char* pCommand, char* pContent){
|
|
|
|
/* [1] Vérification du format
|
|
|
|
===========================================*/
|
|
|
|
int firstSpaceIndex = indexOf(pRequest, ' ');
|
|
|
|
|
|
|
|
if( firstSpaceIndex != 3 && firstSpaceIndex != 4){ // contient aucun espace en position 3 ou 4, on quitte
|
|
|
|
strcpy(pCommand, "ERROR");
|
|
|
|
strcpy(pContent, "");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Séparation des 2 parties
|
|
|
|
===========================================*/
|
|
|
|
int i;
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
for( i = 0 ; i < strlen(pRequest) && pRequest[i] != '\0' ; i++ ){
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
if( i < firstSpaceIndex ) // première partie (pCommand)
|
2015-12-16 23:19:34 +00:00
|
|
|
strcat( pCommand, (char[2]){(char) pRequest[i], '\0'});
|
2015-12-14 20:48:19 +00:00
|
|
|
if( i > firstSpaceIndex ) // seconde partie (pContent)
|
2015-12-16 23:19:34 +00:00
|
|
|
strcat( pContent, (char[2]){(char) pRequest[i], '\0'});
|
2015-12-14 20:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void splitFtpResponse(char* pAnswer, char* ftpCode, char* ftpText){
|
|
|
|
/* [1] Vérification du format
|
|
|
|
===========================================*/
|
|
|
|
int codeLength = 3; // taille du code
|
|
|
|
|
|
|
|
/* [2] Séparation des 2 parties
|
|
|
|
===========================================*/
|
|
|
|
int i;
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
for( i = 0 ; i < strlen(pAnswer) && pAnswer[i] != '\0' ; i++ ){
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
if( i < codeLength ) // première partie (ftpCode)
|
2015-12-16 23:19:34 +00:00
|
|
|
strcat(ftpCode, (char[2]){(char)pAnswer[i], '\0' });
|
2015-12-14 20:48:19 +00:00
|
|
|
if( i > codeLength ) // seconde partie (ftpText)
|
2015-12-16 23:19:34 +00:00
|
|
|
strcat(ftpText, (char[2]){(char)pAnswer[i], '\0' });
|
2015-12-14 20:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int indexOf(char* haystack, char needle){
|
|
|
|
int i;
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
for( i = 0 ; i < strlen(haystack) && haystack[i] != '\0' ; i++ )
|
2015-12-14 20:48:19 +00:00
|
|
|
if( haystack[i] == needle ) // si on trouve le caractère
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int swrite(int* pSocket, char* pBuffer){
|
|
|
|
if( *pSocket == -1 ) return -1; // si SOCKET fermée, on retourne une erreur
|
2015-12-16 10:02:05 +00:00
|
|
|
if( strlen(pBuffer) == 0 ) return 0; // si on a rien à écrire, on n'écrit rien :p
|
2015-12-14 20:48:19 +00:00
|
|
|
|
2015-12-16 18:24:07 +00:00
|
|
|
|
2015-12-17 17:25:39 +00:00
|
|
|
/* 1. On écrit sur la SOCKET */
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&BUF ) printf("SENDLEN_1: %lu\n", strlen(pBuffer));
|
|
|
|
int nbSent = write(*pSocket, pBuffer, strlen(pBuffer));
|
|
|
|
if( DEBUGMOD&BUF ) printf("SENDLEN_2: %d\n", nbSent);
|
2015-12-15 18:44:19 +00:00
|
|
|
|
2015-12-17 17:25:39 +00:00
|
|
|
/* 2. Si on est déconnecté, on retourne une erreur */
|
2015-12-16 10:02:05 +00:00
|
|
|
if( nbSent <= 0 ){
|
|
|
|
if( DEBUGMOD&BUF ) printf("NOTHING TO SEND\n");
|
|
|
|
return -1; // on retourne une erreur
|
|
|
|
}
|
|
|
|
|
|
|
|
if( DEBUGMOD&RVL ) printf("SEND_1\n");
|
|
|
|
if( DEBUGMOD&RVL ) revealString(pBuffer);
|
|
|
|
if( DEBUGMOD&RVL ) printf("SEND_2\n");
|
|
|
|
|
2015-12-16 18:24:07 +00:00
|
|
|
|
2015-12-17 17:25:39 +00:00
|
|
|
/* 3. On retourne le nombre de <char> envoyés */
|
2015-12-16 10:02:05 +00:00
|
|
|
return nbSent;
|
2015-12-14 20:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sread(int* pSocket, char* pBuffer){
|
|
|
|
if( *pSocket == -1 ) return -1; // si SOCKET fermée, on retourne une erreur
|
|
|
|
|
|
|
|
|
2015-12-16 20:38:39 +00:00
|
|
|
/* 1. On vide le buffer avant de lire */
|
2015-12-14 20:48:19 +00:00
|
|
|
memset(pBuffer, '\0', maxBuffLen);
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&BUF ) printf("READLEN_1: %lu\n", strlen(pBuffer));
|
2015-12-16 20:38:39 +00:00
|
|
|
|
|
|
|
/* 2. On lit la SOCKET */
|
2015-12-14 20:48:19 +00:00
|
|
|
int nbRead = read(*pSocket, pBuffer, maxBuffLen);
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&BUF ) printf("READLEN_3: %d\n", nbRead);
|
2015-12-14 20:48:19 +00:00
|
|
|
|
2015-12-16 20:38:39 +00:00
|
|
|
|
|
|
|
/* 3. Si on est déconnecté, on retourne une erreur */
|
2015-12-16 10:02:05 +00:00
|
|
|
if( nbRead <= 0 ){
|
|
|
|
if( DEBUGMOD&BUF ) printf("NOTHING TO READ\n");
|
2015-12-14 20:48:19 +00:00
|
|
|
return -1; // on retourne une erreur
|
|
|
|
}
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&RVL ) printf("READ_1\n");
|
|
|
|
if( DEBUGMOD&RVL ) revealString(pBuffer);
|
|
|
|
if( DEBUGMOD&RVL ) printf("READ_2\n");
|
|
|
|
|
2015-12-16 20:38:39 +00:00
|
|
|
/* 4. On retourne le nombre de caractères lus */
|
2015-12-14 20:48:19 +00:00
|
|
|
return nbRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-17 17:25:39 +00:00
|
|
|
void setSocketTimeout(int* pSocket, const int pTimeout){
|
|
|
|
/* 1. On créé la structure contenant le timeout */
|
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = pTimeout;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
setsockopt(*pSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(struct timeval));
|
|
|
|
|
|
|
|
if( DEBUGMOD&SCK ) printf("Set socket(%d) timeout to %lus\n", *pSocket, (long unsigned) timeout.tv_sec );
|
|
|
|
}
|
|
|
|
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
void xPrint(char* pPattern, char* pBuffer){
|
|
|
|
char tmpBuffer[maxBuffLen];
|
|
|
|
strcpy(tmpBuffer, pBuffer);
|
|
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// on supprimes les retours à la ligne de la fin
|
2015-12-16 10:02:05 +00:00
|
|
|
for( i = strlen(tmpBuffer)-1 ; i >= 0 && tmpBuffer[i] != '\0' ; i-- )
|
2015-12-14 20:48:19 +00:00
|
|
|
if( tmpBuffer[i] == '\n' || tmpBuffer[i] == '\r' ) // si c'est un retour chariot
|
|
|
|
tmpBuffer[i] = '\0'; // on efface
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2015-12-15 18:44:19 +00:00
|
|
|
printf(pPattern, pBuffer);
|
2015-12-16 10:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void revealString(char* pString){
|
|
|
|
/* 1. On créé une copie de @pString */
|
|
|
|
char revealedString[maxBuffLen] = {0};
|
|
|
|
|
|
|
|
/* 2. On rend visible tous les caractères "invisibles" */
|
|
|
|
int i;
|
|
|
|
for( i = 0 ; i < strlen(pString) && pString[i] != '\0' ; i++ ){
|
|
|
|
if( pString[i] == '\r' ){ revealedString[strlen(revealedString)] = '\\'; revealedString[strlen(revealedString)] = 'r'; }
|
|
|
|
else if( pString[i] == '\n' ){ revealedString[strlen(revealedString)] = '\\'; revealedString[strlen(revealedString)] = 'n'; }
|
|
|
|
else revealedString[strlen(revealedString)] = pString[i];
|
|
|
|
|
|
|
|
revealedString[strlen(revealedString)] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3. On affiche la chaîne explicite */
|
2015-12-16 18:24:07 +00:00
|
|
|
if( DEBUGMOD&RVL ) printf("[[%s]]\n", revealedString);
|
2015-12-14 20:48:19 +00:00
|
|
|
}
|