我很困惑何时使用ntohs和ntohl.我知道你在uint16_t和ntohl uint32_t使用ntohs的时候.但是那些使用unsigned int或者指定了特定位数的那些(例如u_int16_t doff:4;).
这是我的工作代码来说明问题:
// Utility/Debugging method for dumping raw packet data
void dump(const unsigned char *data, int length) {
unsigned int i;
static unsigned long pcount = 0;
// Decode Packet Header
struct ether_header *eth_header = (struct ether_header *) data;
printf("\n\n === PACKET %ld HEADER ===\n", pcount);
printf("\nSource MAC: ");
for (i = 0; i < 6; ++i) {
printf("%02x", eth_header->ether_shost[i]);
if (i < 5) {
printf(":");
}
}
printf("\nDestination MAC: ");
unsigned short ethernet_type = ntohs(eth_header->ether_type);
printf("\nType: …Run Code Online (Sandbox Code Playgroud)