イーサネットレベルで動作する単純なエコーサーバー/クライアントを構築しようとしています(生のソケットを使用)。サーバー側はそれ自体で動作し、eth0 ですべての着信パケットを表示します。クライアントは動作し、eth0 でイーサネット パケットを送信します (wireshark で確認したところ、パケットが送信されていることがわかりました)。関心のあるパケットのみを表示するフィルターを作成したいと考えています。(これは宛先/送信元アドレスに基づいています。)
以下のコードで、strncmp が 0 を返す (つまり、文字列が一致する) のに、「if(ethernet_header->h_dest == mac)」が実行に失敗する (一致しない) 理由を説明してください。変数 "mac" と "ethernet_header->h_dest" はどちらも同じ型と長さです。
いくつかの背景: - これは linux 64 ビット (ubuntu) で行われます - 送信/受信に同じマシンで eth0 を使用しています....これは問題ではないと思いますか?
strcmp が一致を返す理由と、そうでない場合の理由がわかりません。何が足りないの??
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 *) ðernet_header->h_proto, 2);
printf("\n\n");
printf("Length: %d\n",len);
}
} else {
printf("Packet size too small (length: %d)!\n",len);
}
}