principalsolucionesentornospaperspaperscontactepapers

/*Servidor de Conexiones inversas eid0 2001*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include "pthread.h"
#include <string.h>
#include <arpa/inet.h>
#include <signal.h>
//#include <linux/time.h>
//#define PUERTO_FTP 21
typedef struct threadinfo{

int socket;
struct sockaddr_in infosocket;
//int threadid;
int comando;
char * comando_data;
unsigned int host;

int esesclavo;
//char * buffercomunicacion;
// pthread_cond_t haydatos;
//pthread_mutex_t lockdatos;
} threadinfo;
threadinfo gwdatoscomunicador;
void * comunicador_master(void *);
void * procesasocket(threadinfo *);
void acaba(int);
int nthread;
int numeroesclavos;
threadinfo datosesclavos[40];
//pthread_t threads[NUM_THREADS];
//tmutexes *memmutexes1;
//threadinfo threadinf[500];

pthread_cond_t comunicador_nohaydatos;
pthread_cond_t comunicador_haydatos;
pthread_mutex_t comunicador_lock;
int haydatos;
int socketprincipal;

pthread_t threadkrap;
unsigned int comunicador_host;
char mensaje[100];

//int comunicador_comando;
//int comunicador_data;

int main(int argc, char* argv[])
{

        int ilens;
        int on;
        int soket=0,sok2=0;
        struct sockaddr_in sin;
        struct sockaddr addr_accept;
        threadinfo * puntinfothread;
        nthread=0;
        signal(SIGINT,acaba);
        //inicializamos mutexes para comunicador master
        pthread_mutex_init (&comunicador_lock, NULL);
        pthread_cond_init (&comunicador_haydatos, NULL);
        pthread_cond_init (&comunicador_nohaydatos, NULL);
        pthread_mutex_lock(&comunicador_lock);
        haydatos=0;
        pthread_cond_signal (&comunicador_nohaydatos);
        pthread_mutex_unlock (&comunicador_lock);
        //pthread_create (&threadkrap, NULL, comunicador_master, 0);
        if (argc<2 )
        {
                printf("eid0 inverse conection listener HUB, \nUsage: %s port\n",argv[0]);
                return 0;
        }
        pthread_create (&threadkrap, NULL, comunicador_master, 0);
        //printf("Iniciando socket hacia fichero %s",argv[1]);

                memset (&sin,0,sizeof(sin));
                sin.sin_family=PF_INET;
                sin.sin_port=htons(atoi(argv[1]));
                //sin.sin_port=htons(1500);
                //char * target_ip=argv[1];
                //char target_ip[60]="192.168.0.1";
                sin.sin_addr.s_addr=INADDR_ANY;
                if ((soket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
                {
                        perror("error de socket");
                        return 0;
                }
                socketprincipal=soket;
                on=1;
                setsockopt(soket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
                if (bind(soket,(const struct sockaddr *) &sin,sizeof(sin)))
                {
                        perror("error en bind");
                                return 0;
                }
                if (listen(soket,3))
                {
                        printf("error en listen");
                                return 0;
                }
                printf("bindeado en port %s\n",argv[1]);

ilens=sizeof(struct sockaddr_in);
//contathread=0;
while (1)
{

                if ((sok2=accept(soket,&addr_accept,&ilens))==-1)
                {
                        perror("error en accept");
                        return 0;
                }
                printf("main: llamando a procesasocket\n");
                puntinfothread=malloc(sizeof(threadinfo));
                puntinfothread->socket=sok2;
                //puntinfothread->threadid=contathread;
                memcpy(&(puntinfothread->infosocket),&addr_accept,sizeof(struct sockaddr));
                pthread_create(&threadkrap, NULL, procesasocket,(void *)puntinfothread);

}
return 0;
}

void * procesasocket(threadinfo * infothread) {
int soket;
// int nbyt;
char buffer[4096];
char bufferaux[50];
int leidos;
unsigned int infohost;
char * pos;
int sizestruct;
struct sockaddr_in esclavohost;
//socket=threadinf[numerothread].socket soket=(infothread)->socket;
leidos=recv(soket,buffer,4096,0);
buffer[leidos]=0;
printf("procesasocket: leido pakete: %s\n",buffer);

if ((pos=strstr(buffer,"TOYBAJOUNFIREWALL"))) {

     sizestruct=sizeof(struct sockaddr_in);
     getpeername(soket,(struct sockaddr *)&esclavohost,&sizestruct);
     infohost=esclavohost.sin_addr.s_addr;
     sprintf(bufferaux,"%d.%d.%d.%d",((unsigned char *)&infohost)[0],((unsigned char *) &infohost)[1], ((unsigned char *)&infohost)[2], ((unsigned char *)&infohost)[3]);
     printf("procesasoket: nuevo esclavo conectado en %s\n",bufferaux);
     pthread_mutex_lock(&comunicador_lock);
     while (haydatos != 0)
       pthread_cond_wait (&comunicador_nohaydatos,&comunicador_lock);
     haydatos=-1;
     gwdatoscomunicador.comando=1;
     gwdatoscomunicador.infosocket=infothread->infosocket;
     gwdatoscomunicador.socket=soket;
     gwdatoscomunicador.host=infohost;
     send(soket,"OK\n",3,0);
     pthread_cond_signal (&comunicador_haydatos);
     pthread_mutex_unlock (&comunicador_lock);
     while (1)
       {
         if ((leidos=recv(soket,&buffer,4095,0))<=0) {printf("El esclavo se ha desconectado\n");
         close(soket);
         pthread_exit(0);
         }
         buffer[4095]=0;
         buffer[leidos]=0;
         if (strstr(buffer,"eid0PING"))
           {
           send(soket,"PONG\n",strlen("PONG\n"),0);
           //printf("controladordeesclavo: PONG!!\n");
           }
         else printf("controladordesclavo: datos de esclavo: %s\n",buffer);
       }

}
else
if ((pos=strstr(buffer,"CONTROL")))//loquevieneahoraes IPDESTINOREDIRECCIONAPUERTO 000.000.000.0000000000000

     {
       printf("procesasoket: ha llegado una orden de redireccion\n");
       pthread_mutex_lock(&comunicador_lock);
       while (haydatos != 0)
         pthread_cond_wait (&comunicador_nohaydatos,&comunicador_lock);
       haydatos = -1;
       gwdatoscomunicador.comando=2;
       pos+=strlen("CONTROL");
       memcpy(&gwdatoscomunicador.comando_data,&pos,leidos-(pos-buffer)-strlen("CONTROL"));
       memcpy(&gwdatoscomunicador.infosocket,&infothread->infosocket,sizeof(struct sockaddr));
     gwdatoscomunicador.socket=soket;
     send(soket,"OK\n",3,0);
     buffer[leidos]=0;
     pthread_cond_signal (&comunicador_haydatos);
     pthread_mutex_unlock (&comunicador_lock);
     }

close(soket);
pthread_exit(0);
}

void * comunicador_master(void * novale) {
//int numeroesclavos;
int tempi;
//threadinfo datosesclavos[40];
unsigned int hostdest;
char bufferaux[100];
char numerochar[4];
int a;
//int buffer[4096];
//int leidos;
numeroesclavos=0;
pthread_mutex_lock(&comunicador_lock);
/* Wait until buffer is not full */
while(1)
{

     //while (gwdatoscomunicador.comando)
     //{
     printf("comunicador master: esperando datos\n");
     while (haydatos == 0)
      pthread_cond_wait (&comunicador_haydatos,&comunicador_lock);
      /* pthread_cond_wait reacquired b->lock before returning */
      //}
      haydatos=0;
      printf("comunicadormaster: entrando en analisis de mensaje--%i\n",gwdatoscomunicador.comando);

/* Write the data and advance write pointer */ if (gwdatoscomunicador.comando==1) //esclavo {

      tempi=0;
      while ((datosesclavos[tempi].host!=gwdatoscomunicador.host) && (tempi<numeroesclavos)) tempi++;
      datosesclavos[tempi].socket=gwdatoscomunicador.socket;
      datosesclavos[tempi].infosocket=gwdatoscomunicador.infosocket;

      datosesclavos[tempi].host=gwdatoscomunicador.host;
      if (tempi==numeroesclavos) {numeroesclavos++;}
     }

