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.

pingmgmt.c 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <stdio.h>
  26. #include <string.h>
  27. #include <ipxe/pinger.h>
  28. #include <ipxe/monojob.h>
  29. #include <ipxe/timer.h>
  30. #include <usr/pingmgmt.h>
  31. /** @file
  32. *
  33. * ICMP ping management
  34. *
  35. */
  36. /**
  37. * Display ping result
  38. *
  39. * @v src Source socket address, or NULL
  40. * @v sequence Sequence number
  41. * @v len Payload length
  42. * @v rc Status code
  43. */
  44. static void ping_callback ( struct sockaddr *peer, unsigned int sequence,
  45. size_t len, int rc ) {
  46. /* Display ping response */
  47. printf ( "%zd bytes from %s: seq=%d",
  48. len, ( peer ? sock_ntoa ( peer ) : "<none>" ), sequence );
  49. if ( rc != 0 )
  50. printf ( ": %s", strerror ( rc ) );
  51. printf ( "\n" );
  52. }
  53. /**
  54. * Ping a host
  55. *
  56. * @v hostname Hostname
  57. * @v timeout Timeout between pings, in ticks
  58. * @v len Payload length
  59. * @v count Number of packets to send (or zero for no limit)
  60. * @v quiet Inhibit output
  61. * @ret rc Return status code
  62. */
  63. int ping ( const char *hostname, unsigned long timeout, size_t len,
  64. unsigned int count, int quiet ) {
  65. int rc;
  66. /* Create pinger */
  67. if ( ( rc = create_pinger ( &monojob, hostname, timeout, len, count,
  68. ( quiet ? NULL : ping_callback ) ) ) != 0 ){
  69. printf ( "Could not start ping: %s\n", strerror ( rc ) );
  70. return rc;
  71. }
  72. /* Wait for ping to complete */
  73. if ( ( rc = monojob_wait ( NULL, 0 ) ) != 0 ) {
  74. if ( ! quiet )
  75. printf ( "Finished: %s\n", strerror ( rc ) );
  76. return rc;
  77. }
  78. return 0;
  79. }