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 9275983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:43:24+00:00 2026-06-18T16:43:24+00:00

I am learning RAW sockets. In the below code i am trying to print

  • 0

I am learning RAW sockets. In the below code i am trying to print all ICMP packets headers information. Looks like some error in the code. Can anyone please help me where i am wrong.

# include <unistd.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <string.h>
# include <netinet/in.h>
# include <stdio.h>
# include<stdlib.h>

main(){
int sockfd,retval,n;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
char buf[10000]; 

sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd < 0){
    perror("sock:");
    exit(1);
}
clilen = sizeof(struct sockaddr_in);    
while(1){
    printf(" before recvfrom\n");   
    n=recvfrom(sockfd,buf,10000,0,(struct sockaddr *)&cliaddr,&clilen);
    printf(" rec'd %d bytes\n",n);
    buf[n]='\0';
    printf(" msg from client = %s\n",buf);
}
}

o/p

before recvfrom
rec'd 60 bytes
msg from client = E
before recvfrom
rec'd 52 bytes
  • 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-18T16:43:26+00:00Added an answer on June 18, 2026 at 4:43 pm

    You are trying to print raw packet data (including headers) as a string. In this case, E which is ascii 0x45 is the first byte of the IP header. The upper 4 bits mean “IPv4” and the lower 4 bits is the IHL (number of 32-bit words in the IP header) which is 5*4 = 20 bytes.

    To properly access this data you should use the IP/ICMP header structs provided by linux. I’ve updated your code a little to illustrate:

    # include <unistd.h>
    # include <sys/socket.h>
    # include <sys/types.h>
    # include <string.h>
    # include <netinet/in.h>
    # include <stdio.h>
    # include<stdlib.h>
    
    #include <netinet/ip.h>
    #include <netinet/ip_icmp.h>
    
    main(){
      int sockfd,retval,n;
      socklen_t clilen;
      struct sockaddr_in cliaddr, servaddr;
      char buf[10000]; 
      int i;
    
      sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
      if (sockfd < 0){
        perror("sock:");
        exit(1);
      }
      clilen = sizeof(struct sockaddr_in);    
      while(1){
        printf(" before recvfrom\n");   
        n=recvfrom(sockfd,buf,10000,0,(struct sockaddr *)&cliaddr,&clilen);
        printf(" rec'd %d bytes\n",n);
    
        struct iphdr *ip_hdr = (struct iphdr *)buf;
    
        printf("IP header is %d bytes.\n", ip_hdr->ihl*4);
    
        for (i = 0; i < n; i++) {
          printf("%02X%s", (uint8_t)buf[i], (i + 1)%16 ? " " : "\n");
        }
        printf("\n");
    
        struct icmphdr *icmp_hdr = (struct icmphdr *)((char *)ip_hdr + (4 * ip_hdr->ihl));
    
        printf("ICMP msgtype=%d, code=%d", icmp_hdr->type, icmp_hdr->code);
      }
    }
    

    Now, if I run that and ping 127.0.0.1: you see output like this:

     before recvfrom
     rec'd 84 bytes
    IP header is 20 bytes.
    45 00 00 54 00 00 40 00 40 01 3C A7 7F 00 00 01
    7F 00 00 01 08 00 A9 DF 11 66 00 01 9A 77 1A 51
    00 00 00 00 BA 1D 0F 00 00 00 00 00 10 11 12 13
    14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23
    24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33
    34 35 36 37 
    ICMP msgtype=8, code=0 before recvfrom
     rec'd 84 bytes
    IP header is 20 bytes.
    45 00 00 54 8D F3 00 00 40 01 EE B3 7F 00 00 01
    7F 00 00 01 00 00 B1 DF 11 66 00 01 9A 77 1A 51
    00 00 00 00 BA 1D 0F 00 00 00 00 00 10 11 12 13
    14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23
    24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33
    34 35 36 37 
    ICMP msgtype=0, code=0 before recvfrom
    

    Here this shows a msgtype 8 (echo request) and msgtype 0 (echo reply). Beware that when accessing data this way from an array you can run into alignment problems (x86/x64 are happy to handle it for you, but other architectures may not be so generous). I’ll leave that as an exercise to the reader ;).

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

Sidebar

Related Questions

I'm learning to use raw sockets, and im trying to prase out the tcp
I'm learning how to work with raw sockets in Linux. I'm trying to create
I am learning about raw sockets. I heard that ping utility is using raw
Learning some VBA. So far, I've constructed this piece of code which should allow
Possible Duplicate: Learning Python: If condition executing all the time This Python code is
More C++ learning questions. I've been using vectors primarily with raw pointers with a
Learning Scala currently and needed to invert a Map to do some inverted value->key
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I'm trying to build myself a little helper library. first, for learning purposes, then
I have the following (and more, but it's only some of it) code in

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.