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

  • Home
  • SEARCH
  • 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 955071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:19:13+00:00 2026-05-16T00:19:13+00:00

I’m trying to build a simple echo server/client that works on ethernet level(using raw

  • 0

I’m trying to build a simple echo server/client that works on ethernet level(using raw sockets).
The server side by itself works and shows all incoming packets on eth0.
The client works and sends ethernet packets on eth0 (I checked this with wireshark and can see the packets going out.)
I now want to make a filter to only look at the packets I’m interested. (This is based on the destination/source addresses.)

In the code below, could someone please explain to me why strncmp returns zero(meaning the strings match) but yet the “if(ethernet_header->h_dest == mac)” fails to execute (don’t match).
Both the variables “mac” and “ethernet_header->h_dest” is the same type and length.

Some more background:
– This is done on linux 64bit (ubuntu)
– I’m using eth0 on the same machine for sending/receiving….I don’t think this should be a problem?

I just don’t understand why strcmp returns a match and if doesn’t. What am I missing??

void ParseEthernetHeader(unsigned char *packet, int len) {
    struct ethhdr *ethernet_header;
 unsigned char mac[ETH_ALEN] = {0x01, 0x55, 0x56, 0x88, 0x32, 0x7c}; 

 if (len > sizeof(struct ethhdr)) {
  ethernet_header = (struct ethhdr *) packet;

  int result = strncmp(ethernet_header->h_dest, mac, ETH_ALEN);
  printf("Result: %d\n", result);

  if(ethernet_header->h_dest == mac) {
   /* First set of 6 bytes are Destination MAC */
   PrintInHex("Destination MAC: ", ethernet_header->h_dest, 6);
   printf("\n");

   /* Second set of 6 bytes are Source MAC */
   PrintInHex("Source MAC: ", ethernet_header->h_source, 6);
   printf("\n");

   /* Last 2 bytes in the Ethernet header are the protocol it carries */
   PrintInHex("Protocol: ", (void *) &ethernet_header->h_proto, 2);
   printf("\n\n");
   printf("Length: %d\n",len);
  }

 } else {
  printf("Packet size too small (length: %d)!\n",len);
 }

}
  • 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-16T00:19:13+00:00Added an answer on May 16, 2026 at 12:19 am

    Neither strncmp nor a naked if should be used for comparing MAC addresses.

    The first won’t work properly in cases where they may have an embedded zero byte which would cause strncmp to state they’re equal when they’re actually not. That’s because a strncmp of the following two values:

    ff ff 00 ff ff ff
    ff ff 00 aa aa aa
    

    would be true (it only checks up to the first zero byte).

    The second won’t work because you’re comparing pointers rather than the contents that the pointers point to. If you have the following memory layout:

    0x12345678 (mac) | 0x11111111 |
    0x1234567c (eth) | 0x11111111 |
    

    then comparing mac with eth with if (mac == eth) will give you false since they are distinct pointers, one ending in 78, the other in 7c.

    You should use memcmp instead since it will compare the raw memory bytes without stopping at an early zero byte:

    int result = memcmp (ethernet_header->h_dest, mac, ETH_ALEN);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.