2 * Copyright (c) 2007 Bruce M. Simpson <bms@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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.
36 #include <sys/types.h>
37 #include <sys/socket.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>
65 struct sockaddr_storage ss;
67 struct sockaddr_dl sdl;
68 struct sockaddr_in sin;
69 struct sockaddr_in6 sin6;
71 typedef union sockunion sockunion_t;
73 void ifmalist_dump_af(const struct ifmaddrs * const ifmap, int const af);
76 ifmalist_dump_af(const struct ifmaddrs * const ifmap, int const af)
78 const struct ifmaddrs *ifma;
80 char myifname[IFNAMSIZ];
82 char addrbuf[INET6_ADDRSTRLEN];
85 char *pafname, *pifname, *plladdr, *pgroup;
100 pafname = "Link-layer";
106 fprintf(stdout, "%s Multicast Group Memberships\n", pafname);
107 fprintf(stdout, "%-20s\t%-16s\t%s\n", "Group", "Link-layer Address",
110 for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
112 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
116 psa = (sockunion_t *)ifma->ifma_addr;
117 if (psa->sa.sa_family != af)
120 switch (psa->sa.sa_family) {
122 pgroup = inet_ntoa(psa->sin.sin_addr);
126 in6addr = &psa->sin6.sin6_addr;
127 inet_ntop(psa->sa.sa_family, in6addr, addrbuf,
133 if ((psa->sdl.sdl_alen == ETHER_ADDR_LEN) ||
134 (psa->sdl.sdl_type == IFT_ETHER)) {
136 ether_ntoa((struct ether_addr *)&psa->sdl.sdl_data);
139 pgroup = addr2ascii(AF_LINK,
141 sizeof(struct sockaddr_dl),
150 /* Link-layer mapping, if any */
151 psa = (sockunion_t *)ifma->ifma_lladdr;
153 if (psa->sa.sa_family == AF_LINK) {
154 if ((psa->sdl.sdl_alen == ETHER_ADDR_LEN) ||
155 (psa->sdl.sdl_type == IFT_ETHER)) {
158 ether_ntoa((struct ether_addr *)&psa->sdl.sdl_data);
161 /* something more exotic */
162 plladdr = addr2ascii(AF_LINK,
164 sizeof(struct sockaddr_dl),
169 /* not a link-layer address */
170 plladdr = "<invalid>";
176 /* Interface upon which the membership exists */
177 psa = (sockunion_t *)ifma->ifma_name;
178 if (psa != NULL && psa->sa.sa_family == AF_LINK) {
179 strlcpy(myifname, link_ntoa(&psa->sdl), IFNAMSIZ);
180 pcolon = strchr(myifname, ':');
188 fprintf(stdout, "%-20s\t%-16s\t%s\n", pgroup, plladdr, pifname);
195 struct ifmaddrs *ifmap;
197 if (getifmaddrs(&ifmap))
198 err(EX_OSERR, "getifmaddrs");
200 ifmalist_dump_af(ifmap, AF_LINK);
202 ifmalist_dump_af(ifmap, AF_INET);
205 ifmalist_dump_af(ifmap, AF_INET6);