Back to index

glibc  2.9
hesiod-proto.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 2000, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
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 <hesiod.h>
00022 #include <netdb.h>
00023 #include <netinet/in.h>
00024 #include <nss.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 
00029 #include "nss_hesiod.h"
00030 
00031 /* Declare a parser for Hesiod protocol entries.  Although the format
00032    of the entries is identical to those in /etc/protocols, here is no
00033    predefined parser for us to use.  */
00034 
00035 #define ENTNAME protoent
00036 
00037 struct protoent_data {};
00038 
00039 #define TRAILING_LIST_MEMBER              p_aliases
00040 #define TRAILING_LIST_SEPARATOR_P  isspace
00041 #include <nss/nss_files/files-parse.c>
00042 LINE_PARSER
00043 ("#",
00044  STRING_FIELD (result->p_name, isspace, 1);
00045  INT_FIELD (result->p_proto, isspace, 1, 10,);
00046  )
00047 
00048 enum nss_status
00049 _nss_hesiod_setprotoent (int stayopen)
00050 {
00051   return NSS_STATUS_SUCCESS;
00052 }
00053 
00054 enum nss_status
00055 _nss_hesiod_endprotoent (void)
00056 {
00057   return NSS_STATUS_SUCCESS;
00058 }
00059 
00060 static enum nss_status
00061 lookup (const char *name, const char *type, struct protoent *proto,
00062        char *buffer, size_t buflen, int *errnop)
00063 {
00064   struct parser_data *data = (void *) buffer;
00065   size_t linebuflen;
00066   void *context;
00067   char **list, **item;
00068   int parse_res;
00069   int found;
00070   int olderr = errno;
00071 
00072   context = _nss_hesiod_init ();
00073   if (context == NULL)
00074     return NSS_STATUS_UNAVAIL;
00075 
00076   list = hesiod_resolve (context, name, type);
00077   if (list == NULL)
00078     {
00079       int err = errno;
00080       hesiod_end (context);
00081       __set_errno (olderr);
00082       return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
00083     }
00084 
00085   linebuflen = buffer + buflen - data->linebuffer;
00086 
00087   item = list;
00088   found = 0;
00089   do
00090     {
00091       size_t len = strlen (*item) + 1;
00092 
00093       if (linebuflen < len)
00094        {
00095          hesiod_free_list (context, list);
00096          hesiod_end (context);
00097          *errnop = ERANGE;
00098          return NSS_STATUS_TRYAGAIN;
00099        }
00100 
00101       memcpy (data->linebuffer, *item, len);
00102 
00103       parse_res = parse_line (buffer, proto, data, buflen, errnop);
00104       if (parse_res == -1)
00105        {
00106          hesiod_free_list (context, list);
00107          hesiod_end (context);
00108          return NSS_STATUS_TRYAGAIN;
00109        }
00110 
00111       if (parse_res > 0)
00112        found = 1;
00113 
00114       ++item;
00115     }
00116   while (*item != NULL && !found);
00117 
00118   hesiod_free_list (context, list);
00119   hesiod_end (context);
00120 
00121   if (found == 0)
00122     {
00123       __set_errno (olderr);
00124       return NSS_STATUS_NOTFOUND;
00125     }
00126 
00127   return NSS_STATUS_SUCCESS;
00128 }
00129 
00130 enum nss_status
00131 _nss_hesiod_getprotobyname_r (const char *name, struct protoent *proto,
00132                            char *buffer, size_t buflen, int *errnop)
00133 {
00134   return lookup (name, "protocol", proto, buffer, buflen, errnop);
00135 }
00136 
00137 enum nss_status
00138 _nss_hesiod_getprotobynumber_r (const int protocol, struct protoent *proto,
00139                             char *buffer, size_t buflen, int *errnop)
00140 {
00141   char protostr[21];
00142 
00143   snprintf (protostr, sizeof protostr, "%d", protocol);
00144 
00145   return lookup (protostr, "protonum", proto, buffer, buflen, errnop);
00146 }