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>
24 #include <netinet/in.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) == ';')
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)
61 if (hesiod_init (&context
) == -1)
62 return NSS_STATUS_UNAVAIL
;
65 return NSS_STATUS_SUCCESS
;
69 _nss_hesiod_setservent (void)
71 enum nss_status status
;
73 __libc_lock_lock (lock
);
75 status
= internal_setservent ();
77 __libc_lock_unlock (lock
);
83 _nss_hesiod_endservent (void)
85 __libc_lock_lock (lock
);
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
;
109 status
= internal_setservent ();
110 if (status
!= NSS_STATUS_SUCCESS
)
113 list
= hesiod_resolve (context
, name
, "service");
115 return errno
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
117 linebuflen
= buffer
+ buflen
- data
->linebuffer
;
123 size_t len
= strlen (*item
) + 1;
125 if (linebuflen
< len
)
127 hesiod_free_list (context
, list
);
129 return NSS_STATUS_TRYAGAIN
;
132 memcpy (data
->linebuffer
, *item
, len
);
134 parse_res
= parse_line (buffer
, serv
, data
, buflen
, errnop
);
137 hesiod_free_list (context
, list
);
138 return NSS_STATUS_TRYAGAIN
;
142 found
= protocol
== NULL
|| strcmp (serv
->s_proto
, protocol
) == 0;
146 while (*item
!= NULL
&& !found
);
148 hesiod_free_list (context
, list
);
150 return found
? NSS_STATUS_SUCCESS
: NSS_STATUS_NOTFOUND
;
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
);