Back to index

glibc  2.9
ether_hton.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996, 1999, 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 /* Type of the lookup function we need here.  */
00028 typedef int (*lookup_function) (const char *, struct etherent *, char *, int,
00029                             int *);
00030 
00031 /* The lookup function for the first entry of this service.  */
00032 extern int __nss_ethers_lookup (service_user **nip, const char *name,
00033                             void **fctp) internal_function;
00034 
00035 
00036 int
00037 ether_hostton (const char *hostname, struct ether_addr *addr)
00038 {
00039   static service_user *startp;
00040   static lookup_function start_fct;
00041   service_user *nip;
00042   union
00043   {
00044     lookup_function f;
00045     void *ptr;
00046   } fct;
00047   int no_more;
00048   enum nss_status status = NSS_STATUS_UNAVAIL;
00049   struct etherent etherent;
00050 
00051   if (startp == NULL)
00052     {
00053       no_more = __nss_ethers_lookup (&nip, "gethostton_r", &fct.ptr);
00054       if (no_more)
00055        startp = (service_user *) -1;
00056       else
00057        {
00058          startp = nip;
00059          start_fct = fct.f;
00060        }
00061     }
00062   else
00063     {
00064       fct.f = start_fct;
00065       no_more = (nip = startp) == (service_user *) -1;
00066     }
00067 
00068   while (no_more == 0)
00069     {
00070       char buffer[1024];
00071 
00072       status = (*fct.f) (hostname, &etherent, buffer, sizeof buffer, &errno);
00073 
00074       no_more = __nss_next2 (&nip, "gethostton_r", NULL, &fct.ptr, status, 0);
00075     }
00076 
00077   if (status == NSS_STATUS_SUCCESS)
00078     memcpy (addr, etherent.e_addr.ether_addr_octet,
00079            sizeof (struct ether_addr));
00080 
00081   return status == NSS_STATUS_SUCCESS ? 0 : -1;
00082 }