4 * Copyright (C) 2001 Jeffrey Fulmer <jdfulmer@armstrong.com>
5 * This file is part of LIBPING
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <sys/types.h>
25 #include <sys/socket.h>
30 #define MAXPACKET 65535
32 #define HDRLEN ICMP_MINLEN
33 #define DATALEN (PKTSIZE-HDRLEN)
34 #define MAXDATA (MAXPKT-HDRLEN-TIMLEN)
40 JOEfreeprotoent( struct protoent *p )
44 if( p->p_aliases != NULL ){
45 for( a = p->p_aliases; *a != NULL; a++ ){
53 JOEfreehostent( struct hostent *h )
58 if( h->h_aliases != NULL ){
59 for( p = h->h_aliases; *p != NULL; ++p )
63 if( h->h_addr_list != NULL ){
64 for( p = h->h_addr_list; *p != NULL; ++p )
66 free (h->h_addr_list);
72 in_checksum( u_short *buf, int len )
74 register long sum = 0;
83 *( u_char* )( &answer ) = *( u_char* )buf;
86 sum = ( sum >> 16 ) + ( sum & 0xffff );
95 send_ping( const char *host, struct sockaddr_in *taddr, struct ping_priv * datum )
99 unsigned char buf[ HDRLEN + DATALEN ];
101 const int proto_buf_len = 1024;
102 char proto_buf[proto_buf_len];
103 struct protoent *proto = NULL;
104 struct protoent proto_datum;
106 struct hostent *hp = NULL;
119 len = HDRLEN + DATALEN;
121 #if defined(__GLIBC__)
122 /* for systems using GNU libc */
123 getprotobyname_r("icmp", &proto_datum, proto_buf, proto_buf_len, &proto);
124 if(( gethostbyname_r( host, &hent, hbf, sizeof(hbf), &hp, &herrno ) < 0 )){
129 proto = getprotobyname_r("icmp", &proto_datum, proto_buf, proto_buf_len);
130 hp = gethostbyname_r( host, &hent, hbf, sizeof(hbf), &herrno );
132 aixbuf = (char*)xmalloc( 9000 );
133 probuf = (char*)xmalloc( 9000 );
134 rc = getprotobyname_r( "icmp", &proto,
135 ( struct protoent_data *)(probuf + sizeof( struct protoent)));
136 rc = gethostbyname_r ( host, (struct hostent *)aixbuf,
137 (struct hostent_data *)(aixbuf + sizeof(struct hostent)));
138 hp = (struct hostent*)aixbuf;
139 #elif ( defined(hpux) || defined(__osf__) )
140 proto = getprotobyname( "icmp" );
141 hp = gethostbyname( host );
144 /* simply hoping that get*byname is thread-safe */
145 proto = getprotobyname( "icmp" );
146 hp = gethostbyname( host );
148 #endif/*OS SPECIFICS*/
150 if( proto == NULL ) {
155 memcpy( &taddr->sin_addr, hp->h_addr_list[0], sizeof( taddr->sin_addr ));
157 taddr->sin_family = AF_INET;
159 else if( inet_aton( host, &taddr->sin_addr ) == 0 ){
163 last = ntohl( taddr->sin_addr.s_addr ) & 0xFF;
164 if(( last == 0x00 ) || ( last == 0xFF )){
168 if(( datum->sock = socket( AF_INET, SOCK_RAW, proto->p_proto )) < 0 ){
175 memset(buf, 0, sizeof(buf));
176 icp = (struct icmp *)buf;
177 icp->icmp_type = ICMP_ECHO;
180 icp->icmp_id = getpid() & 0xFFFF;
181 icp->icmp_seq = icp->icmp_id; /* FIXME this is not nice.. */
182 icp->icmp_cksum = in_checksum((u_short *)icp, len );
184 if(( ss = sendto( datum->sock, buf, sizeof( buf ), 0,
185 (struct sockaddr*)taddr, sizeof( *taddr ))) < 0 ){
193 perror( "malformed packet" );
202 /* JOEfreeprotoent( proto ); */
203 /* JOEfreeprotoent( &proto_datum ); */
204 /* JOEfreehostent( hp ); */
205 /* JOEfreehostent( &hent ); */
210 recv_ping( struct sockaddr_in *taddr, struct ping_priv * datum )
215 unsigned char buf[ HDRLEN + DATALEN ];
217 struct sockaddr_in faddr;
221 to.tv_sec = datum->timo / 100000;
222 to.tv_usec = ( datum->timo - ( to.tv_sec * 100000 ) ) * 10;
225 FD_SET( datum->sock, &readset );
226 /* we use select to see if there is any activity
227 on the socket. If not, then we've requested an
228 unreachable network and we'll time out here. */
229 if(( nf = select( datum->sock + 1, &readset, NULL, NULL, &to )) < 0 ){
240 len = HDRLEN + DATALEN;
241 from = sizeof( faddr );
243 cc = recvfrom( datum->sock, buf, len, 0, (struct sockaddr*)&faddr, &from );
247 perror( "recvfrom" );
252 icp = (struct icmp *)(buf + HDRLEN + DATALEN );
253 if( faddr.sin_addr.s_addr != taddr->sin_addr.s_addr ){
257 if( icp->icmp_id != ( getpid() & 0xFFFF )){
258 printf( "id: %d\n", icp->icmp_id );
266 myping( const char *hostname, int t , struct ping_priv * datum)
270 struct sockaddr_in sa;
271 struct timeval mytime;
273 datum->ident = getpid() & 0xFFFF;
275 if( t == 0 ) datum->timo = 2;
276 else datum->timo = t;
280 (void) gettimeofday( &mytime, (struct timezone *)NULL);
281 if(( err = send_ping( hostname, &sa, datum)) < 0 ){
282 close( datum->sock );
286 rrt = elapsed_time( &mytime );
290 if (datum->rrt > datum->timo * 1000 ) {
291 close( datum->sock );
294 } while( recv_ping( &sa, datum ));
295 close( datum->sock );
301 pinghost( const char *hostname )
303 struct ping_priv datum = ping_priv_default();
304 return myping( hostname, 0, &datum );
308 pingthost( const char *hostname, int t )
310 struct ping_priv datum = ping_priv_default();
311 return myping( hostname, t, &datum );
315 tpinghost( const char *hostname )
318 struct ping_priv datum = ping_priv_default();
320 ret = myping( hostname, 0, &datum );
327 tpingthost( const char *hostname, int t )
330 struct ping_priv datum = ping_priv_default();
332 ret = myping( hostname, t, &datum );