Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / ping_cmd.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <ipxe/command.h>
29 #include <ipxe/parseopt.h>
30 #include <ipxe/timer.h>
31 #include <usr/pingmgmt.h>
32
33 /** @file
34  *
35  * Ping command
36  *
37  */
38
39 /** Default payload length */
40 #define PING_DEFAULT_SIZE 64
41
42 /** Default timeout */
43 #define PING_DEFAULT_TIMEOUT TICKS_PER_SEC
44
45 /** "ping" options */
46 struct ping_options {
47         /** Payload length */
48         unsigned int size;
49         /** Timeout (in ms) */
50         unsigned long timeout;
51         /** Number of packets to send (or zero for no limit) */
52         unsigned int count;
53         /** Inhibit output */
54         int quiet;
55 };
56
57 /** "ping" option list */
58 static struct option_descriptor ping_opts[] = {
59         OPTION_DESC ( "size", 's', required_argument,
60                       struct ping_options, size, parse_integer ),
61         OPTION_DESC ( "timeout", 't', required_argument,
62                       struct ping_options, timeout, parse_timeout ),
63         OPTION_DESC ( "count", 'c', required_argument,
64                       struct ping_options, count, parse_integer ),
65         OPTION_DESC ( "quiet", 'q', no_argument,
66                       struct ping_options, quiet, parse_flag ),
67 };
68
69 /** "ping" command descriptor */
70 static struct command_descriptor ping_cmd =
71         COMMAND_DESC ( struct ping_options, ping_opts, 1, 1, "<host>" );
72
73 /**
74  * The "ping" command
75  *
76  * @v argc              Argument count
77  * @v argv              Argument list
78  * @ret rc              Return status code
79  */
80 static int ping_exec ( int argc, char **argv ) {
81         struct ping_options opts;
82         const char *hostname;
83         int rc;
84
85         /* Initialise options */
86         memset ( &opts, 0, sizeof ( opts ) );
87         opts.size = PING_DEFAULT_SIZE;
88         opts.timeout = PING_DEFAULT_TIMEOUT;
89
90         /* Parse options */
91         if ( ( rc = reparse_options ( argc, argv, &ping_cmd, &opts ) ) != 0 )
92                 return rc;
93
94         /* Parse hostname */
95         hostname = argv[optind];
96
97         /* Ping */
98         if ( ( rc = ping ( hostname, opts.timeout, opts.size,
99                            opts.count, opts.quiet ) ) != 0 )
100                 return rc;
101
102         return 0;
103 }
104
105 /** Ping command */
106 struct command ping_command __command = {
107         .name = "ping",
108         .exec = ping_exec,
109 };