相关疑难解决方法(0)

何时在C中使用ntohs和ntohl?

我很困惑何时使用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)

c packet-capture packet packet-sniffers

3
推荐指数
1
解决办法
2916
查看次数

标签 统计

c ×1

packet ×1

packet-capture ×1

packet-sniffers ×1