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 iph_ihl:4, iph_ver:4;
unsigned char iph_tos;
unsigned short int iph_len;
unsigned short int iph_ident;
unsigned short int iph_flags_offset;
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short int iph_chksum;
unsigned int iph_sourceip;
unsigned int iph_destip;
};
// Simple checksum function, might be needed for some headers
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 (unsigned short)(~sum);
}
int main(){
int sd;
char buffer[4096];
struct ipheader *ip =(struct ipheader *) buffer;
struct icmphdr *icmp =(struct icmphdr *)(buffer + sizeof(struct ipheader));
char *data = buffer + sizeof(struct ipheader)+ sizeof(struct icmphdr);
// Create a raw socket with IP protocol.
sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (sd <0){
perror("socket() error");
exit(-1);
}
// Set the fields in the IP header
ip->iph_ver =4; // Version
ip->iph_ihl =5; // IHL
ip->iph_tos =0; // Type of service
ip->iph_len = htons(sizeof(struct ipheader)+ sizeof(struct icmphdr)+ strlen(data)); // Include IP header, ICMP header, and data length in total length
ip->iph_ident = htons(54321); // Identification
ip->iph_flags_offset = htons(0x4000); // Don't fragment flag, 0 offset
ip->iph_ttl =255; // Time to live
ip->iph_protocol = IPPROTO_ICMP; // Protocol
ip->iph_chksum =0; // Checksum (temporarily set to 0)
ip->iph_sourceip = inet_addr("1.2.3.8"); // Spoofed source IP address
ip->iph_destip = inet_addr("10.9.0.6"); // Destination IP address
// Set the fields in the ICMP header
icmp->type = ICMP_ECHO;
icmp->code =0;
icmp->checksum =0; // Checksum (temporarily set to 0)
icmp->un.echo.id =1234;
icmp->un.echo.sequence =0;
// Copy the message into the buffer after the ICMP header
strcpy(data, "Hello");
// Calculate the IP checksum now that the header is filled out
ip->iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader)+ sizeof(struct icmphdr)+ strlen(data));
// Inform the kernel that headers are included in the packet
int one =1;
const int *val = &one;
if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one))<0){
perror("setsockopt() error");
exit(-1);
}
// Destination address structure
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(0); // Port number (not needed for ICMP)
sin.sin_addr.s_addr = ip->iph_destip;
// Send the packet
if (sendto(sd, buffer, ntohs(ip->iph_len),0,(struct sockaddr *)&sin, sizeof(sin))<0){
perror("sendto() error");
exit(-1);
}
close(sd);
return 0;
} 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!