Hide internal __new_getrlimit function [BZ #18822]
[glibc.git] / hesiod / nss_hesiod / hesiod-service.c
blob1942188517d9450846350837f726b12fb8844f8c
1 /* Copyright (C) 1997-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <hesiod.h>
21 #include <netdb.h>
22 #include <netinet/in.h>
23 #include <nss.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 /* Hesiod uses a format for service entries that differs from the
29 traditional format. We therefore declare our own parser. */
31 #define ENTNAME servent
33 struct servent_data {};
35 #define TRAILING_LIST_MEMBER s_aliases
36 #define TRAILING_LIST_SEPARATOR_P isspace
37 #include <nss/nss_files/files-parse.c>
38 #define ISSC_OR_SPACE(c) ((c) == ';' || isspace (c))
39 LINE_PARSER
40 ("#",
41 STRING_FIELD (result->s_name, ISSC_OR_SPACE, 1);
42 STRING_FIELD (result->s_proto, ISSC_OR_SPACE, 1);
43 INT_FIELD (result->s_port, ISSC_OR_SPACE, 10, 0, htons);
46 enum nss_status
47 _nss_hesiod_setservent (int stayopen)
49 return NSS_STATUS_SUCCESS;
52 enum nss_status
53 _nss_hesiod_endservent (void)
55 return NSS_STATUS_SUCCESS;
58 static enum nss_status
59 lookup (const char *name, const char *type, const char *protocol,
60 struct servent *serv, 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, serv, 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 = protocol == NULL || strcasecmp (serv->s_proto, protocol) == 0;
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_getservbyname_r (const char *name, const char *protocol,
129 struct servent *serv,
130 char *buffer, size_t buflen, int *errnop)
132 return lookup (name, "service", protocol, serv, buffer, buflen, errnop);
135 enum nss_status
136 _nss_hesiod_getservbyport_r (const int port, const char *protocol,
137 struct servent *serv,
138 char *buffer, size_t buflen, int *errnop)
140 char portstr[6]; /* Port numbers are restricted to 16 bits. */
142 snprintf (portstr, sizeof portstr, "%d", ntohs (port));
144 return lookup (portstr, "port", protocol, serv, buffer, buflen, errnop);