Back to index

glibc  2.9
getsourcefilter.c
Go to the documentation of this file.
00001 /* Get 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 <assert.h>
00023 #include <errno.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <netatalk/at.h>
00027 #include <netax25/ax25.h>
00028 #include <netinet/in.h>
00029 #include <netipx/ipx.h>
00030 #include <netpacket/packet.h>
00031 #include <netrose/rose.h>
00032 #include <sys/param.h>
00033 #include <sys/socket.h>
00034 
00035 
00036 static const struct
00037 {
00038   int sol;
00039   int af;
00040   socklen_t size;
00041 }  sol_map[] =
00042   {
00043     /* Sort the array according to importance of the protocols.  Add
00044        more protocols when they become available.  */
00045     { SOL_IP, AF_INET, sizeof (struct sockaddr_in) },
00046     { SOL_IPV6, AF_INET6, sizeof (struct sockaddr_in6) },
00047     { SOL_AX25, AF_AX25, sizeof (struct sockaddr_ax25) },
00048     { SOL_IPX, AF_IPX, sizeof (struct sockaddr_ipx) },
00049     { SOL_ATALK, AF_APPLETALK, sizeof (struct sockaddr_at) },
00050     { SOL_ROSE, AF_ROSE, sizeof (struct sockaddr_rose) },
00051     { SOL_PACKET, AF_PACKET, sizeof (struct sockaddr_ll) }
00052   };
00053 #define NSOL_MAP (sizeof (sol_map) / sizeof (sol_map[0]))
00054 
00055 
00056 /* Try to determine the socket level value.  Ideally both side and
00057    family are set.  But sometimes only the size is correct and the
00058    family value might be bogus.  Loop over the array entries and look
00059    for a perfect match or the first match based on size.  */
00060 int
00061 __get_sol (int af, socklen_t len)
00062 {
00063   int first_size_sol = -1;
00064 
00065   for (size_t cnt = 0; cnt < NSOL_MAP; ++cnt)
00066     {
00067       /* Just a test so that we make sure the special value used to
00068         signal the "we have so far no socket level value" is OK.  */
00069       assert (sol_map[cnt].sol != -1);
00070 
00071       if (len == sol_map[cnt].size)
00072        {
00073          /* The size matches, which is a requirement.  If the family
00074             matches, too, we have a winner.  Otherwise we remember the
00075             socket level value for this protocol if it is the first
00076             match.  */
00077          if (af == sol_map[cnt].af)
00078            /* Bingo!  */
00079            return sol_map[cnt].sol;
00080 
00081          if (first_size_sol == -1)
00082            first_size_sol = sol_map[cnt].sol;
00083       }
00084     }
00085 
00086   return first_size_sol;
00087 }
00088 
00089 
00090 int
00091 getsourcefilter (int s, uint32_t interface, const struct sockaddr *group,
00092                socklen_t grouplen, uint32_t *fmode, uint32_t *numsrc,
00093                struct sockaddr_storage *slist)
00094 {
00095   /* We have to create an struct ip_msfilter object which we can pass
00096      to the kernel.  */
00097   socklen_t needed = GROUP_FILTER_SIZE (*numsrc);
00098   int use_alloca = __libc_use_alloca (needed);
00099 
00100   struct group_filter *gf;
00101   if (use_alloca)
00102     gf = (struct group_filter *) alloca (needed);
00103   else
00104     {
00105       gf = (struct group_filter *) malloc (needed);
00106       if (gf == NULL)
00107        return -1;
00108     }
00109 
00110   gf->gf_interface = interface;
00111   memcpy (&gf->gf_group, group, grouplen);
00112   gf->gf_numsrc = *numsrc;
00113 
00114   /* We need to provide the appropriate socket level value.  */
00115   int result;
00116   int sol = __get_sol (group->sa_family, grouplen);
00117   if (sol == -1)
00118     {
00119       __set_errno (EINVAL);
00120       result = -1;
00121     }
00122   else
00123     {
00124       result = __getsockopt (s, sol, MCAST_MSFILTER, gf, &needed);
00125 
00126       /* If successful, copy the results to the places the caller wants
00127         them in.  */
00128       if (result == 0)
00129        {
00130          *fmode = gf->gf_fmode;
00131          memcpy (slist, gf->gf_slist,
00132                 MIN (*numsrc, gf->gf_numsrc)
00133                 * sizeof (struct sockaddr_storage));
00134          *numsrc = gf->gf_numsrc;
00135        }
00136     }
00137 
00138   if (! use_alloca)
00139     {
00140       int save_errno = errno;
00141       free (gf);
00142       __set_errno (save_errno);
00143     }
00144 
00145   return result;
00146 }