setenv.c: Get rid of alloca.
[glibc.git] / hesiod / nss_hesiod / hesiod-proto.c
blob04ed25d3f3288efa80292f75997cae069859adff
1 /* Copyright (C) 1997-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <hesiod.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <nss.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 NSS_DECLARE_MODULE_FUNCTIONS (hesiod)
29 /* Declare a parser for Hesiod protocol entries. Although the format
30 of the entries is identical to those in /etc/protocols, here is no
31 predefined parser for us to use. */
33 #define ENTNAME protoent
35 struct protoent_data {};
37 #define TRAILING_LIST_MEMBER p_aliases
38 #define TRAILING_LIST_SEPARATOR_P isspace
39 #include <nss/nss_files/files-parse.c>
40 LINE_PARSER
41 ("#",
42 STRING_FIELD (result->p_name, isspace, 1);
43 INT_FIELD (result->p_proto, isspace, 1, 10,);
46 enum nss_status
47 _nss_hesiod_setprotoent (int stayopen)
49 return NSS_STATUS_SUCCESS;
52 enum nss_status
53 _nss_hesiod_endprotoent (void)
55 return NSS_STATUS_SUCCESS;
58 static enum nss_status
59 lookup (const char *name, const char *type, struct protoent *proto,
60 char *buffer, size_t buflen, int *errnop)
62 struct parser_data *data = (void *) buffer;
63 size_t linebuflen;
64 void *context;
65 char **list, **item;
66 int parse_res;
67 int found;
68 int olderr = errno;
70 if (hesiod_init (&context) < 0)
71 return NSS_STATUS_UNAVAIL;
73 list = hesiod_resolve (context, name, type);
74 if (list == NULL)
76 int err = errno;
77 hesiod_end (context);
78 __set_errno (olderr);
79 return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
82 linebuflen = buffer + buflen - data->linebuffer;
84 item = list;
85 found = 0;
88 size_t len = strlen (*item) + 1;
90 if (linebuflen < len)
92 hesiod_free_list (context, list);
93 hesiod_end (context);
94 *errnop = ERANGE;
95 return NSS_STATUS_TRYAGAIN;
98 memcpy (data->linebuffer, *item, len);
100 parse_res = parse_line (buffer, proto, data, buflen, errnop);
101 if (parse_res == -1)
103 hesiod_free_list (context, list);
104 hesiod_end (context);
105 return NSS_STATUS_TRYAGAIN;
108 if (parse_res > 0)
109 found = 1;
111 ++item;
113 while (*item != NULL && !found);
115 hesiod_free_list (context, list);
116 hesiod_end (context);
118 if (found == 0)
120 __set_errno (olderr);
121 return NSS_STATUS_NOTFOUND;
124 return NSS_STATUS_SUCCESS;
127 enum nss_status
128 _nss_hesiod_getprotobyname_r (const char *name, struct protoent *proto,
129 char *buffer, size_t buflen, int *errnop)
131 return lookup (name, "protocol", proto, buffer, buflen, errnop);
134 enum nss_status
135 _nss_hesiod_getprotobynumber_r (const int protocol, struct protoent *proto,
136 char *buffer, size_t buflen, int *errnop)
138 char protostr[21];
140 snprintf (protostr, sizeof protostr, "%d", protocol);
142 return lookup (protostr, "protonum", proto, buffer, buflen, errnop);