/**************************************************************************** * * * Copyright 2004-2011 NetApp Inc. All rights reserved. This file is * * confidential and a trade secret of NetApp Inc. The receipt or * * possession of this file does not convey any rights to reproduce or * * disclose its contents or to manufacture, use, or sell anything it may * * describe, in whole, or in part, without the specific written consent of * * LSI Corporation. * * * ****************************************************************************/ /**************************************************************************** * * * NAME fwdCmd.c * * SUMMARY * * VERSION %version: % * * UPDATE DATE %date_modified: % * * PROGRAMMER %created_by: * * * * Copyright 2002-2011 NetApp Inc. All Rights Reserved. * * * * DESCRIPTION: * * * ****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include "fwdCmd.h" void usage( void ) { fprintf( stderr, "\n" ); fprintf( stderr, FWD_CMD_NAME "\n" ); fprintf( stderr, "Usage: " FWD_CMD_NAME " -i IPADDR -c CMD | [-h] [-p]\n" ); fprintf( stderr, "\t-h\tprint this message\n" ); fprintf( stderr, "\t-p\tuse popen to execute command string\n" ); fprintf( stderr, "\t-i\tIPv4 address\n" ); fprintf( stderr, "\t-c\tCommand String\n" ); fprintf( stderr, "\n" ); } unsigned char isIPv4Format( const char *str ) { int i, j, pos[ 3 ], len; char buf[ IP_STR_BUF ]; unsigned int addr[ 4 ]; unsigned char valid = TRUE; // Length check len = strlen( str ); if( len > IP_STR_LEN || len < IP_STR_LEN_MIN ) return FALSE; strncpy( buf, str, IP_STR_BUF ); // Look for positions of delimiters for( i = 0, j = 0 ; i < len ; i++ ) { if( buf[ i ] == '.' ) { // Exceed the limit if( j >= 3 ) return FALSE; // Record & Terminate string pos[ j ] = i; buf[ i ] = 0; j++; } } // Must have 3 dots at least or at most if( j != 3 ) return FALSE; // Convert strings addr[ 0 ] = strtol( (const char *)&buf[ 0 ], NULL, 10 ); addr[ 1 ] = strtol( (const char *)&buf[ pos[ 0 ] + 1 ], NULL, 10 ); addr[ 2 ] = strtol( (const char *)&buf[ pos[ 1 ] + 1 ], NULL, 10 ); addr[ 3 ] = strtol( (const char *)&buf[ pos[ 2 ] + 1 ], NULL, 10 ); // Check range for( i = 0 ; i < 4 ; i++ ) if( addr[ i ] > 255 ) { valid = FALSE; break; } // Not ZERO snprintf( buf, IP_STR_BUF, "%d.%d.%d.%d", addr[ 0 ], addr[ 1 ], addr[ 2 ], addr[ 3 ] ); if( strcmp( buf, str ) ) valid = FALSE; return valid; } int main( int argc, char **argv ) { int ret = 0, wbytes, sfd; struct sockaddr_in servaddr; char *ipaddr = NULL, ipbuf[ IP_STR_BUF + 1 ]; char cmd[ DEFAULT_BUFSIZE ]; int c; char buffer[ DEFAULT_BUFSIZE ]; int rbytes; int popen_flag=0; int counter = 0; time_t current_time; char* c_time_string; // Handle arguments while( (c = getopt( argc, argv, "i:c:hp" )) != EOF ) { switch( c ) { case 'i': strncpy( ipbuf, optarg, sizeof( ipbuf ) ); if( isIPv4Format( ipbuf ) == TRUE ) ipaddr = ipbuf; break; case 'p': popen_flag=1; break; case 'c': strncpy( cmd, optarg, sizeof( cmd ) ); break; case 'h': default: usage(); return -1; } } // Sanity check if( !ipaddr ) { fprintf( stderr, FWD_CMD_NAME ": Invalid ip addr\n" ); usage(); return -1; } if( strlen( cmd ) <= 0 ) { fprintf( stderr, FWD_CMD_NAME ": No command string\n" ); usage(); return -1; } // Main procedure sfd = socket( PF_INET, SOCK_STREAM, 0 ); if( sfd < 0 ) { fprintf( stderr, FWD_CMD_NAME ": Cannot open socket\n" ); return sfd; } // Set address memset( &servaddr, 0, sizeof( struct sockaddr_in ) ); servaddr.sin_family = PF_INET; servaddr.sin_port = htons( PORT_NUM ); ret = inet_aton(ipaddr, &servaddr.sin_addr ); if( ret < 0 ) { fprintf( stderr, FWD_CMD_NAME ": Cannot set IP address %s\n", ipaddr ); goto stopTest; } while (counter <= 5) { // Connect to server ret = connect( sfd, (struct sockaddr *)&servaddr, sizeof( struct sockaddr_in ) ); if( ret < 0 ) { current_time = time(NULL); /* Convert to local time format. */ c_time_string = ctime(¤t_time); counter++; if (counter <= 5) { sleep(2); } else { fprintf( stderr, FWD_CMD_NAME ": Cannot connect to server %s with 5 retries %s", ipaddr, c_time_string); } } else { break; } } if( ret < 0 ) { goto stopTest; } if(popen_flag==1) { sprintf(buffer, "popen%s", cmd); sprintf(cmd, "%s", buffer); } // Send Pattern wbytes = write( sfd, cmd, strlen( cmd ) ); if(popen_flag==1) { memset(buffer, 0, sizeof(buffer)); while((rbytes = read( sfd, buffer, sizeof(buffer)) > 0)) { if(rbytes>0) { printf("%s", buffer); } } } if( wbytes < 0 ) { ret = -1; goto stopTest; } stopTest: close( sfd ); return ret; }