iPXE
ping_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <errno.h>
00031 #include <getopt.h>
00032 #include <ipxe/command.h>
00033 #include <ipxe/parseopt.h>
00034 #include <ipxe/timer.h>
00035 #include <usr/pingmgmt.h>
00036 
00037 /** @file
00038  *
00039  * Ping command
00040  *
00041  */
00042 
00043 /** Default payload length */
00044 #define PING_DEFAULT_SIZE 64
00045 
00046 /** Default timeout */
00047 #define PING_DEFAULT_TIMEOUT TICKS_PER_SEC
00048 
00049 /** "ping" options */
00050 struct ping_options {
00051         /** Payload length */
00052         unsigned int size;
00053         /** Timeout (in ms) */
00054         unsigned long timeout;
00055         /** Number of packets to send (or zero for no limit) */
00056         unsigned int count;
00057         /** Inhibit output */
00058         int quiet;
00059 };
00060 
00061 /** "ping" option list */
00062 static struct option_descriptor ping_opts[] = {
00063         OPTION_DESC ( "size", 's', required_argument,
00064                       struct ping_options, size, parse_integer ),
00065         OPTION_DESC ( "timeout", 't', required_argument,
00066                       struct ping_options, timeout, parse_timeout ),
00067         OPTION_DESC ( "count", 'c', required_argument,
00068                       struct ping_options, count, parse_integer ),
00069         OPTION_DESC ( "quiet", 'q', no_argument,
00070                       struct ping_options, quiet, parse_flag ),
00071 };
00072 
00073 /** "ping" command descriptor */
00074 static struct command_descriptor ping_cmd =
00075         COMMAND_DESC ( struct ping_options, ping_opts, 1, 1, "<host>" );
00076 
00077 /**
00078  * The "ping" command
00079  *
00080  * @v argc              Argument count
00081  * @v argv              Argument list
00082  * @ret rc              Return status code
00083  */
00084 static int ping_exec ( int argc, char **argv ) {
00085         struct ping_options opts;
00086         const char *hostname;
00087         int rc;
00088 
00089         /* Initialise options */
00090         memset ( &opts, 0, sizeof ( opts ) );
00091         opts.size = PING_DEFAULT_SIZE;
00092         opts.timeout = PING_DEFAULT_TIMEOUT;
00093 
00094         /* Parse options */
00095         if ( ( rc = reparse_options ( argc, argv, &ping_cmd, &opts ) ) != 0 )
00096                 return rc;
00097 
00098         /* Parse hostname */
00099         hostname = argv[optind];
00100 
00101         /* Ping */
00102         if ( ( rc = ping ( hostname, opts.timeout, opts.size,
00103                            opts.count, opts.quiet ) ) != 0 )
00104                 return rc;
00105 
00106         return 0;
00107 }
00108 
00109 /** Ping command */
00110 struct command ping_command __command = {
00111         .name = "ping",
00112         .exec = ping_exec,
00113 };