Question: #include #include #include #include #include #include #include #include / / The IP header's structure struct ipheader { unsigned char iph _ ihl: 4 , iph
#include
#include
#include
#include
#include
#include
#include
#include
The IP header's structure
struct ipheader
unsigned char iphihl: iphver:;
unsigned char iphtos;
unsigned short int iphlen;
unsigned short int iphident;
unsigned short int iphflagsoffset;
unsigned char iphttl;
unsigned char iphprotocol;
unsigned short int iphchksum;
unsigned int iphsourceip;
unsigned int iphdestip;
;
Simple checksum function, might be needed for some headers
unsigned short csumunsigned short buf int nwords
unsigned long sum;
for sum ; nwords ; nwords
sum buf;
sum sum sum & xffff;
sum sum ;
return unsigned short~sum;
int main
int sd;
char buffer;
struct ipheader ip struct ipheader buffer;
struct icmphdr icmp struct icmphdr buffer sizeofstruct ipheader;
char data buffer sizeofstruct ipheader sizeofstruct icmphdr;
Create a raw socket with IP protocol.
sd socketAFINET, SOCKRAW, IPPROTORAW;
if sd
perrorsocket error";
exit;
Set the fields in the IP header
ipiphver ; Version
ipiphihl ; IHL
ipiphtos ; Type of service
ipiphlen htonssizeofstruct ipheader sizeofstruct icmphdr strlendata; Include IP header, ICMP header, and data length in total length
ipiphident htons; Identification
ipiphflagsoffset htonsx; Don't fragment flag, offset
ipiphttl ; Time to live
ipiphprotocol IPPROTOICMP; Protocol
ipiphchksum ; Checksum temporarily set to
ipiphsourceip inetaddr; Spoofed source IP address
ipiphdestip inetaddr; Destination IP address
Set the fields in the ICMP header
icmptype ICMPECHO;
icmpcode ;
icmpchecksum ; Checksum temporarily set to
icmpunecho.id ;
icmpunecho.sequence ;
Copy the message into the buffer after the ICMP header
strcpydata "Hello";
Calculate the IP checksum now that the header is filled out
ipiphchksum csumunsigned short buffer sizeofstruct ipheader sizeofstruct icmphdr strlendata;
Inform the kernel that headers are included in the packet
int one ;
const int val &one;
if setsockoptsd IPPROTOIP IPHDRINCL, val, sizeofone
perrorsetsockopt error";
exit;
Destination address structure
struct sockaddrin sin;
sinsinfamily AFINET;
sinsinport htons; Port number not needed for ICMP
sinsinaddr.saddr ipiphdestip;
Send the packet
if sendtosd buffer, ntohsipiphlenstruct sockaddr &sin sizeofsin
perrorsendto error";
exit;
closesd;
return ;
in this code why icmp response is not coming please modify the code properly to handle icmp request and response
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
