46 |
#include <errno.h> |
#include <errno.h> |
47 |
#include <limits.h> |
#include <limits.h> |
48 |
|
|
49 |
#include "getopt.h" |
#include <getopt.h> |
50 |
#include <icmp.h> |
#include <icmp.h> |
51 |
#include <ping.h> |
#include <ping.h> |
52 |
#include <ping_impl.h> |
#include "ping_common.h" |
53 |
|
#include "ping_impl.h" |
54 |
|
|
55 |
static char short_options[] = "VLhc:dfi:l:np:qRrs:t:v"; |
static char short_options[] = "VLhc:dfi:l:np:qRrs:t:v"; |
56 |
static struct option long_options[] = |
static struct option long_options[] = |
82 |
{NULL, no_argument, NULL, 0} |
{NULL, no_argument, NULL, 0} |
83 |
}; |
}; |
84 |
|
|
85 |
extern int ping_echo __P ((int argc, char **argv)); |
extern int ping_echo (int argc, char **argv); |
86 |
extern int ping_timestamp __P ((int argc, char **argv)); |
extern int ping_timestamp (int argc, char **argv); |
87 |
extern int ping_address __P ((int argc, char **argv)); |
extern int ping_address (int argc, char **argv); |
88 |
extern int ping_router __P ((int argc, char **argv)); |
extern int ping_router (int argc, char **argv); |
89 |
|
|
90 |
PING *ping; |
PING *ping; |
91 |
u_char *data_buffer; |
u_char *data_buffer; |
92 |
size_t data_length = PING_DATALEN; |
size_t data_length = PING_DATALEN; |
93 |
unsigned options; |
unsigned options; |
94 |
unsigned long preload = 0; |
unsigned long preload = 0; |
95 |
int (*ping_type) __P ((int argc, char **argv)) = ping_echo; |
int (*ping_type) (int argc, char **argv) = ping_echo; |
96 |
|
|
97 |
|
|
98 |
static void show_usage (void); |
static void show_usage (void); |
|
static void show_license (void); |
|
|
static void decode_pattern (const char *text, int *pattern_len, |
|
|
u_char *pattern_data); |
|
99 |
static void decode_type (const char *optarg); |
static void decode_type (const char *optarg); |
|
static void init_data_buffer (u_char *pat, int len); |
|
100 |
static int send_echo (PING *ping); |
static int send_echo (PING *ping); |
101 |
|
|
102 |
int |
int |
125 |
{ |
{ |
126 |
case 'V': |
case 'V': |
127 |
printf ("ping - %s %s\n", PACKAGE_NAME, PACKAGE_VERSION); |
printf ("ping - %s %s\n", PACKAGE_NAME, PACKAGE_VERSION); |
128 |
printf ("Copyright (C) 1998,2001 Free Software Foundation, Inc.\n"); |
printf ("Copyright (C) 2005 Free Software Foundation, Inc.\n"); |
129 |
printf ("%s comes with ABSOLUTELY NO WARRANTY.\n", PACKAGE_NAME); |
printf ("%s comes with ABSOLUTELY NO WARRANTY.\n", PACKAGE_NAME); |
130 |
printf ("You may redistribute copies of %s\n", PACKAGE_NAME); |
printf ("You may redistribute copies of %s\n", PACKAGE_NAME); |
131 |
printf ("under the terms of the GNU General Public License.\n"); |
printf ("under the terms of the GNU General Public License.\n"); |
227 |
break; |
break; |
228 |
|
|
229 |
case ICMP_ADDRESS: |
case ICMP_ADDRESS: |
230 |
|
if (!is_root) |
231 |
|
{ |
232 |
|
fprintf (stderr, "ping: option not allowed: --address\n"); |
233 |
|
exit (1); |
234 |
|
} |
235 |
decode_type ("address"); |
decode_type ("address"); |
236 |
break; |
break; |
237 |
|
|
238 |
case ICMP_ROUTERDISCOVERY: |
case ICMP_ROUTERDISCOVERY: |
239 |
|
if (!is_root) |
240 |
|
{ |
241 |
|
fprintf (stderr, "ping: option not allowed: --router\n"); |
242 |
|
exit (1); |
243 |
|
} |
244 |
decode_type ("router"); |
decode_type ("router"); |
245 |
break; |
break; |
246 |
|
|
283 |
return (*ping_type)(argc, argv); |
return (*ping_type)(argc, argv); |
284 |
} |
} |
285 |
|
|
|
void |
|
|
init_data_buffer (u_char *pat, int len) |
|
|
{ |
|
|
int i = 0; |
|
|
u_char *p; |
|
|
|
|
|
if (data_length == 0) |
|
|
return; |
|
286 |
|
|
|
data_buffer = (u_char *) xmalloc (data_length); |
|
|
|
|
|
if (pat) |
|
|
{ |
|
|
for (p = data_buffer; p < data_buffer + data_length; p++) |
|
|
{ |
|
|
*p = pat[i]; |
|
|
if (i++ >= len) |
|
|
i = 0; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
for (i = 0; i < data_length; i++) |
|
|
data_buffer[i] = i; |
|
|
} |
|
|
} |
|
|
|
|
287 |
|
|
288 |
void |
void |
289 |
decode_type (const char *optarg) |
decode_type (const char *optarg) |
305 |
} |
} |
306 |
} |
} |
307 |
|
|
|
void |
|
|
decode_pattern (const char *text, int *pattern_len, u_char *pattern_data) |
|
|
{ |
|
|
int i, c, off; |
|
|
|
|
|
for (i = 0; *text && i < *pattern_len; i++) |
|
|
{ |
|
|
if (sscanf (text, "%2x%n", &c, &off) != 1) |
|
|
{ |
|
|
fprintf (stderr, "ping: error in pattern near %s\n", text); |
|
|
exit (1); |
|
|
} |
|
|
text += off; |
|
|
} |
|
|
*pattern_len = i; |
|
|
} |
|
|
|
|
308 |
int volatile stop = 0; |
int volatile stop = 0; |
309 |
|
|
310 |
RETSIGTYPE |
RETSIGTYPE |
347 |
|
|
348 |
while (!stop) |
while (!stop) |
349 |
{ |
{ |
350 |
int n, len; |
int n; |
351 |
|
|
352 |
FD_ZERO (&fdset); |
FD_ZERO (&fdset); |
353 |
FD_SET (ping->ping_fd, &fdset); |
FD_SET (ping->ping_fd, &fdset); |
466 |
-V, --version output version information and exit\n\ |
-V, --version output version information and exit\n\ |
467 |
Options controlling ICMP request types:\n\ |
Options controlling ICMP request types:\n\ |
468 |
--echo Send ICMP_ECHO requests (default)\n\ |
--echo Send ICMP_ECHO requests (default)\n\ |
469 |
--address Send ICMP_ADDRESS packets\n\ |
* --address Send ICMP_ADDRESS packets\n\ |
470 |
--timestamp Send ICMP_TIMESTAMP packets\n\ |
--timestamp Send ICMP_TIMESTAMP packets\n\ |
471 |
--router Send ICMP_ROUTERDISCOVERY packets\n\ |
* --router Send ICMP_ROUTERDISCOVERY packets\n\ |
472 |
Options valid for all request types:\n\ |
Options valid for all request types:\n\ |
473 |
-c, --count N stop after sending N packets\n\ |
-c, --count N stop after sending N packets\n\ |
474 |
-d, --debug set the SO_DEBUG option\n\ |
-d, --debug set the SO_DEBUG option\n\ |
490 |
Report bugs to <" PACKAGE_BUGREPORT ">.\n\ |
Report bugs to <" PACKAGE_BUGREPORT ">.\n\ |
491 |
"); |
"); |
492 |
} |
} |
|
|
|
|
void |
|
|
show_license (void) |
|
|
{ |
|
|
static char license_text[] = |
|
|
" This program is free software; you can redistribute it and/or modify\n" |
|
|
" it under the terms of the GNU General Public License as published by\n" |
|
|
" the Free Software Foundation; either version 2, or (at your option)\n" |
|
|
" any later version.\n" |
|
|
"\n" |
|
|
" This program is distributed in the hope that it will be useful,\n" |
|
|
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
|
|
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
|
|
" GNU General Public License for more details.\n" |
|
|
"\n" |
|
|
" You should have received a copy of the GNU General Public License\n" |
|
|
" along with this program; if not, write to the Free Software\n" |
|
|
" Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"; |
|
|
printf ("%s", license_text); |
|
|
} |
|
|
|
|