je doit creer un proxy :
de notre coté on se connecte actif
le serveur fonctionne en passif
j'ai essayer de faire un proxy en C et je me trouve en difficulté
je ne c'est pas comment tester se programme.
le code suivant et la partie commande
je n'est pas encore fait la partie données
je remercie d'avance toute personne tetant de m'aider
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
int main (int nbpar, char ** tab)
{
int err, fd_sock, nouvSock, nom, lect, port;
struct in_addr addr;
struct sockaddr_in adrSock1, adrSock2;
char nomMachine[50] ,tampon[256];
struct hostent * caract;
long IP1, IP;
//connection du client au proxy
//creation d'un socket
fd_sock=socket(AF_INET,SOCK_STREAM,0);
if (fd_sock==-1)
{
perror("socket!");
exit(1);
}
//recuperation du nom de la machine
nom=gethostname(nomMachine, sizeof(nomMachine));
if (nom==-1)
{
perror("erreur gethostname");
exit(2);
}
//recuperation de l'adresse IP
caract=(gethostbyname(nomMachine));
if (caract==NULL)
{
perror("erreur gethostbyname");
exit(3);
}
IP1=ntohl(*((long*)caract ->h_addr));
//initialisation du nom externe d'une prise socket
adrSock1.sin_family = AF_INET;
adrSock1.sin_port=0;
adrSock1.sin_addr.s_addr=ntohl(IP1);
//publication d'une socket
err=bind(fd_sock,(struct sockaddr *) &adrSock1,sizeof(adrSock1));
if(err==-1)
{
perror("publication du socket impossible");
exit(4);
}
//attente d'acceptation de la connexion
err=listen(fd_sock, 1);
if(err==-1)
{
perror("attente impossible");
exit(5);
}
//initialisation de la structure sockaddr_in
int lg=sizeof(adrSock1);
err=getsockname(fd_sock,(struct sockaddr *) &adrSock1, &lg);
if(err==-1)
{
perror("erreur getsockname");
exit(6);
}
//Récupérer le port
port=ntohs(adrSock1.sin_port);
lg=sizeof(adrSock1);
nouvSock=accept(fd_sock,(struct sockaddr *) & adrSock1, &lg);
if (nouvSock==-1)
{
perror("impossible d'accpter la connection");
exit(7);
}
//lecture de information
lect=read(nouvSock,tampon,sizeof(tampon));
if (lect == -1)
{
perror("echec lire");
exit (8);
}
while(lect != 0 && tampon[0]!='U')
{
lect=read(nouvSock,tampon,sizeof(tampon));
if (lect == -1)
{
perror("echec lire");
exit (8);
}
}
//recuperation du login et du nom de la machine
int i=0, j=0;
char login[64], nomMach[50];
while (tampon[i]!='@')
{
login[j]=tampon[i];
i++;j++;
}
i++;j=0;
while (tampon[i]!='\n')
{
nomMach[j]=tampon[i];
i++;j++;
}
char * cmd1={"USER "},* cmd2={"PASS "};
strcat(cmd1, login);
//recuperation du mot de pass
lect=read(nouvSock,tampon,sizeof(tampon));
if (lect == -1)
{
perror("echec lire");
exit (8);
}
while(lect != 0 && tampon[0]!='U')
{
lect=read(nouvSock,tampon,sizeof(tampon));
if (lect == -1)
{
perror("echec lire");
exit (8);
}
}
int n=0, m=0;
char pass[64];
while (tampon[n]!='\n')
{
pass[m]=tampon[n];
n++;m++;
}
strcat(cmd2,pass);
//recuperation de la commande list
char list[64];
lect=read(nouvSock,list,sizeof(list));
if (lect == -1)
{
perror("echec lire");
exit (8);
}
while(lect != 0 && list[0]!='U')
{
lect=read(nouvSock,list,sizeof(list));
if (lect == -1)
{
perror("echec lire");
exit (8);
}
}
//recuperation de l'adresse IP
caract=(gethostbyname(nomMach));
if (caract==NULL)
{
perror("erreur gethostbyname");
exit(3);
}
IP=ntohl(*((long*)caract->h_addr));
//initialisation du nom externe d'une prise socket
adrSock2.sin_family = AF_INET;
adrSock2.sin_port=21;
adrSock2.sin_addr.s_addr=ntohl(IP);
//connection du proxy au serveur
int fd_sock3;
//creation d'un socket
fd_sock3=socket(AF_INET,SOCK_STREAM,0);
if (fd_sock==-1)
{
perror("socket!");
exit(1);
}
//etablissement de la connexion
err=connect(fd_sock3,(struct sockaddr *) &adrSock2, sizeof(adrSock2));
if (err==-1)
{
perror("connection impossible");
exit(2);
}
//ecriture des information retourné
err=write(fd_sock3,cmd1,sizeof(cmd1));
if (err==-1)
{
perror("erreur ecriture login");
exit(2);
}
err=write(fd_sock3,cmd2,sizeof(cmd2));
if (err==-1)
{
perror("erreur ecriture pass");
exit(2);
}
err=write(fd_sock3,list,sizeof(list));
if (err==-1)
{
perror("erreur ecriture list");
exit(2);
}
//test de la connexion
char res[64];
err=read(fd_sock3,res,sizeof(res));
if (err==-1)
{
perror("erreur lecture");
exit(2);
}
close(fd_sock);
}
bonne chance!