📄 fping.c
字号:
{ nm = cpystr( get_host_by_address( *ipa ) ); add_addr( name, nm, *ipa ); }/* ELSE */ }/* IF */ else add_addr( name, name, *ipa ); return; }/* IF */ /* input name is not an IP addr, maybe it's a host name */ host_ent = gethostbyname( name ); if( host_ent == NULL ) { if( h_errno == TRY_AGAIN ) { u_sleep( DNS_TIMEOUT ); host_ent = gethostbyname( name ); }/* IF */ if( host_ent == NULL ) {#ifdef NIS_GROUPS /* maybe it's the name of a NIS netgroup */ char *machine, *user_ignored, *domain_ignored; setnetgrent( name ); if( getnetgrent( &machine, &user_ignored, &domain_ignored ) == 0 ) { endnetgrent(); if( !quiet_flag ) fprintf( stderr, "%s address not found\n", name ); num_noaddress++; return; }/* IF */ else add_name( cpystr( machine ) ); while( getnetgrent( &machine, &user_ignored, &domain_ignored ) ) add_name( cpystr( machine ) ); endnetgrent(); return;#else if( !quiet_flag ) fprintf( stderr, "%s address not found\n", name ); num_noaddress++; return ; #endif /* NIS_GROUPS */ }/* IF */ }/* IF */ host_add = ( struct in_addr* )*( host_ent->h_addr_list ); if( host_add == NULL ) { if( !quiet_flag ) fprintf( stderr, "%s has no address data\n", name ); num_noaddress++; return; }/* IF */ else { /* it is indeed a hostname with a real address */ while( host_add ) { if( name_flag && addr_flag ) add_addr( name, na_cat( name, *host_add ), *host_add ); else if( addr_flag ) { nm = cpystr( inet_ntoa( *host_add ) ); add_addr( name, nm, *host_add ); }/* ELSE IF */ else add_addr( name, name, *host_add ); if( !multif_flag ) break; host_add = ( struct in_addr* )( host_ent->h_addr_list[++i] ); }/* WHILE */ }/* ELSE */} /* add_name() *//************************************************************ Function: na_cat************************************************************* Inputs: char* name, struct in_addr ipaddr Returns: char* Description:************************************************************/#ifdef _NO_PROTOchar *na_cat( name, ipaddr )char *name;struct in_addr ipaddr;#elsechar *na_cat( char *name, struct in_addr ipaddr )#endif /* _NO_PROTO */{ char *nm, *as; as = inet_ntoa( ipaddr ); nm = ( char* )malloc( strlen( name ) + strlen( as ) + 4 ); if( !nm ) crash_and_burn( "can't allocate some space for a string" ); strcpy( nm, name ); strcat( nm, " (" ); strcat( nm, as ); strcat( nm, ")" ); return( nm );} /* na_cat() *//************************************************************ Function: add_addr************************************************************* Inputs: char* name, char* host, struct in_addr ipaddr Description: add address to linked list of targets to be pinged assume memory for *name and *host is ours!!!************************************************************/#ifdef _NO_PROTOvoid add_addr( name, host, ipaddr )char *name;char *host;struct in_addr ipaddr;#elsevoid add_addr( char *name, char *host, struct in_addr ipaddr )#endif /* _NO_PROTO */{ HOST_ENTRY *p; int n, *i; p = ( HOST_ENTRY* )malloc( sizeof( HOST_ENTRY ) ); if( !p ) crash_and_burn( "can't allocate HOST_ENTRY" ); memset( ( char* ) p, 0, sizeof( HOST_ENTRY ) ); p->name = name; p->host = host; p->saddr.sin_family = AF_INET; p->saddr.sin_addr = ipaddr; p->timeout = timeout; p->running = 1; p->min_reply = 10000000; if( strlen( p->host ) > max_hostname_len ) max_hostname_len = strlen( p->host ); /* array for response time results */ if( !loop_flag ) { i = ( int* )malloc( trials * sizeof( int ) ); if( !i ) crash_and_burn( "can't allocate resp_times array" ); for( n = 1; n < trials; n++ ) i[n] = RESP_UNUSED; p->resp_times = i; }/* IF */#if defined( DEBUG ) || defined( _DEBUG ) /* likewise for sent times */ if( sent_times_flag ) { i = ( int* )malloc( trials * sizeof( int ) ); if( !i ) crash_and_burn( "can't allocate sent_times array" ); for( n = 1; n < trials; n++ ) i[n] = RESP_UNUSED; p->sent_times = i; }/* IF */#endif /* DEBUG || _DEBUG */ if( !rrlist ) { rrlist = p; p->next = p; p->prev = p; }/* IF */ else { p->next = rrlist; p->prev = rrlist->prev; p->prev->next = p; p->next->prev = p; }/* ELSE */ num_hosts++;} /* add_addr() *//************************************************************ Function: remove_job************************************************************* Inputs: HOST_ENTRY *h Description:************************************************************/#ifdef _NO_PROTOvoid remove_job( h )HOST_ENTRY *h;#elsevoid remove_job( HOST_ENTRY *h )#endif /* _NO_PROTO */{#if defined( DEBUG ) || defined( _DEBUG ) if( trace_flag ) printf( "removing job for %s\n", h->host );#endif /* DEBUG || _DEBUG */ h->running = 0; h->waiting = 0; --num_jobs; if( num_jobs ) { /* remove us from list of active jobs */ h->prev->next = h->next; h->next->prev = h->prev; if( h==cursor ) cursor = h->next; }/* IF */ else { cursor = NULL; rrlist = NULL; }/* ELSE */} /* remove_job() *//************************************************************ Function: get_host_by_address************************************************************* Inputs: struct in_addr in Returns: char* Description:************************************************************/#ifdef _NO_PROTOchar *get_host_by_address( in )struct in_addr in;#elsechar *get_host_by_address( struct in_addr in )#endif /* _NO_PROTO */{ struct hostent *h; h = gethostbyaddr( ( char* )&in, sizeof( struct in_addr ),AF_INET ); if( h == NULL || h->h_name == NULL ) return inet_ntoa( in ); else return ( char* )h->h_name;} /* get_host_by_address() *//************************************************************ Function: cpystr************************************************************* Inputs: char* string Returns: char* Description:************************************************************/#ifdef _NO_PROTOchar *cpystr( string )char *string;#elsechar *cpystr( char *string )#endif /* _NO_PROTO */{ char *dst; if( string ) { dst = ( char* )malloc( 1 + strlen( string ) ); if( !dst ) crash_and_burn( "can't allocate some space for a string" ); strcpy( dst, string ); return dst; }/* IF */ else return NULL;} /* cpystr() *//************************************************************ Function: crash_and_burn************************************************************* Inputs: char* message Description:************************************************************/ #ifdef _NO_PROTOvoid crash_and_burn( message )char *message;#elsevoid crash_and_burn( char *message )#endif /* _NO_PROTO */{ if( verbose_flag ) fprintf( stderr, "%s: %s\n", prog, message ); exit( 4 );} /* crash_and_burn() *//************************************************************ Function: errno_crash_and_burn************************************************************* Inputs: char* message Description:************************************************************/#ifdef _NO_PROTOvoid errno_crash_and_burn( message )char *message;#elsevoid errno_crash_and_burn( char *message )#endif /* _NO_PROTO */{ if( verbose_flag ) fprintf( stderr, "%s: %s : %s\n", prog, message, strerror( errno ) ); exit( 4 );} /* errno_crash_and_burn() *//************************************************************ Function: timeval_diff************************************************************* Inputs: struct timeval *a, struct timeval *b Returns: long Description: timeval_diff now returns result in hundredths of milliseconds ie, tens of microseconds ************************************************************/#ifdef _NO_PROTOlong timeval_diff( a, b )struct timeval *a, *b;#elselong timeval_diff( struct timeval *a, struct timeval *b )#endif /* _NO_PROTO */{ double temp; temp = ( ( ( a->tv_sec * 1000000 ) + a->tv_usec ) - ( ( b->tv_sec * 1000000 ) + b->tv_usec ) ) / 10; return ( long )temp;} /* timeval_diff() *//************************************************************ Function: sprint_tm************************************************************* Inputs: int t Returns: char* Description: render time into a string with three digits of precision input is in tens of microseconds************************************************************/#ifdef _NO_PROTOchar * sprint_tm( t )int t;#elsechar * sprint_tm( int t )#endif /* _NO_PROTO */{ static char buf[10]; /* <= 0.99 ms */ if( t < 100 ) { sprintf( buf, "0.%02d", t ); return( buf ); }/* IF */ /* 1.00 - 9.99 ms */ if( t < 1000 ) { sprintf( buf, "%d.%02d", t / 100, t % 100 ); return( buf ); }/* IF */ /* 10.0 - 99.9 ms */ if( t < 10000 ) { sprintf( buf, "%d.%d", t / 100, ( t % 100 ) / 10 ); return( buf ); }/* IF */ /* >= 100 ms */ sprintf( buf, "%d", t / 100 ); return( buf );} /* sprint_tm() *//************************************************************ Function: u_sleep************************************************************* Inputs: int u_sec Description:************************************************************/#ifdef _NO_PROTOvoid u_sleep( u_sec )int u_sec;#elsevoid u_sleep( int u_sec )#endif /* _NO_PROTO */{ int nfound, slen, n; struct timeval to; fd_set readset, writeset; to.tv_sec = u_sec / 1000000; to.tv_usec = u_sec - ( to.tv_sec * 1000000 ); FD_ZERO( &readset ); FD_ZERO( &writeset ); nfound = select( 0, &readset, &writeset, NULL, &to ); if( nfound < 0 ) errno_crash_and_burn( "select" ); return;} /* u_sleep() *//************************************************************ Function: recvfrom_wto************************************************************* Inputs: int s, char* buf, int len, struct sockaddr *saddr, int timo Returns: int Description: receive with timeout returns length of data read or -1 if timeout crash_and_burn on any other errrors************************************************************/#ifdef _NO_PROTOint recvfrom_wto( s, buf, len, saddr, timo )int s; char *buf; int len; struct sockaddr *saddr; int timo;#elseint recvfrom_wto( int s, char *buf, int len, struct sockaddr *saddr, int timo )#endif /* _NO_PROTO */{ int nfound, slen, n; struct timeval to; fd_set readset, writeset; to.tv_sec = timo / 100000; to.tv_usec = ( timo - ( to.tv_sec * 100000 ) ) * 10; FD_ZERO( &readset ); FD_ZERO( &writeset ); FD_SET( s, &readset ); nfound = select( s + 1, &readset, &writeset, NULL, &to ); if( nfound < 0 ) errno_crash_and_burn( "select" ); if( nfound == 0 ) return -1; /* timeout */ slen = sizeof( struct sockaddr ); n = recvfrom( s, buf, len, 0, saddr, &slen ); if( n < 0 ) errno_crash_and_burn( "recvfrom" ); return n;} /* recvfrom_wto() *//************************************************************ Function: usage************************************************************* Inputs: none (void) Description:************************************************************/#ifdef _NO_PROTOvoid usage()#elsevoid usage( void )#endif /* _NO_PROTO */{ fprintf( stderr, "\n" ); fprintf( stderr, "Usage: %s [options] [targets...]\n", prog ); fprintf( stderr, " -a show targets that are alive\n" ); fprintf( stderr, " -A show targets by address\n" ); fprintf( stderr, " -b n amount of ping data to send, in bytes (default %d)\n", ping_data_size ); fprintf( stderr, " -B f set exponential backoff factor to f\n" ); fprintf( stderr, " -c n count of pings to send to each target (default %d)\n", count ); fprintf( stderr, " -C n same as -c, report results in verbose format\n" ); fprintf( stderr, " -e show elapsed time on return packets\n" ); fprintf( stderr, " -f file read list of targets from a file ( - means stdin) (only if no -g specified)\n" ); fprintf( stderr, " -g generate target list (only if no -f specified)\n" ); fprintf( stderr, " (specify the start and end IP in the target list, or supply a IP netmask)\n" ); fprintf( stderr, " (ex. %s -g 192.168.1.0 192.168.1.255 or %s -g 192.168.1.0/24)\n", prog, prog ); fprintf( stderr, " -i n interval between sending ping packets (in millisec) (default %d)\n", interval / 100 ); fprintf( stderr, " -l loop sending pings forever\n" ); fprintf( stderr, " -m ping multiple interfaces on target host\n" ); fprintf( stderr, " -n show targets by name (-d is equivalent)\n" ); fprintf( stderr, " -p n interval between ping packets to one target (in millisec)\n" ); fprintf( stderr, " (in looping and counting modes, default %d)\n", perhost_interval / 100 ); fprintf( stderr, " -q quiet (don't show per-target/per-ping results)\n" ); fprintf( stderr, " -Q n same as -q, but show summary every n seconds\n" ); fprintf( stderr, " -r n number of retries (default %d)\n", retry ); fprintf( stderr, " -s print final stats\n" ); fprintf( stderr, " -t n individual target initial timeout (in millisec) (default %d)\n", timeout / 100 ); fprintf( stderr, " -u show targets that are unreachable\n" ); fprintf( stderr, " -v show version\n" ); fprintf( stderr, " targets list of targets to check (if no -f specified)\n" ); fprintf( stderr, "\n"); exit( 3 );} /* usage() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -