1 /* Get source filter. Linux version.
2 Copyright (C) 2004, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
25 #include <netatalk/at.h>
26 #include <netax25/ax25.h>
27 #include <netinet/in.h>
28 #include <netipx/ipx.h>
29 #include <netpacket/packet.h>
30 #include <netrose/rose.h>
31 #include <sys/param.h>
32 #include <sys/socket.h>
42 /* Sort the array according to importance of the protocols. Add
43 more protocols when they become available. */
44 { SOL_IP
, AF_INET
, sizeof (struct sockaddr_in
) },
45 { SOL_IPV6
, AF_INET6
, sizeof (struct sockaddr_in6
) },
46 { SOL_AX25
, AF_AX25
, sizeof (struct sockaddr_ax25
) },
47 { SOL_IPX
, AF_IPX
, sizeof (struct sockaddr_ipx
) },
48 { SOL_ATALK
, AF_APPLETALK
, sizeof (struct sockaddr_at
) },
49 { SOL_ROSE
, AF_ROSE
, sizeof (struct sockaddr_rose
) },
50 { SOL_PACKET
, AF_PACKET
, sizeof (struct sockaddr_ll
) }
52 #define NSOL_MAP (sizeof (sol_map) / sizeof (sol_map[0]))
55 /* Try to determine the socket level value. Ideally both side and
56 family are set. But sometimes only the size is correct and the
57 family value might be bogus. Loop over the array entries and look
58 for a perfect match or the first match based on size. */
60 __get_sol (int af
, socklen_t len
)
62 int first_size_sol
= -1;
64 for (size_t cnt
= 0; cnt
< NSOL_MAP
; ++cnt
)
66 /* Just a test so that we make sure the special value used to
67 signal the "we have so far no socket level value" is OK. */
68 assert (sol_map
[cnt
].sol
!= -1);
70 if (len
== sol_map
[cnt
].size
)
72 /* The size matches, which is a requirement. If the family
73 matches, too, we have a winner. Otherwise we remember the
74 socket level value for this protocol if it is the first
76 if (af
== sol_map
[cnt
].af
)
78 return sol_map
[cnt
].sol
;
80 if (first_size_sol
== -1)
81 first_size_sol
= sol_map
[cnt
].sol
;
85 return first_size_sol
;
90 getsourcefilter (int s
, uint32_t interface
, const struct sockaddr
*group
,
91 socklen_t grouplen
, uint32_t *fmode
, uint32_t *numsrc
,
92 struct sockaddr_storage
*slist
)
94 /* We have to create an struct ip_msfilter object which we can pass
96 socklen_t needed
= GROUP_FILTER_SIZE (*numsrc
);
97 int use_alloca
= __libc_use_alloca (needed
);
99 struct group_filter
*gf
;
101 gf
= (struct group_filter
*) alloca (needed
);
104 gf
= (struct group_filter
*) malloc (needed
);
109 gf
->gf_interface
= interface
;
110 memcpy (&gf
->gf_group
, group
, grouplen
);
111 gf
->gf_numsrc
= *numsrc
;
113 /* We need to provide the appropriate socket level value. */
115 int sol
= __get_sol (group
->sa_family
, grouplen
);
118 __set_errno (EINVAL
);
123 result
= __getsockopt (s
, sol
, MCAST_MSFILTER
, gf
, &needed
);
125 /* If successful, copy the results to the places the caller wants
129 *fmode
= gf
->gf_fmode
;
130 memcpy (slist
, gf
->gf_slist
,
131 MIN (*numsrc
, gf
->gf_numsrc
)
132 * sizeof (struct sockaddr_storage
));
133 *numsrc
= gf
->gf_numsrc
;
139 int save_errno
= errno
;
141 __set_errno (save_errno
);