else if (gwdatoscomunicador.comando==2) //cliente {

      tempi=0;
      memcpy(bufferaux,gwdatoscomunicador.comando_data,15);//copiamoslaipdestino
      bufferaux[15]=0;
      //printf("comunicador_master: comandodata=%s\n",bufferaux);

hostdest=0;
for (a=0;a<4;a++)

        {memcpy(numerochar,&(bufferaux[a*4]),3);
      numerochar[4]=0;
      hostdest|=(atoi(numerochar)<<(a*8));}

//hostdest=inet_addr(bufferaux);

      printf("comunicador_master: mandando mensaje a %s, %x--%x\n",bufferaux,hostdest,datosesclavos[tempi].host);
      while ((datosesclavos[tempi].host!=hostdest) && (tempi<numeroesclavos)) tempi++;
      send(datosesclavos[tempi].socket,gwdatoscomunicador.comando_data+15,strlen(gwdatoscomunicador.comando_data)-15,0);

}//comando=2
/* Signal that the buffer is now not empty */ //printf("signaling nohaydatos\n");
pthread_cond_signal (&comunicador_nohaydatos); }
pthread_mutex_unlock (&comunicador_lock);

}

void acaba(senyal)
{
int tempi;
shutdown(socketprincipal,1);
close(socketprincipal);
tempi=0;
while (tempi<numeroesclavos)

     {
       shutdown(datosesclavos[tempi].socket,1);
       close(datosesclavos[tempi].socket);
     tempi++;}

printf("Acabado!!\n");
//sleep(1000);
pthread_exit(0);
//commit(1);
//return 0;
}