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.0KB

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