11573Srgrimes/*
21573Srgrimes * Copyright (c) 1983, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 4. Neither the name of the University nor the names of its contributors
141573Srgrimes *    may be used to endorse or promote products derived from this software
151573Srgrimes *    without specific prior written permission.
161573Srgrimes *
171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271573Srgrimes * SUCH DAMAGE.
281573Srgrimes */
291573Srgrimes
301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
311573Srgrimesstatic char sccsid[] = "@(#)getprotoname.c	8.1 (Berkeley) 6/4/93";
321573Srgrimes#endif /* LIBC_SCCS and not lint */
3392889Sobrien#include <sys/cdefs.h>
3492889Sobrien__FBSDID("$FreeBSD$");
351573Srgrimes
36211276Sume#include <errno.h>
371573Srgrimes#include <netdb.h>
38158115Sume#include <nsswitch.h>
391573Srgrimes#include <string.h>
40145279Sume#include "netdb_private.h"
41158115Sume#ifdef NS_CACHING
42158115Sume#include "nscache.h"
43158115Sume#endif
44158115Sume#include "nss_tls.h"
451573Srgrimes
46158115Sumestatic const ns_src defaultsrc[] = {
47158115Sume	{ NSSRC_FILES, NS_SUCCESS },
48158115Sume	{ NULL, 0 }
49158115Sume};
50158115Sume
51158115Sume#ifdef NS_CACHING
52158115Sumeextern int __proto_id_func(char *, size_t *, va_list, void *);
53158115Sumeextern int __proto_marshal_func(char *, size_t *, void *, va_list, void *);
54158115Sumeextern int __proto_unmarshal_func(char *, size_t, void *, va_list, void *);
55158115Sume#endif
56158115Sume
57158115Sumestatic int
58158115Sumefiles_getprotobyname(void *retval, void *mdata, va_list ap)
591573Srgrimes{
60157779Sume	struct protoent pe;
61157779Sume	struct protoent_data *ped;
6292889Sobrien	char **cp;
63145279Sume	int error;
641573Srgrimes
65158115Sume	char *name;
66158115Sume	struct protoent	*pptr;
67158115Sume	char *buffer;
68158115Sume	size_t buflen;
69158115Sume	int *errnop;
70157779Sume
71158115Sume	name = va_arg(ap, char *);
72158115Sume	pptr = va_arg(ap, struct protoent *);
73158115Sume	buffer = va_arg(ap, char *);
74158115Sume	buflen = va_arg(ap, size_t);
75158115Sume	errnop = va_arg(ap, int *);
76158115Sume
77158115Sume
78158115Sume	if ((ped = __protoent_data_init()) == NULL) {
79211276Sume		*errnop = errno;
80158115Sume		return (NS_NOTFOUND);
81158115Sume	}
82158115Sume
83157779Sume	__setprotoent_p(ped->stayopen, ped);
84157779Sume	while ((error = __getprotoent_p(&pe, ped)) == 0) {
85157779Sume		if (strcmp(pe.p_name, name) == 0)
861573Srgrimes			break;
87157779Sume		for (cp = pe.p_aliases; *cp != 0; cp++)
881573Srgrimes			if (strcmp(*cp, name) == 0)
891573Srgrimes				goto found;
901573Srgrimes	}
911573Srgrimesfound:
92145279Sume	if (!ped->stayopen)
93157779Sume		__endprotoent_p(ped);
94158115Sume	if (error != 0) {
95211276Sume		*errnop = errno;
96158115Sume		return (NS_NOTFOUND);
97158115Sume	}
98158115Sume	if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
99211276Sume		*errnop = errno;
100211276Sume		return (NS_RETURN);
101158115Sume	}
102158115Sume
103158115Sume	*((struct protoent **)retval) = pptr;
104158115Sume	return (NS_SUCCESS);
1051573Srgrimes}
106145279Sume
107158115Sume
108158115Sumeint
109158115Sumegetprotobyname_r(const char *name, struct protoent *pptr, char *buffer,
110158115Sume    size_t buflen, struct protoent **result)
111158115Sume{
112158115Sume#ifdef NS_CACHING
113158115Sume	static const nss_cache_info cache_info =
114158115Sume		NS_COMMON_CACHE_INFO_INITIALIZER(
115158115Sume		protocols, (void *)nss_lt_name,
116158115Sume		__proto_id_func, __proto_marshal_func, __proto_unmarshal_func);
117158115Sume#endif
118158115Sume	static const ns_dtab dtab[] = {
119158115Sume		{ NSSRC_FILES, files_getprotobyname, NULL },
120158115Sume#ifdef NS_CACHING
121158115Sume		NS_CACHE_CB(&cache_info)
122158115Sume#endif
123158115Sume		{ NULL, NULL, NULL }
124158115Sume	};
125158115Sume	int	rv, ret_errno;
126158115Sume
127158115Sume	ret_errno = 0;
128158115Sume	*result = NULL;
129158115Sume	rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotobyname_r",
130158115Sume	    defaultsrc, name, pptr, buffer, buflen, &ret_errno);
131158115Sume
132211276Sume	if (rv != NS_SUCCESS) {
133211276Sume		errno = ret_errno;
134213453Sume		return (ret_errno);
135211276Sume	}
136211276Sume	return (0);
137158115Sume}
138158115Sume
139145279Sumestruct protoent *
140145279Sumegetprotobyname(const char *name)
141145279Sume{
142145279Sume	struct protodata *pd;
143157779Sume	struct protoent *rval;
144145279Sume
145145279Sume	if ((pd = __protodata_init()) == NULL)
146145279Sume		return (NULL);
147157779Sume	if (getprotobyname_r(name, &pd->proto, pd->data, sizeof(pd->data),
148157779Sume	    &rval) != 0)
149145279Sume		return (NULL);
150157779Sume	return (rval);
151145279Sume}
152