getnetnamadr.c revision 113977
1193323Sed/*- 2193323Sed * Copyright (c) 1994, Garrett Wollman 3193323Sed * 4193323Sed * Redistribution and use in source and binary forms, with or without 5193323Sed * modification, are permitted provided that the following conditions 6193323Sed * are met: 7193323Sed * 1. Redistributions of source code must retain the above copyright 8193323Sed * notice, this list of conditions and the following disclaimer. 9193323Sed * 2. Redistributions in binary form must reproduce the above copyright 10210299Sed * notice, this list of conditions and the following disclaimer in the 11198090Srdivacky * documentation and/or other materials provided with the distribution. 12193323Sed * 13198090Srdivacky * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 14198090Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15198090Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16198090Srdivacky * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21249423Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22249423Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23249423Sdim * SUCH DAMAGE. 24193323Sed */ 25207618Srdivacky 26249423Sdim#include <sys/cdefs.h> 27212904Sdim__FBSDID("$FreeBSD: head/lib/libc/net/getnetnamadr.c 113977 2003-04-24 18:05:48Z nectar $"); 28193323Sed 29198090Srdivacky#include "namespace.h" 30249423Sdim#include <sys/param.h> 31249423Sdim#include <sys/socket.h> 32249423Sdim#include <netinet/in.h> 33193323Sed#include <arpa/inet.h> 34193323Sed#include <netdb.h> 35234353Sdim#include <stdio.h> 36212904Sdim#include <ctype.h> 37212904Sdim#include <string.h> 38218893Sdim#include <stdarg.h> 39193323Sed#include <nsswitch.h> 40218893Sdim#include "un-namespace.h" 41218893Sdim 42218893Sdimextern int _ht_getnetbyname(void *, void *, va_list); 43212904Sdimextern int _dns_getnetbyname(void *, void *, va_list); 44193323Sedextern int _nis_getnetbyname(void *, void *, va_list); 45198892Srdivackyextern int _ht_getnetbyaddr(void *, void *, va_list); 46193323Sedextern int _dns_getnetbyaddr(void *, void *, va_list); 47198090Srdivackyextern int _nis_getnetbyaddr(void *, void *, va_list); 48218893Sdim 49198090Srdivacky/* Network lookup order if nsswitch.conf is broken or nonexistant */ 50207618Srdivackystatic const ns_src default_src[] = { 51198090Srdivacky { NSSRC_FILES, NS_SUCCESS }, 52193323Sed { NSSRC_DNS, NS_SUCCESS }, 53218893Sdim { 0 } 54218893Sdim}; 55218893Sdim 56218893Sdimstruct netent * 57193323Sedgetnetbyname(const char *name) 58193323Sed{ 59218893Sdim struct netent *hp = 0; 60218893Sdim int rval; 61218893Sdim 62210299Sed 63193323Sed static const ns_dtab dtab[] = { 64210299Sed NS_FILES_CB(_ht_getnetbyname, NULL) 65193323Sed { NSSRC_DNS, _dns_getnetbyname, NULL }, 66198090Srdivacky NS_NIS_CB(_nis_getnetbyname, NULL) /* force -DHESIOD */ 67193323Sed { 0 } 68198090Srdivacky }; 69193323Sed 70207618Srdivacky rval = _nsdispatch((void *)&hp, dtab, NSDB_NETWORKS, "getnetbyname", 71193323Sed default_src, name); 72207618Srdivacky 73193323Sed if (rval != NS_SUCCESS) 74218893Sdim return NULL; 75218893Sdim else 76218893Sdim return hp; 77218893Sdim} 78218893Sdim 79193323Sedstruct netent * 80193323Sedgetnetbyaddr(u_long addr, int af) 81218893Sdim{ 82218893Sdim struct netent *hp = 0; 83218893Sdim int rval; 84218893Sdim 85218893Sdim static const ns_dtab dtab[] = { 86218893Sdim NS_FILES_CB(_ht_getnetbyaddr, NULL) 87218893Sdim { NSSRC_DNS, _dns_getnetbyaddr, NULL }, 88193323Sed NS_NIS_CB(_nis_getnetbyaddr, NULL) /* force -DHESIOD */ 89212904Sdim { 0 } 90218893Sdim }; 91218893Sdim 92234353Sdim rval = _nsdispatch((void *)&hp, dtab, NSDB_NETWORKS, "getnetbyaddr", 93234353Sdim default_src, addr, af); 94234353Sdim 95234353Sdim if (rval != NS_SUCCESS) 96234353Sdim return NULL; 97234353Sdim else 98218893Sdim return hp; 99218893Sdim} 100193323Sed 101239462Sdimvoid 102239462Sdimsetnetent(stayopen) 103239462Sdim int stayopen; 104239462Sdim{ 105239462Sdim _setnethtent(stayopen); 106239462Sdim _setnetdnsent(stayopen); 107239462Sdim} 108239462Sdim 109239462Sdimvoid 110239462Sdimendnetent() 111193323Sed{ 112210299Sed _endnethtent(); 113193323Sed _endnetdnsent(); 114234353Sdim} 115218893Sdim