Back to index

glibc  2.9
ether_ntoh.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996,1997,1999,2001,2002,2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <netinet/ether.h>
00022 #include <netinet/if_ether.h>
00023 #include <string.h>
00024 
00025 #include <nss/nsswitch.h>
00026 
00027 
00028 /* Type of the lookup function we need here.  */
00029 typedef int (*lookup_function) (const struct ether_addr *, struct etherent *,
00030                             char *, size_t, int *);
00031 
00032 /* The lookup function for the first entry of this service.  */
00033 extern int __nss_ethers_lookup (service_user **nip, const char *name,
00034                             void **fctp) internal_function;
00035 
00036 
00037 int
00038 ether_ntohost (char *hostname, const struct ether_addr *addr)
00039 {
00040   static service_user *startp;
00041   static lookup_function start_fct;
00042   service_user *nip;
00043   union
00044   {
00045     lookup_function f;
00046     void *ptr;
00047   } fct;
00048   int no_more;
00049   enum nss_status status = NSS_STATUS_UNAVAIL;
00050   struct etherent etherent;
00051 
00052   if (startp == NULL)
00053     {
00054       no_more = __nss_ethers_lookup (&nip, "getntohost_r", &fct.ptr);
00055       if (no_more)
00056        startp = (service_user *) -1;
00057       else
00058        {
00059          startp = nip;
00060          start_fct = fct.f;
00061        }
00062     }
00063   else
00064     {
00065       fct.f = start_fct;
00066       no_more = (nip = startp) == (service_user *) -1;
00067     }
00068 
00069   while (no_more == 0)
00070     {
00071       char buffer[1024];
00072 
00073       status = (*fct.f) (addr, &etherent, buffer, sizeof buffer, &errno);
00074 
00075       no_more = __nss_next2 (&nip, "getntohost_r", NULL, &fct.ptr, status, 0);
00076     }
00077 
00078   if (status == NSS_STATUS_SUCCESS)
00079     /* XXX This is a potential cause of trouble because the size of
00080        the HOSTNAME buffer is not known but the interface does not
00081        provide this information.  */
00082     strcpy (hostname, etherent.e_name);
00083 
00084   return status == NSS_STATUS_SUCCESS ? 0 : -1;
00085 }