~ubuntu-branches/ubuntu/gutsy/dhcp/gutsy

« back to all changes in this revision

Viewing changes to common/icmp.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2004-10-30 21:07:22 UTC
  • Revision ID: james.westby@ubuntu.com-20041030210722-p0u4682vtncfxnnn
Tags: upstream-2.0pl5
ImportĀ upstreamĀ versionĀ 2.0pl5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* icmp.c
 
2
 
 
3
   ICMP Protocol engine - for sending out pings and receiving
 
4
   responses. */
 
5
 
 
6
/*
 
7
 * Copyright (c) 1997, 1998 The Internet Software Consortium.
 
8
 * All rights reserved.
 
9
 *
 
10
 * Redistribution and use in source and binary forms, with or without
 
11
 * modification, are permitted provided that the following conditions
 
12
 * are met:
 
13
 *
 
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.
 
22
 *
 
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
 
35
 * SUCH DAMAGE.
 
36
 *
 
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''.
 
42
 */
 
43
 
 
44
#ifndef lint
 
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";
 
47
#endif /* not lint */
 
48
 
 
49
#include "dhcpd.h"
 
50
#include "netinet/ip.h"
 
51
#include "netinet/ip_icmp.h"
 
52
 
 
53
static int icmp_protocol_initialized;
 
54
static int icmp_protocol_fd;
 
55
 
 
56
/* Initialize the ICMP protocol. */
 
57
 
 
58
void icmp_startup (routep, handler)
 
59
        int routep;
 
60
        void (*handler) PROTO ((struct iaddr, u_int8_t *, int));
 
61
{
 
62
        struct protoent *proto;
 
63
        int protocol = 1;
 
64
        int state;
 
65
 
 
66
        /* Only initialize icmp once. */
 
67
        if (icmp_protocol_initialized)
 
68
                error ("attempted to reinitialize icmp protocol");
 
69
        icmp_protocol_initialized = 1;
 
70
 
 
71
        /* Get the protocol number (should be 1). */
 
72
        proto = getprotobyname ("icmp");
 
73
        if (proto)
 
74
                protocol = proto -> p_proto;
 
75
 
 
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");
 
80
 
 
81
        /* Make sure it does routing... */
 
82
        state = 0;
 
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");
 
86
 
 
87
        add_protocol ("icmp", icmp_protocol_fd, icmp_echoreply,
 
88
                      (void *)handler);
 
89
}
 
90
 
 
91
int icmp_echorequest (addr)
 
92
        struct iaddr *addr;
 
93
{
 
94
        struct sockaddr_in to;
 
95
        struct icmp icmp;
 
96
        int status;
 
97
 
 
98
        if (!icmp_protocol_initialized)
 
99
                error ("attempt to use ICMP protocol before initialization.");
 
100
 
 
101
#ifdef HAVE_SA_LEN
 
102
        to.sin_len = sizeof to;
 
103
#endif
 
104
        to.sin_family = AF_INET;
 
105
        to.sin_port = 0; /* unused. */
 
106
        memcpy (&to.sin_addr, addr -> iabuf, sizeof to.sin_addr); /* XXX */
 
107
 
 
108
        icmp.icmp_type = ICMP_ECHO;
 
109
        icmp.icmp_code = 0;
 
110
        icmp.icmp_cksum = 0;
 
111
        icmp.icmp_seq = 0;
 
112
#ifdef PTRSIZE_64BIT
 
113
        icmp.icmp_id = (((u_int32_t)(u_int64_t)addr) ^
 
114
                        (u_int32_t)(((u_int64_t)addr) >> 32));
 
115
#else
 
116
        icmp.icmp_id = (u_int32_t)addr;
 
117
#endif
 
118
 
 
119
        icmp.icmp_cksum = wrapsum (checksum ((unsigned char *)&icmp,
 
120
                                             sizeof icmp, 0));
 
121
 
 
122
        /* Send the ICMP packet... */
 
123
        status = sendto (icmp_protocol_fd, (char *)&icmp, sizeof icmp, 0,
 
124
                         (struct sockaddr *)&to, sizeof to);
 
125
        if (status < 0)
 
126
                warn ("icmp_echorequest %s: %m", inet_ntoa(to.sin_addr));
 
127
 
 
128
        if (status != sizeof icmp)
 
129
                return 0;
 
130
        return 1;
 
131
}
 
132
 
 
133
void icmp_echoreply (protocol)
 
134
        struct protocol *protocol;
 
135
{
 
136
        struct icmp *icfrom;
 
137
        struct sockaddr_in from;
 
138
        u_int8_t icbuf [1500];
 
139
        int status;
 
140
        int len;
 
141
        struct iaddr ia;
 
142
        void (*handler) PROTO ((struct iaddr, u_int8_t *, int));
 
143
 
 
144
        len = sizeof from;
 
145
        status = recvfrom (protocol -> fd, (char *)icbuf, sizeof icbuf, 0,
 
146
                          (struct sockaddr *)&from, &len);
 
147
        if (status < 0) {
 
148
                warn ("icmp_echoreply: %m");
 
149
                return;
 
150
        }
 
151
 
 
152
        /* Probably not for us. */
 
153
        if (status < (sizeof (struct ip)) + (sizeof *icfrom)) {
 
154
                return;
 
155
        }
 
156
 
 
157
        len = status - sizeof (struct ip);
 
158
        icfrom = (struct icmp *)(icbuf + sizeof (struct ip));
 
159
 
 
160
        /* Silently discard ICMP packets that aren't echoreplies. */
 
161
        if (icfrom -> icmp_type != ICMP_ECHOREPLY) {
 
162
                return;
 
163
        }
 
164
 
 
165
        /* If we were given a second-stage handler, call it. */
 
166
        if (protocol -> local) {
 
167
                handler = ((void (*) PROTO ((struct iaddr,
 
168
                                            u_int8_t *, int)))
 
169
                           protocol -> local);
 
170
                memcpy (ia.iabuf, &from.sin_addr, sizeof from.sin_addr);
 
171
                ia.len = sizeof from.sin_addr;
 
172
 
 
173
                (*handler) (ia, icbuf, len);
 
174
        }
 
175
}