User:Maryc


 * CONEXION EN LINUX ICMP
 * include  
 * #include  
 * #include 
 * #include 
 * #include 
 * #define ICMP_ECHO              8
 * #define IPLEN          sizeof(struct iphdr)
 * #define ICMPLEN        sizeof(struct icmphdr)
 * typedef unsigned char u8;
 * typedef unsigned short u16;
 * typedef unsigned long u32;
 * struct icmphdr {
 *        u8              type;
 *        u8              code;
 *        u16             checksum;
 *        u16             id;
 *        u16             sequence;
 * };
 * unsigned short csum (u16 *buf, int nwords) {
 *        unsigned long sum;
 *        for (sum = 0; nwords > 0; nwords--)
 *                sum += *buf++;
 *                sum = (sum >> 16) + (sum & 0xffff);
 *                sum += (sum >> 16);
 *        return ~sum;
 * }
 * main {
 *        int i,sd,one,len;
 *        unsigned char buff[BUFSIZ],in[BUFSIZ];
 *        char data[56];
 *        char *tmp;
 *        char s1[200];
 *        struct sockaddr_in sin;
 *        struct iphdr *ip = (struct iphdr*) malloc(IPLEN);
 *        struct icmphdr *icmp = (struct icmphdr*) malloc(ICMPLEN);
 * char recibe_dat[64];
 * printf("Ingresa IP: ");
 * fgets(recibe_dat,64,stdin);
 * srand ((unsigned) time(NULL));
 *        sd=socket (PF_INET, SOCK_RAW, IPPROTO_ICMP);
 *        sin.sin_family=AF_INET;
 *        sin.sin_port=0;
 *        sin.sin_addr.s_addr=inet_addr(recibe_dat);
 *        memset (buff,0,sizeof(buff));
 *        for (i=0; i<56; i++)
 *                data[i]=i;
 *        ip->version=4;
 *        ip->ihl=5;
 *        ip->tos=0;
 *        ip->tot_len=IPLEN+ICMPLEN+sizeof(data);
 *        ip->id=0;
 *        ip->frag_off=0;
 *        ip->ttl=64;
 *        ip->protocol=IPPROTO_ICMP;
 *        ip->check=0;
 *        ip->saddr=inet_addr("192.168.0.69");
 *        ip->daddr=inet_addr(recibe_dat);
 *        ip->check = csum ((u16*) buff, ip->tot_len >> 1);
 *        icmp->type=ICMP_ECHO;
 *        icmp->code=0;
 *        icmp->checksum=0;
 *        icmp->id=1;
 *        icmp->sequence=1;
 *        tmp = (char*) malloc(ICMPLEN+sizeof(data));
 *        memcpy (tmp, icmp, ICMPLEN);
 *        memcpy (tmp+ICMPLEN, data, sizeof(data));
 *        icmp->checksum=csum((u16*) tmp, ICMPLEN+sizeof(data) >> 1);
 *        memcpy (buff, ip, IPLEN);
 *        memcpy (buff+IPLEN, icmp, ICMPLEN);
 *        memcpy (buff+IPLEN+ICMPLEN, data, sizeof(data));
 *        one=1;
 *        if (setsockopt (sd, IPPROTO_IP, IP_HDRINCL, &one, sizeof (one)) < 0)
 *                printf ("Aviso!! no se pudo hacer el paquete\n");
 *        int a = 0;
 *  do{
 *         if (sendto (sd, buff, ip->tot_len, 0, (struct sockaddr *) &sin, sizeof (sin)) < 0)  {
 *             printf ("Error al enviar!!\n");
 *                 exit(1);
 *       } else {
 *        sleep(1);
 * int s3;
 *          s3 = recv(sd, s1, 100, 0);
 *          if(s3 > 0){         
 *             printf ("Paquete enviado y recibido satisfactoriamente!! \nTamaÃ±o del Paquete: %d bytes\nNumero del Paquete: %d \n",s3+14,a+1);
 *             a++;
 *                 //printf("%02x:%02x:%02x:%02x:%02x:%02x \n",s1[0],s1[1],s1[2],s1[3],s1[4],s1[5]);
 *                 }
 *         } 
 *        }while(a < 4);
 * }
 *        if (setsockopt (sd, IPPROTO_IP, IP_HDRINCL, &one, sizeof (one)) < 0)
 *                printf ("Aviso!! no se pudo hacer el paquete\n");
 *        int a = 0;
 *  do{
 *         if (sendto (sd, buff, ip->tot_len, 0, (struct sockaddr *) &sin, sizeof (sin)) < 0)  {
 *             printf ("Error al enviar!!\n");
 *                 exit(1);
 *       } else {
 *        sleep(1);
 * int s3;
 *          s3 = recv(sd, s1, 100, 0);
 *          if(s3 > 0){         
 *             printf ("Paquete enviado y recibido satisfactoriamente!! \nTamaÃ±o del Paquete: %d bytes\nNumero del Paquete: %d \n",s3+14,a+1);
 *             a++;
 *                 //printf("%02x:%02x:%02x:%02x:%02x:%02x \n",s1[0],s1[1],s1[2],s1[3],s1[4],s1[5]);
 *                 }
 *         } 
 *        }while(a < 4);
 * }
 * }