Do broken CRC32
This commit is contained in:
parent
489637433a
commit
4565c89b59
1 changed files with 48 additions and 5 deletions
|
@ -2,6 +2,7 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <inttypes.h>
|
||||||
#include <linux/if_packet.h>
|
#include <linux/if_packet.h>
|
||||||
#include <net/ethernet.h>
|
#include <net/ethernet.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
@ -14,6 +15,34 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
uint32_t crc32_table[256];
|
||||||
|
|
||||||
|
void precompute_crc32_table(void) {
|
||||||
|
for (size_t index = 0; index < 256; index++) {
|
||||||
|
uint32_t value = index;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 8; i++) {
|
||||||
|
uint32_t low_bit = value & 1;
|
||||||
|
value = value >> 1;
|
||||||
|
if (low_bit) {
|
||||||
|
value ^= 0xEDB88320;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
crc32_table[index] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t crc32(unsigned char *buf, size_t len) {
|
||||||
|
uint32_t value = 0xffffffff;
|
||||||
|
|
||||||
|
for (size_t index = 0; index < len; index++) {
|
||||||
|
value = crc32_table[(value & 0xff) ^ buf[index]] ^ (value >> 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value ^ 0xffffffff;
|
||||||
|
}
|
||||||
|
|
||||||
char hexify(int nybble) {
|
char hexify(int nybble) {
|
||||||
assert(0 <= nybble && nybble <= 16);
|
assert(0 <= nybble && nybble <= 16);
|
||||||
return "0123456789abcdef"[nybble];
|
return "0123456789abcdef"[nybble];
|
||||||
|
@ -35,6 +64,9 @@ int main(int argc, char **argv) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set up CRC32
|
||||||
|
precompute_crc32_table();
|
||||||
|
|
||||||
// Create a packet socket
|
// Create a packet socket
|
||||||
errno = 0;
|
errno = 0;
|
||||||
int packet_socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
int packet_socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
||||||
|
@ -63,6 +95,10 @@ int main(int argc, char **argv) {
|
||||||
for(;;) {
|
for(;;) {
|
||||||
unsigned char frame[1522]; // 1522 is the largest a 802.3 frame can be
|
unsigned char frame[1522]; // 1522 is the largest a 802.3 frame can be
|
||||||
|
|
||||||
|
if (fflush(stdout) == -1) {
|
||||||
|
err(1, "fflush");
|
||||||
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
ssize_t frame_length = recv(packet_socket, frame, sizeof(frame), MSG_TRUNC);
|
ssize_t frame_length = recv(packet_socket, frame, sizeof(frame), MSG_TRUNC);
|
||||||
if (frame_length == -1) {
|
if (frame_length == -1) {
|
||||||
|
@ -77,6 +113,17 @@ int main(int argc, char **argv) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compute the CRC32 of the frame
|
||||||
|
uint32_t crc32_residue = crc32(frame, frame_length);
|
||||||
|
|
||||||
|
if (crc32_residue != 0xc704dd7b) {
|
||||||
|
errno = 0;
|
||||||
|
if (printf("CRC failed (%04"PRIx32")\n", crc32_residue) == -1) {
|
||||||
|
err(1, "printf");
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Extract the MACs
|
// Extract the MACs
|
||||||
// 012345 012345
|
// 012345 012345
|
||||||
// dest source
|
// dest source
|
||||||
|
@ -104,7 +151,7 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if(printf("%s -> %s ethertype: %02x%02x", source_mac, destination_mac, ethertype >> 8, ethertype & 0xff) == -1) {
|
if(printf("%s -> %s ethertype: %04" PRIx16, source_mac, destination_mac, ethertype) == -1) {
|
||||||
err(1, "printf");
|
err(1, "printf");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,10 +165,6 @@ int main(int argc, char **argv) {
|
||||||
err(1, "printf");
|
err(1, "printf");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fflush(stdout) == -1) {
|
|
||||||
err(1, "fflush");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue