creating packets

This is a discussion on creating packets within the Linux Programming forums, part of the Platform Specific Boards category; hello! my packetgenerator, isnt working properly. when i use tcpdump to look at the packets i am sending they dont ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    creating packets

    hello!

    my packetgenerator, isnt working properly.
    when i use tcpdump to look at the packets i am sending they dont equal the ones i was specifying in the source code. this isnt working.

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <netinet/tcp.h>
    #include <netinet/ip.h>
    #include <netinet/udp.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <linux/slab.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    char datagram[4096];
    
    struct iphdr *iphead = (struct iphdr *)datagram;
    struct tcphdr *tcphead = (struct tcphdr *)datagram + sizeof(iphead); 
    
    
    void help(void);
    
    unsigned short 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;
    }
    
    int main (int argc, char **argv)
    {
        char opt;
        unsigned int count, i;
        int sockfd;
        struct sockaddr_in dest;    
        int sin_size;
        char buffer[2048];
        int destport, sourceport;
        char *sourceip, *destip;
        const int on = 1;
            
        srand(time(NULL));
        
        if(getuid() != 0){
    	fprintf(stderr, "you must be r00t to build packets\n");
    	exit(1);
        }
            
        while((opt = getopt(argc, argv, "S:D:P:p:c:")) != EOF){
    	switch(opt){
    	    case 'S': { sourceip = optarg; break; }
    	    case 'D': { destip = optarg; break; }
    	    case 'P': { sourceport = atoi(optarg); break; }
    	    case 'p': { destport = atoi(optarg); break; }
    	    case 'c': { count = atoi(optarg); break; }
    	    default:  { help(); break; }
    	}
        }    
    
        if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1){
    	perror("socket");
    	exit(1);
        }
        
        sin_size = sizeof(struct sockaddr);
    
        dest.sin_family = AF_INET;
        dest.sin_port = htons(destport);
        dest.sin_addr.s_addr = inet_addr(destip);
    
        memset(&(dest.sin_zero), '\0', 8);    
        memset(datagram, 0, 4096);
        memset(iphead, 0, sizeof(iphead));
        memset(tcphead, 0, sizeof(tcphead));
        
        
        iphead->ihl = 5;
        iphead->version = 4;
        iphead->tos = 0;
        iphead->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
        iphead->id = htons(getpid());
        iphead->frag_off = 0;
        iphead->ttl = 255;
        iphead->protocol = 6;
        iphead->check = csum((unsigned short *)datagram, iphead->tot_len >> 1);
        iphead->saddr = INADDR_ANY;
        iphead->daddr = inet_addr(destip);
    
        tcphead->source = htons(sourceport);
        tcphead->dest = htons(destport);
        tcphead->seq = htons(random());
        tcphead->ack_seq = 0;
        tcphead->fin = 0;
        tcphead->syn = 1;
        tcphead->rst = 0;
        tcphead->psh = 0;
        tcphead->ack = 0;
        tcphead->urg = 0;
        tcphead->window = htons(65535); 
        tcphead->check = 0;
        tcphead->urg_ptr = 0;
    
    
        if(setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0){
    	perror("setsockopt");
    	exit(1);
        }
    
        if( (sendto(sockfd, datagram, iphead->tot_len, 0, (struct sockaddr *)&dest, sizeof(struct sockaddr))) == -1){
        	perror("sendto");
    	exit(1);
        }
        
        if( (recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&dest, &sin_size)) == -1){
    	perror("recvfrom");
    	exit(1);
        }
    
        close(sockfd);
        return 0;
    }
    
    void help()
    {
        printf("usage: ./userpacket -S <sourceip> -D <destip> -P <sourceport> -p <destport> -c <# of packets>\n\n");
    }
    i hope that someone of you can help me.
    maybe you have a clue whats wrong with that code.

    i compiled and executed that code on an x86 arch with slackware.

    thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>they dont equal the ones i was specifying in the source code
    Care to clarify a bit more please...?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    yes.

    i updated my code and got it finally working.
    now i can send udp packets.
    still not working are tcp and icmp packets.
    because the tcp and icmp header checksums are wrong.

    is there a pseudoheader needed for both or only for tcp packets?

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. how to recieve all packets coming to machine??
    By shady_Dev in forum Networking/Device Communication
    Replies: 6
    Last Post: 03-29-2008, 11:21 AM
  3. Creating a cheat proxy with winpcap
    By *DEAD* in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-01-2007, 08:34 AM
  4. Accessing and editing packets of other applications
    By Inder in forum Linux Programming
    Replies: 1
    Last Post: 09-01-2006, 01:00 PM
  5. Recieve packets
    By valt in forum C++ Programming
    Replies: 9
    Last Post: 02-04-2006, 12:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21