Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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