iPXE
pingmgmt.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <ipxe/pinger.h>
00030 #include <ipxe/monojob.h>
00031 #include <ipxe/timer.h>
00032 #include <usr/pingmgmt.h>
00033 
00034 /** @file
00035  *
00036  * ICMP ping management
00037  *
00038  */
00039 
00040 /**
00041  * Display ping result
00042  *
00043  * @v src               Source socket address, or NULL
00044  * @v sequence          Sequence number
00045  * @v len               Payload length
00046  * @v rc                Status code
00047  */
00048 static void ping_callback ( struct sockaddr *peer, unsigned int sequence,
00049                             size_t len, int rc ) {
00050 
00051         /* Display ping response */
00052         printf ( "%zd bytes from %s: seq=%d",
00053                  len, ( peer ? sock_ntoa ( peer ) : "<none>" ), sequence );
00054         if ( rc != 0 )
00055                 printf ( ": %s", strerror ( rc ) );
00056         printf ( "\n" );
00057 }
00058 
00059 /**
00060  * Ping a host
00061  *
00062  * @v hostname          Hostname
00063  * @v timeout           Timeout between pings, in ticks
00064  * @v len               Payload length
00065  * @v count             Number of packets to send (or zero for no limit)
00066  * @v quiet             Inhibit output
00067  * @ret rc              Return status code
00068  */
00069 int ping ( const char *hostname, unsigned long timeout, size_t len,
00070            unsigned int count, int quiet ) {
00071         int rc;
00072 
00073         /* Create pinger */
00074         if ( ( rc = create_pinger ( &monojob, hostname, timeout, len, count,
00075                                     ( quiet ? NULL : ping_callback ) ) ) != 0 ){
00076                 printf ( "Could not start ping: %s\n", strerror ( rc ) );
00077                 return rc;
00078         }
00079 
00080         /* Wait for ping to complete */
00081         if ( ( rc = monojob_wait ( NULL, 0 ) ) != 0 ) {
00082                 if ( ! quiet )
00083                         printf ( "Finished: %s\n", strerror ( rc ) );
00084                 return rc;
00085         }
00086 
00087         return 0;
00088 }