Passing an ip address to iph/tcphdr [SOCK_RAW]

This is a discussion on Passing an ip address to iph/tcphdr [SOCK_RAW] within the C Programming forums, part of the General Programming Boards category; This code compiles and run fines, however, I have not been able to change the ip address to a real ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Passing an ip address to iph/tcphdr [SOCK_RAW]

    This code compiles and run fines, however, I have not been able to change the ip address to a real adddress. Any help would be greatly appreciated.

    Thanks


    The function in question:

    Code:
    #include <stdio.h>#include <string.h>
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <netinet/tcp.h>
    #include <netinet/ip.h>
    #include <arpa/inet.h>
    #include "ourCheckSum.h"
    #include "RSTInjector.h"
    
    
    
    
    
    
    
    
    
    
    int RSTInjector(const char  connectionSource[32], int connectionPort, const char  connectionDest[32])
    {
    
    
    //Var
    int socketMaster;
    char datagram[4096]; //Contains entire packet iphdr + tcphdr
    struct ip *iph = (struct ip *) datagram;
    struct tcphdr *tcph = (struct tcphdr *) datagram +sizeof(struct ip);
    struct sockaddr_in sin;
    struct sockaddr_in din;
    
    
    //Var mem
        connectionSource = malloc(4);
        connectionDest = malloc(4);
        
    
    
    //Creat Socket
    socketMaster = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
        if (socketMaster == -1){
            perror("Could Not Open Socket");
            return -1;
        }
        
    //
    sin.sin_family = AF_INET;
    din.sin_family = AF_INET;
    sin.sin_port = htons(connectionPort);
    din.sin_port = htons(connectionPort);
    sin.sin_addr.s_addr = inet_addr(connectionDest); // DEST or SRC
    din.sin_addr.s_addr = inet_addr(connectionSource);
    
    
    //Clean datagram
    memset(datagram, 0, 4096);
    
    
    //Construct Headers
    //IP Headers
    iph->ip_hl = 5;
    iph->ip_v = 4;
    iph->ip_tos = 0;
    iph->ip_len = sizeof(struct ip) + sizeof(struct tcphdr);
    iph->ip_id = htonl(2539);
    iph->ip_off = 0;
    iph->ip_ttl = 255;
    iph->ip_p = 6;
    iph->ip_sum = 0; //csum will be calculated later
    iph->ip_src.s_addr = inet_addr(connectionSource); //Connection Source
    iph->ip_dst.s_addr = inet_addr(connectionDest); //Uses previuse host or connection dest
    
    
    //TCP Headers
    tcph->th_sport = htons(connectionPort); //TCP Port
    tcph->th_dport = htons(connectionPort); //TCP Port
    tcph->th_seq = rand(); // We really dont care as this is to close a connection
    tcph->th_ack = 0; //Is 0 in first packet -- Lets hope it dosent have to be the real value
    tcph->th_x2 = 0;
    tcph->th_off = 0;
    tcph->th_flags = TH_RST; //RST the connection, killing it
    tcph->th_win = htonl(65535); //Not sure about this number
    tcph->th_sum = 0; // Kernal *should* fill this value in correctly
    tcph->th_urp = 0; //What does this do?
    
    
    //Checksum call
    //IP Header
    iph->ip_sum = ourCheckSum((unsigned short *) datagram, iph->ip_len >> 1);
        
        
    //Inform Kernal of header inclusion
        {
        int one = 1;
        const int *val = &one;
        if (setsockopt(socketMaster, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0){
            perror("Could Not Set HDRINCL");
        }
        }
    
    
    //Inject RST Packet
        if (sendto(socketMaster, datagram, iph->ip_len, 0, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
            perror("Could not inject RST packet");
            return  -1;
            close(socketMaster);
        }
        else 
        return 0;
        close(socketMaster);
    
    
    
    
                  
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by November View Post
    Code:
    //Var mem
        connectionSource = malloc(4);
        connectionDest = malloc(4);
    Why are you throwing away the connectionSource and connectionDest pointers (I assume they're strings) that were passed into your function? And what does the magic value 4 represent?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array address passing help please??
    By blackCat in forum C Programming
    Replies: 9
    Last Post: 03-09-2009, 07:33 AM
  2. Passing an address to a function
    By Vicious in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2004, 12:05 PM
  3. strlen() and passing by address
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 07-12-2003, 01:49 PM
  4. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 12:25 AM
  5. Sock_raw
    By JagWire in forum Linux Programming
    Replies: 2
    Last Post: 07-30-2002, 03:17 AM

Tags for this Thread


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