2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / getsourcefilter.c
bloba6f89a3cc919449ec5be09f31b838385b04c4fe8
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <alloca.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <netatalk/at.h>
27 #include <netax25/ax25.h>
28 #include <netinet/in.h>
29 #include <netipx/ipx.h>
30 #include <netpacket/packet.h>
31 #include <netrose/rose.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
36 static const struct
38 int sol;
39 int af;
40 socklen_t size;
41 } sol_map[] =
43 /* Sort the array according to importance of the protocols. Add
44 more protocols when they become available. */
45 { SOL_IP, AF_INET, sizeof (struct sockaddr_in) },
46 { SOL_IPV6, AF_INET6, sizeof (struct sockaddr_in6) },
47 { SOL_AX25, AF_AX25, sizeof (struct sockaddr_ax25) },
48 { SOL_IPX, AF_IPX, sizeof (struct sockaddr_ipx) },
49 { SOL_ATALK, AF_APPLETALK, sizeof (struct sockaddr_at) },
50 { SOL_ROSE, AF_ROSE, sizeof (struct sockaddr_rose) },
51 { SOL_PACKET, AF_PACKET, sizeof (struct sockaddr_ll) }
53 #define NSOL_MAP (sizeof (sol_map) / sizeof (sol_map[0]))
56 /* Try to determine the socket level value. Ideally both side and
57 family are set. But sometimes only the size is correct and the
58 family value might be bogus. Loop over the array entries and look
59 for a perfect match or the first match based on size. */
60 int
61 __get_sol (int af, socklen_t len)
63 int first_size_sol = -1;
65 for (size_t cnt = 0; cnt < NSOL_MAP; ++cnt)
67 /* Just a test so that we make sure the special value used to
68 signal the "we have so far no socket level value" is OK. */
69 assert (sol_map[cnt].sol != -1);
71 if (len == sol_map[cnt].size)
73 /* The size matches, which is a requirement. If the family
74 matches, too, we have a winner. Otherwise we remember the
75 socket level value for this protocol if it is the first
76 match. */
77 if (af == sol_map[cnt].af)
78 /* Bingo! */
79 return sol_map[cnt].sol;
81 if (first_size_sol == -1)
82 first_size_sol = sol_map[cnt].sol;
86 return first_size_sol;
90 int
91 getsourcefilter (int s, uint32_t interface, const struct sockaddr *group,
92 socklen_t grouplen, uint32_t *fmode, uint32_t *numsrc,
93 struct sockaddr_storage *slist)
95 /* We have to create an struct ip_msfilter object which we can pass
96 to the kernel. */
97 socklen_t needed = GROUP_FILTER_SIZE (*numsrc);
98 int use_alloca = __libc_use_alloca (needed);
100 struct group_filter *gf;
101 if (use_alloca)
102 gf = (struct group_filter *) alloca (needed);
103 else
105 gf = (struct group_filter *) malloc (needed);
106 if (gf == NULL)
107 return -1;
110 gf->gf_interface = interface;
111 memcpy (&gf->gf_group, group, grouplen);
112 gf->gf_numsrc = *numsrc;
114 /* We need to provide the appropriate socket level value. */
115 int result;
116 int sol = __get_sol (group->sa_family, grouplen);
117 if (sol == -1)
119 __set_errno (EINVAL);
120 result = -1;
122 else
124 result = __getsockopt (s, sol, MCAST_MSFILTER, gf, &needed);
126 /* If successful, copy the results to the places the caller wants
127 them in. */
128 if (result == 0)
130 *fmode = gf->gf_fmode;
131 memcpy (slist, gf->gf_slist,
132 MIN (*numsrc, gf->gf_numsrc)
133 * sizeof (struct sockaddr_storage));
134 *numsrc = gf->gf_numsrc;
138 if (! use_alloca)
140 int save_errno = errno;
141 free (gf);
142 __set_errno (save_errno);
145 return result;