//------------------------------------------------------------------- // trywatch.cpp // // This application is provided for testing the ability of our // 'watch235.c' pseudo device-driver to intercept packets that // are received by the RTL8139 network adapter integrated into // each student workstation in our Harney-235 classroom. // // programmer: ALLAN CRUSE // written on: 06 DEC 2004 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for read(), write(), close() char filename[] = "/proc/watch235"; int main( int argc, char **argv ) { // install the driver-module and open the pseudo-file system( "/sbin/insmod watch235.o" ); int fd = open( filename, O_RDONLY ); if ( fd < 0 ) { perror( filename ); exit(1); } // read a received network-packet from the pseudo-file unsigned char buffer[ 1536 ]; int nbytes = read( fd, buffer, sizeof( buffer ) ); printf( "\nread %d bytes from \'%s\' \n", nbytes, filename ); // display the packet in hexadecimal and ascii formats for (int i = 0; i < nbytes; i += 16) { printf( "\n%03X: ", i ); for (int j = 0; j < 16; j++) { if ( i+j < nbytes ) printf( "%02X ", buffer[ i+j ] ); else printf( " " ); } for (int j = 0; j < 16; j++) { unsigned char ch = ' '; if ( i+j < nbytes ) ch = buffer[ i+j ]; if (( ch < 0x20 )||( ch > 0x7E )) ch = '.'; printf( "%c", ch ); } } printf( "\n\n" ); // close the file and remove the driver-module close( fd ); system( "/sbin/rmmod watch235" ); }