]>
Commit | Line | Data |
---|---|---|
489aa29d | 1 | /* Get source filter. Linux version. |
d4697bc9 | 2 | Copyright (C) 2004-2014 Free Software Foundation, Inc. |
489aa29d UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2004. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
489aa29d UD |
19 | |
20 | #include <alloca.h> | |
9030e7c4 | 21 | #include <assert.h> |
489aa29d UD |
22 | #include <errno.h> |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
e054f494 | 25 | #include <stdint.h> |
9030e7c4 UD |
26 | #include <netatalk/at.h> |
27 | #include <netax25/ax25.h> | |
489aa29d | 28 | #include <netinet/in.h> |
9030e7c4 UD |
29 | #include <netipx/ipx.h> |
30 | #include <netpacket/packet.h> | |
31 | #include <netrose/rose.h> | |
32 | #include <sys/param.h> | |
489aa29d UD |
33 | #include <sys/socket.h> |
34 | ||
35 | ||
9030e7c4 UD |
36 | static const struct |
37 | { | |
38 | int sol; | |
39 | int af; | |
40 | socklen_t size; | |
41 | } sol_map[] = | |
42 | { | |
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) } | |
52 | }; | |
53 | #define NSOL_MAP (sizeof (sol_map) / sizeof (sol_map[0])) | |
54 | ||
55 | ||
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) | |
62 | { | |
63 | int first_size_sol = -1; | |
64 | ||
65 | for (size_t cnt = 0; cnt < NSOL_MAP; ++cnt) | |
66 | { | |
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); | |
70 | ||
71 | if (len == sol_map[cnt].size) | |
72 | { | |
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; | |
80 | ||
81 | if (first_size_sol == -1) | |
82 | first_size_sol = sol_map[cnt].sol; | |
83 | } | |
84 | } | |
85 | ||
86 | return first_size_sol; | |
87 | } | |
88 | ||
89 | ||
489aa29d | 90 | int |
d3c99ad1 | 91 | getsourcefilter (int s, uint32_t interface, const struct sockaddr *group, |
489aa29d UD |
92 | socklen_t grouplen, uint32_t *fmode, uint32_t *numsrc, |
93 | struct sockaddr_storage *slist) | |
94 | { | |
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); | |
9030e7c4 | 98 | int use_alloca = __libc_use_alloca (needed); |
489aa29d UD |
99 | |
100 | struct group_filter *gf; | |
9030e7c4 UD |
101 | if (use_alloca) |
102 | gf = (struct group_filter *) alloca (needed); | |
103 | else | |
489aa29d UD |
104 | { |
105 | gf = (struct group_filter *) malloc (needed); | |
106 | if (gf == NULL) | |
107 | return -1; | |
108 | } | |
489aa29d UD |
109 | |
110 | gf->gf_interface = interface; | |
111 | memcpy (&gf->gf_group, group, grouplen); | |
9030e7c4 | 112 | gf->gf_numsrc = *numsrc; |
489aa29d | 113 | |
9030e7c4 | 114 | /* We need to provide the appropriate socket level value. */ |
0292b0dd | 115 | int result; |
9030e7c4 UD |
116 | int sol = __get_sol (group->sa_family, grouplen); |
117 | if (sol == -1) | |
118 | { | |
119 | __set_errno (EINVAL); | |
0292b0dd | 120 | result = -1; |
9030e7c4 | 121 | } |
0292b0dd | 122 | else |
a334319f | 123 | { |
0292b0dd UD |
124 | result = __getsockopt (s, sol, MCAST_MSFILTER, gf, &needed); |
125 | ||
126 | /* If successful, copy the results to the places the caller wants | |
127 | them in. */ | |
128 | if (result == 0) | |
129 | { | |
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; | |
135 | } | |
489aa29d UD |
136 | } |
137 | ||
9030e7c4 | 138 | if (! use_alloca) |
489aa29d UD |
139 | { |
140 | int save_errno = errno; | |
141 | free (gf); | |
142 | __set_errno (save_errno); | |
143 | } | |
144 | ||
145 | return result; | |
146 | } |