//------------------------------------------------------------------- // sendto.cpp // // This application will transmit a short test-message to the // computer whose node-name is specified on the command-line, // provided the 'ethers' file in the current directory has an // entry for that node and provided our 'nicf.c' Linux driver // for the 82573L network interface controller got installed. // // compile using: $ g++ sendto.cpp -o sendto // execute using: $ ./sendto // // programmer: ALLAN CRUSE // written on: 07 MAY 2009 //------------------------------------------------------------------- #include // for printf(), perror(), fopen(), fgets() #include // for open() #include // for strtol(), exit() #include // for write() #include // for strlen() #include // for ioctl() char filname[] = "ethers"; char devname[] = "/dev/nic"; char message[] = " CS336/CS621: Computer Networks and Network Programming \n"; unsigned char dstn[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; int main( int argc, char **argv ) { // check for presence of the command-line argument if ( argc == 1 ) { fprintf( stderr, "nodename missing\n" ); exit(1); } // check for presence of the required database-file FILE *fd = fopen( filname, "ro" ); if ( !fd ) { perror( filname ); exit(1); } // lookup the destination-node's hardware MAC-address char line[ 80 ] = {0}; while ( fgets( line, 80, fd ) ) if ( strstr( line, argv[1] ) ) { printf( "\nline from \'%s\': %s \n", filname, line ); for (int i = 0; i < 6; i++) dstn[i] = strtol( line+i*3, NULL, 16 ); break; } // open the device-file int fp = open( devname, O_RDWR ); if ( fp < 0 ) { perror( devname ); exit(1); } // set the device-driver packets' destination-address if ( ioctl( fp, 1, dstn ) ) { perror( "ioctl" ); exit(1); } // write our test-mesage to the device-file int nbytes = write( fp, message, strlen( message ) ); printf( "Wrote %d bytes to \'%s\' \n\n", nbytes, argv[1] ); }