1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
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. */
25 #include <libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
31 /* Get the declaration of the parser function. */
32 #define ENTNAME protoent
34 #include "../nss/nss_files/files-parse.c"
36 __libc_lock_define_initialized (static, lock)
41 struct response *next;
44 static struct response *start = NULL;
45 static struct response *next = NULL;
48 saveit (int instatus, char *inkey, int inkeylen, char *inval,
49 int invallen, char *indata)
51 if (instatus != YP_TRUE)
54 if (inkey && inkeylen > 0 && inval && invallen > 0)
58 start = malloc (sizeof (struct response));
63 next->next = malloc (sizeof (struct response));
67 next->val = malloc (invallen + 1);
68 strncpy (next->val, inval, invallen);
69 next->val[invallen] = '\0';
76 internal_nis_setprotoent (void)
79 struct ypall_callback ypcb;
81 yp_get_default_domain (&domainname);
85 if (start->val != NULL)
93 ypcb.foreach = saveit;
95 yp_all (domainname, "protocols.bynumber", &ypcb);
98 return NSS_STATUS_SUCCESS;
102 _nss_nis_setprotoent (void)
104 enum nss_status status;
106 __libc_lock_lock (lock);
108 status = internal_nis_setprotoent ();
110 __libc_lock_unlock (lock);
116 _nss_nis_endprotoent (void)
118 __libc_lock_lock (lock);
120 while (start != NULL)
122 if (start->val != NULL)
131 __libc_lock_unlock (lock);
133 return NSS_STATUS_SUCCESS;
136 static enum nss_status
137 internal_nis_getprotoent_r (struct protoent *proto,
138 char *buffer, size_t buflen)
140 struct parser_data *data = (void *) buffer;
144 internal_nis_setprotoent ();
146 /* Get the next entry until we found a correct one. */
152 return NSS_STATUS_NOTFOUND;
153 p = strcpy (buffer, next->val);
159 parse_res = _nss_files_parse_protoent (p, proto, data, buflen);
160 if (!parse_res && errno == ERANGE)
161 return NSS_STATUS_TRYAGAIN;
165 return NSS_STATUS_SUCCESS;
169 _nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen)
171 enum nss_status status;
173 __libc_lock_lock (lock);
175 status = internal_nis_getprotoent_r (proto, buffer, buflen);
177 __libc_lock_unlock (lock);
183 _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
184 char *buffer, size_t buflen)
186 struct parser_data *data = (void *) buffer;
187 enum nss_status retval;
188 char *domain, *result, *p;
193 __set_errno (EINVAL);
194 return NSS_STATUS_UNAVAIL;
197 if (yp_get_default_domain (&domain))
198 return NSS_STATUS_UNAVAIL;
200 retval = yperr2nss (yp_match (domain, "protocols.byname", name,
201 strlen (name), &result, &len));
203 if (retval != NSS_STATUS_SUCCESS)
205 if (retval == NSS_STATUS_TRYAGAIN)
206 __set_errno (EAGAIN);
210 if ((size_t) (len + 1) > buflen)
213 __set_errno (ERANGE);
214 return NSS_STATUS_TRYAGAIN;
217 p = strncpy (buffer, result, len);
223 parse_res = _nss_files_parse_protoent (p, proto, data, buflen);
228 return NSS_STATUS_TRYAGAIN;
230 return NSS_STATUS_NOTFOUND;
233 return NSS_STATUS_SUCCESS;
237 _nss_nis_getprotobynumber_r (int number, struct protoent *proto,
238 char *buffer, size_t buflen)
240 struct parser_data *data = (void *) buffer;
241 enum nss_status retval;
242 char *domain, *result, *p;
243 int len, nlen, parse_res;
246 if (yp_get_default_domain (&domain))
247 return NSS_STATUS_UNAVAIL;
249 nlen = sprintf (buf, "%d", number);
251 retval = yperr2nss (yp_match (domain, "protocols.bynumber", buf,
252 nlen, &result, &len));
254 if (retval != NSS_STATUS_SUCCESS)
256 if (retval == NSS_STATUS_TRYAGAIN)
257 __set_errno (EAGAIN);
261 if ((size_t) (len + 1) > buflen)
264 __set_errno (ERANGE);
265 return NSS_STATUS_TRYAGAIN;
268 p = strncpy (buffer, result, len);
274 parse_res = _nss_files_parse_protoent (p, proto, data, buflen);
279 return NSS_STATUS_TRYAGAIN;
281 return NSS_STATUS_NOTFOUND;
284 return NSS_STATUS_SUCCESS;