//--------------------------------------------------------------------- // rxtester.cpp // // This utility displays the raw network packets received by the // network interface on this workstation. It is adapted from an // example that appeared in Linux Journal (June 2001) pp. 26-31. // // NOTE: a user normally needs 'root' privileges when executing. // // programmer: ALLAN CRUSE // written on: 06 APR 2005 //--------------------------------------------------------------------- #include // for printf(), perror() #include // for exit() #include // for ETH_P_ALL #include // for htons() #include // for socket(), recvfrom() void dump_packet( char *buf, int n ) { static int pktid = 0; printf( "\npacket #%d ", ++pktid ); for (int i = 0; i < n; i+=16) { printf( "\n%04X: ", i ); for (int j = 0; j < 16; j++) { unsigned char ch = ( i + j < n ) ? buf[ i+j ] : 0; if ( i + j < n ) printf( "%02X ", ch ); else printf( " " ); } for (int j = 0; j < 16; j++) { unsigned char ch = ( i + j < n ) ? buf[ i+j ] : ' '; if (( ch < 0x20 )||( ch > 0x7E )) ch = '.'; printf( "%c", ch ); } } printf( "\n" ); } int main( void ) { int sock = socket( PF_PACKET, SOCK_RAW, htons( ETH_P_ALL ) ); if ( sock < 0 ) { perror( "socket" ); exit( 1 ); } while ( 1 ) { printf( "-------\n" ); char buffer[ 2048 ]; int n = recvfrom( sock, buffer, 2048, 0, NULL, NULL ); printf( "%d bytes read\n", n ); dump_packet( buffer, n ); } printf( "\n" ); }