| 1 | /* Set source filter. Linux version. |
| 2 | Copyright (C) 2004-2023 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <string.h> |
| 21 | #include <netinet/in.h> |
| 22 | #include <scratch_buffer.h> |
| 23 | #include "getsourcefilter.h" |
| 24 | |
| 25 | |
| 26 | int |
| 27 | setsourcefilter (int s, uint32_t interface, const struct sockaddr *group, |
| 28 | socklen_t grouplen, uint32_t fmode, uint32_t numsrc, |
| 29 | const struct sockaddr_storage *slist) |
| 30 | { |
| 31 | /* We have to create an struct ip_msfilter object which we can pass |
| 32 | to the kernel. */ |
| 33 | size_t needed = GROUP_FILTER_SIZE (numsrc); |
| 34 | |
| 35 | struct scratch_buffer buf; |
| 36 | scratch_buffer_init (&buf); |
| 37 | if (!scratch_buffer_set_array_size (&buf, 1, needed)) |
| 38 | return -1; |
| 39 | struct group_filter *gf = buf.data; |
| 40 | |
| 41 | gf->gf_interface = interface; |
| 42 | memcpy (&gf->gf_group, group, grouplen); |
| 43 | gf->gf_fmode = fmode; |
| 44 | gf->gf_numsrc = numsrc; |
| 45 | memcpy (gf->gf_slist, slist, numsrc * sizeof (struct sockaddr_storage)); |
| 46 | |
| 47 | /* We need to provide the appropriate socket level value. */ |
| 48 | int result; |
| 49 | int sol = __get_sol (group->sa_family, grouplen); |
| 50 | if (sol == -1) |
| 51 | { |
| 52 | __set_errno (EINVAL); |
| 53 | result = -1; |
| 54 | } |
| 55 | else |
| 56 | result = __setsockopt (s, sol, MCAST_MSFILTER, gf, needed); |
| 57 | |
| 58 | scratch_buffer_free (&buf); |
| 59 | |
| 60 | return result; |
| 61 | } |
| 62 | |