Отображение CRC32 в генераторе Ethernet-пакетов Mausezahn / How to show SW-generated mimic of HW-generated CRC32 for Ethernet adapters by Mausezahn
Warning: This does not work when padding bytes added, because they unknown. (fixed)
How to modify layer1.c:
(i am sorry, code correct displaying and highlighting still impossible here)
I extremely tired to fight with wordpress, so please manually replace symmetric unicode double quotes to normal 0x22 quotes manually. Only i need is to display copyable source code here. But it impossible at all with wordpress.
diff -u layer1.c.old layer1.c
--- layer1.c.old 2009-11-23 00:11:38.000000000 +0200 +++ layer1.c 2014-02-28 11:42:46.000000000 +0300 @@ -60,10 +60,19 @@ err_buf[LIBNET_ERRBUF_SIZE], message[MAX_PAYLOAD_SIZE*3]; - u_int8_t bytestring[MAX_PAYLOAD_SIZE]; + u_int8_t bytestring[MAX_PAYLOAD_SIZE+6+6+2]; libnet_ptag_t t; libnet_t *l; + int sum; + u_long crc; + unsigned int crc_table[] = + { + 0x4DBDF21C, 0x500AE278, 0x76D3D2D4, 0x6B64C2B0, + 0x3B61B38C, 0x26D6A3E8, 0x000F9344, 0x1DB88320, + 0xA005713C, 0xBDB26158, 0x9B6B51F4, 0x86DC4190, + 0xD6D930AC, 0xCB6E20C8, 0xEDB71064, 0xF0000000 + }; if (tx.dot1Q) @@ -294,6 +303,35 @@ fprintf(stderr, " mz/send_eth: %s", libnet_geterror(l)); return -1; } + + // Calculate sum and crc32. WARNING, all bytes must be defined while generating packet. + bytestring[0] = tx.eth_dst[0]; + bytestring[1] = tx.eth_dst[1]; + bytestring[2] = tx.eth_dst[2]; + bytestring[3] = tx.eth_dst[3]; + bytestring[4] = tx.eth_dst[4]; + bytestring[5] = tx.eth_dst[5]; + bytestring[6] = tx.eth_src[0]; + bytestring[7] = tx.eth_src[1]; + bytestring[8] = tx.eth_src[2]; + bytestring[9] = tx.eth_src[3]; + bytestring[10] = tx.eth_src[4]; + bytestring[11] = tx.eth_src[5]; + bytestring[12] = (tx.eth_type>>8) & 0xff; + bytestring[13] = tx.eth_type & 0xff; + for (i=0; i<tx.eth_payload_s; i++) bytestring[i+6+6+2]=tx.eth_payload[i]; + + sum=0; + crc=0; + for (i=0; i<tx.eth_payload_s+6+6+2; i++) { + sum = (sum + bytestring[i]) & 0xff; + + // http://www.edaboard.com/thread120700.html + crc = (crc >> 4) ^ crc_table[(crc ^ (bytestring[i] >> 0)) & 0x0F]; /* lower nibble */ + crc = (crc >> 4) ^ crc_table[(crc ^ (bytestring[i] >> 4)) & 0x0F]; /* upper nibble */ + //printf("%02x ",(uint)bytestring[i]); + } + printf(" send: Len=%d Sum=%d CRC32=%08lx (%02x %02x %02x %02x on wire) \n", tx.eth_payload_s+6+6+2, sum, crc, (uint)(crc & 0xff), (uint)(crc>>8 & 0xff), (uint)(crc>>16 & 0xff), (uint)(crc>>24 & 0xff)); if (verbose) {