Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:50:17+00:00 2026-06-18T07:50:17+00:00

i have a C program that’s able to receive ICMP requests and display them,

  • 0

i have a C program that’s able to receive ICMP requests and display them, but however will not send out any ICMP requests itself from what I’ve seen using Wireshark.

I have included the required libraries and i’m using a IP header file and ICMP header file that’s omitted.

Any help would be appreciated. Please ignore the random Printf statements, there just to help me out.

Thanks

**

***IP.h*****

#ifndef __IP_H__
#define __IP_H__

struct iphdr {

   uint8_t   hdrlen:4;    
   uint8_t   version:4;
   uint8_t   ecn:2;       
   uint8_t   dscp:6;     
   uint16_t  length;
   uint16_t  ident;
   uint16_t  fragoff:13;
   uint16_t  flags:3;
   uint8_t   ttl;
   uint8_t   protocol;
   uint16_t  checksum;
   uint32_t  srcip;
   uint32_t  dstip;
   uint32_t  options[ ];  
} __attribute__((__packed__));

#endif // __IP_H__

****ICMP.H****

#ifndef __ICMP_H__
#define __ICMP_H__

struct icmphdr {
   uint8_t   type;         
   uint8_t   code;          
   uint16_t  checksum;      
   uint16_t  id;          
   uint16_t  seqNum;       
   uint32_t  data[ ];       
} __attribute__((__packed__));


char * messages[ ] = {
   "Echo reply",
   "Type 1",
   "Type 2",
   "Destination unreachable",
   "Source quench",
   "Redirect",
   "Type 6",
   "Type 7",
   "Echo request",
   "Router advertisement",
   "Router discovery",
   "Time exceeded",
   "Parameter problem",
   "Timestamp request",
   "Timestamp reply",
   "Information request (obsol.)",
   "Information reply (obsol.)",
   "Address mask request",
   "Address mask reply"
};

#endif // __ICMP_H__


****Main C File ****

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/time.h>

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>

    #include "ip.h"
    #include "icmp.h"

    struct timeval timeout;
    struct timeval end;


    #define BUF_SIZE   1024
    #define ICMP       1
    #define ECHO_REQ   8
    #define ECHO_REPL  0
    #define HOSTADDR   "8.8.8.8"

    char *
    iptos (uint32_t ipaddr) {
       static char ips[16];

       sprintf(ips, "%d.%d.%d.%d",
          (ipaddr >> 24),
          (ipaddr >> 16) & 0xff,
          (ipaddr >>  8) & 0xff,
          (ipaddr      ) & 0xff );
       return ips;
    }

    uint16_t
    sum (uint16_t initial, void * buffer, int bytes) {
       uint32_t   total;
       uint16_t * ptr;
       int        words;

       total = initial;
       ptr   = (uint16_t *) buffer;
       words = (bytes + 1) / 2; // +1 & truncation on / handles any odd byte at end

       while (words--) total += *ptr++;
       while (total & 0xffff0000) total = (total >> 16) + (total & 0xffff);

       return (uint16_t) total;
    }




    void
    some_function ( ) {

       struct sockaddr_storage peer_addr;
       socklen_t               peer_addrlen;

       struct sockaddr_in      addr;
       struct sockaddr_in      dstaddr;
       struct iphdr   *        ip;
       struct icmphdr *        icmp;
       struct timeval *        sent;
       int skt;
       int sequence;
       long int length;
       fd_set rdfds;
       int ready;
       int rtt;

       char buff [BUF_SIZE];



       skt = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
       if (skt < 0) {
          perror ("socket()");
          exit (1);
       }

       addr.sin_family      = AF_INET;
       addr.sin_port        = 0;
       addr.sin_addr.s_addr = INADDR_ANY;

       if (bind (skt, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) {
          perror ("Can't bind socket");
          exit (1);
       }
    printf("aaa");
       ip = (struct iphdr *)buff;
       peer_addrlen = (socklen_t) sizeof (struct sockaddr_storage);

       memset (&dstaddr, 0, sizeof(struct sockaddr_in));
       dstaddr.sin_addr.s_addr = inet_addr(HOSTADDR);
       dstaddr.sin_family = AF_INET;

       memset (buff, 0, sizeof(buff));
       icmp = (struct icmphdr *) buff;
       icmp->type   = ECHO_REQ;
       icmp->id     = htons(getpid( ) & 0xffff);
       icmp->seqNum = htons(sequence++);

    printf("aabba");


       if (gettimeofday ((struct timeval *)icmp->data, NULL)) {
          perror ("Can't establish send time");
          exit (1);
       }

       length = sizeof(struct icmphdr) + sizeof(struct timeval);
       icmp->checksum = ~htons(sum (0, buff, length));


    printf("aaa");

       if (sendto (skt, buff, length, 0,
             (struct sockaddr *) &dstaddr, sizeof(struct sockaddr_in)) <= 0) {
          perror ("sendto()");
          exit (1);


    }
    printf("aaa");
     timeout.tv_sec  = 1;
       timeout.tv_usec = 0;
       FD_ZERO(&rdfds);
       FD_SET (skt, &rdfds);

       ready = select (skt + 1, &rdfds, NULL, NULL, &timeout);
       if (ready < 0) {
          perror ("Select()");
          exit (1);
       }

     memset (buff, 0, sizeof(buff));
       if (recvfrom (skt, buff, sizeof(buff), 0,
            (struct sockaddr *) &peer_addr, &peer_addrlen) <= 0) exit (1);

       if (gettimeofday (&end, NULL)) {   // Timestamp reception
          perror ("Can't establish time of receipt");
          exit (1);
       }

       if (ip->version != 4 ||
           sum (0, buff, sizeof(struct iphdr)) != 0xffff ||
           ip->protocol != ICMP)
          exit(1);

       length = ntohs(ip->length) - ip->hdrlen * 4;       // Length of IP payload
       icmp = (struct icmphdr *)((uint32_t *)ip + ip->hdrlen);   // Find ICMP hdr

       if (icmp->type != ECHO_REPL || sum (0, icmp, length) != 0xffff) {
          fprintf (stderr, "Received %s\n", messages[icmp->type]);
          exit (1);
       }


       sent = (struct timeval *)icmp->data;
       if ((rtt = (end.tv_usec - sent->tv_usec) / 100) < 0)
          rtt += 10000;  // We've cycled to a new second
       rtt += (end.tv_sec - sent->tv_sec) * 10000;  // Add any seconds

       printf ("%ld bytes from %s: icmp_req=%d ttl=%d time=%0.1f ms\n",
          length,
          iptos(ntohl(ip->srcip)),
          ntohs(icmp->seqNum),
          ip->ttl,
          ((float)rtt) / 10);

    }
    int main(){
    printf("aaa");
    some_function();
    return(0);

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:50:18+00:00Added an answer on June 18, 2026 at 7:50 am

    The problem is the wrong checksum.

    Change this line

    icmp->checksum = htons(sum (0, buff, length));
    

    with

    icmp->checksum = ~(sum (0, buff, length));
    

    the sum function already care about byte order, no need to translate it again with htons

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have program that requires Python 3, but I develop Django and it uses
I have program that has a variable that should never change. However, somehow, it
I have a program that encrypts files, but adds the extension .safe to the
I have a program that is developed in MVC 3 Razor Syntax but whenever
I have a program that draws Mandelbrot and Julia sets, displaying them with OpenCV(in
have a program that fades out on mouse leave event. but the problem is
I have a program that has a help jmenuItem that will open the html
I have a program that has two separate sections: one of them should be
I have a program that prints every client's order, like restaurants tickets, but I
I have a Program that will auto run each night, run a query, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.