]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/netstat/mcast.c
This commit was generated by cvs2svn to compensate for changes in r170222,
[FreeBSD/FreeBSD.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
39 #include <net/if.h>
40 #include <net/if_var.h>
41 #include <net/if_mib.h>
42 #include <net/if_types.h>
43 #include <net/if_dl.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
47 #include <arpa/inet.h>
48 #include <netdb.h>
49
50 #include <ctype.h>
51 #include <err.h>
52 #include <ifaddrs.h>
53 #include <sysexits.h>
54
55 #include <stddef.h>
56 #include <stdarg.h>
57 #include <stdlib.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <string.h>
61
62 #include "netstat.h"
63
64 #define __NETSTAT_BURN_BRIDGES
65
66 #ifdef __NETSTAT_BURN_BRIDGES
67
68 union sockunion {
69         struct sockaddr_storage ss;
70         struct sockaddr         sa;
71         struct sockaddr_dl      sdl;
72         struct sockaddr_in      sin;
73         struct sockaddr_in6     sin6;
74 };
75 typedef union sockunion sockunion_t;
76
77 void ifmalist_dump_af(const struct ifmaddrs * const ifmap, int const af);
78
79 void
80 ifmalist_dump_af(const struct ifmaddrs * const ifmap, int const af)
81 {
82         const struct ifmaddrs *ifma;
83         sockunion_t *psa;
84         char myifname[IFNAMSIZ];
85 #ifdef INET6
86         char addrbuf[INET6_ADDRSTRLEN];
87 #endif
88         char *pcolon;
89         char *pafname, *pifname, *plladdr, *pgroup;
90 #ifdef INET6
91         void *in6addr;
92 #endif
93
94         switch (af) {
95         case AF_INET:
96                 pafname = "IPv4";
97                 break;
98 #ifdef INET6
99         case AF_INET6:
100                 pafname = "IPv6";
101                 break;
102 #endif
103         case AF_LINK:
104                 pafname = "Link-layer";
105                 break;
106         default:
107                 return;         /* XXX */
108         }
109
110         fprintf(stdout, "%s Multicast Group Memberships\n", pafname);
111         fprintf(stdout, "%-20s\t%-16s\t%s\n", "Group", "Link-layer Address",
112             "Netif");
113
114         for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
115
116                 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
117                         continue;
118
119                 /* Group address */
120                 psa = (sockunion_t *)ifma->ifma_addr;
121                 if (psa->sa.sa_family != af)
122                         continue;
123
124                 switch (psa->sa.sa_family) {
125                 case AF_INET:
126                         pgroup = inet_ntoa(psa->sin.sin_addr);
127                         break;
128 #ifdef INET6
129                 case AF_INET6:
130                         in6addr = &psa->sin6.sin6_addr;
131                         inet_ntop(psa->sa.sa_family, in6addr, addrbuf,
132                             sizeof(addrbuf));
133                         pgroup = addrbuf;
134                         break;
135 #endif
136                 case AF_LINK:
137                         if ((psa->sdl.sdl_alen == ETHER_ADDR_LEN) ||
138                             (psa->sdl.sdl_type == IFT_ETHER)) {
139                                 pgroup =
140 ether_ntoa((struct ether_addr *)&psa->sdl.sdl_data);
141 #ifdef notyet
142                         } else {
143                                 pgroup = addr2ascii(AF_LINK,
144                                     &psa->sdl,
145                                     sizeof(struct sockaddr_dl),
146                                     addrbuf);
147 #endif
148                         }
149                         break;
150                 default:
151                         continue;       /* XXX */
152                 }
153
154                 /* Link-layer mapping, if any */
155                 psa = (sockunion_t *)ifma->ifma_lladdr;
156                 if (psa != NULL) {
157                         if (psa->sa.sa_family == AF_LINK) {
158                                 if ((psa->sdl.sdl_alen == ETHER_ADDR_LEN) ||
159                                     (psa->sdl.sdl_type == IFT_ETHER)) {
160                                         /* IEEE 802 */
161                                         plladdr =
162 ether_ntoa((struct ether_addr *)&psa->sdl.sdl_data);
163 #ifdef notyet
164                                 } else {
165                                         /* something more exotic */
166                                         plladdr = addr2ascii(AF_LINK,
167                                             &psa->sdl,
168                                             sizeof(struct sockaddr_dl),
169                                             addrbuf);
170 #endif
171                                 }
172                         } else {
173                                 /* not a link-layer address */
174                                 plladdr = "<invalid>";
175                         }
176                 } else {
177                         plladdr = "<none>";
178                 }
179
180                 /* Interface upon which the membership exists */
181                 psa = (sockunion_t *)ifma->ifma_name;
182                 if (psa != NULL && psa->sa.sa_family == AF_LINK) {
183                         strlcpy(myifname, link_ntoa(&psa->sdl), IFNAMSIZ);
184                         pcolon = strchr(myifname, ':');
185                         if (pcolon)
186                                 *pcolon = '\0';
187                         pifname = myifname;
188                 } else {
189                         pifname = "";
190                 }
191
192                 fprintf(stdout, "%-20s\t%-16s\t%s\n", pgroup, plladdr, pifname);
193         }
194 }
195
196 void
197 ifmalist_dump(void)
198 {
199         struct ifmaddrs *ifmap;
200
201         fprintf(stderr,
202              "WARNING: This functionality is deprecated, and will be removed\n"
203              "in FreeBSD 7.0. Please consider using ifmcstat(8) instead.\n");
204
205         if (getifmaddrs(&ifmap))
206                 err(EX_OSERR, "getifmaddrs");
207
208         ifmalist_dump_af(ifmap, AF_LINK);
209         fputs("\n", stdout);
210         ifmalist_dump_af(ifmap, AF_INET);
211 #ifdef INET6
212         fputs("\n", stdout);
213         ifmalist_dump_af(ifmap, AF_INET6);
214 #endif
215
216         freeifmaddrs(ifmap);
217 }
218 #endif /* __NETSTAT_BURN_BRIDGES */