You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ping_cmd.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <getopt.h>
  26. #include <ipxe/command.h>
  27. #include <ipxe/parseopt.h>
  28. #include <ipxe/timer.h>
  29. #include <usr/pingmgmt.h>
  30. /** @file
  31. *
  32. * Ping command
  33. *
  34. */
  35. /** Default payload length */
  36. #define PING_DEFAULT_SIZE 64
  37. /** Default timeout */
  38. #define PING_DEFAULT_TIMEOUT TICKS_PER_SEC
  39. /** "ping" options */
  40. struct ping_options {
  41. /** Payload length */
  42. unsigned int size;
  43. /** Timeout (in ms) */
  44. unsigned long timeout;
  45. };
  46. /** "ping" option list */
  47. static struct option_descriptor ping_opts[] = {
  48. OPTION_DESC ( "size", 's', required_argument,
  49. struct ping_options, size, parse_integer ),
  50. OPTION_DESC ( "timeout", 't', required_argument,
  51. struct ping_options, timeout, parse_timeout ),
  52. };
  53. /** "ping" command descriptor */
  54. static struct command_descriptor ping_cmd =
  55. COMMAND_DESC ( struct ping_options, ping_opts, 1, 1, "<host>" );
  56. /**
  57. * The "ping" command
  58. *
  59. * @v argc Argument count
  60. * @v argv Argument list
  61. * @ret rc Return status code
  62. */
  63. static int ping_exec ( int argc, char **argv ) {
  64. struct ping_options opts;
  65. const char *hostname;
  66. int rc;
  67. /* Initialise options */
  68. memset ( &opts, 0, sizeof ( opts ) );
  69. opts.size = PING_DEFAULT_SIZE;
  70. opts.timeout = PING_DEFAULT_TIMEOUT;
  71. /* Parse options */
  72. if ( ( rc = reparse_options ( argc, argv, &ping_cmd, &opts ) ) != 0 )
  73. return rc;
  74. /* Parse hostname */
  75. hostname = argv[optind];
  76. /* Ping */
  77. if ( ( rc = ping ( hostname, opts.timeout, opts.size ) ) != 0 )
  78. return rc;
  79. return 0;
  80. }
  81. /** Ping command */
  82. struct command ping_command __command = {
  83. .name = "ping",
  84. .exec = ping_exec,
  85. };