getnetnamadr.c revision 157779
13070Spst/*- 23070Spst * Copyright (c) 1994, Garrett Wollman 33070Spst * 43070Spst * Redistribution and use in source and binary forms, with or without 53070Spst * modification, are permitted provided that the following conditions 63070Spst * are met: 73070Spst * 1. Redistributions of source code must retain the above copyright 83070Spst * notice, this list of conditions and the following disclaimer. 93070Spst * 2. Redistributions in binary form must reproduce the above copyright 103070Spst * notice, this list of conditions and the following disclaimer in the 113070Spst * documentation and/or other materials provided with the distribution. 123070Spst * 133070Spst * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 143070Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 153070Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 163070Spst * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 173070Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 183070Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 193070Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 203070Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 213070Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 223070Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 233070Spst * SUCH DAMAGE. 243070Spst */ 253070Spst 2692986Sobrien#include <sys/cdefs.h> 2792986Sobrien__FBSDID("$FreeBSD: head/lib/libc/net/getnetnamadr.c 157779 2006-04-15 16:20:27Z ume $"); 283070Spst 29113977Snectar#include "namespace.h" 30145626Sume#include "reentrant.h" 313070Spst#include <sys/param.h> 323070Spst#include <sys/socket.h> 333070Spst#include <netinet/in.h> 343070Spst#include <arpa/inet.h> 353070Spst#include <netdb.h> 363070Spst#include <stdio.h> 373070Spst#include <ctype.h> 38157779Sume#include <errno.h> 39145626Sume#include <stdlib.h> 403070Spst#include <string.h> 4165532Snectar#include <stdarg.h> 4265532Snectar#include <nsswitch.h> 43113977Snectar#include "un-namespace.h" 44145602Sume#include "netdb_private.h" 453070Spst 4665532Snectarextern int _ht_getnetbyname(void *, void *, va_list); 4765532Snectarextern int _dns_getnetbyname(void *, void *, va_list); 4865532Snectarextern int _nis_getnetbyname(void *, void *, va_list); 4965532Snectarextern int _ht_getnetbyaddr(void *, void *, va_list); 5065532Snectarextern int _dns_getnetbyaddr(void *, void *, va_list); 5165532Snectarextern int _nis_getnetbyaddr(void *, void *, va_list); 523070Spst 5365532Snectar/* Network lookup order if nsswitch.conf is broken or nonexistant */ 54157779Sumestatic const ns_src default_src[] = { 5565532Snectar { NSSRC_FILES, NS_SUCCESS }, 5665532Snectar { NSSRC_DNS, NS_SUCCESS }, 5765532Snectar { 0 } 583070Spst}; 593070Spst 60157779SumeNETDB_THREAD_ALLOC(netent_data) 61157779SumeNETDB_THREAD_ALLOC(netdata) 62145626Sume 63145626Sumestatic void 64157779Sumenetent_data_free(void *ptr) 653070Spst{ 66157779Sume struct netent_data *ned = ptr; 67145626Sume 68157779Sume if (ned == NULL) 69145626Sume return; 70157779Sume ned->stayopen = 0; 71157779Sume _endnethtent(ned); 72157779Sume free(ned); 73145626Sume} 74145626Sume 75145626Sumestatic void 76157779Sumenetdata_free(void *ptr) 77145626Sume{ 78157779Sume free(ptr); 79145626Sume} 80145626Sume 81157779Sumeint 82157779Sume__copy_netent(struct netent *ne, struct netent *nptr, char *buf, size_t buflen) 83145626Sume{ 84157779Sume char *cp; 85157779Sume int i, n; 86157779Sume int numptr, len; 87145626Sume 88157779Sume /* Find out the amount of space required to store the answer. */ 89157779Sume numptr = 1; /* NULL ptr */ 90157779Sume len = (char *)ALIGN(buf) - buf; 91157779Sume for (i = 0; ne->n_aliases[i]; i++, numptr++) { 92157779Sume len += strlen(ne->n_aliases[i]) + 1; 93157779Sume } 94157779Sume len += strlen(ne->n_name) + 1; 95157779Sume len += numptr * sizeof(char*); 96157779Sume 97157779Sume if (len > (int)buflen) { 98157779Sume errno = ERANGE; 99157779Sume return (-1); 100157779Sume } 101157779Sume 102157779Sume /* copy net value and type */ 103157779Sume nptr->n_addrtype = ne->n_addrtype; 104157779Sume nptr->n_net = ne->n_net; 105157779Sume 106157779Sume cp = (char *)ALIGN(buf) + numptr * sizeof(char *); 107157779Sume 108157779Sume /* copy official name */ 109157779Sume n = strlen(ne->n_name) + 1; 110157779Sume strcpy(cp, ne->n_name); 111157779Sume nptr->n_name = cp; 112157779Sume cp += n; 113157779Sume 114157779Sume /* copy aliases */ 115157779Sume nptr->n_aliases = (char **)ALIGN(buf); 116157779Sume for (i = 0 ; ne->n_aliases[i]; i++) { 117157779Sume n = strlen(ne->n_aliases[i]) + 1; 118157779Sume strcpy(cp, ne->n_aliases[i]); 119157779Sume nptr->n_aliases[i] = cp; 120157779Sume cp += n; 121157779Sume } 122157779Sume nptr->n_aliases[i] = NULL; 123157779Sume 124157779Sume return (0); 125145626Sume} 126145626Sume 127145626Sumeint 128157779Sumegetnetbyname_r(const char *name, struct netent *ne, char *buffer, 129157779Sume size_t buflen, struct netent **result, int *h_errorp) 130145626Sume{ 131157779Sume int rval, ret_errno; 1323070Spst 13365532Snectar static const ns_dtab dtab[] = { 13465532Snectar NS_FILES_CB(_ht_getnetbyname, NULL) 13565532Snectar { NSSRC_DNS, _dns_getnetbyname, NULL }, 13665532Snectar NS_NIS_CB(_nis_getnetbyname, NULL) /* force -DHESIOD */ 13765532Snectar { 0 } 138145626Sume }; 13965532Snectar 140157779Sume rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS, 141157779Sume "getnetbyname_r", default_src, name, ne, buffer, buflen, 142157779Sume &ret_errno, h_errorp); 143145626Sume 144157779Sume return ((rval == NS_SUCCESS) ? 0 : -1); 1453070Spst} 1463070Spst 147145626Sumeint 148157779Sumegetnetbyaddr_r(uint32_t addr, int af, struct netent *ne, char *buffer, 149157779Sume size_t buflen, struct netent **result, int *h_errorp) 1503070Spst{ 151157779Sume int rval, ret_errno; 1523070Spst 15365532Snectar static const ns_dtab dtab[] = { 15465532Snectar NS_FILES_CB(_ht_getnetbyaddr, NULL) 15565532Snectar { NSSRC_DNS, _dns_getnetbyaddr, NULL }, 15665532Snectar NS_NIS_CB(_nis_getnetbyaddr, NULL) /* force -DHESIOD */ 15765532Snectar { 0 } 158145626Sume }; 1593070Spst 160157779Sume rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS, 161157779Sume "getnetbyaddr_r", default_src, addr, af, ne, buffer, buflen, 162157779Sume &ret_errno, h_errorp); 16365532Snectar 164157779Sume return ((rval == NS_SUCCESS) ? 0 : -1); 1653070Spst} 1663070Spst 167145626Sumestruct netent * 168145626Sumegetnetbyname(const char *name) 169145626Sume{ 170145626Sume struct netdata *nd; 171157779Sume struct netent *rval; 172157779Sume int ret_h_errno; 173145626Sume 174145626Sume if ((nd = __netdata_init()) == NULL) 175157779Sume return (NULL); 176157779Sume if (getnetbyname_r(name, &nd->net, nd->data, sizeof(nd->data), &rval, 177157779Sume &ret_h_errno) != 0) 178157779Sume return (NULL); 179157779Sume return (rval); 180145626Sume} 181145626Sume 182145626Sumestruct netent * 183146244Sumegetnetbyaddr(uint32_t addr, int af) 184145626Sume{ 185145626Sume struct netdata *nd; 186157779Sume struct netent *rval; 187157779Sume int ret_h_errno; 188145626Sume 189145626Sume if ((nd = __netdata_init()) == NULL) 190157779Sume return (NULL); 191157779Sume if (getnetbyaddr_r(addr, af, &nd->net, nd->data, sizeof(nd->data), 192157779Sume &rval, &ret_h_errno) != 0) 193157779Sume return (NULL); 194157779Sume return (rval); 195145626Sume} 196145626Sume 197145626Sumevoid 198145626Sumesetnetent(int stayopen) 199145626Sume{ 200157779Sume struct netent_data *ned; 201145626Sume 202157779Sume if ((ned = __netent_data_init()) == NULL) 203145626Sume return; 204157779Sume _setnethtent(stayopen, ned); 205157779Sume _setnetdnsent(stayopen); 206145626Sume} 207145626Sume 208145626Sumevoid 209145626Sumeendnetent(void) 210145626Sume{ 211157779Sume struct netent_data *ned; 212145626Sume 213157779Sume if ((ned = __netent_data_init()) == NULL) 214145626Sume return; 215157779Sume _endnethtent(ned); 216157779Sume _endnetdnsent(); 217145626Sume} 218