Article 5S99D creating IP header using IP_HDRINCL option is not working

creating IP header using IP_HDRINCL option is not working

by
m.r_m0j0
from LinuxQuestions.org on (#5S99D)
i wanted to send icmp packet with custom ip header so i wrote the following code but when i run the program nothing happens there is no packet in wireshark and there is no error from the program and i don't know what is happening

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;
}

}latest?d=yIl2AUoC8zA latest?i=q2IQPrFKYso:jM5q-wElBOg:F7zBnMy latest?i=q2IQPrFKYso:jM5q-wElBOg:V_sGLiP latest?d=qj6IDK7rITs latest?i=q2IQPrFKYso:jM5q-wElBOg:gIN9vFw
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments