1 /* Copyright (C) 1997-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
21 #include <netinet/in.h>
27 NSS_DECLARE_MODULE_FUNCTIONS (hesiod
)
29 /* Declare a parser for Hesiod protocol entries. Although the format
30 of the entries is identical to those in /etc/protocols, here is no
31 predefined parser for us to use. */
33 #define ENTNAME protoent
35 struct protoent_data
{};
37 #define TRAILING_LIST_MEMBER p_aliases
38 #define TRAILING_LIST_SEPARATOR_P isspace
39 #include <nss/nss_files/files-parse.c>
42 STRING_FIELD (result
->p_name
, isspace
, 1);
43 INT_FIELD (result
->p_proto
, isspace
, 1, 10,);
47 _nss_hesiod_setprotoent (int stayopen
)
49 return NSS_STATUS_SUCCESS
;
53 _nss_hesiod_endprotoent (void)
55 return NSS_STATUS_SUCCESS
;
58 static enum nss_status
59 lookup (const char *name
, const char *type
, struct protoent
*proto
,
60 char *buffer
, size_t buflen
, int *errnop
)
62 struct parser_data
*data
= (void *) buffer
;
70 if (hesiod_init (&context
) < 0)
71 return NSS_STATUS_UNAVAIL
;
73 list
= hesiod_resolve (context
, name
, type
);
79 return err
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
82 linebuflen
= buffer
+ buflen
- data
->linebuffer
;
88 size_t len
= strlen (*item
) + 1;
92 hesiod_free_list (context
, list
);
95 return NSS_STATUS_TRYAGAIN
;
98 memcpy (data
->linebuffer
, *item
, len
);
100 parse_res
= parse_line (buffer
, proto
, data
, buflen
, errnop
);
103 hesiod_free_list (context
, list
);
104 hesiod_end (context
);
105 return NSS_STATUS_TRYAGAIN
;
113 while (*item
!= NULL
&& !found
);
115 hesiod_free_list (context
, list
);
116 hesiod_end (context
);
120 __set_errno (olderr
);
121 return NSS_STATUS_NOTFOUND
;
124 return NSS_STATUS_SUCCESS
;
128 _nss_hesiod_getprotobyname_r (const char *name
, struct protoent
*proto
,
129 char *buffer
, size_t buflen
, int *errnop
)
131 return lookup (name
, "protocol", proto
, buffer
, buflen
, errnop
);
135 _nss_hesiod_getprotobynumber_r (const int protocol
, struct protoent
*proto
,
136 char *buffer
, size_t buflen
, int *errnop
)
140 snprintf (protostr
, sizeof protostr
, "%d", protocol
);
142 return lookup (protostr
, "protonum", proto
, buffer
, buflen
, errnop
);