]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/netstat/mcast.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / netstat / mcast.c
1 /*-
2  * Copyright (c) 2007 Bruce M. Simpson <bms@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * Print the running system's current multicast group memberships.
33  * As this relies on getifmaddrs(), it may not be used with a core file.
34  */
35
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/if_mib.h>
43 #include <net/if_types.h>
44 #include <net/if_dl.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <netinet/if_ether.h>
48 #include <arpa/inet.h>
49 #include <netdb.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <ifaddrs.h>
54 #include <sysexits.h>
55
56 #include <stddef.h>
57 #include <stdarg.h>
58 #include <stdlib.h>
59 #include <stdint.h>
60 #include <stdio.h>
61 #include <string.h>
62
63 #include "netstat.h"
64
65 #define __NETSTAT_BURN_BRIDGES
66
67 #ifdef __NETSTAT_BURN_BRIDGES
68
69 union sockunion {
70         struct sockaddr_storage ss;
71         struct sockaddr         sa;
72         struct sockaddr_dl      sdl;
73         struct sockaddr_in      sin;
74         struct sockaddr_in6     sin6;
75 };
76 typedef union sockunion sockunion_t;
77
78 void ifmalist_dump_af(const struct ifmaddrs * const ifmap, int const af);
79
80 void
81 ifmalist_dump_af(const struct ifmaddrs * const ifmap, int const af)
82 {
83         const struct ifmaddrs *ifma;
84         sockunion_t *psa;
85         char myifname[IFNAMSIZ];
86 #ifdef INET6
87         char addrbuf[INET6_ADDRSTRLEN];
88 #endif
89         char *pcolon;
90         char *pafname, *pifname, *plladdr, *pgroup;
91 #ifdef INET6
92         void *in6addr;
93 #endif
94
95         switch (af) {
96         case AF_INET:
97                 pafname = "IPv4";
98                 break;
99 #ifdef INET6
100         case AF_INET6:
101                 pafname = "IPv6";
102                 break;
103 #endif
104         case AF_LINK:
105                 pafname = "Link-layer";
106                 break;
107         default:
108                 return;         /* XXX */
109         }
110
111         fprintf(stdout, "%s Multicast Group Memberships\n", pafname);
112         fprintf(stdout, "%-20s\t%-16s\t%s\n", "Group", "Link-layer Address",
113             "Netif");
114
115         for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
116
117                 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
118                         continue;
119
120                 /* Group address */
121                 psa = (sockunion_t *)ifma->ifma_addr;
122                 if (psa->sa.sa_family != af)
123                         continue;
124
125                 switch (psa->sa.sa_family) {
126                 case AF_INET:
127                         pgroup = inet_ntoa(psa->sin.sin_addr);
128                         break;
129 #ifdef INET6
130                 case AF_INET6:
131                         in6addr = &psa->sin6.sin6_addr;
132                         inet_ntop(psa->sa.sa_family, in6addr, addrbuf,
133                             sizeof(addrbuf));
134                         pgroup = addrbuf;
135                         break;
136 #endif
137                 case AF_LINK:
138                         if ((psa->sdl.sdl_alen == ETHER_ADDR_LEN) ||
139                             (psa->sdl.sdl_type == IFT_ETHER)) {
140                                 pgroup =
141 ether_ntoa((struct ether_addr *)&psa->sdl.sdl_data);
142 #ifdef notyet
143                         } else {
144                                 pgroup = addr2ascii(AF_LINK,
145                                     &psa->sdl,
146                                     sizeof(struct sockaddr_dl),
147                                     addrbuf);
148 #endif
149                         }
150                         break;
151                 default:
152                         continue;       /* XXX */
153                 }
154
155                 /* Link-layer mapping, if any */
156                 psa = (sockunion_t *)ifma->ifma_lladdr;
157                 if (psa != NULL) {
158                         if (psa->sa.sa_family == AF_LINK) {
159                                 if ((psa->sdl.sdl_alen == ETHER_ADDR_LEN) ||
160                                     (psa->sdl.sdl_type == IFT_ETHER)) {
161                                         /* IEEE 802 */
162                                         plladdr =
163 ether_ntoa((struct ether_addr *)&psa->sdl.sdl_data);
164 #ifdef notyet
165                                 } else {
166                                         /* something more exotic */
167                                         plladdr = addr2ascii(AF_LINK,
168                                             &psa->sdl,
169                                             sizeof(struct sockaddr_dl),
170                                             addrbuf);
171 #endif
172                                 }
173                         } else {
174                                 /* not a link-layer address */
175                                 plladdr = "<invalid>";
176                         }
177                 } else {
178                         plladdr = "<none>";
179                 }
180
181                 /* Interface upon which the membership exists */
182                 psa = (sockunion_t *)ifma->ifma_name;
183                 if (psa != NULL && psa->sa.sa_family == AF_LINK) {
184                         strlcpy(myifname, link_ntoa(&psa->sdl), IFNAMSIZ);
185                         pcolon = strchr(myifname, ':');
186                         if (pcolon)
187                                 *pcolon = '\0';
188                         pifname = myifname;
189                 } else {
190                         pifname = "";
191                 }
192
193                 fprintf(stdout, "%-20s\t%-16s\t%s\n", pgroup, plladdr, pifname);
194         }
195 }
196
197 void
198 ifmalist_dump(void)
199 {
200         struct ifmaddrs *ifmap;
201
202         fprintf(stderr,
203              "WARNING: This functionality is deprecated, and will be removed\n"
204              "in FreeBSD 7.0. Please consider using ifmcstat(8) instead.\n");
205
206         if (getifmaddrs(&ifmap))
207                 err(EX_OSERR, "getifmaddrs");
208
209         ifmalist_dump_af(ifmap, AF_LINK);
210         fputs("\n", stdout);
211         ifmalist_dump_af(ifmap, AF_INET);
212 #ifdef INET6
213         fputs("\n", stdout);
214         ifmalist_dump_af(ifmap, AF_INET6);
215 #endif
216
217         freeifmaddrs(ifmap);
218 }
219 #endif /* __NETSTAT_BURN_BRIDGES */