summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/hci/commands/ping_cmd.c
blob: 34807696f4bc144e2059d109fb38a24cac320051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <ipxe/timer.h>
#include <usr/pingmgmt.h>

/** @file
 *
 * Ping command
 *
 */

/** Default payload length */
#define PING_DEFAULT_SIZE 64

/** Default timeout */
#define PING_DEFAULT_TIMEOUT TICKS_PER_SEC

/** "ping" options */
struct ping_options {
	/** Payload length */
	unsigned int size;
	/** Timeout (in ms) */
	unsigned long timeout;
	/** Number of packets to send (or zero for no limit) */
	unsigned int count;
	/** Inhibit output */
	int quiet;
};

/** "ping" option list */
static struct option_descriptor ping_opts[] = {
	OPTION_DESC ( "size", 's', required_argument,
		      struct ping_options, size, parse_integer ),
	OPTION_DESC ( "timeout", 't', required_argument,
		      struct ping_options, timeout, parse_timeout ),
	OPTION_DESC ( "count", 'c', required_argument,
		      struct ping_options, count, parse_integer ),
	OPTION_DESC ( "quiet", 'q', no_argument,
		      struct ping_options, quiet, parse_flag ),
};

/** "ping" command descriptor */
static struct command_descriptor ping_cmd =
	COMMAND_DESC ( struct ping_options, ping_opts, 1, 1, "<host>" );

/**
 * The "ping" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Return status code
 */
static int ping_exec ( int argc, char **argv ) {
	struct ping_options opts;
	const char *hostname;
	int rc;

	/* Initialise options */
	memset ( &opts, 0, sizeof ( opts ) );
	opts.size = PING_DEFAULT_SIZE;
	opts.timeout = PING_DEFAULT_TIMEOUT;

	/* Parse options */
	if ( ( rc = reparse_options ( argc, argv, &ping_cmd, &opts ) ) != 0 )
		return rc;

	/* Parse hostname */
	hostname = argv[optind];

	/* Ping */
	if ( ( rc = ping ( hostname, opts.timeout, opts.size,
			   opts.count, opts.quiet ) ) != 0 )
		return rc;

	return 0;
}

/** Ping command */
struct command ping_command __command = {
	.name = "ping",
	.exec = ping_exec,
};