您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

pingmgmt.c 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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, or NULL
  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, ( peer ? sock_ntoa ( peer ) : "<none>" ), 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. * @v count Number of packets to send (or zero for no limit)
  56. * @v quiet Inhibit output
  57. * @ret rc Return status code
  58. */
  59. int ping ( const char *hostname, unsigned long timeout, size_t len,
  60. unsigned int count, int quiet ) {
  61. int rc;
  62. /* Create pinger */
  63. if ( ( rc = create_pinger ( &monojob, hostname, timeout, len, count,
  64. ( quiet ? NULL : ping_callback ) ) ) != 0 ){
  65. printf ( "Could not start ping: %s\n", strerror ( rc ) );
  66. return rc;
  67. }
  68. /* Wait for ping to complete */
  69. if ( ( rc = monojob_wait ( NULL, 0 ) ) != 0 ) {
  70. if ( ! quiet )
  71. printf ( "Finished: %s\n", strerror ( rc ) );
  72. return rc;
  73. }
  74. return 0;
  75. }