3
ICMP Protocol engine - for sending out pings and receiving
7
* Copyright (c) 1997, 1998 The Internet Software Consortium.
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
19
* 3. Neither the name of The Internet Software Consortium nor the names
20
* of its contributors may be used to endorse or promote products derived
21
* from this software without specific prior written permission.
23
* THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
* DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37
* This software has been written for the Internet Software Consortium
38
* by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
39
* Enterprises. To learn more about the Internet Software Consortium,
40
* see ``http://www.vix.com/isc''. To learn more about Vixie
41
* Enterprises, see ``http://www.vix.com''.
45
static char copyright[] =
46
"$Id: icmp.c,v 1.7.2.2 1999/03/29 23:20:00 mellon Exp $ Copyright (c) 1997, 1998 The Internet Software Consortium. All rights reserved.\n";
50
#include "netinet/ip.h"
51
#include "netinet/ip_icmp.h"
53
static int icmp_protocol_initialized;
54
static int icmp_protocol_fd;
56
/* Initialize the ICMP protocol. */
58
void icmp_startup (routep, handler)
60
void (*handler) PROTO ((struct iaddr, u_int8_t *, int));
62
struct protoent *proto;
66
/* Only initialize icmp once. */
67
if (icmp_protocol_initialized)
68
error ("attempted to reinitialize icmp protocol");
69
icmp_protocol_initialized = 1;
71
/* Get the protocol number (should be 1). */
72
proto = getprotobyname ("icmp");
74
protocol = proto -> p_proto;
76
/* Get a raw socket for the ICMP protocol. */
77
icmp_protocol_fd = socket (AF_INET, SOCK_RAW, protocol);
78
if (icmp_protocol_fd < 0)
79
error ("unable to create icmp socket: %m");
81
/* Make sure it does routing... */
83
if (setsockopt (icmp_protocol_fd, SOL_SOCKET, SO_DONTROUTE,
84
(char *)&state, sizeof state) < 0)
85
error ("Unable to disable SO_DONTROUTE on ICMP socket: %m");
87
add_protocol ("icmp", icmp_protocol_fd, icmp_echoreply,
91
int icmp_echorequest (addr)
94
struct sockaddr_in to;
98
if (!icmp_protocol_initialized)
99
error ("attempt to use ICMP protocol before initialization.");
102
to.sin_len = sizeof to;
104
to.sin_family = AF_INET;
105
to.sin_port = 0; /* unused. */
106
memcpy (&to.sin_addr, addr -> iabuf, sizeof to.sin_addr); /* XXX */
108
icmp.icmp_type = ICMP_ECHO;
113
icmp.icmp_id = (((u_int32_t)(u_int64_t)addr) ^
114
(u_int32_t)(((u_int64_t)addr) >> 32));
116
icmp.icmp_id = (u_int32_t)addr;
119
icmp.icmp_cksum = wrapsum (checksum ((unsigned char *)&icmp,
122
/* Send the ICMP packet... */
123
status = sendto (icmp_protocol_fd, (char *)&icmp, sizeof icmp, 0,
124
(struct sockaddr *)&to, sizeof to);
126
warn ("icmp_echorequest %s: %m", inet_ntoa(to.sin_addr));
128
if (status != sizeof icmp)
133
void icmp_echoreply (protocol)
134
struct protocol *protocol;
137
struct sockaddr_in from;
138
u_int8_t icbuf [1500];
142
void (*handler) PROTO ((struct iaddr, u_int8_t *, int));
145
status = recvfrom (protocol -> fd, (char *)icbuf, sizeof icbuf, 0,
146
(struct sockaddr *)&from, &len);
148
warn ("icmp_echoreply: %m");
152
/* Probably not for us. */
153
if (status < (sizeof (struct ip)) + (sizeof *icfrom)) {
157
len = status - sizeof (struct ip);
158
icfrom = (struct icmp *)(icbuf + sizeof (struct ip));
160
/* Silently discard ICMP packets that aren't echoreplies. */
161
if (icfrom -> icmp_type != ICMP_ECHOREPLY) {
165
/* If we were given a second-stage handler, call it. */
166
if (protocol -> local) {
167
handler = ((void (*) PROTO ((struct iaddr,
170
memcpy (ia.iabuf, &from.sin_addr, sizeof from.sin_addr);
171
ia.len = sizeof from.sin_addr;
173
(*handler) (ia, icbuf, len);