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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:52:48+00:00 2026-05-30T08:52:48+00:00

Trying to write a handler for a packet sniffer. I’m having issues with casting

  • 0

Trying to write a handler for a packet sniffer. I’m having issues with casting and inet_ntoa(). Code is as follows:

uint32_t *iphdr_srcaddr = malloc(sizeof(uint32_t));
if (*packet_ethertype == ETHERTYPE_IP) { /* IPv4 */
    // copy packet data to vars
    memcpy(iphdr_srcaddr, packet+26, 4);

    // change to host-byte-order
    *iphdr_srcaddr = ntohl(*iphdr_srcaddr);

    struct in_addr *test;
    test = (struct in_addr*) iphdr_srcaddr;

    printf("uint32_t: %u\n", *iphdr_srcaddr); // Gives the correct long integer for the address
    printf("struct in_addr: %u\n", test->s_addr); // Gives the correct long integer through the cast

    char *test2;
    test2 = inet_ntoa(*test);
}

Now if I try to printf("%s\n", test) I get SEGV. I’m sure I’m mixing up pointers, values and doing some sort of stupid casting. Error received during run below:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff787ec61 in __strlen_sse2 () from /lib/libc.so.6

Compilation warning as well, I’m sure this is pointing me in the correct direction, but I’m not sure what to it means and how I can fix it:

mypcap.c: In function ‘handle_sniffed’:
mypcap.c:61:15: warning: assignment makes pointer from integer without a cast [enabled by default]

This refers to the line test2 = inet_ntoa(*test);

  • 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-05-30T08:52:50+00:00Added an answer on May 30, 2026 at 8:52 am

    The warning probably indicates that you don’t have a correct prototype in scope for inet_ntoa() (because you haven’t included the right header). This means that the compiler assumes it has a return type of int.

    You’re also passing test to printf() when you should be passing test2.

    In addition:

    • There’s no need to use malloc() to allocate a single uint32_t;
    • You don’t need to call ntohl() because inet_ntoa() expects its input in network byte order; and
    • inet_ntoa() is out of date – inet_ntop() should be used in new code.

    Try:

    #include <arpa/inet.h>
    
    if (*packet_ethertype == ETHERTYPE_IP) { /* IPv4 */
        struct in_addr sin_addr;
        char straddr[INET_ADDRSTRLEN];
    
        memcpy(&sin_addr.s_addr, packet+26, 4);
    
        if (inet_ntop(AF_INET, &sin_addr, straddr, sizeof straddr))
            printf("%s\n", straddr);
        else
            perror("inet_ntop");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an attempt to create cleaner code, I am trying to write event handler
I'm trying to write a blog post which includes a code segment inside a
I was trying to write a code to handle exceptions, but overriding another exception
I'm having a problem with a couple of event handler classes I'm trying to
I'm trying to write an event handler that fires every time a node in
I'm trying to write code that will disable submit button (or all submit buttons)
I'm trying to write some python code which can create multipart mime http requests
I'm playing around, trying to write some code to use the tr.im APIs to
I'm trying to write some generic http response handler functions that sometimes open UIAlertViews.
I am trying to write a code that checkpoints multithreaded applications. Since fork function

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.