Optimize tr_freehook
[glibc.git] / hesiod / nss_hesiod / hesiod-service.c
blobcb5edd2859e66115157b26a8d3c7d2bb8d5c1a52
1 /* Copyright (C) 1997, 2000, 2002 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <hesiod.h>
22 #include <netdb.h>
23 #include <netinet/in.h>
24 #include <nss.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "nss_hesiod.h"
31 /* Hesiod uses a format for service entries that differs from the
32 traditional format. We therefore declare our own parser. */
34 #define ENTNAME servent
36 struct servent_data {};
38 #define TRAILING_LIST_MEMBER s_aliases
39 #define TRAILING_LIST_SEPARATOR_P isspace
40 #include <nss/nss_files/files-parse.c>
41 #define ISSC_OR_SPACE(c) ((c) == ';' || isspace (c))
42 LINE_PARSER
43 ("#",
44 STRING_FIELD (result->s_name, ISSC_OR_SPACE, 1);
45 STRING_FIELD (result->s_proto, ISSC_OR_SPACE, 1);
46 INT_FIELD (result->s_port, ISSC_OR_SPACE, 10, 0, htons);
49 enum nss_status
50 _nss_hesiod_setservent (int stayopen)
52 return NSS_STATUS_SUCCESS;
55 enum nss_status
56 _nss_hesiod_endservent (void)
58 return NSS_STATUS_SUCCESS;
61 static enum nss_status
62 lookup (const char *name, const char *type, const char *protocol,
63 struct servent *serv, char *buffer, size_t buflen, int *errnop)
65 struct parser_data *data = (void *) buffer;
66 size_t linebuflen;
67 void *context;
68 char **list, **item;
69 int parse_res;
70 int found;
71 int olderr = errno;
73 context = _nss_hesiod_init ();
74 if (context == NULL)
75 return NSS_STATUS_UNAVAIL;
77 list = hesiod_resolve (context, name, type);
78 if (list == NULL)
80 int err = errno;
81 hesiod_end (context);
82 __set_errno (olderr);
83 return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
86 linebuflen = buffer + buflen - data->linebuffer;
88 item = list;
89 found = 0;
92 size_t len = strlen (*item) + 1;
94 if (linebuflen < len)
96 hesiod_free_list (context, list);
97 hesiod_end (context);
98 *errnop = ERANGE;
99 return NSS_STATUS_TRYAGAIN;
102 memcpy (data->linebuffer, *item, len);
104 parse_res = parse_line (buffer, serv, data, buflen, errnop);
105 if (parse_res == -1)
107 hesiod_free_list (context, list);
108 hesiod_end (context);
109 return NSS_STATUS_TRYAGAIN;
112 if (parse_res > 0)
113 found = protocol == NULL || strcasecmp (serv->s_proto, protocol) == 0;
115 ++item;
117 while (*item != NULL && !found);
119 hesiod_free_list (context, list);
120 hesiod_end (context);
122 if (found == 0)
124 __set_errno (olderr);
125 return NSS_STATUS_NOTFOUND;
128 return NSS_STATUS_SUCCESS;
131 enum nss_status
132 _nss_hesiod_getservbyname_r (const char *name, const char *protocol,
133 struct servent *serv,
134 char *buffer, size_t buflen, int *errnop)
136 return lookup (name, "service", protocol, serv, buffer, buflen, errnop);
139 enum nss_status
140 _nss_hesiod_getservbyport_r (const int port, const char *protocol,
141 struct servent *serv,
142 char *buffer, size_t buflen, int *errnop)
144 char portstr[6]; /* Port numbers are restricted to 16 bits. */
146 snprintf (portstr, sizeof portstr, "%d", ntohs (port));
148 return lookup (portstr, "port", protocol, serv, buffer, buflen, errnop);