iPXE
ping_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <getopt.h>
33 #include <ipxe/command.h>
34 #include <ipxe/parseopt.h>
35 #include <ipxe/timer.h>
36 #include <usr/pingmgmt.h>
37 
38 /** @file
39  *
40  * Ping command
41  *
42  */
43 
44 /** Default payload length */
45 #define PING_DEFAULT_SIZE 64
46 
47 /** Default timeout */
48 #define PING_DEFAULT_TIMEOUT TICKS_PER_SEC
49 
50 /** "ping" options */
51 struct ping_options {
52  /** Payload length */
53  unsigned int size;
54  /** Timeout (in ms) */
55  unsigned long timeout;
56  /** Number of packets to send (or zero for no limit) */
57  unsigned int count;
58  /** Inhibit output */
59  int quiet;
60 };
61 
62 /** "ping" option list */
63 static struct option_descriptor ping_opts[] = {
64  OPTION_DESC ( "size", 's', required_argument,
65  struct ping_options, size, parse_integer ),
66  OPTION_DESC ( "timeout", 't', required_argument,
68  OPTION_DESC ( "count", 'c', required_argument,
69  struct ping_options, count, parse_integer ),
70  OPTION_DESC ( "quiet", 'q', no_argument,
71  struct ping_options, quiet, parse_flag ),
72 };
73 
74 /** "ping" command descriptor */
76  COMMAND_DESC ( struct ping_options, ping_opts, 1, 1, "<host>" );
77 
78 /**
79  * The "ping" command
80  *
81  * @v argc Argument count
82  * @v argv Argument list
83  * @ret rc Return status code
84  */
85 static int ping_exec ( int argc, char **argv ) {
86  struct ping_options opts;
87  const char *hostname;
88  int rc;
89 
90  /* Initialise options */
91  memset ( &opts, 0, sizeof ( opts ) );
92  opts.size = PING_DEFAULT_SIZE;
93  opts.timeout = PING_DEFAULT_TIMEOUT;
94 
95  /* Parse options */
96  if ( ( rc = reparse_options ( argc, argv, &ping_cmd, &opts ) ) != 0 )
97  return rc;
98 
99  /* Parse hostname */
100  hostname = argv[optind];
101 
102  /* Ping */
103  if ( ( rc = ping ( hostname, opts.timeout, opts.size,
104  opts.count, opts.quiet ) ) != 0 )
105  return rc;
106 
107  return 0;
108 }
109 
110 /** Ping command */
111 COMMAND ( ping, ping_exec );
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:92
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int size
Payload length.
Definition: ping_cmd.c:53
int optind
Current option index.
Definition: getopt.c:52
Error codes.
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:115
uint16_t size
Buffer size.
Definition: dwmac.h:14
int quiet
Inhibit output.
Definition: ping_cmd.c:59
COMMAND(ping, ping_exec)
Ping command.
iPXE timers
A command descriptor.
Definition: parseopt.h:78
static int ping_exec(int argc, char **argv)
The "ping" command.
Definition: ping_cmd.c:85
#define PING_DEFAULT_SIZE
Default payload length.
Definition: ping_cmd.c:45
ICMP ping management.
Parse command-line options.
static struct option_descriptor ping_opts[]
"ping" option list
Definition: ping_cmd.c:63
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static unsigned int count
Number of entries.
Definition: dwmac.h:225
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition: parseopt.c:402
FILE_SECBOOT(PERMITTED)
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:227
unsigned int count
Number of packets to send (or zero for no limit)
Definition: ping_cmd.c:57
Command line option parsing.
Option does not take an argument.
Definition: getopt.h:17
unsigned long timeout
Timeout (in ms)
Definition: ping_cmd.c:55
#define PING_DEFAULT_TIMEOUT
Default timeout.
Definition: ping_cmd.c:48
static struct command_descriptor ping_cmd
"ping" command descriptor
Definition: ping_cmd.c:75
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:68
"ping" options
Definition: ping_cmd.c:51
Option requires an argument.
Definition: getopt.h:19
A command-line option descriptor.
Definition: parseopt.h:24
static union @447 opts
"cert<xxx>" option list
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:109
void timeout(int)
String functions.
int ping(const char *hostname, unsigned long timeout, size_t len, unsigned int count, int quiet)
Ping a host.
Definition: pingmgmt.c:70
void * memset(void *dest, int character, size_t len) __nonnull