Debian Codegraph @

ping/ping_address.c

raw
1 /*
2 Copyright (C) 2001-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 /* NOTE: most of existing routers simply discard ICMP_ADDRESS requests. */
20 #include <config.h>
21
22 #include <sys/param.h>
23 #include <sys/socket.h>
24 #include <sys/file.h>
25 #include <sys/time.h>
26
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29
30 /*#include <netinet/ip_icmp.h> -- deliberately not including this */
31 #ifdef HAVE_NETINET_IP_VAR_H
32 # include <netinet/ip_var.h>
33 #endif
34
35 #include <netdb.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <attribute.h>
43
44 #include <ping.h>
45 #include <ping_impl.h>
46
47 static int recv_address (int code, void *closure,
48 struct sockaddr_in *dest, struct sockaddr_in *from,
49 struct ip *ip, icmphdr_t * icmp, int datalen);
50 static void print_address (int dupflag, void *closure,
51 struct sockaddr_in *dest, struct sockaddr_in *from,
52 struct ip *ip, icmphdr_t * icmp, int datalen);
53 static int address_finish (void);
54
55 int
56 ping_address (char *hostname)
57 {
58 ping_set_type (ping, ICMP_ADDRESS);
59 ping_set_event_handler (ping, recv_address, NULL);
60 ping_set_packetsize (ping, ICMP_MASKLEN);
61 ping_set_count (ping, 1);
62
63 if (ping_set_dest (ping, hostname))
64 error (EXIT_FAILURE, 0, "unknown host");
65
66 printf ("PING %s (%s): sending address mask request\n",
67 ping->ping_hostname,
68 inet_ntoa (ping->ping_dest.ping_sockaddr.sin_addr));
69
70 return ping_run (ping, address_finish);
71 }
72
73
74 int
75 recv_address (int code, void *closure,
76 struct sockaddr_in *dest, struct sockaddr_in *from,
77 struct ip *ip, icmphdr_t *icmp, int datalen)
78 {
79 switch (code)
80 {
81 case PEV_RESPONSE:
82 case PEV_DUPLICATE:
83 print_address (code == PEV_DUPLICATE,
84 closure, dest, from, ip, icmp, datalen);
85 break;
86 case PEV_NOECHO:;
87 print_icmp_header (from, ip, icmp, datalen);
88 }
89 return 0;
90 }
91
92 void
93 print_address (int dupflag, void *closure MAYBE_UNUSED,
94 struct sockaddr_in *dest MAYBE_UNUSED,
95 struct sockaddr_in *from,
96 struct ip *ip MAYBE_UNUSED, icmphdr_t *icmp, int datalen)
97 {
98 struct in_addr addr;
99
100 printf ("%d bytes from %s: icmp_seq=%u", datalen,
101 inet_ntoa (*(struct in_addr *) &from->sin_addr.s_addr),
102 ntohs (icmp->icmp_seq));
103 if (dupflag)
104 printf (" (DUP!)");
105 printf ("\n");
106 addr.s_addr = icmp->icmp_mask;
107 printf ("icmp_mask = %s", inet_ntoa (addr));
108 printf ("\n");
109 return;
110 }
111
112
113 int
114 address_finish (void)
115 {
116 return 0;
117 }