Back to index

glibc  2.9
setsourcefilter.c
Go to the documentation of this file.
00001 /* Set source filter.  Linux version.
00002    Copyright (C) 2004, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <alloca.h>
00022 #include <errno.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <netinet/in.h>
00026 #include <sys/socket.h>
00027 
00028 
00029 /* Defined in getsourcefilter.c.  */
00030 extern int __get_sol (int af, socklen_t len);
00031 
00032 
00033 int
00034 setsourcefilter (int s, uint32_t interface, const struct sockaddr *group,
00035                socklen_t grouplen, uint32_t fmode, uint32_t numsrc,
00036                const struct sockaddr_storage *slist)
00037 {
00038   /* We have to create an struct ip_msfilter object which we can pass
00039      to the kernel.  */
00040   size_t needed = GROUP_FILTER_SIZE (numsrc);
00041   int use_alloca = __libc_use_alloca (needed);
00042 
00043   struct group_filter *gf;
00044   if (use_alloca)
00045     gf = (struct group_filter *) alloca (needed);
00046   else
00047     {
00048       gf = (struct group_filter *) malloc (needed);
00049       if (gf == NULL)
00050        return -1;
00051     }
00052 
00053   gf->gf_interface = interface;
00054   memcpy (&gf->gf_group, group, grouplen);
00055   gf->gf_fmode = fmode;
00056   gf->gf_numsrc = numsrc;
00057   memcpy (gf->gf_slist, slist, numsrc * sizeof (struct sockaddr_storage));
00058 
00059   /* We need to provide the appropriate socket level value.  */
00060   int result;
00061   int sol = __get_sol (group->sa_family, grouplen);
00062   if (sol == -1)
00063     {
00064       __set_errno (EINVAL);
00065       result = -1;
00066     }
00067   else
00068     result = __setsockopt (s, sol, MCAST_MSFILTER, gf, needed);
00069 
00070   if (! use_alloca)
00071     {
00072       int save_errno = errno;
00073       free (gf);
00074       __set_errno (save_errno);
00075     }
00076 
00077   return result;
00078 }