//------------------------------------------------------------------- // tweakttl.cpp // // This program utilizes an Internet Protocol socket-option to // adjust the 'Time-to-Live' field in an outgoing UDP message. // // compile using: $ g++ tweakttl.cpp -o tweakttl // execute using: $ ./tweakttl // // programmer: ALLAN CRUSE // written on: 10 FEB 2009 //------------------------------------------------------------------- #include // for gethostbyname() #include // for printf(), perror() #include // for exit() #include // for strncpy() #include // for inet_ntoa() int main( int argc, char **argv ) { //----------------------------------------------- // check for the required command-line arguments //----------------------------------------------- if ( argc < 4 ) { printf( "usage: %s \n", argv[0] ); exit(1); } //---------------------------------------------- // get the hostname for the destination station //---------------------------------------------- char peername[ 64 ] = {0}; strncpy( peername, argv[ 1 ], 63 ); //------------------------------------------- // get the port-number for the socket to use //------------------------------------------- unsigned short port = atoi( argv[ 2 ] ); //------------------------------------------ // get the value for the Time-to-Live field //------------------------------------------ unsigned char ttl = atoi( argv[ 3 ] ); int len = sizeof( ttl ); //------------------------------------- // get the peer's IPv4 network address //------------------------------------- char peeraddr[ 16 ] = {0}; struct hostent *pp = gethostbyname( peername ); if ( !pp ) { herror( "gethostbyname" ); exit(1); } strcpy( peeraddr, inet_ntoa( *(in_addr*)pp->h_addr ) ); //---------------------------------------------------- // initialize an internet socket-address for the peer //---------------------------------------------------- struct sockaddr_in paddr; socklen_t palen = sizeof( paddr ); bzero( &paddr, palen ); paddr.sin_family = AF_INET; paddr.sin_port = htons( port ); paddr.sin_addr.s_addr = *(uint32_t*)pp->h_addr; //------------------------------------------------------ // ask the kernel to create an internet datagram socket //------------------------------------------------------ int sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_IP ); if ( sock < 0 ) { perror( "socket" ); exit(1); } //----------------------------------------------------- // set the value of this socket's 'Time-to-Live' field //----------------------------------------------------- if ( setsockopt( sock, SOL_IP, IP_TTL, &ttl, len ) < 0 ) { perror( "setsockopt TTL" ); exit(1); } //--------------------------------------------- // create the message-string to be transmitted //--------------------------------------------- char msg[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 *"; int mlen = strlen( msg ); //-------------------------------------------------- // try to send this message to the destination host //-------------------------------------------------- int tx = sendto( sock, msg, mlen, 0, (sockaddr*)&paddr, palen ); if ( tx < 0 ) { perror( "sendto" ); exit(1); } printf( " sent %d bytes toward port %d", tx, port ); printf( " on \'%s\' (%s)", peername, peeraddr ); printf( " with TTL=%u \n", ttl ); }