[英]What endianess is used to store information in call to recvfrom()
Here is a simple code in socket API: 这是套接字API中的简单代码:
sock_raw = socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
//...
data_size = recvfrom(sock_raw , buffer , bufsize , 0 , (struct sockaddr*)&saddr , (socklen_t*)&saddr_size);
//...
void print_ethernet_header(unsigned char* buffer) { struct ethhdr *eth = (struct ethhdr *)buffer; fprintf(logfile , " |-Protocol : %x \\n",ntohs(eth->h_proto)); }
In this question I was explained that h_proto
member of struct ethhdr
is stored in big-endian format. 在这个问题中,我被解释为
struct ethhdr
h_proto
成员以big-endian格式存储。 Is it valid to say that the other members of this structure( h_source
and h_dest
are as well stored in big-endian format? And in such case is it valid to say that recvfrom() function stores data to buffer in big-endian format (I failed to find any info about this in man page to this function)? I think that this should be true because of rfc1042 , but I am not sure. 可以说此结构的其他成员(
h_source
和h_dest
也都以big-endian格式存储吗?在这种情况下,recvfrom()函数将数据以big-endian格式存储到缓冲区是否有效(我无法在此功能的手册页中找到有关此的任何信息)?我认为由于rfc1042 ,这应该是正确的,但我不确定。
recvfrom
reads data from a socket and stores it in a buffer, but it doesn't process the data: it stores it as it's received. recvfrom
从套接字读取数据并将其存储在缓冲区中,但它不处理数据:它将在收到数据时将其存储。 The order of values in the data depends on the network protocol. 数据中值的顺序取决于网络协议。 Most network protocols use big-endian (“network order”), so the data provided by
recvfrom
is typically big-endian — but that's not a result of recvfrom
. 大多数网络协议使用big-endian(“网络顺序”),因此
recvfrom
提供的数据通常是big-endian,但这不是recvfrom
的结果。
In most cases you should use ntohs
and ntohl
to translate values as appropriate, and process arrays in the documented order. 在大多数情况下,应使用
ntohs
和ntohl
适当地转换值,并按记录的顺序处理数组。 For h_source
and h_dest
, the documented order is indeed big-endian. 对于
h_source
和h_dest
,记录的顺序实际上是big-endian。 If you're writing kernel code, there are usually macros or helper functions available to do this for you; 如果您正在编写内核代码,通常可以使用宏或辅助函数来完成此操作。 even
printk
has %pM
format specifiers to handle MAC addresses. 甚至
printk
都具有%pM
格式说明符来处理MAC地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.