Home > Software design >  creating ip header using IP_HDRINCL option not doing anything
creating ip header using IP_HDRINCL option not doing anything

Time:11-25

i wanted to send an icmp packet by writing my own IP header so i used the IP_HDRINCL option the code compiled and runs without any errors but there is no packet being sent when i run the program and capture packets using wireshark no error message from the program as well here is the code:

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define BUFFSIZE 2048

char sendbuf[BUFFSIZE];
int seqno=0;

unsigned short chksum(void *data,int bytes){
    unsigned int sum=0;
    unsigned short *octate=data,result;

    while(bytes>1){
        sum =*octate  ;
        bytes-=2;
    }
    if(bytes==1){
        sum =*(unsigned char *)octate;
    }
    while(sum>>16){
        sum=(sum>>16) (sum&0xffff);
    }
    result=~sum;
    return result;
}

struct addrinfo * getaddr(char *name){                               //convert user input host to address structure
    int e;
    struct addrinfo hints,*res;

    memset(&hints,0,sizeof(hints));
    hints.ai_flags=AI_CANONNAME;
    hints.ai_family=AF_INET;
    if(e=getaddrinfo(name,0,&hints,&res)!=0){
        printf("getaddrinfo error: %s\n",gai_strerror(e));
    }
    return res;
}

void create_packet(struct sockaddr *d){

    /*      Creating IP Packet           */

    struct ip *ip;
    ip=(struct ip*)sendbuf;
    ip->ip_v=4;                                                   
    ip->ip_hl=5;                                                 
    ip->ip_tos=0;                            
    ip->ip_len=20 8;                                             
    ip->ip_id=8848;                          
    ip->ip_off=IP_DF;                        
    ip->ip_ttl=7;                                             
    ip->ip_p=IPPROTO_ICMP;
    
    char srcip[]="192.168.1.69";
    struct addrinfo *source = getaddr(srcip);

    struct sockaddr_in *dest=(struct sockaddr_in *)d;
    struct sockaddr_in *src=(struct sockaddr_in *)source->ai_addr;
    ip->ip_src=src->sin_addr;
    ip->ip_dst=dest->sin_addr;

    ip->ip_sum=0;
    ip->ip_sum=chksum(ip,sizeof(*ip));

    
    /*      Creating ICMP Packet           */
    struct icmp *icmp;
    
    icmp=(struct icmp *)(sendbuf 20);
    icmp->icmp_type=ICMP_ECHO;
    icmp->icmp_code=0;
    icmp->icmp_id=getpid();
    icmp->icmp_seq=  seqno;
    icmp->icmp_cksum=0;
    icmp->icmp_cksum=chksum(icmp,8);
}


void main(int argc,char **argv){
    int sock;
    int on=1;
    struct addrinfo *addr=getaddr(argv[1]);
    if((sock=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))==-1){
        perror("socket error: ");
        return;
    }
    if(setsockopt(sock,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on))==-1){
        perror("setsockopt error");
        return;
    } 
    create_packet(addr->ai_addr);

    if(sendto(sock,sendbuf,28,0,addr->ai_addr,addr->ai_addrlen)==-1){
        perror("sendto error");
        return;
    }

}

CodePudding user response:

While I can't be certain based on your code alone, I suspect that you're running this program on a little-endian processor (x86-based), and IP expects its numbers in big-endian order. This means that any multi-byte fields (ip_len, ip_off) are written in the wrong order. I suspect that if you change the following 2 lines:

ip->ip_len=20 8;
ip->ip_off=IP_DF;

to this:

ip->ip_len=htons(20 8);
ip->ip_off=htons(IP_DF);

that the packets will be properly sent.

Incidentally the ip->ip_id field is also in big-endian order, but unless you're sending multiple fragments it doesn't much matter.

  • Related