Debian Codegraph @

ping/ping_common.c

raw
1 /*
2 Copyright (C) 2005-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/types.h>
22 #include <sys/socket.h>
23 #include <sys/time.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netdb.h>
31 #include <xalloc.h>
32 #include <attribute.h>
33 #include <timespec.h>
34
35 #include "ping_common.h"
36
37 extern unsigned char *data_buffer;
38 extern size_t data_length;
39
40 static void _ping_freebuf (PING * p);
41 extern unsigned int options;
42
43 size_t
44 ping_cvt_number (const char *optarg, size_t maxval, int allow_zero)
45 {
46 char *p;
47 unsigned long int n;
48
49 n = strtoul (optarg, &p, 0);
50 if (*p)
51 error (EXIT_FAILURE, 0, "invalid value (`%s' near `%s')", optarg, p);
52
53 if (n == 0 && !allow_zero)
54 error (EXIT_FAILURE, 0, "option value too small: %s", optarg);
55
56 if (maxval && n > maxval)
57 error (EXIT_FAILURE, 0, "option value too big: %s", optarg);
58
59 return n;
60 }
61
62 void
63 init_data_buffer (unsigned char *pat, size_t len)
64 {
65 size_t i = 0;
66 unsigned char *p;
67
68 if (data_length == 0)
69 return;
70
71 data_buffer = xmalloc (data_length);
72
73 if (pat)
74 {
75 for (p = data_buffer; p < data_buffer + data_length; p++)
76 {
77 *p = pat[i];
78 if (++i >= len)
79 i = 0;
80 }
81 }
82 else
83 {
84 for (i = 0; i < data_length; i++)
85 data_buffer[i] = i;
86 }
87 }
88
89 void
90 decode_pattern (const char *text, int *pattern_len,
91 unsigned char *pattern_data)
92 {
93 int i, c, off;
94
95 for (i = 0; *text && i < *pattern_len; i++)
96 {
97 if (sscanf (text, "%2x%n", &c, &off) != 1)
98 error (EXIT_FAILURE, 0, "error in pattern near %s", text);
99
100 text += off;
101 pattern_data[i] = c;
102 }
103 *pattern_len = i;
104 }
105
106 double
107 nabs (double a)
108 {
109 return (a < 0) ? -a : a;
110 }
111
112 double
113 nsqrt (double a, double prec)
114 {
115 double x0, x1;
116
117 if (a < 0)
118 return 0;
119 if (a < prec)
120 return 0;
121 x1 = a / 2;
122 do
123 {
124 x0 = x1;
125 x1 = (x0 + a / x0) / 2;
126 }
127 while (nabs (x1 - x0) > prec);
128
129 return x1;
130 }
131
132 int
133 is_normed_time (n_time t)
134 {
135 /* A set MSB indicates non-normalised time standard. */
136 return (t & (1UL << 31)) ? 0 : 1;
137 }
138
139 const char *
140 ping_cvt_time (char *buf, size_t buflen, n_time t)
141 {
142 n_time t_red;
143
144 t_red = t & ((1UL << 31) - 1);
145
146 if (is_normed_time (t))
147 snprintf (buf, buflen, "%u", t_red);
148 else
149 snprintf (buf, buflen, "<%u>", t_red);
150
151 return buf;
152 }
153
154 int
155 _ping_setbuf (PING *p, bool use_ipv6)
156 {
157 if (!p->ping_buffer)
158 {
159 p->ping_buffer = malloc (_PING_BUFLEN (p, use_ipv6));
160 if (!p->ping_buffer)
161 return -1;
162 }
163 if (!p->ping_cktab)
164 {
165 p->ping_cktab = malloc (p->ping_cktab_size);
166 if (!p->ping_cktab)
167 return -1;
168 memset (p->ping_cktab, 0, p->ping_cktab_size);
169 }
170 return 0;
171 }
172
173 int
174 ping_set_data (PING *p, void *data, size_t off, size_t len, bool use_ipv6)
175 {
176 icmphdr_t *icmp;
177
178 if (_ping_setbuf (p, use_ipv6))
179 return -1;
180 if (p->ping_datalen < off + len)
181 return -1;
182
183 icmp = (icmphdr_t *) p->ping_buffer;
184 memcpy (icmp->icmp_data + off, data, len);
185
186 return 0;
187 }
188
189 void
190 ping_set_count (PING *ping, size_t count)
191 {
192 ping->ping_count = count;
193 }
194
195 void
196 ping_set_sockopt (PING *ping, int opt, void *val, int valsize)
197 {
198 setsockopt (ping->ping_fd, SOL_SOCKET, opt, (char *) &val, valsize);
199 }
200
201 void
202 ping_set_interval (PING *ping, size_t interval)
203 {
204 ping->ping_interval = interval;
205 }
206
207 void
208 _ping_freebuf (PING *p)
209 {
210 if (p->ping_buffer)
211 {
212 free (p->ping_buffer);
213 p->ping_buffer = NULL;
214 }
215 if (p->ping_cktab)
216 {
217 free (p->ping_cktab);
218 p->ping_cktab = NULL;
219 }
220 }
221
222 void
223 ping_unset_data (PING *p)
224 {
225 _ping_freebuf (p);
226 }
227
228 bool
229 ping_timeout_p (struct timespec *start_time, int timeout)
230 {
231 if (timeout == -1)
232 return false;
233 return timespec_sub (current_timespec (), *start_time).tv_sec >= timeout;
234 }
235
236 char *
237 ipaddr2str (struct sockaddr *from, socklen_t fromlen)
238 {
239 int err;
240 size_t len;
241 char *buf, ipstr[INET6_ADDRSTRLEN], hoststr[256];
242
243 err = getnameinfo (from, fromlen, ipstr, sizeof (ipstr),
244 NULL, 0, NI_NUMERICHOST);
245 if (err)
246 {
247 const char *errmsg;
248
249 if (err == EAI_SYSTEM)
250 errmsg = strerror (errno);
251 else
252 errmsg = gai_strerror (err);
253
254 fprintf (stderr, "ping: getnameinfo: %s\n", errmsg);
255 return xstrdup ("unknown");
256 }
257
258 if (options & OPT_NUMERIC)
259 return xstrdup (ipstr);
260
261 err = getnameinfo (from, fromlen, hoststr, sizeof (hoststr), NULL, 0,
262 #ifdef NI_IDN
263 NI_IDN | NI_NAMEREQD
264 #else
265 NI_NAMEREQD
266 #endif
267 );
268 if (err)
269 return xstrdup (ipstr);
270
271 len = strlen (ipstr) + strlen (hoststr) + 4; /* Pair of parentheses, a space
272 and a NUL. */
273 buf = xmalloc (len);
274 snprintf (buf, len, "%s (%s)", hoststr, ipstr);
275
276 return buf;
277 }
278
279 char *
280 sinaddr2str (struct in_addr ina)
281 {
282 struct hostent *hp;
283
284 if (options & OPT_NUMERIC)
285 return xstrdup (inet_ntoa (ina));
286
287 hp = gethostbyaddr ((char *) &ina, sizeof (ina), AF_INET);
288 if (hp == NULL)
289 return xstrdup (inet_ntoa (ina));
290 else
291 {
292 char *buf, *ipstr;
293 int len;
294
295 ipstr = inet_ntoa (ina);
296 len = strlen (ipstr) + 1;
297
298 if (hp->h_name)
299 len += strlen (hp->h_name) + 4; /* parentheses, space, and NUL */
300
301 buf = xmalloc (len);
302 if (hp->h_name)
303 snprintf (buf, len, "%s (%s)", hp->h_name, ipstr);
304 else
305 snprintf (buf, len, "%s", ipstr);
306 return buf;
307 }
308 }