//------------------------------------------------------------------- // sendback.cpp // // This program receives packets from the network interface and // returns any whose protocol-type it recognizes to the sender. // // programmer: ALLAN CRUSE // written on: 01 MAY 2005 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for read(), write() #include // for memcpy() #include // for htons() #include // ioctl() #define MY_TYPE 0x0635 // our protocol type-identifier char nicname[] = "/dev/nic"; unsigned char nicaddr[6]; int main( int argc, char **argv ) { // open device-file and get hardware-address int nic = open( nicname, O_RDWR ); if ( nic < 0 ) { perror( nicname ); exit(1); } if ( ioctl( nic, 0, &nicaddr ) < 0 ) { perror( "ioctl" ); exit(1); } // show the station-name and hardware-address char hostname[ 64 ] = {0}; gethostname( hostname, 64 ); printf( "\n\tstation-hostname: %s ", hostname ); printf( "\n\thardware-address: " ); for (int i = 0; i < 6; i++) { printf( "%02X", nicaddr[i] ); if ( i < 5 ) printf( ":" ); else printf( " " ); } printf( "\n\nPress -C to terminate...\n" ); unsigned char packet[4096] = {0}; for(;;) { int nbytes = read( nic, packet, sizeof( packet ) ); if ( nbytes < 0 ) continue; if ( *(short*)(packet+12) == ntohs( MY_TYPE ) ) { memcpy( packet+0, packet+6, 6 ); memcpy( packet+6, nicaddr, 6 ); nbytes = write( nic, packet, nbytes ); } } }