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
23 #include <netinet/in.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))
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
);
50 _nss_hesiod_setservent (int stayopen
)
52 return NSS_STATUS_SUCCESS
;
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
;
73 context
= _nss_hesiod_init ();
75 return NSS_STATUS_UNAVAIL
;
77 list
= hesiod_resolve (context
, name
, type
);
83 return err
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
86 linebuflen
= buffer
+ buflen
- data
->linebuffer
;
92 size_t len
= strlen (*item
) + 1;
96 hesiod_free_list (context
, list
);
99 return NSS_STATUS_TRYAGAIN
;
102 memcpy (data
->linebuffer
, *item
, len
);
104 parse_res
= parse_line (buffer
, serv
, data
, buflen
, errnop
);
107 hesiod_free_list (context
, list
);
108 hesiod_end (context
);
109 return NSS_STATUS_TRYAGAIN
;
113 found
= protocol
== NULL
|| strcasecmp (serv
->s_proto
, protocol
) == 0;
117 while (*item
!= NULL
&& !found
);
119 hesiod_free_list (context
, list
);
120 hesiod_end (context
);
124 __set_errno (olderr
);
125 return NSS_STATUS_NOTFOUND
;
128 return NSS_STATUS_SUCCESS
;
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
);
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
);