All pastes #179057 Raw Edit

Anonymous

public text v1 · immutable
#179057 ·published 2006-09-21 21:24 UTC
rendered paste body
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>

/* Checksum creater from Mixter's tutorial */
unsigned short ip_csum (unsigned short *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 argc, char **argv[]) {
        int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
        char datagram[4096], *buf = "mark";
        struct ip *iph = (struct ip *) datagram;
        struct icmphdr *icmph = (struct icmphdr *)(datagram + sizeof(struct ip));
        struct sockaddr_in sin;
        int one = 1;
        const int *val = &one;
        
        memset(datagram, 0, 4096);              

        sin.sin_family = AF_INET;
        sin.sin_port = htons(25);
        sin.sin_addr.s_addr = inet_addr("127.0.0.1");
        
        iph->ip_hl = 5; 
        iph->ip_v = 4;
        iph->ip_tos = 0;
        iph->ip_len = sizeof(struct ip) + 8 + strlen(buf);
        iph->ip_id = htonl(8765);
        iph->ip_off = 0;
        iph->ip_ttl = 255;
        iph->ip_p = 1;
        iph->ip_sum = 0;
        iph->ip_src.s_addr = inet_addr("127.0.0.1");
        iph->ip_dst.s_addr = sin.sin_addr.s_addr;
        
        icmph->type = 8;
        icmph->code = 0;
//      icmph->checksum = in_cksum((unsigned short*) datagram + sizeof(struct ip),8 + strlen(buf));
//      iph->ip_sum = ip_csum ((unsigned short *) datagram, iph->ip_len >> 1);
        iph->ip_sum = 0xff;
        strcpy(datagram + sizeof( struct ip) + 8, buf);

        if (setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
        printf ("Warning: Cannot set HDRINCL!\n");

        if(sendto(sockfd, datagram, iph->ip_len, 0, (struct sockaddr *)&sin, sizeof(sin))<0)
                perror("sendto");
        return 0;       

}