#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<termios.h>
#include<sys/time.h>
#include<stdlib.h>
#include<fcntl.h>
#define LG_BUFFER=1
/*lancement des options de session*/
LANCEMENT (socket1)
{
}
/*procedure de negociation d'option telnet*/
NEGOCIATION (mess_nego1,socket1)
{
unsigned char *mess_nego11,*rep_mess_nego1;
/*int socket1;*/
rep_mess_nego1=mess_nego11=mess_nego1;
printf("\nN° traitement des types de message %d %d %d valeur de la socket %d",*mess_nego11,*(mess_nego11+1),*(mess_nego11+2),socket1);
switch(*(mess_nego11+1))
{
case 250: printf("\nSB %d demande de sous negociation",*(mess_nego11+1));
case 251: printf("\nWILL %d demande de negociation",*(mess_nego11+1));
OPTION_NEGOCIEE(mess_nego11,socket);
break;
case 252: printf("\nWONT %d refus de negociation",*(mess_nego11+1));
*(rep_mess_nego1+1)=254;
write(socket1,rep_mess_nego1,3);
printf("\nReponse a WONT = %d",*(rep_mess_nego1+1));
break;
case 253: printf("\nDO %d demande de negociation",*(mess_nego11+1));
OPTION_NEGOCIEE(mess_nego11,socket);
break;
case 254: printf("\nDONT %d refus de negociation",*(mess_nego11+1));
*(rep_mess_nego1+1)=252;
write(socket1,rep_mess_nego1,3);
printf("\nReponse a WONT = %d",*(rep_mess_nego1+1));
break;
default: printf("\nErreur de traitement sur neociation N° option %d",*(mess_nego11+1));
break;
}
}
/*N° d'option a negocier*/
OPTION_NEGOCIEE(mess_nego11,socket)
{
printf("\nprocedure de negociation d'option" );
printf("\nVerification de l'option a negocier" );
}
/*DEBUT DE LA ROUTINE PRINCIPALE*/
main(argc,argv)
int argc;
char **argv;
{/*Debut du main*/
unsigned char *buffer_RX,*buffer_TX;
unsigned char *mess_nego;
int socket1,deblocage,connection,nb_lu,nb_ec,i,j,k,flag_nego_option;
struct sockaddr_in addr_distant;
struct servent *service_distant;
struct termios term_initial,term_com,sock_ini,sock_com;
struct timeval delai;
fd_set set;
flag_nego_option=0;
/*Preparation des parametres de connection*/
printf("\nadresse %s",argv[1]);
service_distant=getservbyname("telnet","tcp" );
memset(&addr_distant,0,sizeof(struct sockaddr_in));
addr_distant.sin_family=PF_INET;
addr_distant.sin_port=htons(service_distant->s_port);
if((inet_aton(argv[1],&addr_distant.sin_addr.s_addr))==0)
printf("\nimpossible de remplir le champ s_addr" );
printf("\nvaleur apres inet_aton() \t%08X",ntohl(addr_distant.sin_addr.s_addr));
printf("\nDEBUT DE PROGRAMME" );
printf("\ncration de la socket" );
/*Creation du point de communicatio*/
if((socket1=socket(AF_INET,SOCK_STREAM,0))<0)
printf("\nCreation de socket echouee" );
printf("\nNumero de socket \t%d",socket1);
printf("\nDeblocage de la socket" );
if((fcntl(socket1,F_SETFL,O_NONBLOCK))<0)
printf("\nProbleme sur fcntl" );
printf("\naddr_distant.sin_family \t%d",addr_distant.sin_family);
printf("\naddr_distant.sin_port\t%d",addr_distant.sin_port);
printf("\naddr_distant.sin_addr.s_addr\t%s\n",inet_ntoa(addr_distant.sin_addr.s_addr));
/*Procedure de mis en place du mode non bloquant sur stdin*/
tcgetattr(STDIN_FILENO,&term_initial);
tcgetattr(STDIN_FILENO,&term_com);
term_com.c_iflag &=ICRNL;
term_com.c_lflag &=~ICANON;/*,ECHONL,ICANON,IEXTEN);*/
term_com.c_lflag &=~ECHO;
term_com.c_cc[VTIME]=0;
term_com.c_cc[VMIN]=0;
if(tcsetattr(STDIN_FILENO,TCSANOW,&term_com)!=0)
printf("\ntransformation du terminal echoue" );
tcgetattr(socket1,&sock_ini);
tcgetattr(socket1,&sock_com);
sock_com.c_iflag |=OCRNL;
sock_com.c_oflag &=OPOST;
if(tcsetattr(socket1,TCSANOW,&sock_com)!=0)
printf("\ntransformation du terminal socket1 echoue" );
printf("\ncaracteres attendus %c %c %c",255,24,250);
/*Connection au point distant*/
printf("\ntentative de connection %d",connection=0);
if((connection=connect(socket1,(struct sockaddr*)&addr_distant,sizeof(addr_distant)))>0)
printf("\nvaleur de retour apres connect %d\n",connection);
else printf("\nconnection OK=%d",connection);
/*Modification des delaie pour passer en mode non bloquant*/
delai.tv_sec=0;
delai.tv_usec=0;
while(1)
{/*Debut du while 1*/
/*Initialisation de l'ensemble des descripteurs a multiplexer*/
FD_ZERO (&set);
FD_SET (socket1,&set);
FD_SET (STDIN_FILENO,&set);
/*Selection de l'ensemble des descripteurs a multiplexer en reception*/
if(select(socket1+1,&set,&set,NULL,&delai)<0)
break;
if(FD_ISSET(STDIN_FILENO,&set))
{/*Debut du if*/
/*Initialisation d un bloc memoire pour un caractere*/
if((buffer_RX=(char*) calloc (LG_BUFFER,sizeof (char)))==NULL)
printf("\nImpossible d'allouer la memoire buffer_RX STDIN_FILENO" );
nb_lu=0;
if((nb_lu=read(STDIN_FILENO,buffer_RX,LG_BUFFER))<0)
printf("\nerreur sur lecture d'entree ou rien a ecrire retour de nb_lu %d",nb_lu);
if(buffer_RX[nb_lu-1]==10)
{
buffer_RX=realloc(buffer_RX,LG_BUFFER+2,sizeof(char));
buffer_RX[nb_lu-1]=13;
buffer_RX[nb_lu]=10;
printf("\nles codes envoyes sont %d %d nb_lu=%d\n",buffer_RX[0],buffer_RX[1],nb_lu);
nb_lu=nb_lu+1;
}
nb_ec=write(socket1,buffer_RX,nb_lu);
free(buffer_RX);
}/*Fin du if*/
/*On teste l'arrivee de donnees sur la socket1*/
if(FD_ISSET(socket1,&set))
{/*Debut du if*/
if((buffer_RX=(unsigned char*) calloc (LG_BUFFER,sizeof (unsigned char)))==NULL)
printf("\nImpossible d'allouer la memoire buffer_RX pour socket1" );
nb_lu=0;
i=0;
while((nb_lu=recv(socket1,buffer_RX,LG_BUFFER,MSG_PEEK))>0)
{/*Debut du while*/
if(nb_lu==0)
printf("\nCaractere NULL recu = %d",buffer_RX[0]); if(buffer_RX[0]==255)
{/*Un caractere IAC est arrive*/
i=1;
if((mess_nego=(unsigned char*) calloc (i,sizeof (unsigned char)))==NULL)
printf("\nErreur sur attribution memoire mess_nego" );
flag_nego_option=1;
*mess_nego=*buffer_RX;
mess_nego=%d buffer_RX=%d",*mess_nego,*buffer_RX,mess_nego,buffer_RX);*/
while(flag_nego_option==1)
{/*debut du while*/
if((mess_nego=realloc(mess_nego,(i+1)*sizeof(unsigned char)))==NULL)
printf("\nErreur sur attribution memoire" );
nb_lu=read(socket1,mess_nego+i,LG_BUFFER);
if(*(mess_nego+i)>=236 && *(mess_nego+i)<255)
{ /*Phase de test 250>IAC<255*/
printf("\non passe le if de *mess_nego valeur *mess_nego+i = %d et *mess_nego = %d ",*(mess_nego+i),*(mess_nego));
++i;
if((mess_nego=realloc(mess_nego,(i+1)*sizeof(unsigned char)))==NULL)
printf("\nErreur sur attribution memoire" );
nb_lu=read(socket1,mess_nego+i,LG_BUFFER);
printf ("\nvaleur de i = %d",i);
printf("\nnouveau mess_nego = " );
for(j=0;j<=i;j++)
printf(" j= %d %d",j,*(mess_nego+j));
printf("\nValeur transmise a OPTION_NEGOCIEE %d %d",mess_nego,*mess_nego,*(mess_nego+1));
NEGOCIATION(mess_nego,socket1);
}
flag_nego_option=0;
free(mess_nego);
printf("\nle free(mes_nego) est ok valeur de l'adresse mess_nego %d %d\n",mess_nego,*mess_nego);
memset(mess_nego,0,strlen(mess_nego));
++i;
} /*fin de while(flag_nego_option)*/
printf("\nfin du if(buffer_RX[0]==255)" );
} /*fin de if(buffer_RX[0]==255)*/
else
{/*Debut du else*/
write(STDOUT_FILENO,buffer_RX,nb_lu);
} /*Fin du else*/
nb_lu=0;
}/*fin du while*/
free(buffer_RX);
memset(buffer_RX,0,strlen(buffer_RX));
}/*Fin du if*/
}/*Fin du while 1*/
/*printf("\n" );*/
printf("\nSortie de boucle avec ou sans raison nb_lu = %d",nb_lu);
close(socket1);
if((tcsetattr(STDIN_FILENO,TCSANOW,&term_initial))!=0)
printf("\nremeise en etat du stdin echoue\n" );
if((tcsetattr(socket1,TCSANOW,&sock_ini))!=0)
printf("\nremeise en etat du stdin echoue\n" );
printf ("\nSortie du prog\n" );
}/*fin du main*/