1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/* Copyright (C) 1998, 2001, 2005, 2007, 2008 Free Software Foundation, Inc.
This file is part of GNU Inetutils.
GNU Inetutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Inetutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Inetutils; see the file COPYING. If not, write
to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA 02110-1301 USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/time.h>
#include <signal.h>
/*#include <netinet/ip_icmp.h> -- deliberately not including this */
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "ping.h"
static size_t _ping_packetsize (PING * p);
size_t
_ping_packetsize (PING * p)
{
if (p->ping_type == ICMP_TIMESTAMP || p->ping_type == ICMP_TIMESTAMPREPLY)
return 20;
return 8 + p->ping_datalen;
}
PING *
ping_init (int type, int ident)
{
int fd;
struct protoent *proto;
PING *p;
/* Initialize raw ICMP socket */
proto = getprotobyname ("icmp");
if (!proto)
{
fprintf (stderr, "ping: unknown protocol icmp.\n");
return NULL;
}
fd = socket (AF_INET, SOCK_RAW, proto->p_proto);
if (fd < 0)
{
if (errno == EPERM)
fprintf (stderr, "ping: ping must run as root\n");
return NULL;
}
/* Allocate PING structure and initialize it to default values */
p = malloc (sizeof (*p));
if (!p)
{
close (fd);
return p;
}
memset (p, 0, sizeof (*p));
p->ping_fd = fd;
p->ping_type = type;
p->ping_count = 0;
p->ping_interval = PING_DEFAULT_INTERVAL;
p->ping_datalen = sizeof (icmphdr_t);
/* Make sure we use only 16 bits in this field, id for icmp is a u_short. */
p->ping_ident = ident & 0xFFFF;
p->ping_cktab_size = PING_CKTABSIZE;
return p;
}
void
ping_reset (PING * p)
{
p->ping_num_xmit = 0;
p->ping_num_recv = 0;
p->ping_num_rept = 0;
}
void
ping_set_type (PING * p, int type)
{
p->ping_type = type;
}
int
ping_xmit (PING * p)
{
int i, buflen;
if (_ping_setbuf (p, USE_IPV6))
return -1;
buflen = _ping_packetsize (p);
/* Mark sequence number as sent */
_PING_CLR (p, p->ping_num_xmit % p->ping_cktab_size);
/* Encode ICMP header */
switch (p->ping_type)
{
case ICMP_ECHO:
icmp_echo_encode (p->ping_buffer, buflen, p->ping_ident,
p->ping_num_xmit);
break;
case ICMP_TIMESTAMP:
icmp_timestamp_encode (p->ping_buffer, buflen, p->ping_ident,
p->ping_num_xmit);
break;
case ICMP_ADDRESS:
icmp_address_encode (p->ping_buffer, buflen, p->ping_ident,
p->ping_num_xmit);
break;
default:
icmp_generic_encode (p->ping_buffer, buflen, p->ping_type,
p->ping_ident, p->ping_num_xmit);
break;
}
i = sendto (p->ping_fd, (char *) p->ping_buffer, buflen, 0,
(struct sockaddr *) &p->ping_dest.ping_sockaddr, sizeof (struct sockaddr_in));
if (i < 0)
perror ("ping: sendto");
else
{
p->ping_num_xmit++;
if (i != buflen)
printf ("ping: wrote %s %d chars, ret=%d\n",
p->ping_hostname, buflen, i);
}
return 0;
}
static int
my_echo_reply (PING * p, icmphdr_t * icmp)
{
struct ip *orig_ip = &icmp->icmp_ip;
icmphdr_t *orig_icmp = (icmphdr_t *) (orig_ip + 1);
return (orig_ip->ip_dst.s_addr == p->ping_dest.ping_sockaddr.sin_addr.s_addr
&& orig_ip->ip_p == IPPROTO_ICMP
&& orig_icmp->icmp_type == ICMP_ECHO
&& orig_icmp->icmp_id == p->ping_ident);
}
int
ping_recv (PING * p)
{
int fromlen = sizeof (p->ping_from.ping_sockaddr);
int n, rc;
icmphdr_t *icmp;
struct ip *ip;
int dupflag;
n = recvfrom (p->ping_fd,
(char *) p->ping_buffer, _PING_BUFLEN (p, USE_IPV6), 0,
(struct sockaddr *) &p->ping_from.ping_sockaddr, &fromlen);
if (n < 0)
return -1;
rc = icmp_generic_decode (p->ping_buffer, n, &ip, &icmp);
if (rc < 0)
{
/*FIXME: conditional */
fprintf (stderr, "packet too short (%d bytes) from %s\n", n,
inet_ntoa (p->ping_from.ping_sockaddr.sin_addr));
return -1;
}
switch (icmp->icmp_type)
{
case ICMP_ECHOREPLY:
case ICMP_TIMESTAMPREPLY:
case ICMP_ADDRESSREPLY:
/* case ICMP_ROUTERADV: */
if (icmp->icmp_id != p->ping_ident)
return -1;
if (rc)
fprintf (stderr, "checksum mismatch from %s\n",
inet_ntoa (p->ping_from.ping_sockaddr.sin_addr));
p->ping_num_recv++;
if (_PING_TST (p, icmp->icmp_seq % p->ping_cktab_size))
{
p->ping_num_rept++;
p->ping_num_recv--;
dupflag = 1;
}
else
{
_PING_SET (p, icmp->icmp_seq % p->ping_cktab_size);
dupflag = 0;
}
if (p->ping_event.handler)
(*p->ping_event.handler) (dupflag ? PEV_DUPLICATE : PEV_RESPONSE,
p->ping_closure,
&p->ping_dest.ping_sockaddr,
&p->ping_from.ping_sockaddr, ip, icmp, n);
break;
case ICMP_ECHO:
case ICMP_TIMESTAMP:
case ICMP_ADDRESS:
return -1;
default:
if (!my_echo_reply (p, icmp))
return -1;
if (p->ping_event.handler)
(*p->ping_event.handler) (PEV_NOECHO,
p->ping_closure,
&p->ping_dest.ping_sockaddr,
&p->ping_from.ping_sockaddr, ip, icmp, n);
}
return 0;
}
void
ping_set_event_handler (PING * ping, ping_efp pf, void *closure)
{
ping->ping_event.handler = pf;
ping->ping_closure = closure;
}
void
ping_set_packetsize (PING * ping, size_t size)
{
ping->ping_datalen = size;
}
int
ping_set_dest (PING * ping, char *host)
{
struct sockaddr_in *s_in = &ping->ping_dest.ping_sockaddr;
s_in->sin_family = AF_INET;
if (inet_aton (host, &s_in->sin_addr))
ping->ping_hostname = strdup (host);
else
{
struct hostent *hp = gethostbyname (host);
if (!hp)
return 1;
s_in->sin_family = hp->h_addrtype;
if (hp->h_length > (int) sizeof (s_in->sin_addr))
hp->h_length = sizeof (s_in->sin_addr);
memcpy (&s_in->sin_addr, hp->h_addr, hp->h_length);
ping->ping_hostname = strdup (hp->h_name);
}
return 0;
}
|