/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1983, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <errno.h>32#include <netdb.h>33#include <nsswitch.h>34#include "netdb_private.h"35#ifdef NS_CACHING36#include "nscache.h"37#endif38#include "nss_tls.h"3940static const ns_src defaultsrc[] = {41{ NSSRC_FILES, NS_SUCCESS },42{ NULL, 0 }43};4445static int46files_getprotobynumber(void *retval, void *mdata, va_list ap)47{48struct protoent pe;49struct protoent_data *ped;50int error;5152int number;53struct protoent *pptr;54char *buffer;55size_t buflen;56int *errnop;5758number = va_arg(ap, int);59pptr = va_arg(ap, struct protoent *);60buffer = va_arg(ap, char *);61buflen = va_arg(ap, size_t);62errnop = va_arg(ap, int *);6364if ((ped = __protoent_data_init()) == NULL) {65*errnop = errno;66return (NS_NOTFOUND);67}6869__setprotoent_p(ped->stayopen, ped);70while ((error = __getprotoent_p(&pe, ped)) == 0)71if (pe.p_proto == number)72break;73if (!ped->stayopen)74__endprotoent_p(ped);75if (error != 0) {76*errnop = errno;77return (NS_NOTFOUND);78}79if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {80*errnop = errno;81return (NS_RETURN);82}8384*((struct protoent **)retval) = pptr;85return (NS_SUCCESS);86}8788int89getprotobynumber_r(int proto, struct protoent *pptr, char *buffer,90size_t buflen, struct protoent **result)91{92#ifdef NS_CACHING93static const nss_cache_info cache_info =94NS_COMMON_CACHE_INFO_INITIALIZER(95protocols, (void *)nss_lt_id,96__proto_id_func, __proto_marshal_func, __proto_unmarshal_func);97#endif9899static const ns_dtab dtab[] = {100{ NSSRC_FILES, files_getprotobynumber, NULL },101#ifdef NS_CACHING102NS_CACHE_CB(&cache_info)103#endif104{ NULL, NULL, NULL }105};106int rv, ret_errno;107108ret_errno = 0;109*result = NULL;110rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotobynumber_r",111defaultsrc, proto, pptr, buffer, buflen, &ret_errno);112113if (rv != NS_SUCCESS) {114errno = ret_errno;115return (ret_errno);116}117return (0);118}119120struct protoent *121getprotobynumber(int proto)122{123struct protodata *pd;124struct protoent *rval;125126if ((pd = __protodata_init()) == NULL)127return (NULL);128if (getprotobynumber_r(proto, &pd->proto, pd->data, sizeof(pd->data),129&rval) != 0)130return (NULL);131return (rval);132}133134135