Update.
[glibc.git] / hesiod / nss_hesiod / hesiod-service.c
blobe0ca96531098b7c3cb166eb59f7e66b81f04d085
1 /* Copyright (C) 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <bits/libc-lock.h>
21 #include <errno.h>
22 #include <hesiod.h>
23 #include <netdb.h>
24 #include <netinet/in.h>
25 #include <nss.h>
26 #include <stdlib.h>
27 #include <string.h>
30 /* Hesiod uses a format for service entries that differs from the
31 traditional format. We therefore declare our own parser. */
33 #define ENTNAME servent
35 #define ENTDATA servent_data
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 ISSEMICOLON(c) ((c) == ';')
42 LINE_PARSER
43 ("",
44 (void) entdata;
45 STRING_FIELD (result->s_name, ISSEMICOLON, 1);
46 STRING_FIELD (result->s_proto, ISSEMICOLON, 1);
47 INT_FIELD (result->s_port, ISSEMICOLON, 10, 0, htons);
51 /* Locks the static variables in this file. */
52 __libc_lock_define_initialized (static, lock);
54 static void *context = NULL;
56 static enum nss_status
57 internal_setservent (void)
59 if (!context)
61 if (hesiod_init (&context) == -1)
62 return NSS_STATUS_UNAVAIL;
65 return NSS_STATUS_SUCCESS;
68 enum nss_status
69 _nss_hesiod_setservent (void)
71 enum nss_status status;
73 __libc_lock_lock (lock);
75 status = internal_setservent ();
77 __libc_lock_unlock (lock);
79 return status;
82 enum nss_status
83 _nss_hesiod_endservent (void)
85 __libc_lock_lock (lock);
87 if (context)
89 hesiod_end (context);
90 context = NULL;
93 __libc_lock_unlock (lock);
95 return NSS_STATUS_SUCCESS;
98 static enum nss_status
99 lookup (const char *name, const char *protocol, struct servent *serv,
100 char *buffer, size_t buflen, int *errnop)
102 enum nss_status status;
103 struct parser_data *data = (void *) buffer;
104 size_t linebuflen;
105 char **list, **item;
106 int parse_res;
107 int found;
109 status = internal_setservent ();
110 if (status != NSS_STATUS_SUCCESS)
111 return status;
113 list = hesiod_resolve (context, name, "service");
114 if (list == NULL)
115 return errno == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
117 linebuflen = buffer + buflen - data->linebuffer;
119 item = list;
120 found = 0;
123 size_t len = strlen (*item) + 1;
125 if (linebuflen < len)
127 hesiod_free_list (context, list);
128 *errnop = ERANGE;
129 return NSS_STATUS_TRYAGAIN;
132 memcpy (data->linebuffer, *item, len);
134 parse_res = parse_line (buffer, serv, data, buflen, errnop);
135 if (parse_res == -1)
137 hesiod_free_list (context, list);
138 return NSS_STATUS_TRYAGAIN;
141 if (parse_res > 0)
142 found = protocol == NULL || strcmp (serv->s_proto, protocol) == 0;
144 ++item;
146 while (*item != NULL && !found);
148 hesiod_free_list (context, list);
150 return found ? NSS_STATUS_SUCCESS : NSS_STATUS_NOTFOUND;
153 enum nss_status
154 _nss_hesiod_getservbyname_r (const char *name, const char *protocol,
155 struct servent *serv,
156 char *buffer, size_t buflen, int *errnop)
158 enum nss_status status;
160 __libc_lock_lock (lock);
162 status = lookup (name, protocol, serv, buffer, buflen, errnop);
164 __libc_lock_unlock (lock);
166 return status;