S
S
Sdelan_v_CCCP2014-09-16 13:26:45
Network administration
Sdelan_v_CCCP, 2014-09-16 13:26:45

In what byte order is the MAC address written in the ethernet packet?

Hello! I am interested in the question, is it worth translating the MAC address extracted from the ethernet packet from network order to local byte order? Or is it written as 6 separate single byte numbers?
Thanks for answers. I am using pcap. The code is something like this

typedef unsigned short u16;

u16 extract_16bits(const void *p)
{
    return ((u16)ntohs(*(const u16 *)(p)));
}
void callback(u_char *args, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
     int data_offset = 12; //DMAC + SMAC
     u16 etype = extract_16bits(packet + data_offset);
       ....
}
void main(void) 
{
      ...
      pcap_loop(descr, -1, callback, NULL);
      ...
}

I'm interested in the moment when extracting the mac address from the packet, do I need to do something like ntoh to the mac address

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
throughtheether, 2014-09-16
@Sdelan_v_CCCP

Or is it written as 6 separate single byte numbers?

Quoting from IEEE 802.3-2008 section 1 (3.2.3 Address fields):
a) Each address field shall be 48 bits in length.
...
d) Each octet of each address field shall be transmitted least significant bit first.

UPD :
Probably worth explaining. The address 12:34:56:78:9a:bc will be transmitted from left to right (first the first byte from the left, then the second, etc.), while the bits of the byte will be transmitted in the order "LSB transmitted first". That is, byte 12 will look like this on the wire (from left to right, "time" increases): 01001000 . How this compares to your code without seeing it is hard to judge.

A
Andrew, 2014-09-16
@OLS

What data type are you using for local endianness?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question