//------------------------------------------------------------------- // nicread.cpp // // This application opens the '/dev/nic' device-file in order // to read and display every packet that the device receives, // assuming our driver ('receiver.c') has been installed. // // programmer: ALLAN CRUSE // written on: 13 APR 2005 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for read() char devname[] = "/dev/nic"; // name of the device special file int main( int argc, char **argv ) { // open the device-file int fd = open( devname, O_RDONLY ); if ( fd < 0 ) { perror( devname ); exit(1); } // loop forever -- or until killed by a signal (e.g., CTRL-C) int count = 0; for(;;) { char buffer[ 2048 ] = {0}; int nbytes = read( fd, buffer, sizeof( buffer ) ); if ( nbytes < 0 ) { perror( "read" ); exit(1); } printf( "\npacket #%d (%d bytes)\n", ++count, nbytes ); if ( nbytes > 64 ) nbytes = 64; // don't flood the screen for (int i = 0; i < nbytes; i+=16) { printf( "\n%04X: ", i ); for (int j = 0; j < 16; j++) { unsigned char ch = buffer[i+j]; if ( i+j < nbytes ) printf( "%02X ", ch ); else printf( " " ); } for (int j = 0; j < 16; j++) { char ch = buffer[i+j]; if (( ch < 0x20 )||( ch > 0x7E )) ch = '.'; if ( i+j < nbytes ) printf( "%c", ch ); else printf( " " ); } } printf( "\n\n" ); } }