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 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <getopt.h>
32 #include <ipxe/command.h>
33 #include <ipxe/parseopt.h>
34 #include <ipxe/timer.h>
35 #include <usr/pingmgmt.h>
36 
37 /** @file
38  *
39  * Ping command
40  *
41  */
42 
43 /** Default payload length */
44 #define PING_DEFAULT_SIZE 64
45 
46 /** Default timeout */
47 #define PING_DEFAULT_TIMEOUT TICKS_PER_SEC
48 
49 /** "ping" options */
50 struct ping_options {
51  /** Payload length */
52  unsigned int size;
53  /** Timeout (in ms) */
54  unsigned long timeout;
55  /** Number of packets to send (or zero for no limit) */
56  unsigned int count;
57  /** Inhibit output */
58  int quiet;
59 };
60 
61 /** "ping" option list */
62 static struct option_descriptor ping_opts[] = {
63  OPTION_DESC ( "size", 's', required_argument,
64  struct ping_options, size, parse_integer ),
65  OPTION_DESC ( "timeout", 't', required_argument,
67  OPTION_DESC ( "count", 'c', required_argument,
68  struct ping_options, count, parse_integer ),
69  OPTION_DESC ( "quiet", 'q', no_argument,
70  struct ping_options, quiet, parse_flag ),
71 };
72 
73 /** "ping" command descriptor */
75  COMMAND_DESC ( struct ping_options, ping_opts, 1, 1, "<host>" );
76 
77 /**
78  * The "ping" command
79  *
80  * @v argc Argument count
81  * @v argv Argument list
82  * @ret rc Return status code
83  */
84 static int ping_exec ( int argc, char **argv ) {
85  struct ping_options opts;
86  const char *hostname;
87  int rc;
88 
89  /* Initialise options */
90  memset ( &opts, 0, sizeof ( opts ) );
91  opts.size = PING_DEFAULT_SIZE;
92  opts.timeout = PING_DEFAULT_TIMEOUT;
93 
94  /* Parse options */
95  if ( ( rc = reparse_options ( argc, argv, &ping_cmd, &opts ) ) != 0 )
96  return rc;
97 
98  /* Parse hostname */
99  hostname = argv[optind];
100 
101  /* Ping */
102  if ( ( rc = ping ( hostname, opts.timeout, opts.size,
103  opts.count, opts.quiet ) ) != 0 )
104  return rc;
105 
106  return 0;
107 }
108 
109 /** Ping command */
110 struct command ping_command __command = {
111  .name = "ping",
112  .exec = ping_exec,
113 };
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:91
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int size
Payload length.
Definition: ping_cmd.c:52
int optind
Current option index.
Definition: getopt.c:51
Error codes.
A command-line command.
Definition: command.h:9
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
int quiet
Inhibit output.
Definition: ping_cmd.c:58
iPXE timers
A command descriptor.
Definition: parseopt.h:77
static int ping_exec(int argc, char **argv)
The "ping" command.
Definition: ping_cmd.c:84
#define PING_DEFAULT_SIZE
Default payload length.
Definition: ping_cmd.c:44
ICMP ping management.
Parse command-line options.
static struct option_descriptor ping_opts[]
"ping" option list
Definition: ping_cmd.c:62
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition: parseopt.c:401
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
unsigned int count
Number of packets to send (or zero for no limit)
Definition: ping_cmd.c:56
Command line option parsing.
Option does not take an argument.
Definition: getopt.h:16
unsigned long timeout
Timeout (in ms)
Definition: ping_cmd.c:54
static union @437 opts
"cert<xxx>" option list
const char * name
Name of the command.
Definition: command.h:11
#define PING_DEFAULT_TIMEOUT
Default timeout.
Definition: ping_cmd.c:47
static struct command_descriptor ping_cmd
"ping" command descriptor
Definition: ping_cmd.c:74
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
"ping" options
Definition: ping_cmd.c:50
Option requires an argument.
Definition: getopt.h:18
uint16_t count
Number of entries.
Definition: ena.h:22
A command-line option descriptor.
Definition: parseopt.h:23
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
struct command ping_command __command
Ping command.
Definition: ping_cmd.c:110
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:69
void * memset(void *dest, int character, size_t len) __nonnull