Debian Codegraph @

ping/libping.c

raw
1 /*
2 Copyright (C) 2008-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 #include <config.h>
20
21 #include <sys/param.h>
22 #include <sys/socket.h>
23 #include <sys/file.h>
24 #include <sys/time.h>
25
26 #include <netinet/in.h>
27 /*#include <netinet/ip_icmp.h> -- deliberately not including this */
28 #include <arpa/inet.h>
29 #include <netdb.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <string.h>
35 #if defined HAVE_IDN2_H && defined HAVE_IDN2
36 # include <idn2.h>
37 #elif defined HAVE_IDNA_H
38 # include <idna.h>
39 #endif
40
41 #include <timespec.h>
42
43 #include "ping.h"
44
45 static int useless_ident = 0; /* Relevant at least for Linux. */
46
47 static size_t _ping_packetsize (PING * p);
48
49 size_t
50 _ping_packetsize (PING *p)
51 {
52 if (p->ping_type == ICMP_TIMESTAMP || p->ping_type == ICMP_TIMESTAMPREPLY)
53 return ICMP_TSLEN;
54
55 if (p->ping_type == ICMP_ADDRESS || p->ping_type == ICMP_ADDRESSREPLY)
56 return ICMP_MASKLEN;
57
58 return PING_HEADER_LEN + p->ping_datalen;
59 }
60
61 PING *
62 ping_init (int type, int ident)
63 {
64 int fd;
65 struct protoent *proto;
66 PING *p;
67
68 /* Initialize raw ICMP socket */
69 proto = getprotobyname ("icmp");
70 if (!proto)
71 {
72 fprintf (stderr, "ping: unknown protocol icmp.\n");
73 return NULL;
74 }
75
76 fd = socket (AF_INET, SOCK_RAW, proto->p_proto);
77 if (fd < 0)
78 {
79 if (errno == EPERM || errno == EACCES)
80 {
81 errno = 0;
82
83 /* At least Linux can allow subprivileged users to send ICMP
84 * packets formally encapsulated and built as a datagram socket,
85 * but then the identity number is set by the kernel itself.
86 */
87 fd = socket (AF_INET, SOCK_DGRAM, proto->p_proto);
88 if (fd < 0)
89 {
90 if (errno == EPERM || errno == EACCES
91 || errno == EPROTONOSUPPORT)
92 fprintf (stderr,
93 "ping: Lacking privilege for icmp socket.\n");
94 else
95 fprintf (stderr, "ping: %s\n", strerror (errno));
96
97 return NULL;
98 }
99
100 useless_ident++; /* SOCK_DGRAM overrides our set identity. */
101 }
102 else
103 return NULL;
104 }
105
106 /* Allocate PING structure and initialize it to default values */
107 p = malloc (sizeof (*p));
108 if (!p)
109 {
110 close (fd);
111 return p;
112 }
113
114 memset (p, 0, sizeof (*p));
115
116 p->ping_fd = fd;
117 p->ping_type = type;
118 p->ping_count = 0;
119 p->ping_interval = PING_DEFAULT_INTERVAL;
120 p->ping_datalen = sizeof (icmphdr_t);
121 /* Make sure we use only 16 bits in this field, id for icmp is a unsigned short. */
122 p->ping_ident = ident & 0xFFFF;
123 p->ping_cktab_size = PING_CKTABSIZE;
124 p->ping_start_time = current_timespec ();
125 return p;
126 }
127
128 void
129 ping_reset (PING *p)
130 {
131 p->ping_num_xmit = 0;
132 p->ping_num_recv = 0;
133 p->ping_num_rept = 0;
134 }
135
136 void
137 ping_set_type (PING *p, int type)
138 {
139 p->ping_type = type;
140 }
141
142 int
143 ping_xmit (PING *p)
144 {
145 int i, buflen;
146
147 if (_ping_setbuf (p, USE_IPV6))
148 return -1;
149
150 buflen = _ping_packetsize (p);
151
152 /* Mark sequence number as sent */
153 _PING_CLR (p, p->ping_num_xmit);
154
155 /* Encode ICMP header */
156 switch (p->ping_type)
157 {
158 case ICMP_ECHO:
159 icmp_echo_encode (p->ping_buffer, buflen, p->ping_ident,
160 p->ping_num_xmit);
161 break;
162
163 case ICMP_TIMESTAMP:
164 icmp_timestamp_encode (p->ping_buffer, buflen, p->ping_ident,
165 p->ping_num_xmit);
166 break;
167
168 case ICMP_ADDRESS:
169 icmp_address_encode (p->ping_buffer, buflen, p->ping_ident,
170 p->ping_num_xmit);
171 break;
172
173 default:
174 icmp_generic_encode (p->ping_buffer, buflen, p->ping_type,
175 p->ping_ident, p->ping_num_xmit);
176 break;
177 }
178
179 i = sendto (p->ping_fd, (char *) p->ping_buffer, buflen, 0,
180 (struct sockaddr *) &p->ping_dest.ping_sockaddr,
181 sizeof (struct sockaddr_in));
182 if (i < 0)
183 return -1;
184 else
185 {
186 p->ping_num_xmit++;
187 if (i != buflen)
188 printf ("ping: wrote %s %d chars, ret=%d\n",
189 p->ping_hostname, buflen, i);
190 }
191 return 0;
192 }
193
194 static int
195 my_echo_reply (PING *p, icmphdr_t *icmp)
196 {
197 struct ip *orig_ip = &icmp->icmp_ip;
198 icmphdr_t *orig_icmp = (icmphdr_t *) (orig_ip + 1);
199
200 return (orig_ip->ip_dst.s_addr == p->ping_dest.ping_sockaddr.sin_addr.s_addr
201 && orig_ip->ip_p == IPPROTO_ICMP
202 && orig_icmp->icmp_type == ICMP_ECHO
203 && (ntohs (orig_icmp->icmp_id) == p->ping_ident || useless_ident));
204 }
205
206 int
207 ping_recv (PING *p)
208 {
209 socklen_t fromlen = sizeof (p->ping_from.ping_sockaddr);
210 int n, rc;
211 icmphdr_t *icmp;
212 struct ip *ip;
213 int dupflag;
214
215 n = recvfrom (p->ping_fd,
216 (char *) p->ping_buffer, _PING_BUFLEN (p, USE_IPV6), 0,
217 (struct sockaddr *) &p->ping_from.ping_sockaddr, &fromlen);
218 if (n < 0)
219 return -1;
220
221 rc = icmp_generic_decode (p->ping_buffer, n, &ip, &icmp);
222 if (rc < 0)
223 {
224 /*FIXME: conditional */
225 fprintf (stderr, "packet too short (%d bytes) from %s\n", n,
226 inet_ntoa (p->ping_from.ping_sockaddr.sin_addr));
227 return -1;
228 }
229
230 switch (icmp->icmp_type)
231 {
232 case ICMP_ECHOREPLY:
233 case ICMP_TIMESTAMPREPLY:
234 case ICMP_ADDRESSREPLY:
235 /* case ICMP_ROUTERADV: */
236
237 if (ntohs (icmp->icmp_id) != p->ping_ident && useless_ident == 0)
238 return -1;
239
240 if (rc)
241 fprintf (stderr, "checksum mismatch from %s\n",
242 inet_ntoa (p->ping_from.ping_sockaddr.sin_addr));
243
244 p->ping_num_recv++;
245 if (_PING_TST (p, ntohs (icmp->icmp_seq)))
246 {
247 p->ping_num_rept++;
248 p->ping_num_recv--;
249 dupflag = 1;
250 }
251 else
252 {
253 _PING_SET (p, ntohs (icmp->icmp_seq));
254 dupflag = 0;
255 }
256
257 if (p->ping_event.handler)
258 (*p->ping_event.handler) (dupflag ? PEV_DUPLICATE : PEV_RESPONSE,
259 p->ping_closure,
260 &p->ping_dest.ping_sockaddr,
261 &p->ping_from.ping_sockaddr, ip, icmp, n);
262 break;
263
264 case ICMP_ECHO:
265 case ICMP_TIMESTAMP:
266 case ICMP_ADDRESS:
267 return -1;
268
269 default:
270 if (!my_echo_reply (p, icmp))
271 return -1;
272
273 if (p->ping_event.handler)
274 (*p->ping_event.handler) (PEV_NOECHO,
275 p->ping_closure,
276 &p->ping_dest.ping_sockaddr,
277 &p->ping_from.ping_sockaddr, ip, icmp, n);
278 }
279 return 0;
280 }
281
282 void
283 ping_set_event_handler (PING *ping, ping_efp pf, void *closure)
284 {
285 ping->ping_event.handler = pf;
286 ping->ping_closure = closure;
287 }
288
289 void
290 ping_set_packetsize (PING *ping, size_t size)
291 {
292 ping->ping_datalen = size;
293 }
294
295 int
296 ping_set_dest (PING *ping, const char *host)
297 {
298 #if HAVE_DECL_GETADDRINFO
299 int rc;
300 struct addrinfo hints, *res;
301 char *rhost;
302
303 # if defined HAVE_IDN || defined HAVE_IDN2
304 rc = idna_to_ascii_lz (host, &rhost, 0); /* RHOST is allocated. */
305 if (rc)
306 return 1;
307 host = rhost;
308 # else
309 rhost = NULL;
310 # endif
311
312 memset (&hints, 0, sizeof (hints));
313 hints.ai_family = AF_INET;
314 hints.ai_flags = AI_CANONNAME;
315 # ifdef AI_IDN
316 hints.ai_flags |= AI_IDN;
317 # endif
318 # ifdef AI_CANONIDN
319 hints.ai_flags |= AI_CANONIDN;
320 # endif
321
322 rc = getaddrinfo (host, NULL, &hints, &res);
323
324 if (rc)
325 {
326 free (rhost);
327 return 1;
328 }
329
330 memcpy (&ping->ping_dest.ping_sockaddr, res->ai_addr, res->ai_addrlen);
331 if (res->ai_canonname)
332 ping->ping_hostname = strdup (res->ai_canonname);
333 else
334 # if defined HAVE_IDN || defined HAVE_IDN2
335 ping->ping_hostname = host;
336 # else
337 ping->ping_hostname = strdup (host);
338 # endif
339
340 freeaddrinfo (res);
341
342 return 0;
343 #else /* !HAVE_DECL_GETADDRINFO */
344
345 struct sockaddr_in *s_in = &ping->ping_dest.ping_sockaddr;
346 s_in->sin_family = AF_INET;
347 # ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
348 s_in->sin_len = sizeof (*s_in);
349 # endif
350 if (inet_aton (host, &s_in->sin_addr))
351 ping->ping_hostname = strdup (host);
352 else
353 {
354 struct hostent *hp;
355 # if defined HAVE_IDN || defined HAVE_IDN2
356 char *rhost;
357 int rc;
358
359 rc = idna_to_ascii_lz (host, &rhost, 0);
360 if (rc)
361 return 1;
362 hp = gethostbyname (rhost);
363 free (rhost);
364 # else/* !HAVE_IDN && !HAVE_IDN2 */
365 hp = gethostbyname (host);
366 # endif
367 if (!hp)
368 return 1;
369
370 s_in->sin_family = hp->h_addrtype;
371 if (hp->h_length > (int) sizeof (s_in->sin_addr))
372 hp->h_length = sizeof (s_in->sin_addr);
373
374 memcpy (&s_in->sin_addr, hp->h_addr, hp->h_length);
375 ping->ping_hostname = strdup (hp->h_name);
376 }
377 return 0;
378 #endif /* !HAVE_DECL_GETADDRINFO */
379 }