4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <nss_dbdefs.h>
34 int str2protoent(const char *, int, void *,
37 static int proto_stayopen
;
39 * Unsynchronized, but it affects only
40 * efficiency, not correctness
43 static DEFINE_NSS_DB_ROOT(db_root
);
44 static DEFINE_NSS_GETENT(context
);
47 _nss_initf_proto(nss_db_params_t
*p
)
49 p
->name
= NSS_DBNAM_PROTOCOLS
;
50 p
->default_config
= NSS_DEFCONF_PROTOCOLS
;
54 getprotobyname_r(const char *name
, struct protoent
*result
,
55 char *buffer
, int buflen
)
60 if (name
== (const char *)NULL
) {
64 NSS_XbyY_INIT(&arg
, result
, buffer
, buflen
, str2protoent
);
66 arg
.stayopen
= proto_stayopen
;
67 res
= nss_search(&db_root
, _nss_initf_proto
,
68 NSS_DBOP_PROTOCOLS_BYNAME
, &arg
);
70 (void) NSS_XbyY_FINI(&arg
);
71 return ((struct protoent
*)arg
.returnval
);
75 getprotobynumber_r(int proto
, struct protoent
*result
, char *buffer
, int buflen
)
80 NSS_XbyY_INIT(&arg
, result
, buffer
, buflen
, str2protoent
);
81 arg
.key
.number
= proto
;
82 arg
.stayopen
= proto_stayopen
;
83 res
= nss_search(&db_root
, _nss_initf_proto
,
84 NSS_DBOP_PROTOCOLS_BYNUMBER
, &arg
);
86 (void) NSS_XbyY_FINI(&arg
);
87 return ((struct protoent
*)arg
.returnval
);
93 proto_stayopen
= stay
;
94 nss_setent(&db_root
, _nss_initf_proto
, &context
);
102 nss_endent(&db_root
, _nss_initf_proto
, &context
);
103 nss_delete(&db_root
);
108 getprotoent_r(struct protoent
*result
, char *buffer
, int buflen
)
113 NSS_XbyY_INIT(&arg
, result
, buffer
, buflen
, str2protoent
);
114 /* No stayopen flag; of course you stay open for iteration */
115 res
= nss_getent(&db_root
, _nss_initf_proto
, &context
, &arg
);
117 (void) NSS_XbyY_FINI(&arg
);
118 return ((struct protoent
*)arg
.returnval
);
122 * Return values: 0 = success, 1 = parse error, 2 = erange ...
123 * The structure pointer passed in is a structure in the caller's space
124 * wherein the field pointers would be set to areas in the buffer if
125 * need be. instring and buffer should be separate areas. Let's not
129 str2protoent(const char *instr
, int lenstr
,
130 void *ent
/* it is really (struct protoent *) */,
131 char *buffer
, int buflen
)
133 struct protoent
*proto
= (struct protoent
*)ent
;
134 const char *p
, *numstart
, *namestart
, *limit
;
135 int numlen
, namelen
= 0;
139 if ((instr
>= buffer
&& (buffer
+ buflen
) > instr
) ||
140 (buffer
>= instr
&& (instr
+ lenstr
) > buffer
)) {
141 return (NSS_STR_PARSE_PARSE
);
147 while (p
< limit
&& isspace(*p
)) {
151 while (p
< limit
&& !isspace(*p
)) {
152 p
++; /* Skip over the canonical name */
154 namelen
= (int)(p
- namestart
);
156 if (buflen
<= namelen
) { /* not enough buffer */
157 return (NSS_STR_PARSE_ERANGE
);
159 (void) memcpy(buffer
, namestart
, namelen
);
160 buffer
[namelen
] = '\0';
161 proto
->p_name
= buffer
;
163 while (p
< limit
&& isspace(*p
)) {
167 /* Syntax error -- no proto number */
168 return (NSS_STR_PARSE_PARSE
);
172 p
++; /* Find the end of the proto number */
173 } while (p
< limit
&& !isspace(*p
));
174 numlen
= (int)(p
- numstart
);
175 if (numlen
>= (int)sizeof (numbuf
)) {
176 /* Syntax error -- supposed number is too long */
177 return (NSS_STR_PARSE_PARSE
);
179 (void) memcpy(numbuf
, numstart
, (size_t)numlen
);
180 numbuf
[numlen
] = '\0';
181 proto
->p_proto
= (int)strtol(numbuf
, &numend
, 10);
182 if (*numend
!= '\0') {
183 /* Syntax error -- protocol number isn't a number */
184 return (NSS_STR_PARSE_PARSE
);
187 while (p
< limit
&& isspace(*p
)) {
191 * Although nss_files_XY_all calls us with # stripped,
192 * we should be able to deal with it here in order to
195 if (p
>= limit
|| *p
== '#') { /* no aliases, no problem */
198 ptr
= (char **)ROUND_UP(buffer
+ namelen
+ 1,
200 if ((char *)ptr
>= buffer
+ buflen
) {
201 /* hope they don't try to peek in */
202 proto
->p_aliases
= 0;
203 return (NSS_STR_PARSE_ERANGE
);
206 proto
->p_aliases
= ptr
;
207 return (NSS_STR_PARSE_SUCCESS
);
210 proto
->p_aliases
= _nss_netdb_aliases(p
, lenstr
- (int)(p
- instr
),
211 buffer
+ namelen
+ 1, buflen
- namelen
- 1);
212 return (NSS_STR_PARSE_SUCCESS
);