2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * This is where we have chosen to combine every useful bit of code for
2N/A * all the Solaris frontends to lookup hosts, services, and netdir information
2N/A * for inet family (udp, tcp) transports. gethostbyYY(), getservbyYY(), and
2N/A * netdir_getbyYY() are all implemented on top of this code. Similarly,
2N/A * netdir_options, taddr2uaddr, and uaddr2taddr for inet transports also
2N/A * find a home here.
2N/A *
2N/A * If the netconfig structure supplied has NO nametoaddr libs (i.e. a "-"
2N/A * in /etc/netconfig), this code calls the name service switch, and
2N/A * therefore, /etc/nsswitch.conf is effectively the only place that
2N/A * dictates hosts/serv lookup policy.
2N/A * If an administrator chooses to bypass the name service switch by
2N/A * specifying third party supplied nametoaddr libs in /etc/netconfig, this
2N/A * implementation does NOT call the name service switch, it merely loops
2N/A * through the nametoaddr libs. In this case, if this code was called
2N/A * from gethost/servbyYY() we marshal the inet specific struct into
2N/A * transport independent netbuf or hostserv, and unmarshal the resulting
2N/A * nd_addrlist or hostservlist back into hostent and servent, as the case
2N/A * may be.
2N/A *
2N/A * Goes without saying that most of the future bugs in gethost/servbyYY
2N/A * and netdir_getbyYY are lurking somewhere here.
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include <ctype.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A#include <stropts.h>
2N/A#include <sys/types.h>
2N/A#include <sys/byteorder.h>
2N/A#include <sys/ioctl.h>
2N/A#include <sys/param.h>
2N/A#include <sys/time.h>
2N/A#include <errno.h>
2N/A#include <fcntl.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A#include <sys/utsname.h>
2N/A#include <netdb.h>
2N/A#include <netconfig.h>
2N/A#include <netdir.h>
2N/A#include <tiuser.h>
2N/A#include <sys/socket.h>
2N/A#include <sys/sockio.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <net/if.h>
2N/A#include <inet/ip.h>
2N/A#include <inet/ip6_asp.h>
2N/A#include <sys/dlpi.h>
2N/A#include <nss_dbdefs.h>
2N/A#include <nss_netdir.h>
2N/A#include <syslog.h>
2N/A#include <nsswitch.h>
2N/A#include <note.h>
2N/A#include "nss.h"
2N/A
2N/A#define MAXIFS 32
2N/A#define UDPDEV "/dev/udp"
2N/A#define UDP6DEV "/dev/udp6"
2N/A
2N/A#define DOOR_GETHOSTBYNAME_R _switch_gethostbyname_r
2N/A#define DOOR_GETHOSTBYADDR_R _switch_gethostbyaddr_r
2N/A#define DOOR_GETIPNODEBYNAME_R _switch_getipnodebyname_r
2N/A#define DOOR_GETIPNODEBYADDR_R _switch_getipnodebyaddr_r
2N/A
2N/A#define DONT_SORT "SORT_ADDRS=NO"
2N/A#define DONT_SORT2 "SORT_ADDRS=FALSE"
2N/A#define LINESIZE 100
2N/A
2N/A/*
2N/A * constant values of addresses for HOST_SELF_BIND, HOST_SELF_CONNECT
2N/A * and localhost.
2N/A *
2N/A * The following variables are static to the extent that they should
2N/A * not be visible outside of this file.
2N/A */
2N/Astatic char *localaddr[] = {"\000\000\000\000", NULL};
2N/Astatic char *connectaddr[] = {"\177\000\000\001", NULL};
2N/Astatic char *localaddr6[] =
2N/A{"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000", NULL};
2N/Astatic char *connectaddr6[] =
2N/A{"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001", NULL};
2N/A
2N/A/* IPv4 nd_addrlist */
2N/Astatic mutex_t nd_addr_lock = DEFAULTMUTEX;
2N/Astatic struct sockaddr_in sa_con;
2N/Astatic struct netbuf nd_conbuf = {sizeof (sa_con),\
2N/A sizeof (sa_con), (char *)&sa_con};
2N/Astatic struct nd_addrlist nd_conaddrlist = {1, &nd_conbuf};
2N/A
2N/A/* IPv6 nd_addrlist */
2N/Astatic mutex_t nd6_addr_lock = DEFAULTMUTEX;
2N/Astatic struct sockaddr_in6 sa6_con;
2N/Astatic struct netbuf nd6_conbuf = {sizeof (sa6_con),\
2N/A sizeof (sa6_con), (char *)&sa6_con};
2N/Astatic struct nd_addrlist nd6_conaddrlist = {1, &nd6_conbuf};
2N/A
2N/A#define LOCALHOST "localhost"
2N/A
2N/Astruct servent *_switch_getservbyname_r(const char *, const char *,
2N/A struct servent *, char *, int);
2N/Astruct servent *_switch_getservbyport_r(int, const char *, struct servent *,
2N/A char *, int);
2N/A
2N/Astatic int __herrno2netdir(int h_errnop);
2N/Astatic struct ifinfo *get_local_info(void);
2N/Astatic int getbroadcastnets(struct netconfig *, struct in_addr **);
2N/Astatic int hent2ndaddr(int, char **, int *, struct nd_addrlist **);
2N/Astatic int ndaddr2hent(int, const char *, struct nd_addrlist *,
2N/A struct hostent *, char *, int);
2N/Astatic int hsents2ndhostservs(struct hostent *, struct servent *, ushort_t,
2N/A struct nd_hostservlist **);
2N/Astatic int ndaddr2srent(const char *, const char *, ushort_t, struct servent *,
2N/A char *, int);
2N/Astatic int ndhostserv2hent(struct netbuf *, struct nd_hostservlist *,
2N/A struct hostent *, char *, int);
2N/Astatic int ndhostserv2srent(int, const char *, struct nd_hostservlist *,
2N/A struct servent *, char *, int);
2N/Astatic int nd2herrno(int nerr);
2N/Astatic void order_haddrlist_inet(char **haddrlist, size_t addrcount);
2N/Astatic void order_haddrlist_inet6(char **haddrlist, size_t addrcount);
2N/Astatic int dstcmp(const void *, const void *);
2N/Astatic int nss_strioctl(int af, int cmd, void *ptr, int ilen);
2N/Astatic struct in_addr _inet_makeaddr(in_addr_t, in_addr_t);
2N/Astatic boolean_t _read_nsw_file(void);
2N/A
2N/A/*
2N/A * Begin: PART I
2N/A * Top Level Interfaces that gethost/serv/netdir funnel through.
2N/A */
2N/A
2N/A/*
2N/A * gethost/servbyname always call this function; if they call
2N/A * with nametoaddr libs in nconf, we call netdir_getbyname
2N/A * implementation: __classic_netdir_getbyname, otherwise nsswitch.
2N/A *
2N/A * netdir_getbyname calls this only if nametoaddr libs are NOT
2N/A * specified for inet transports; i.e. it's supposed to follow
2N/A * the name service switch.
2N/A */
2N/Aint
2N/A_get_hostserv_inetnetdir_byname(struct netconfig *nconf,
2N/A struct nss_netdirbyname_in *args, union nss_netdirbyname_out *res)
2N/A{
2N/A int server_port;
2N/A int *servp = &server_port;
2N/A char **haddrlist;
2N/A uint32_t dotnameaddr;
2N/A char *dotnamelist[2];
2N/A struct in_addr *inaddrs = NULL;
2N/A struct in6_addr v6nameaddr;
2N/A char **baddrlist = NULL;
2N/A
2N/A
2N/A if (nconf == NULL) {
2N/A _nderror = ND_BADARG;
2N/A return (ND_BADARG);
2N/A }
2N/A
2N/A /*
2N/A * 1. gethostbyname()/netdir_getbyname() special cases:
2N/A */
2N/A switch (args->op_t) {
2N/A
2N/A case NSS_HOST:
2N/A /*
2N/A * Worth the performance gain -- assuming a lot of inet apps
2N/A * actively use "localhost".
2N/A */
2N/A if (strcmp(args->arg.nss.host.name, LOCALHOST) == 0) {
2N/A
2N/A (void) mutex_lock(&nd_addr_lock);
2N/A IN_SET_LOOPBACK_ADDR(&sa_con);
2N/A _nderror = ndaddr2hent(AF_INET, args->arg.nss.host.name,
2N/A &nd_conaddrlist, res->nss.host.hent,
2N/A args->arg.nss.host.buf,
2N/A args->arg.nss.host.buflen);
2N/A (void) mutex_unlock(&nd_addr_lock);
2N/A if (_nderror != ND_OK)
2N/A *(res->nss.host.herrno_p) =
2N/A nd2herrno(_nderror);
2N/A return (_nderror);
2N/A }
2N/A /*
2N/A * If the caller passed in a dot separated IP notation to
2N/A * gethostbyname, return that back as the address.
2N/A * The nd_addr_lock mutex was added to be truely re-entrant.
2N/A */
2N/A if (inet_aton(args->arg.nss.host.name,
2N/A (struct in_addr *)&dotnameaddr)) {
2N/A (void) mutex_lock(&nd_addr_lock);
2N/A (void) memset(&sa_con, 0, sizeof (sa_con));
2N/A sa_con.sin_family = AF_INET;
2N/A sa_con.sin_addr.s_addr = dotnameaddr;
2N/A _nderror = ndaddr2hent(AF_INET, args->arg.nss.host.name,
2N/A &nd_conaddrlist, res->nss.host.hent,
2N/A args->arg.nss.host.buf,
2N/A args->arg.nss.host.buflen);
2N/A (void) mutex_unlock(&nd_addr_lock);
2N/A if (_nderror != ND_OK)
2N/A *(res->nss.host.herrno_p) =
2N/A nd2herrno(_nderror);
2N/A return (_nderror);
2N/A }
2N/A break;
2N/A
2N/A case NSS_HOST6:
2N/A /*
2N/A * Handle case of literal address string.
2N/A */
2N/A if (strchr(args->arg.nss.host6.name, ':') != NULL &&
2N/A (inet_pton(AF_INET6, args->arg.nss.host6.name,
2N/A &v6nameaddr) != 0)) {
2N/A int ret;
2N/A
2N/A (void) mutex_lock(&nd6_addr_lock);
2N/A (void) memset(&sa6_con, 0, sizeof (sa6_con));
2N/A sa6_con.sin6_family = AF_INET6;
2N/A (void) memcpy(&(sa6_con.sin6_addr.s6_addr),
2N/A &v6nameaddr, sizeof (struct in6_addr));
2N/A ret = ndaddr2hent(AF_INET6,
2N/A args->arg.nss.host6.name,
2N/A &nd6_conaddrlist, res->nss.host.hent,
2N/A args->arg.nss.host6.buf,
2N/A args->arg.nss.host6.buflen);
2N/A (void) mutex_unlock(&nd6_addr_lock);
2N/A if (ret != ND_OK)
2N/A *(res->nss.host.herrno_p) = nd2herrno(ret);
2N/A else
2N/A res->nss.host.hent->h_aliases = NULL;
2N/A return (ret);
2N/A }
2N/A break;
2N/A
2N/A case NETDIR_BY:
2N/A if (args->arg.nd_hs == 0) {
2N/A _nderror = ND_BADARG;
2N/A return (ND_BADARG);
2N/A }
2N/A /*
2N/A * If servname is NULL, return 0 as the port number
2N/A * If servname is rpcbind, return 111 as the port number
2N/A * If servname is a number, return it back as the port
2N/A * number.
2N/A */
2N/A if (args->arg.nd_hs->h_serv == 0) {
2N/A *servp = htons(0);
2N/A } else if (strcmp(args->arg.nd_hs->h_serv,
2N/A "rpcbind") == 0) {
2N/A *servp = htons(111);
2N/A } else if (strspn(args->arg.nd_hs->h_serv,
2N/A "0123456789") ==
2N/A strlen(args->arg.nd_hs->h_serv)) {
2N/A *servp = htons(atoi(args->arg.nd_hs->h_serv));
2N/A } else {
2N/A /* i.e. need to call a name service on this */
2N/A servp = NULL;
2N/A }
2N/A
2N/A /*
2N/A * If the hostname is HOST_SELF_BIND, we return 0.0.0.0
2N/A * so the binding can be contacted through all
2N/A * interfaces. If the hostname is HOST_SELF_CONNECT,
2N/A * we return 127.0.0.1 so the address can be connected
2N/A * to locally. If the hostname is HOST_ANY, we return
2N/A * no addresses because IP doesn't know how to specify
2N/A * a service without a host. And finally if we specify
2N/A * HOST_BROADCAST then we ask a tli fd to tell us what
2N/A * the broadcast addresses are for any udp
2N/A * interfaces on this machine.
2N/A */
2N/A if (args->arg.nd_hs->h_host == 0) {
2N/A _nderror = ND_NOHOST;
2N/A return (ND_NOHOST);
2N/A } else if ((strcmp(args->arg.nd_hs->h_host,
2N/A HOST_SELF_BIND) == 0)) {
2N/A haddrlist = localaddr;
2N/A } else if ((strcmp(args->arg.nd_hs->h_host,
2N/A HOST_SELF_CONNECT) == 0)) {
2N/A haddrlist = connectaddr;
2N/A } else if ((strcmp(args->arg.nd_hs->h_host,
2N/A LOCALHOST) == 0)) {
2N/A haddrlist = connectaddr;
2N/A } else if ((int)(dotnameaddr =
2N/A inet_addr(args->arg.nd_hs->h_host)) != -1) {
2N/A /*
2N/A * If the caller passed in a dot separated IP
2N/A * notation to netdir_getbyname, convert that
2N/A * back into address.
2N/A */
2N/A
2N/A dotnamelist[0] = (char *)&dotnameaddr;
2N/A dotnamelist[1] = NULL;
2N/A haddrlist = dotnamelist;
2N/A } else if ((strcmp(args->arg.nd_hs->h_host,
2N/A HOST_BROADCAST) == 0)) {
2N/A /*
2N/A * Now that inaddrs and baddrlist are
2N/A * dynamically allocated, care must be
2N/A * taken in freeing up the
2N/A * memory at each 'return()' point.
2N/A *
2N/A * Early return protection (using
2N/A * FREE_return()) is needed only in NETDIR_BY
2N/A * cases because dynamic allocation is used
2N/A * when args->op_t == NETDIR_BY.
2N/A *
2N/A * Early return protection is not needed in
2N/A * haddrlist==0 conditionals because dynamic
2N/A * allocation guarantees haddrlist!=0.
2N/A *
2N/A * Early return protection is not needed in most
2N/A * servp!=0 conditionals because this is handled
2N/A * (and returned) first.
2N/A */
2N/A#define FREE_return(ret) \
2N/A { \
2N/A if (inaddrs) \
2N/A free(inaddrs); \
2N/A if (baddrlist) \
2N/A free(baddrlist); \
2N/A _nderror = ret; \
2N/A return (ret); \
2N/A NOTE(NOTREACHED)\
2N/A }
2N/A int i, bnets;
2N/A
2N/A bnets = getbroadcastnets(nconf, &inaddrs);
2N/A if (bnets == 0) {
2N/A _nderror = ND_NOHOST;
2N/A return (ND_NOHOST);
2N/A }
2N/A baddrlist = malloc((bnets+1)*sizeof (char *));
2N/A if (baddrlist == NULL)
2N/A FREE_return(ND_NOMEM);
2N/A for (i = 0; i < bnets; i++)
2N/A baddrlist[i] = (char *)&inaddrs[i];
2N/A baddrlist[i] = NULL;
2N/A haddrlist = baddrlist;
2N/A } else {
2N/A /* i.e. need to call a name service on this */
2N/A haddrlist = 0;
2N/A }
2N/A
2N/A if (haddrlist && servp) {
2N/A int ret;
2N/A /*
2N/A * Convert h_addr_list into nd_addrlist.
2N/A * malloc's will be done, freed using
2N/A * netdir_free.
2N/A */
2N/A ret = hent2ndaddr(AF_INET, haddrlist, servp,
2N/A res->nd_alist);
2N/A FREE_return(ret);
2N/A }
2N/A break;
2N/A
2N/A
2N/A case NETDIR_BY6:
2N/A if (args->arg.nd_hs == 0) {
2N/A _nderror = ND_BADARG;
2N/A return (ND_BADARG);
2N/A }
2N/A /*
2N/A * If servname is NULL, return 0 as the port number.
2N/A * If servname is rpcbind, return 111 as the port number
2N/A * If servname is a number, return it back as the port
2N/A * number.
2N/A */
2N/A if (args->arg.nd_hs->h_serv == 0) {
2N/A *servp = htons(0);
2N/A } else if (strcmp(args->arg.nd_hs->h_serv,
2N/A "rpcbind") == 0) {
2N/A *servp = htons(111);
2N/A } else if (strspn(args->arg.nd_hs->h_serv, "0123456789")
2N/A == strlen(args->arg.nd_hs->h_serv)) {
2N/A *servp = htons(atoi(args->arg.nd_hs->h_serv));
2N/A } else {
2N/A /* i.e. need to call a name service on this */
2N/A servp = NULL;
2N/A }
2N/A
2N/A /*
2N/A * If the hostname is HOST_SELF_BIND, we return ipv6
2N/A * localaddress so the binding can be contacted through
2N/A * all interfaces.
2N/A * If the hostname is HOST_SELF_CONNECT, we return
2N/A * ipv6 loopback address so the address can be connected
2N/A * to locally.
2N/A * If the hostname is HOST_ANY, we return no addresses
2N/A * because IP doesn't know how to specify a service
2N/A * without a host.
2N/A * And finally if we specify HOST_BROADCAST then we
2N/A * disallow since IPV6 does not have any
2N/A * broadcast concept.
2N/A */
2N/A if (args->arg.nd_hs->h_host == 0) {
2N/A return (ND_NOHOST);
2N/A } else if ((strcmp(args->arg.nd_hs->h_host,
2N/A HOST_SELF_BIND) == 0)) {
2N/A haddrlist = localaddr6;
2N/A } else if ((strcmp(args->arg.nd_hs->h_host,
2N/A HOST_SELF_CONNECT) == 0)) {
2N/A haddrlist = connectaddr6;
2N/A } else if ((strcmp(args->arg.nd_hs->h_host,
2N/A LOCALHOST) == 0)) {
2N/A haddrlist = connectaddr6;
2N/A } else if (strchr(args->arg.nd_hs->h_host, ':')
2N/A != NULL) {
2N/A
2N/A /*
2N/A * If the caller passed in a dot separated IP notation
2N/A * to netdir_getbyname, convert that back into address.
2N/A */
2N/A
2N/A if ((inet_pton(AF_INET6,
2N/A args->arg.nd_hs->h_host,
2N/A &v6nameaddr)) != 0) {
2N/A dotnamelist[0] = (char *)&v6nameaddr;
2N/A dotnamelist[1] = NULL;
2N/A haddrlist = dotnamelist;
2N/A }
2N/A else
2N/A /* not sure what to return */
2N/A return (ND_NOHOST);
2N/A
2N/A } else if ((strcmp(args->arg.nd_hs->h_host,
2N/A HOST_BROADCAST) == 0)) {
2N/A /*
2N/A * Don't support broadcast in
2N/A * IPV6
2N/A */
2N/A return (ND_NOHOST);
2N/A } else {
2N/A /* i.e. need to call a name service on this */
2N/A haddrlist = 0;
2N/A }
2N/A
2N/A if (haddrlist && servp) {
2N/A int ret;
2N/A /*
2N/A * Convert h_addr_list into nd_addrlist.
2N/A * malloc's will be done, freed
2N/A * using netdir_free.
2N/A */
2N/A ret = hent2ndaddr(AF_INET6, haddrlist,
2N/A servp, res->nd_alist);
2N/A FREE_return(ret);
2N/A }
2N/A break;
2N/A
2N/A
2N/A }
2N/A
2N/A /*
2N/A * 2. Most common scenario. This is the way we ship /etc/netconfig.
2N/A * Emphasis on improving performance in the "if" part.
2N/A */
2N/A if (nconf->nc_nlookups == 0) {
2N/A struct hostent *he = NULL, *tmphe;
2N/A struct servent *se;
2N/A int ret;
2N/A nss_XbyY_buf_t *ndbuf4switch = 0;
2N/A
2N/A switch (args->op_t) {
2N/A
2N/A case NSS_HOST:
2N/A
2N/A he = DOOR_GETHOSTBYNAME_R(args->arg.nss.host.name,
2N/A res->nss.host.hent, args->arg.nss.host.buf,
2N/A args->arg.nss.host.buflen,
2N/A res->nss.host.herrno_p);
2N/A if (he == NULL) {
2N/A _nderror = (*res->nss.host.herrno_p == 0) ? ND_NOHOST :
2N/A __herrno2netdir(*res->nss.host.herrno_p);
2N/A return (_nderror);
2N/A }
2N/A return (_nderror = ND_OK);
2N/A
2N/A case NSS_HOST6:
2N/A
2N/A he = DOOR_GETIPNODEBYNAME_R(args->arg.nss.host6.name,
2N/A res->nss.host.hent, args->arg.nss.host.buf,
2N/A args->arg.nss.host6.buflen,
2N/A args->arg.nss.host6.af_family,
2N/A args->arg.nss.host6.flags,
2N/A res->nss.host.herrno_p);
2N/A
2N/A if (he == NULL) {
2N/A _nderror = (*res->nss.host.herrno_p == 0) ? ND_NOHOST :
2N/A __herrno2netdir(*res->nss.host.herrno_p);
2N/A return (_nderror);
2N/A }
2N/A return (_nderror = ND_OK);
2N/A
2N/A case NSS_SERV:
2N/A
2N/A se = _switch_getservbyname_r(args->arg.nss.serv.name,
2N/A args->arg.nss.serv.proto,
2N/A res->nss.serv, args->arg.nss.serv.buf,
2N/A args->arg.nss.serv.buflen);
2N/A
2N/A _nderror = ND_OK;
2N/A if (se == 0)
2N/A _nderror = ND_NOSERV;
2N/A return (_nderror);
2N/A
2N/A case NETDIR_BY:
2N/A
2N/A if (servp == 0) {
2N/A char *proto = (strcmp(nconf->nc_proto,
2N/A NC_TCP) == 0) ? NC_TCP : NC_UDP;
2N/A
2N/A /*
2N/A * We go through all this for just one port number,
2N/A * which is most often constant. How about linking in
2N/A * an indexed database of well-known ports in the name
2N/A * of performance ?
2N/A */
2N/A ndbuf4switch = _nss_XbyY_buf_alloc(
2N/A sizeof (struct servent), NSS_BUFLEN_SERVICES);
2N/A if (ndbuf4switch == 0)
2N/A FREE_return(ND_NOMEM);
2N/A se = _switch_getservbyname_r(args->arg.nd_hs->h_serv,
2N/A proto, ndbuf4switch->result,
2N/A ndbuf4switch->buffer, ndbuf4switch->buflen);
2N/A if (!se) {
2N/A NSS_XbyY_FREE(&ndbuf4switch);
2N/A FREE_return(ND_NOSERV);
2N/A }
2N/A server_port = se->s_port;
2N/A NSS_XbyY_FREE(&ndbuf4switch);
2N/A }
2N/A
2N/A if (haddrlist == 0) {
2N/A int h_errnop = 0;
2N/A
2N/A ndbuf4switch = _nss_XbyY_buf_alloc(
2N/A sizeof (struct hostent),
2N/A NSS_BUFLEN_HOSTS);
2N/A if (ndbuf4switch == 0) {
2N/A _nderror = ND_NOMEM;
2N/A return (ND_NOMEM);
2N/A }
2N/A /*
2N/A * Search the ipnodes (v6) path first,
2N/A * search will return the v4 addresses
2N/A * as v4mapped addresses.
2N/A */
2N/A if ((tmphe = DOOR_GETIPNODEBYNAME_R(
2N/A args->arg.nd_hs->h_host,
2N/A ndbuf4switch->result, ndbuf4switch->buffer,
2N/A ndbuf4switch->buflen, args->arg.nss.host6.af_family,
2N/A args->arg.nss.host6.flags, &h_errnop)) != NULL)
2N/A he = __mappedtov4(tmphe, &h_errnop);
2N/A
2N/A if (he == NULL) {
2N/A /* Failover case, try hosts db for v4 address */
2N/A he = DOOR_GETHOSTBYNAME_R(
2N/A args->arg.nd_hs->h_host,
2N/A ndbuf4switch->result, ndbuf4switch->buffer,
2N/A ndbuf4switch->buflen, &h_errnop);
2N/A if (he == NULL) {
2N/A NSS_XbyY_FREE(&ndbuf4switch);
2N/A _nderror = h_errnop ?
2N/A __herrno2netdir(h_errnop) :
2N/A ND_NOHOST;
2N/A return (_nderror);
2N/A }
2N/A /*
2N/A * Convert h_addr_list into nd_addrlist.
2N/A * malloc's will be done, freed using
2N/A * netdir_free.
2N/A */
2N/A ret = hent2ndaddr(AF_INET, he->h_addr_list,
2N/A &server_port, res->nd_alist);
2N/A } else {
2N/A /*
2N/A * Convert h_addr_list into nd_addrlist.
2N/A * malloc's will be done, freed using
2N/A * netdir_free.
2N/A */
2N/A ret = hent2ndaddr(AF_INET, he->h_addr_list,
2N/A &server_port, res->nd_alist);
2N/A freehostent(he);
2N/A }
2N/A
2N/A _nderror = ret;
2N/A NSS_XbyY_FREE(&ndbuf4switch);
2N/A return (ret);
2N/A } else {
2N/A int ret;
2N/A /*
2N/A * Convert h_addr_list into nd_addrlist.
2N/A * malloc's will be done, freed using netdir_free.
2N/A */
2N/A ret = hent2ndaddr(AF_INET, haddrlist,
2N/A &server_port, res->nd_alist);
2N/A FREE_return(ret);
2N/A }
2N/A
2N/A
2N/A case NETDIR_BY6:
2N/A
2N/A if (servp == 0) {
2N/A char *proto = (strcmp(nconf->nc_proto,
2N/A NC_TCP) == 0) ? NC_TCP : NC_UDP;
2N/A
2N/A /*
2N/A * We go through all this for just
2N/A * one port number,
2N/A * which is most often constant.
2N/A * How about linking in
2N/A * an indexed database of well-known
2N/A * ports in the name
2N/A * of performance ?
2N/A */
2N/A ndbuf4switch = _nss_XbyY_buf_alloc(
2N/A sizeof (struct servent),
2N/A NSS_BUFLEN_SERVICES);
2N/A if (ndbuf4switch == 0)
2N/A FREE_return(ND_NOMEM);
2N/A se = _switch_getservbyname_r(
2N/A args->arg.nd_hs->h_serv,
2N/A proto, ndbuf4switch->result,
2N/A ndbuf4switch->buffer, ndbuf4switch->buflen);
2N/A if (!se) {
2N/A NSS_XbyY_FREE(&ndbuf4switch);
2N/A FREE_return(ND_NOSERV);
2N/A }
2N/A server_port = se->s_port;
2N/A NSS_XbyY_FREE(&ndbuf4switch);
2N/A }
2N/A
2N/A if (haddrlist == 0) {
2N/A int h_errnop = 0;
2N/A
2N/A ndbuf4switch = _nss_XbyY_buf_alloc(
2N/A sizeof (struct hostent),
2N/A NSS_BUFLEN_HOSTS);
2N/A if (ndbuf4switch == 0) {
2N/A _nderror = ND_NOMEM;
2N/A return (ND_NOMEM);
2N/A }
2N/A he = DOOR_GETIPNODEBYNAME_R(
2N/A args->arg.nd_hs->h_host,
2N/A ndbuf4switch->result, ndbuf4switch->buffer,
2N/A ndbuf4switch->buflen,
2N/A args->arg.nss.host6.af_family,
2N/A args->arg.nss.host6.flags, &h_errnop);
2N/A if (he == NULL) {
2N/A NSS_XbyY_FREE(&ndbuf4switch);
2N/A _nderror = h_errnop ?
2N/A __herrno2netdir(h_errnop) :
2N/A ND_NOHOST;
2N/A return (_nderror);
2N/A }
2N/A /*
2N/A * Convert h_addr_list into nd_addrlist.
2N/A * malloc's will be done,
2N/A * freed using netdir_free.
2N/A */
2N/A ret = hent2ndaddr(AF_INET6,
2N/A ((struct hostent *)
2N/A (ndbuf4switch->result))->h_addr_list,
2N/A &server_port, res->nd_alist);
2N/A _nderror = ret;
2N/A NSS_XbyY_FREE(&ndbuf4switch);
2N/A return (ret);
2N/A } else {
2N/A int ret;
2N/A /*
2N/A * Convert h_addr_list into nd_addrlist.
2N/A * malloc's will be done,
2N/A * freed using netdir_free.
2N/A */
2N/A ret = hent2ndaddr(AF_INET6, haddrlist,
2N/A &server_port, res->nd_alist);
2N/A FREE_return(ret);
2N/A }
2N/A
2N/A default:
2N/A _nderror = ND_BADARG;
2N/A return (ND_BADARG); /* should never happen */
2N/A }
2N/A
2N/A } else {
2N/A /* haddrlist is no longer used, so clean up */
2N/A if (inaddrs)
2N/A free(inaddrs);
2N/A if (baddrlist)
2N/A free(baddrlist);
2N/A }
2N/A
2N/A /*
2N/A * 3. We come this far only if nametoaddr libs are specified for
2N/A * inet transports and we are called by gethost/servbyname only.
2N/A */
2N/A switch (args->op_t) {
2N/A struct nd_hostserv service;
2N/A struct nd_addrlist *addrs;
2N/A int ret;
2N/A
2N/A case NSS_HOST:
2N/A
2N/A service.h_host = (char *)args->arg.nss.host.name;
2N/A service.h_serv = NULL;
2N/A if ((_nderror = __classic_netdir_getbyname(nconf,
2N/A &service, &addrs)) != ND_OK) {
2N/A *(res->nss.host.herrno_p) = nd2herrno(_nderror);
2N/A return (_nderror);
2N/A }
2N/A /*
2N/A * convert addresses back into sockaddr for gethostbyname.
2N/A */
2N/A ret = ndaddr2hent(AF_INET, service.h_host, addrs,
2N/A res->nss.host.hent, args->arg.nss.host.buf,
2N/A args->arg.nss.host.buflen);
2N/A if (ret != ND_OK)
2N/A *(res->nss.host.herrno_p) = nd2herrno(ret);
2N/A netdir_free((char *)addrs, ND_ADDRLIST);
2N/A _nderror = ret;
2N/A return (ret);
2N/A
2N/A case NSS_SERV:
2N/A
2N/A if (args->arg.nss.serv.proto == NULL) {
2N/A /*
2N/A * A similar HACK showed up in Solaris 2.3.
2N/A * The caller wild-carded proto -- i.e. will
2N/A * accept a match using tcp or udp for the port
2N/A * number. Since we have no hope of getting
2N/A * directly to a name service switch backend
2N/A * from here that understands this semantics,
2N/A * we try calling the netdir interfaces first
2N/A * with "tcp" and then "udp".
2N/A */
2N/A args->arg.nss.serv.proto = "tcp";
2N/A _nderror = _get_hostserv_inetnetdir_byname(nconf, args,
2N/A res);
2N/A if (_nderror != ND_OK) {
2N/A args->arg.nss.serv.proto = "udp";
2N/A _nderror =
2N/A _get_hostserv_inetnetdir_byname(nconf,
2N/A args, res);
2N/A }
2N/A return (_nderror);
2N/A }
2N/A
2N/A /*
2N/A * Third-parties should optimize their nametoaddr
2N/A * libraries for the HOST_SELF case.
2N/A */
2N/A service.h_host = HOST_SELF;
2N/A service.h_serv = (char *)args->arg.nss.serv.name;
2N/A if ((_nderror = __classic_netdir_getbyname(nconf,
2N/A &service, &addrs)) != ND_OK) {
2N/A return (_nderror);
2N/A }
2N/A /*
2N/A * convert addresses back into servent for getservbyname.
2N/A */
2N/A _nderror = ndaddr2srent(service.h_serv,
2N/A args->arg.nss.serv.proto,
2N/A ((struct sockaddr_in *)addrs->n_addrs->buf)->sin_port,
2N/A res->nss.serv,
2N/A args->arg.nss.serv.buf, args->arg.nss.serv.buflen);
2N/A netdir_free((char *)addrs, ND_ADDRLIST);
2N/A return (_nderror);
2N/A
2N/A default:
2N/A _nderror = ND_BADARG;
2N/A return (ND_BADARG); /* should never happen */
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * gethostbyaddr/servbyport always call this function; if they call
2N/A * with nametoaddr libs in nconf, we call netdir_getbyaddr
2N/A * implementation __classic_netdir_getbyaddr, otherwise nsswitch.
2N/A *
2N/A * netdir_getbyaddr calls this only if nametoaddr libs are NOT
2N/A * specified for inet transports; i.e. it's supposed to follow
2N/A * the name service switch.
2N/A */
2N/Aint
2N/A_get_hostserv_inetnetdir_byaddr(struct netconfig *nconf,
2N/A struct nss_netdirbyaddr_in *args, union nss_netdirbyaddr_out *res)
2N/A{
2N/A if (nconf == 0) {
2N/A _nderror = ND_BADARG;
2N/A return (_nderror);
2N/A }
2N/A
2N/A /*
2N/A * 1. gethostbyaddr()/netdir_getbyaddr() special cases:
2N/A */
2N/A switch (args->op_t) {
2N/A
2N/A case NSS_HOST:
2N/A /*
2N/A * Worth the performance gain: assuming a lot of inet apps
2N/A * actively use "127.0.0.1".
2N/A */
2N/A /* LINTED pointer cast */
2N/A if (*(uint32_t *)(args->arg.nss.host.addr) ==
2N/A htonl(INADDR_LOOPBACK)) {
2N/A (void) mutex_lock(&nd_addr_lock);
2N/A IN_SET_LOOPBACK_ADDR(&sa_con);
2N/A _nderror = ndaddr2hent(AF_INET, LOCALHOST,
2N/A &nd_conaddrlist, res->nss.host.hent,
2N/A args->arg.nss.host.buf,
2N/A args->arg.nss.host.buflen);
2N/A (void) mutex_unlock(&nd_addr_lock);
2N/A if (_nderror != ND_OK)
2N/A *(res->nss.host.herrno_p) =
2N/A nd2herrno(_nderror);
2N/A return (_nderror);
2N/A }
2N/A break;
2N/A
2N/A case NETDIR_BY:
2N/A case NETDIR_BY_NOSRV:
2N/A {
2N/A struct sockaddr_in *sin;
2N/A
2N/A if (args->arg.nd_nbuf == NULL) {
2N/A _nderror = ND_BADARG;
2N/A return (_nderror);
2N/A }
2N/A
2N/A /*
2N/A * Validate the address which was passed
2N/A * as the request.
2N/A */
2N/A sin = (struct sockaddr_in *)args->arg.nd_nbuf->buf;
2N/A
2N/A if ((args->arg.nd_nbuf->len !=
2N/A sizeof (struct sockaddr_in)) ||
2N/A (sin->sin_family != AF_INET)) {
2N/A _nderror = ND_BADARG;
2N/A return (_nderror);
2N/A }
2N/A }
2N/A break;
2N/A
2N/A case NETDIR_BY6:
2N/A case NETDIR_BY_NOSRV6:
2N/A {
2N/A struct sockaddr_in6 *sin6;
2N/A
2N/A if (args->arg.nd_nbuf == NULL) {
2N/A _nderror = ND_BADARG;
2N/A return (_nderror);
2N/A }
2N/A
2N/A /*
2N/A * Validate the address which was passed
2N/A * as the request.
2N/A */
2N/A sin6 = (struct sockaddr_in6 *)args->arg.nd_nbuf->buf;
2N/A
2N/A if ((args->arg.nd_nbuf->len !=
2N/A sizeof (struct sockaddr_in6)) ||
2N/A (sin6->sin6_family != AF_INET6)) {
2N/A _nderror = ND_BADARG;
2N/A return (_nderror);
2N/A }
2N/A }
2N/A break;
2N/A
2N/A }
2N/A
2N/A /*
2N/A * 2. Most common scenario. This is the way we ship /etc/netconfig.
2N/A * Emphasis on improving performance in the "if" part.
2N/A */
2N/A if (nconf->nc_nlookups == 0) {
2N/A struct hostent *he = NULL, *tmphe;
2N/A struct servent *se = NULL;
2N/A nss_XbyY_buf_t *ndbuf4host = 0;
2N/A nss_XbyY_buf_t *ndbuf4serv = 0;
2N/A char *proto =
2N/A (strcmp(nconf->nc_proto, NC_TCP) == 0) ? NC_TCP : NC_UDP;
2N/A struct sockaddr_in *sa;
2N/A struct sockaddr_in6 *sin6;
2N/A struct in_addr *addr4 = 0;
2N/A struct in6_addr v4mapbuf;
2N/A int h_errnop;
2N/A
2N/A switch (args->op_t) {
2N/A
2N/A case NSS_HOST:
2N/A
2N/A he = DOOR_GETHOSTBYADDR_R(args->arg.nss.host.addr,
2N/A args->arg.nss.host.len, args->arg.nss.host.type,
2N/A res->nss.host.hent, args->arg.nss.host.buf,
2N/A args->arg.nss.host.buflen,
2N/A res->nss.host.herrno_p);
2N/A if (he == NULL)
2N/A _nderror = (*res->nss.host.herrno_p == 0) ? ND_NOHOST :
2N/A __herrno2netdir(*res->nss.host.herrno_p);
2N/A else
2N/A _nderror = ND_OK;
2N/A return (_nderror);
2N/A
2N/A
2N/A case NSS_HOST6:
2N/A he = DOOR_GETIPNODEBYADDR_R(args->arg.nss.host.addr,
2N/A args->arg.nss.host.len, args->arg.nss.host.type,
2N/A res->nss.host.hent, args->arg.nss.host.buf,
2N/A args->arg.nss.host.buflen,
2N/A res->nss.host.herrno_p);
2N/A
2N/A if (he == NULL)
2N/A _nderror = (*res->nss.host.herrno_p == 0) ? ND_NOHOST :
2N/A __herrno2netdir(*res->nss.host.herrno_p);
2N/A else
2N/A _nderror = ND_OK;
2N/A return (_nderror);
2N/A
2N/A
2N/A case NSS_SERV:
2N/A
2N/A se = _switch_getservbyport_r(args->arg.nss.serv.port,
2N/A args->arg.nss.serv.proto,
2N/A res->nss.serv, args->arg.nss.serv.buf,
2N/A args->arg.nss.serv.buflen);
2N/A
2N/A if (se == 0)
2N/A _nderror = ND_NOSERV;
2N/A else
2N/A _nderror = ND_OK;
2N/A return (_nderror);
2N/A
2N/A case NETDIR_BY:
2N/A case NETDIR_BY_NOSRV:
2N/A
2N/A ndbuf4serv = _nss_XbyY_buf_alloc(sizeof (struct servent),
2N/A NSS_BUFLEN_SERVICES);
2N/A if (ndbuf4serv == 0) {
2N/A _nderror = ND_NOMEM;
2N/A return (_nderror);
2N/A }
2N/A sa = (struct sockaddr_in *)(args->arg.nd_nbuf->buf);
2N/A addr4 = (struct in_addr *)&(sa->sin_addr);
2N/A
2N/A /*
2N/A * if NETDIR_BY_NOSRV or port == 0 skip the service
2N/A * lookup.
2N/A */
2N/A if (args->op_t != NETDIR_BY_NOSRV && sa->sin_port != 0) {
2N/A se = _switch_getservbyport_r(sa->sin_port, proto,
2N/A ndbuf4serv->result, ndbuf4serv->buffer,
2N/A ndbuf4serv->buflen);
2N/A if (!se) {
2N/A NSS_XbyY_FREE(&ndbuf4serv);
2N/A /*
2N/A * We can live with this - i.e. the address
2N/A * does not
2N/A * belong to a well known service. The caller
2N/A * traditionally accepts a stringified port
2N/A * number
2N/A * as the service name. The state of se is used
2N/A * ahead to indicate the same.
2N/A * However, we do not tolerate this nonsense
2N/A * when we cannot get a host name. See below.
2N/A */
2N/A }
2N/A }
2N/A
2N/A ndbuf4host = _nss_XbyY_buf_alloc(sizeof (struct hostent),
2N/A NSS_BUFLEN_HOSTS);
2N/A if (ndbuf4host == 0) {
2N/A if (ndbuf4serv)
2N/A NSS_XbyY_FREE(&ndbuf4serv);
2N/A _nderror = ND_NOMEM;
2N/A return (_nderror);
2N/A }
2N/A
2N/A /*
2N/A * Since we're going to search the ipnodes (v6) path first,
2N/A * we need to treat the address as a v4mapped address.
2N/A */
2N/A
2N/A IN6_INADDR_TO_V4MAPPED(addr4, &v4mapbuf);
2N/A if ((tmphe = DOOR_GETIPNODEBYADDR_R((char *)&v4mapbuf,
2N/A 16, AF_INET6, ndbuf4host->result,
2N/A ndbuf4host->buffer,
2N/A ndbuf4host->buflen, &h_errnop)) != NULL)
2N/A he = __mappedtov4(tmphe, &h_errnop);
2N/A
2N/A if (!he) {
2N/A /* Failover case, try hosts db for v4 address */
2N/A he = DOOR_GETHOSTBYADDR_R((char *)
2N/A &(sa->sin_addr.s_addr), 4,
2N/A sa->sin_family, ndbuf4host->result,
2N/A ndbuf4host->buffer, ndbuf4host->buflen,
2N/A &h_errnop);
2N/A if (!he) {
2N/A NSS_XbyY_FREE(&ndbuf4host);
2N/A if (ndbuf4serv)
2N/A NSS_XbyY_FREE(&ndbuf4serv);
2N/A _nderror = (h_errnop == 0) ? ND_NOHOST :
2N/A __herrno2netdir(h_errnop);
2N/A return (_nderror);
2N/A }
2N/A /*
2N/A * Convert host names and service names into hostserv
2N/A * pairs. malloc's will be done, freed using
2N/A * netdir_free.
2N/A */
2N/A h_errnop = hsents2ndhostservs(he, se,
2N/A sa->sin_port, res->nd_hslist);
2N/A } else {
2N/A /*
2N/A * Convert host names and service names into hostserv
2N/A * pairs. malloc's will be done, freed using
2N/A * netdir_free.
2N/A */
2N/A h_errnop = hsents2ndhostservs(he, se,
2N/A sa->sin_port, res->nd_hslist);
2N/A freehostent(he);
2N/A }
2N/A
2N/A NSS_XbyY_FREE(&ndbuf4host);
2N/A if (ndbuf4serv)
2N/A NSS_XbyY_FREE(&ndbuf4serv);
2N/A _nderror = __herrno2netdir(h_errnop);
2N/A return (_nderror);
2N/A
2N/A case NETDIR_BY6:
2N/A case NETDIR_BY_NOSRV6:
2N/A
2N/A ndbuf4serv = _nss_XbyY_buf_alloc(sizeof (struct servent),
2N/A NSS_BUFLEN_SERVICES);
2N/A if (ndbuf4serv == 0) {
2N/A _nderror = ND_NOMEM;
2N/A return (ND_NOMEM);
2N/A }
2N/A sin6 = (struct sockaddr_in6 *)(args->arg.nd_nbuf->buf);
2N/A
2N/A /*
2N/A * if NETDIR_BY_NOSRV6 or port == 0 skip the service
2N/A * lookup.
2N/A */
2N/A if (args->op_t != NETDIR_BY_NOSRV6 && sin6->sin6_port == 0) {
2N/A se = _switch_getservbyport_r(sin6->sin6_port, proto,
2N/A ndbuf4serv->result, ndbuf4serv->buffer,
2N/A ndbuf4serv->buflen);
2N/A if (!se) {
2N/A NSS_XbyY_FREE(&ndbuf4serv);
2N/A /*
2N/A * We can live with this - i.e. the address does
2N/A * not * belong to a well known service. The
2N/A * caller traditionally accepts a stringified
2N/A * port number
2N/A * as the service name. The state of se is used
2N/A * ahead to indicate the same.
2N/A * However, we do not tolerate this nonsense
2N/A * when we cannot get a host name. See below.
2N/A */
2N/A }
2N/A }
2N/A
2N/A ndbuf4host = _nss_XbyY_buf_alloc(sizeof (struct hostent),
2N/A NSS_BUFLEN_HOSTS);
2N/A if (ndbuf4host == 0) {
2N/A if (ndbuf4serv)
2N/A NSS_XbyY_FREE(&ndbuf4serv);
2N/A _nderror = ND_NOMEM;
2N/A return (_nderror);
2N/A }
2N/A he = DOOR_GETIPNODEBYADDR_R((char *)&(sin6->sin6_addr),
2N/A 16, sin6->sin6_family, ndbuf4host->result,
2N/A ndbuf4host->buffer,
2N/A ndbuf4host->buflen, &h_errnop);
2N/A if (!he) {
2N/A NSS_XbyY_FREE(&ndbuf4host);
2N/A if (ndbuf4serv)
2N/A NSS_XbyY_FREE(&ndbuf4serv);
2N/A _nderror = __herrno2netdir(h_errnop);
2N/A return (_nderror);
2N/A }
2N/A /*
2N/A * Convert host names and service names into hostserv
2N/A * pairs. malloc's will be done, freed using netdir_free.
2N/A */
2N/A h_errnop = hsents2ndhostservs(he, se,
2N/A sin6->sin6_port, res->nd_hslist);
2N/A
2N/A NSS_XbyY_FREE(&ndbuf4host);
2N/A if (ndbuf4serv)
2N/A NSS_XbyY_FREE(&ndbuf4serv);
2N/A _nderror = __herrno2netdir(h_errnop);
2N/A return (_nderror);
2N/A
2N/A default:
2N/A _nderror = ND_BADARG;
2N/A return (_nderror); /* should never happen */
2N/A }
2N/A
2N/A }
2N/A /*
2N/A * 3. We come this far only if nametoaddr libs are specified for
2N/A * inet transports and we are called by gethost/servbyname only.
2N/A */
2N/A switch (args->op_t) {
2N/A struct netbuf nbuf;
2N/A struct nd_hostservlist *addrs;
2N/A struct sockaddr_in sa;
2N/A
2N/A case NSS_HOST:
2N/A
2N/A /* LINTED pointer cast */
2N/A sa.sin_addr.s_addr = *(uint32_t *)args->arg.nss.host.addr;
2N/A sa.sin_family = AF_INET;
2N/A /* Hopefully, third-parties get this optimization */
2N/A sa.sin_port = 0;
2N/A nbuf.buf = (char *)&sa;
2N/A nbuf.len = nbuf.maxlen = sizeof (sa);
2N/A if ((_nderror = __classic_netdir_getbyaddr(nconf,
2N/A &addrs, &nbuf)) != 0) {
2N/A *(res->nss.host.herrno_p) = nd2herrno(_nderror);
2N/A return (_nderror);
2N/A }
2N/A /*
2N/A * convert the host-serv pairs into h_aliases and hent.
2N/A */
2N/A _nderror = ndhostserv2hent(&nbuf, addrs, res->nss.host.hent,
2N/A args->arg.nss.host.buf, args->arg.nss.host.buflen);
2N/A if (_nderror != ND_OK)
2N/A *(res->nss.host.herrno_p) = nd2herrno(_nderror);
2N/A netdir_free((char *)addrs, ND_HOSTSERVLIST);
2N/A return (_nderror);
2N/A
2N/A case NSS_SERV:
2N/A
2N/A if (args->arg.nss.serv.proto == NULL) {
2N/A /*
2N/A * A similar HACK showed up in Solaris 2.3.
2N/A * The caller wild-carded proto -- i.e. will
2N/A * accept a match on tcp or udp for the port
2N/A * number. Since we have no hope of getting
2N/A * directly to a name service switch backend
2N/A * from here that understands this semantics,
2N/A * we try calling the netdir interfaces first
2N/A * with "tcp" and then "udp".
2N/A */
2N/A args->arg.nss.serv.proto = "tcp";
2N/A _nderror = _get_hostserv_inetnetdir_byaddr(nconf, args,
2N/A res);
2N/A if (_nderror != ND_OK) {
2N/A args->arg.nss.serv.proto = "udp";
2N/A _nderror =
2N/A _get_hostserv_inetnetdir_byaddr(nconf,
2N/A args, res);
2N/A }
2N/A return (_nderror);
2N/A }
2N/A
2N/A /*
2N/A * Third-party nametoaddr_libs should be optimized for
2N/A * this case. It also gives a special semantics twist to
2N/A * netdir_getbyaddr. Only for the INADDR_ANY case, it gives
2N/A * higher priority to service lookups (over host lookups).
2N/A * If service lookup fails, the backend returns ND_NOSERV to
2N/A * facilitate lookup in the "next" naming service.
2N/A * BugId: 1075403.
2N/A */
2N/A sa.sin_addr.s_addr = INADDR_ANY;
2N/A sa.sin_family = AF_INET;
2N/A sa.sin_port = (ushort_t)args->arg.nss.serv.port;
2N/A sa.sin_zero[0] = '\0';
2N/A nbuf.buf = (char *)&sa;
2N/A nbuf.len = nbuf.maxlen = sizeof (sa);
2N/A if ((_nderror = __classic_netdir_getbyaddr(nconf,
2N/A &addrs, &nbuf)) != ND_OK) {
2N/A return (_nderror);
2N/A }
2N/A /*
2N/A * convert the host-serv pairs into s_aliases and servent.
2N/A */
2N/A _nderror = ndhostserv2srent(args->arg.nss.serv.port,
2N/A args->arg.nss.serv.proto, addrs, res->nss.serv,
2N/A args->arg.nss.serv.buf, args->arg.nss.serv.buflen);
2N/A netdir_free((char *)addrs, ND_HOSTSERVLIST);
2N/A return (_nderror);
2N/A
2N/A default:
2N/A _nderror = ND_BADARG;
2N/A return (_nderror); /* should never happen */
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Part II: Name Service Switch interfacing routines.
2N/A */
2N/A
2N/Astatic DEFINE_NSS_DB_ROOT(db_root_hosts);
2N/Astatic DEFINE_NSS_DB_ROOT(db_root_ipnodes);
2N/Astatic DEFINE_NSS_DB_ROOT(db_root_services);
2N/A
2N/A
2N/Aint
2N/A__nss2herrno(nss_status_t nsstat)
2N/A{
2N/A switch (nsstat) {
2N/A case NSS_SUCCESS:
2N/A /* no macro-defined success code for h_errno */
2N/A return (0);
2N/A case NSS_NOTFOUND:
2N/A return (HOST_NOT_FOUND);
2N/A case NSS_TRYAGAIN:
2N/A return (TRY_AGAIN);
2N/A case NSS_UNAVAIL:
2N/A return (NO_RECOVERY);
2N/A case NSS_NISSERVDNS_TRYAGAIN:
2N/A return (TRY_AGAIN);
2N/A }
2N/A /* anything else */
2N/A return (NO_RECOVERY);
2N/A}
2N/A
2N/Anss_status_t
2N/A_herrno2nss(int h_errno)
2N/A{
2N/A switch (h_errno) {
2N/A case 0:
2N/A return (NSS_SUCCESS);
2N/A case TRY_AGAIN:
2N/A return (NSS_TRYAGAIN);
2N/A case NO_RECOVERY:
2N/A case NETDB_INTERNAL:
2N/A return (NSS_UNAVAIL);
2N/A case HOST_NOT_FOUND:
2N/A case NO_DATA:
2N/A default:
2N/A return (NSS_NOTFOUND);
2N/A }
2N/A}
2N/A
2N/Astatic int
2N/A__herrno2netdir(int h_errnop)
2N/A{
2N/A switch (h_errnop) {
2N/A case 0:
2N/A return (ND_OK);
2N/A case HOST_NOT_FOUND:
2N/A return (ND_NOHOST);
2N/A case TRY_AGAIN:
2N/A return (ND_TRY_AGAIN);
2N/A case NO_RECOVERY:
2N/A case NETDB_INTERNAL:
2N/A return (ND_NO_RECOVERY);
2N/A case NO_DATA:
2N/A return (ND_NO_DATA);
2N/A default:
2N/A return (ND_NOHOST);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * The _switch_getXXbyYY_r() routines should be static. They used to
2N/A * be exported in SunOS 5.3, and in fact publicised as work-around
2N/A * interfaces for getting CNAME/aliases, and therefore, we preserve
2N/A * their signatures here. Just in case.
2N/A */
2N/A
2N/Astruct hostent *
2N/A_switch_gethostbyname_r(const char *name, struct hostent *result, char *buffer,
2N/A int buflen, int *h_errnop)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
2N/A arg.key.name = name;
2N/A arg.stayopen = 0;
2N/A res = nss_search(&db_root_hosts, _nss_initf_hosts,
2N/A NSS_DBOP_HOSTS_BYNAME, &arg);
2N/A arg.status = res;
2N/A *h_errnop = arg.h_errno;
2N/A if (arg.returnval != NULL)
2N/A order_haddrlist_af(result->h_addrtype, result->h_addr_list);
2N/A return ((struct hostent *)NSS_XbyY_FINI(&arg));
2N/A}
2N/A
2N/Astruct hostent *
2N/A_switch_getipnodebyname_r(const char *name, struct hostent *result,
2N/A char *buffer, int buflen, int af_family, int flags, int *h_errnop)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent6);
2N/A arg.key.ipnode.name = name;
2N/A arg.key.ipnode.af_family = af_family;
2N/A arg.key.ipnode.flags = flags;
2N/A arg.stayopen = 0;
2N/A res = nss_search(&db_root_ipnodes, _nss_initf_ipnodes,
2N/A NSS_DBOP_IPNODES_BYNAME, &arg);
2N/A arg.status = res;
2N/A *h_errnop = arg.h_errno;
2N/A if (arg.returnval != NULL)
2N/A order_haddrlist_af(result->h_addrtype, result->h_addr_list);
2N/A return ((struct hostent *)NSS_XbyY_FINI(&arg));
2N/A}
2N/A
2N/Astruct hostent *
2N/A_switch_gethostbyaddr_r(const char *addr, int len, int type,
2N/A struct hostent *result, char *buffer, int buflen, int *h_errnop)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
2N/A arg.key.hostaddr.addr = addr;
2N/A arg.key.hostaddr.len = len;
2N/A arg.key.hostaddr.type = type;
2N/A arg.stayopen = 0;
2N/A res = nss_search(&db_root_hosts, _nss_initf_hosts,
2N/A NSS_DBOP_HOSTS_BYADDR, &arg);
2N/A arg.status = res;
2N/A *h_errnop = arg.h_errno;
2N/A return (struct hostent *)NSS_XbyY_FINI(&arg);
2N/A}
2N/A
2N/Astruct hostent *
2N/A_switch_getipnodebyaddr_r(const char *addr, int len, int type,
2N/A struct hostent *result, char *buffer, int buflen, int *h_errnop)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent6);
2N/A arg.key.hostaddr.addr = addr;
2N/A arg.key.hostaddr.len = len;
2N/A arg.key.hostaddr.type = type;
2N/A arg.stayopen = 0;
2N/A res = nss_search(&db_root_ipnodes, _nss_initf_ipnodes,
2N/A NSS_DBOP_IPNODES_BYADDR, &arg);
2N/A arg.status = res;
2N/A *h_errnop = arg.h_errno;
2N/A return (struct hostent *)NSS_XbyY_FINI(&arg);
2N/A}
2N/A
2N/Astatic void
2N/A_nss_initf_services(nss_db_params_t *p)
2N/A{
2N/A p->name = NSS_DBNAM_SERVICES;
2N/A p->default_config = NSS_DEFCONF_SERVICES;
2N/A}
2N/A
2N/Astruct servent *
2N/A_switch_getservbyname_r(const char *name, const char *proto,
2N/A struct servent *result, char *buffer, int buflen)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2servent);
2N/A arg.key.serv.serv.name = name;
2N/A arg.key.serv.proto = proto;
2N/A arg.stayopen = 0;
2N/A res = nss_search(&db_root_services, _nss_initf_services,
2N/A NSS_DBOP_SERVICES_BYNAME, &arg);
2N/A arg.status = res;
2N/A return ((struct servent *)NSS_XbyY_FINI(&arg));
2N/A}
2N/A
2N/Astruct servent *
2N/A_switch_getservbyport_r(int port, const char *proto, struct servent *result,
2N/A char *buffer, int buflen)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2servent);
2N/A arg.key.serv.serv.port = port;
2N/A arg.key.serv.proto = proto;
2N/A arg.stayopen = 0;
2N/A res = nss_search(&db_root_services, _nss_initf_services,
2N/A NSS_DBOP_SERVICES_BYPORT, &arg);
2N/A arg.status = res;
2N/A return ((struct servent *)NSS_XbyY_FINI(&arg));
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Return values: 0 = success, 1 = parse error, 2 = erange ...
2N/A * The structure pointer passed in is a structure in the caller's space
2N/A * wherein the field pointers would be set to areas in the buffer if
2N/A * need be. instring and buffer should be separate areas.
2N/A *
2N/A * Defined here because we need it and we (libnsl) cannot have a dependency
2N/A * on libsocket (however, libsocket always depends on libnsl).
2N/A */
2N/Aint
2N/Astr2servent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
2N/A{
2N/A struct servent *serv = (struct servent *)ent;
2N/A const char *p, *fieldstart, *limit, *namestart;
2N/A ssize_t fieldlen, namelen = 0;
2N/A char numbuf[12];
2N/A char *numend;
2N/A
2N/A if ((instr >= buffer && (buffer + buflen) > instr) ||
2N/A (buffer >= instr && (instr + lenstr) > buffer)) {
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
2N/A
2N/A p = instr;
2N/A limit = p + lenstr;
2N/A
2N/A while (p < limit && isspace(*p)) {
2N/A p++;
2N/A }
2N/A namestart = p;
2N/A while (p < limit && !isspace(*p)) {
2N/A p++; /* Skip over the canonical name */
2N/A }
2N/A namelen = p - namestart;
2N/A
2N/A if (buflen <= namelen) { /* not enough buffer */
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A }
2N/A (void) memcpy(buffer, namestart, namelen);
2N/A buffer[namelen] = '\0';
2N/A serv->s_name = buffer;
2N/A
2N/A while (p < limit && isspace(*p)) {
2N/A p++;
2N/A }
2N/A
2N/A fieldstart = p;
2N/A do {
2N/A if (p > limit || isspace(*p)) {
2N/A /* Syntax error -- no port/proto */
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
2N/A } while (*p++ != '/');
2N/A fieldlen = p - fieldstart - 1;
2N/A if (fieldlen == 0 || fieldlen >= sizeof (numbuf)) {
2N/A /* Syntax error -- supposed number is empty or too long */
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
2N/A (void) memcpy(numbuf, fieldstart, fieldlen);
2N/A numbuf[fieldlen] = '\0';
2N/A serv->s_port = htons((int)strtol(numbuf, &numend, 10));
2N/A if (*numend != '\0') {
2N/A /* Syntax error -- port number isn't a number */
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
2N/A
2N/A fieldstart = p;
2N/A while (p < limit && !isspace(*p)) {
2N/A p++; /* Scan the protocol name */
2N/A }
2N/A fieldlen = p - fieldstart + 1; /* Include '\0' this time */
2N/A if (fieldlen > buflen - namelen - 1) {
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A }
2N/A serv->s_proto = buffer + namelen + 1;
2N/A (void) memcpy(serv->s_proto, fieldstart, fieldlen - 1);
2N/A serv->s_proto[fieldlen - 1] = '\0';
2N/A
2N/A while (p < limit && isspace(*p)) {
2N/A p++;
2N/A }
2N/A /*
2N/A * Although nss_files_XY_all calls us with # stripped,
2N/A * we should be able to deal with it here in order to
2N/A * be more useful.
2N/A */
2N/A if (p >= limit || *p == '#') { /* no aliases, no problem */
2N/A char **ptr;
2N/A
2N/A ptr = (char **)ROUND_UP(buffer + namelen + 1 + fieldlen,
2N/A sizeof (char *));
2N/A if ((char *)ptr >= buffer + buflen) {
2N/A /* hope they don't try to peek in */
2N/A serv->s_aliases = 0;
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A } else {
2N/A *ptr = 0;
2N/A serv->s_aliases = ptr;
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A }
2N/A }
2N/A serv->s_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)),
2N/A buffer + namelen + 1 + fieldlen,
2N/A (int)(buflen - namelen - 1 - fieldlen));
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Part III: All `n sundry routines that are useful only in this
2N/A * module. In the interest of keeping this source file shorter,
2N/A * we would create them a new module only if the linker allowed
2N/A * "library-static" functions.
2N/A *
2N/A * Routines to order addresses based on local interfaces and netmasks,
2N/A * to get and check reserved ports, and to get broadcast nets.
2N/A */
2N/A
2N/Aunion __v4v6addr {
2N/A struct in6_addr in6;
2N/A struct in_addr in4;
2N/A};
2N/A
2N/Astruct __ifaddr {
2N/A sa_family_t af;
2N/A union __v4v6addr addr;
2N/A union __v4v6addr mask;
2N/A};
2N/A
2N/Astruct ifinfo {
2N/A int count;
2N/A struct __ifaddr *addresses;
2N/A};
2N/A
2N/Atypedef enum {ADDR_ONLINK = 0, ADDR_OFFLINK} addr_class_t;
2N/A#define ADDR_NUMCLASSES 2
2N/A
2N/Atypedef enum {IF_ADDR, IF_MASK} __ifaddr_type;
2N/Astatic int __inet_ifassign(sa_family_t, struct __ifaddr *, __ifaddr_type,
2N/A void *);
2N/Aint __inet_address_is_local_af(void *, sa_family_t, void *);
2N/A
2N/A#define ifaf(index) (localinfo->addresses[index].af)
2N/A#define ifaddr4(index) (localinfo->addresses[index].addr.in4)
2N/A#define ifaddr6(index) (localinfo->addresses[index].addr.in6)
2N/A#define ifmask4(index) (localinfo->addresses[index].mask.in4)
2N/A#define ifmask6(index) (localinfo->addresses[index].mask.in6)
2N/A#define ifinfosize(n) (sizeof (struct ifinfo) + (n)*sizeof (struct __ifaddr))
2N/A
2N/A#define lifraddrp(lifr) ((lifr.lifr_addr.ss_family == AF_INET6) ? \
2N/A (void *)&((struct sockaddr_in6 *)&lifr.lifr_addr)->sin6_addr : \
2N/A (void *)&((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr)
2N/A
2N/A#define ifassign(lifr, index, type) \
2N/A __inet_ifassign(lifr.lifr_addr.ss_family, \
2N/A &localinfo->addresses[index], type, \
2N/A lifraddrp(lifr))
2N/A
2N/A/*
2N/A * The number of nanoseconds the order_haddrlist_inet() function waits
2N/A * to retreive IP interface information. The default is five minutes.
2N/A */
2N/A#define IFINFOTIMEOUT ((hrtime_t)300 * NANOSEC)
2N/A
2N/A/*
2N/A * Sort the addresses in haddrlist. Since the sorting algorithms are
2N/A * address-family specific, the work is done in the address-family
2N/A * specific order_haddrlist_<family> functions.
2N/A *
2N/A * Do not sort addresses if SORT_ADDRS variable is set to NO or FALSE
2N/A * in the configuration file /etc/default/nss. This is useful in case
2N/A * the order of addresses returned by the nameserver needs to be
2N/A * maintained. (DNS round robin feature is one example)
2N/A */
2N/Avoid
2N/Aorder_haddrlist_af(sa_family_t af, char **haddrlist)
2N/A{
2N/A size_t addrcount;
2N/A char **addrptr;
2N/A static boolean_t checksortcfg = B_TRUE;
2N/A static boolean_t nosort = B_FALSE;
2N/A static mutex_t checksortcfg_lock = DEFAULTMUTEX;
2N/A
2N/A if (haddrlist == NULL)
2N/A return;
2N/A
2N/A /*
2N/A * Check if SORT_ADDRS is set to NO or FALSE in the configuration
2N/A * file. We do not have to sort addresses in that case.
2N/A */
2N/A (void) mutex_lock(&checksortcfg_lock);
2N/A if (checksortcfg == B_TRUE) {
2N/A checksortcfg = B_FALSE;
2N/A nosort = _read_nsw_file();
2N/A }
2N/A (void) mutex_unlock(&checksortcfg_lock);
2N/A
2N/A if (nosort)
2N/A return;
2N/A
2N/A /* Count the addresses to sort */
2N/A addrcount = 0;
2N/A for (addrptr = haddrlist; *addrptr != NULL; addrptr++)
2N/A addrcount++;
2N/A
2N/A /*
2N/A * If there's only one address or no addresses to sort, then
2N/A * there's nothing for us to do.
2N/A */
2N/A if (addrcount <= 1)
2N/A return;
2N/A
2N/A /* Call the address-family specific sorting functions. */
2N/A switch (af) {
2N/A case AF_INET:
2N/A order_haddrlist_inet(haddrlist, addrcount);
2N/A break;
2N/A case AF_INET6:
2N/A order_haddrlist_inet6(haddrlist, addrcount);
2N/A break;
2N/A default:
2N/A break;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Move any local (on-link) addresses toward the beginning of haddrlist.
2N/A * The order within these two classes is preserved.
2N/A *
2N/A * The interface list is retrieved no more often than every
2N/A * IFINFOTIMEOUT nanoseconds. Access to the interface list is
2N/A * protected by an RW lock.
2N/A *
2N/A * If this function encounters an error, haddrlist is unaltered.
2N/A */
2N/Astatic void
2N/Aorder_haddrlist_inet(char **haddrlist, size_t addrcount)
2N/A{
2N/A static struct ifinfo *localinfo = NULL;
2N/A static hrtime_t then = 0; /* the last time localinfo was updated */
2N/A hrtime_t now;
2N/A static rwlock_t localinfo_lock = DEFAULTRWLOCK;
2N/A uint8_t *sortbuf;
2N/A size_t sortbuf_size;
2N/A struct in_addr **inaddrlist = (struct in_addr **)haddrlist;
2N/A struct in_addr **sorted;
2N/A struct in_addr **classnext[ADDR_NUMCLASSES];
2N/A uint_t classcount[ADDR_NUMCLASSES];
2N/A addr_class_t *sortclass;
2N/A int i;
2N/A int rc;
2N/A
2N/A
2N/A /*
2N/A * The classes in the sortclass array correspond to the class
2N/A * of the address in the haddrlist list of the same index.
2N/A * The classes are:
2N/A *
2N/A * ADDR_ONLINK on-link address
2N/A * ADDR_OFFLINK off-link address
2N/A */
2N/A sortbuf_size = addrcount *
2N/A (sizeof (struct in_addr *) + sizeof (addr_class_t));
2N/A if ((sortbuf = malloc(sortbuf_size)) == NULL)
2N/A return;
2N/A /* LINTED pointer cast */
2N/A sorted = (struct in_addr **)sortbuf;
2N/A /* LINTED pointer cast */
2N/A sortclass = (addr_class_t *)(sortbuf +
2N/A (addrcount * sizeof (struct in_addr *)));
2N/A
2N/A /*
2N/A * Get a read lock, and check if the interface information
2N/A * is too old.
2N/A */
2N/A (void) rw_rdlock(&localinfo_lock);
2N/A now = gethrtime();
2N/A if (localinfo == NULL || ((now - then) > IFINFOTIMEOUT)) {
2N/A /* Need to update I/F info. Upgrade to write lock. */
2N/A (void) rw_unlock(&localinfo_lock);
2N/A (void) rw_wrlock(&localinfo_lock);
2N/A /*
2N/A * Another thread might have updated "then" between
2N/A * the rw_unlock() and rw_wrlock() calls above, so
2N/A * re-check the timeout.
2N/A */
2N/A if (localinfo == NULL || ((now - then) > IFINFOTIMEOUT)) {
2N/A if (localinfo != NULL)
2N/A free(localinfo);
2N/A if ((localinfo = get_local_info()) == NULL) {
2N/A (void) rw_unlock(&localinfo_lock);
2N/A free(sortbuf);
2N/A return;
2N/A }
2N/A then = now;
2N/A }
2N/A /* Downgrade to read lock */
2N/A (void) rw_unlock(&localinfo_lock);
2N/A (void) rw_rdlock(&localinfo_lock);
2N/A /*
2N/A * Another thread may have updated the I/F info,
2N/A * so verify that the 'localinfo' pointer still
2N/A * is non-NULL.
2N/A */
2N/A if (localinfo == NULL) {
2N/A (void) rw_unlock(&localinfo_lock);
2N/A free(sortbuf);
2N/A return;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Classify the addresses. We also maintain the classcount
2N/A * array to keep track of the number of addresses in each
2N/A * class.
2N/A */
2N/A (void) memset(classcount, 0, sizeof (classcount));
2N/A for (i = 0; i < addrcount; i++) {
2N/A if (__inet_address_is_local_af(localinfo, AF_INET,
2N/A inaddrlist[i]))
2N/A sortclass[i] = ADDR_ONLINK;
2N/A else
2N/A sortclass[i] = ADDR_OFFLINK;
2N/A classcount[sortclass[i]]++;
2N/A }
2N/A
2N/A /* Don't need the interface list anymore in this call */
2N/A (void) rw_unlock(&localinfo_lock);
2N/A
2N/A /*
2N/A * Each element in the classnext array points to the next
2N/A * element for that class in the sorted address list. 'rc' is
2N/A * the running count of elements as we sum the class
2N/A * sub-totals.
2N/A */
2N/A for (rc = 0, i = 0; i < ADDR_NUMCLASSES; i++) {
2N/A classnext[i] = &sorted[rc];
2N/A rc += classcount[i];
2N/A }
2N/A
2N/A /* Now for the actual rearrangement of the addresses */
2N/A for (i = 0; i < addrcount; i++) {
2N/A *(classnext[sortclass[i]]) = inaddrlist[i];
2N/A classnext[sortclass[i]]++;
2N/A }
2N/A
2N/A /* Copy the sorted list to inaddrlist */
2N/A (void) memcpy(inaddrlist, sorted,
2N/A addrcount * sizeof (struct in_addr *));
2N/A free(sortbuf);
2N/A}
2N/A
2N/A/*
2N/A * This function implements the IPv6 Default Address Selection's
2N/A * destination address ordering mechanism. The algorithm is described
2N/A * in getaddrinfo(3SOCKET).
2N/A */
2N/Astatic void
2N/Aorder_haddrlist_inet6(char **haddrlist, size_t addrcount)
2N/A{
2N/A struct dstinforeq *dinfo, *dinfoptr;
2N/A struct in6_addr **in6addrlist = (struct in6_addr **)haddrlist;
2N/A struct in6_addr **in6addr;
2N/A
2N/A if ((dinfo = calloc(addrcount, sizeof (struct dstinforeq))) == NULL)
2N/A return;
2N/A
2N/A /* Initialize the dstinfo array we'll use for SIOCGDSTINFO */
2N/A dinfoptr = dinfo;
2N/A for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) {
2N/A dinfoptr->dir_daddr = **in6addr;
2N/A dinfoptr++;
2N/A }
2N/A
2N/A if (nss_strioctl(AF_INET6, SIOCGDSTINFO, dinfo,
2N/A addrcount * sizeof (struct dstinforeq)) < 0) {
2N/A free(dinfo);
2N/A return;
2N/A }
2N/A
2N/A /* Sort the dinfo array */
2N/A qsort(dinfo, addrcount, sizeof (struct dstinforeq), dstcmp);
2N/A
2N/A /* Copy the addresses back into in6addrlist */
2N/A dinfoptr = dinfo;
2N/A for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) {
2N/A **in6addr = dinfoptr->dir_daddr;
2N/A dinfoptr++;
2N/A }
2N/A
2N/A free(dinfo);
2N/A}
2N/A
2N/A/*
2N/A * Determine number of leading bits that are common between two addresses.
2N/A * Only consider bits which fall within the prefix length plen.
2N/A */
2N/Astatic uint_t
2N/Aip_addr_commonbits_v6(const in6_addr_t *a1, const in6_addr_t *a2)
2N/A{
2N/A uint_t bits;
2N/A uint_t i;
2N/A uint32_t diff; /* Bits that differ */
2N/A
2N/A for (i = 0; i < 4; i++) {
2N/A if (a1->_S6_un._S6_u32[i] != a2->_S6_un._S6_u32[i])
2N/A break;
2N/A }
2N/A bits = i * 32;
2N/A
2N/A if (bits == IPV6_ABITS)
2N/A return (IPV6_ABITS);
2N/A
2N/A /*
2N/A * Find number of leading common bits in the word which might
2N/A * have some common bits by searching for the first one from the left
2N/A * in the xor of the two addresses.
2N/A */
2N/A diff = ntohl(a1->_S6_un._S6_u32[i] ^ a2->_S6_un._S6_u32[i]);
2N/A if (diff & 0xffff0000ul)
2N/A diff >>= 16;
2N/A else
2N/A bits += 16;
2N/A if (diff & 0xff00)
2N/A diff >>= 8;
2N/A else
2N/A bits += 8;
2N/A if (diff & 0xf0)
2N/A diff >>= 4;
2N/A else
2N/A bits += 4;
2N/A if (diff & 0xc)
2N/A diff >>= 2;
2N/A else
2N/A bits += 2;
2N/A if (!(diff & 2))
2N/A bits++;
2N/A
2N/A /*
2N/A * We don't need to shift and check for the last bit. The
2N/A * check for IPV6_ABITS above would have caught that.
2N/A */
2N/A
2N/A return (bits);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * The following group of functions named rule_*() are individual
2N/A * sorting rules for the AF_INET6 address sorting algorithm. The
2N/A * functions compare two addresses (described by two dstinforeq
2N/A * structures), and determines if one is "greater" than the other, or
2N/A * if the two are equal according to that rule.
2N/A */
2N/Atypedef int (*rulef_t)(const struct dstinforeq *, const struct dstinforeq *);
2N/A
2N/A/*
2N/A * These values of these constants are no accident. Since qsort()
2N/A * implements the AF_INET6 address sorting, the comparison function
2N/A * must return an integer less than, equal to, or greater than zero to
2N/A * indicate if the first address is considered "less than", "equal
2N/A * to", or "greater than" the second one. Since we want the best
2N/A * addresses first on the list, "less than" is considered preferrable.
2N/A */
2N/A#define RULE_PREFER_DA -1
2N/A#define RULE_PREFER_DB 1
2N/A#define RULE_EQUAL 0
2N/A
2N/A/* Prefer the addresses that is reachable. */
2N/Astatic int
2N/Arule_reachable(const struct dstinforeq *da, const struct dstinforeq *db)
2N/A{
2N/A if (da->dir_dreachable == db->dir_dreachable)
2N/A return (RULE_EQUAL);
2N/A if (da->dir_dreachable)
2N/A return (RULE_PREFER_DA);
2N/A return (RULE_PREFER_DB);
2N/A}
2N/A
2N/A/* Prefer the address whose scope matches that of its source address. */
2N/Astatic int
2N/Arule_matchscope(const struct dstinforeq *da, const struct dstinforeq *db)
2N/A{
2N/A boolean_t da_scope_match, db_scope_match;
2N/A
2N/A da_scope_match = da->dir_dscope == da->dir_sscope;
2N/A db_scope_match = db->dir_dscope == db->dir_sscope;
2N/A
2N/A if (da_scope_match == db_scope_match)
2N/A return (RULE_EQUAL);
2N/A if (da_scope_match)
2N/A return (RULE_PREFER_DA);
2N/A return (RULE_PREFER_DB);
2N/A}
2N/A
2N/A/* Avoid the address with the link local source address. */
2N/Astatic int
2N/Arule_avoidlinklocal(const struct dstinforeq *da, const struct dstinforeq *db)
2N/A{
2N/A if (da->dir_sscope == IP6_SCOPE_LINKLOCAL &&
2N/A da->dir_dscope != IP6_SCOPE_LINKLOCAL &&
2N/A db->dir_sscope != IP6_SCOPE_LINKLOCAL)
2N/A return (RULE_PREFER_DB);
2N/A if (db->dir_sscope == IP6_SCOPE_LINKLOCAL &&
2N/A db->dir_dscope != IP6_SCOPE_LINKLOCAL &&
2N/A da->dir_sscope != IP6_SCOPE_LINKLOCAL)
2N/A return (RULE_PREFER_DA);
2N/A return (RULE_EQUAL);
2N/A}
2N/A
2N/A/* Prefer the address whose source address isn't deprecated. */
2N/Astatic int
2N/Arule_deprecated(const struct dstinforeq *da, const struct dstinforeq *db)
2N/A{
2N/A if (da->dir_sdeprecated == db->dir_sdeprecated)
2N/A return (RULE_EQUAL);
2N/A if (db->dir_sdeprecated)
2N/A return (RULE_PREFER_DA);
2N/A return (RULE_PREFER_DB);
2N/A}
2N/A
2N/A/* Prefer the address whose label matches that of its source address. */
2N/Astatic int
2N/Arule_label(const struct dstinforeq *da, const struct dstinforeq *db)
2N/A{
2N/A if (da->dir_labelmatch == db->dir_labelmatch)
2N/A return (RULE_EQUAL);
2N/A if (da->dir_labelmatch)
2N/A return (RULE_PREFER_DA);
2N/A return (RULE_PREFER_DB);
2N/A}
2N/A
2N/A/* Prefer the address with the higher precedence. */
2N/Astatic int
2N/Arule_precedence(const struct dstinforeq *da, const struct dstinforeq *db)
2N/A{
2N/A if (da->dir_precedence == db->dir_precedence)
2N/A return (RULE_EQUAL);
2N/A if (da->dir_precedence > db->dir_precedence)
2N/A return (RULE_PREFER_DA);
2N/A return (RULE_PREFER_DB);
2N/A}
2N/A
2N/A/* Prefer the address whose output interface isn't an IP tunnel */
2N/Astatic int
2N/Arule_native(const struct dstinforeq *da, const struct dstinforeq *db)
2N/A{
2N/A boolean_t isatun, isbtun;
2N/A
2N/A /* Get the common case out of the way early */
2N/A if (da->dir_dmactype == db->dir_dmactype)
2N/A return (RULE_EQUAL);
2N/A
2N/A isatun = da->dir_dmactype == DL_IPV4 || da->dir_dmactype == DL_IPV6;
2N/A isbtun = db->dir_dmactype == DL_IPV4 || db->dir_dmactype == DL_IPV6;
2N/A
2N/A if (isatun == isbtun)
2N/A return (RULE_EQUAL);
2N/A if (isbtun)
2N/A return (RULE_PREFER_DA);
2N/A return (RULE_PREFER_DB);
2N/A}
2N/A
2N/A/* Prefer the address with the smaller scope. */
2N/Astatic int
2N/Arule_scope(const struct dstinforeq *da, const struct dstinforeq *db)
2N/A{
2N/A if (da->dir_dscope == db->dir_dscope)
2N/A return (RULE_EQUAL);
2N/A if (da->dir_dscope < db->dir_dscope)
2N/A return (RULE_PREFER_DA);
2N/A return (RULE_PREFER_DB);
2N/A}
2N/A
2N/A/*
2N/A * Prefer the address that has the most leading bits in common with its
2N/A * source address.
2N/A */
2N/Astatic int
2N/Arule_prefix(const struct dstinforeq *da, const struct dstinforeq *db)
2N/A{
2N/A uint_t da_commonbits, db_commonbits;
2N/A boolean_t da_isipv4, db_isipv4;
2N/A
2N/A da_isipv4 = IN6_IS_ADDR_V4MAPPED(&da->dir_daddr);
2N/A db_isipv4 = IN6_IS_ADDR_V4MAPPED(&db->dir_daddr);
2N/A
2N/A /*
2N/A * At this point, the order doesn't matter if the two addresses
2N/A * aren't of the same address family.
2N/A */
2N/A if (da_isipv4 != db_isipv4)
2N/A return (RULE_EQUAL);
2N/A
2N/A da_commonbits = ip_addr_commonbits_v6(&da->dir_daddr, &da->dir_saddr);
2N/A db_commonbits = ip_addr_commonbits_v6(&db->dir_daddr, &db->dir_saddr);
2N/A
2N/A if (da_commonbits > db_commonbits)
2N/A return (RULE_PREFER_DA);
2N/A if (da_commonbits < db_commonbits)
2N/A return (RULE_PREFER_DB);
2N/A return (RULE_EQUAL);
2N/A}
2N/A
2N/A/*
2N/A * This is the function passed to qsort() that does the AF_INET6
2N/A * address comparisons. It compares two addresses using a list of
2N/A * rules. The rules are applied in order until one prefers one
2N/A * address over the other.
2N/A */
2N/Astatic int
2N/Adstcmp(const void *da, const void *db)
2N/A{
2N/A int index, result;
2N/A rulef_t rules[] = {
2N/A rule_reachable,
2N/A rule_matchscope,
2N/A rule_avoidlinklocal,
2N/A rule_deprecated,
2N/A rule_label,
2N/A rule_precedence,
2N/A rule_native,
2N/A rule_scope,
2N/A rule_prefix,
2N/A NULL
2N/A };
2N/A
2N/A result = 0;
2N/A for (index = 0; rules[index] != NULL; index++) {
2N/A result = (rules[index])(da, db);
2N/A if (result != RULE_EQUAL)
2N/A break;
2N/A }
2N/A
2N/A return (result);
2N/A}
2N/A
2N/A/*
2N/A * Given haddrlist and a port number, mallocs and populates a new
2N/A * nd_addrlist. The new nd_addrlist maintains the order of the addresses
2N/A * in haddrlist, which have already been sorted by order_haddrlist_inet()
2N/A * or order_haddrlist_inet6(). For IPv6 this function filters out
2N/A * IPv4-mapped IPv6 addresses.
2N/A */
2N/Aint
2N/Ahent2ndaddr(int af, char **haddrlist, int *servp, struct nd_addrlist **nd_alist)
2N/A{
2N/A struct nd_addrlist *result;
2N/A int num;
2N/A struct netbuf *na;
2N/A struct sockaddr_in *sinbuf, *sin;
2N/A struct sockaddr_in6 *sin6buf, *sin6;
2N/A struct in_addr **inaddr, **inaddrlist;
2N/A struct in6_addr **in6addr, **in6addrlist;
2N/A
2N/A /* Address count */
2N/A num = 0;
2N/A if (af == AF_INET6) {
2N/A in6addrlist = (struct in6_addr **)haddrlist;
2N/A
2N/A /*
2N/A * Exclude IPv4-mapped IPv6 addresses from the count, as
2N/A * these are not included in the nd_addrlist we return.
2N/A */
2N/A for (in6addr = in6addrlist; *in6addr != NULL; in6addr++)
2N/A if (!IN6_IS_ADDR_V4MAPPED(*in6addr))
2N/A num++;
2N/A } else {
2N/A inaddrlist = (struct in_addr **)haddrlist;
2N/A
2N/A for (inaddr = inaddrlist; *inaddr != NULL; inaddr++)
2N/A num++;
2N/A }
2N/A if (num == 0)
2N/A return (ND_NOHOST);
2N/A
2N/A result = malloc(sizeof (struct nd_addrlist));
2N/A if (result == 0)
2N/A return (ND_NOMEM);
2N/A
2N/A result->n_cnt = num;
2N/A result->n_addrs = calloc(num, sizeof (struct netbuf));
2N/A if (result->n_addrs == 0) {
2N/A free(result);
2N/A return (ND_NOMEM);
2N/A }
2N/A
2N/A na = result->n_addrs;
2N/A if (af == AF_INET) {
2N/A sinbuf = calloc(num, sizeof (struct sockaddr_in));
2N/A if (sinbuf == NULL) {
2N/A free(result->n_addrs);
2N/A free(result);
2N/A return (ND_NOMEM);
2N/A }
2N/A
2N/A sin = sinbuf;
2N/A for (inaddr = inaddrlist; *inaddr != NULL; inaddr++) {
2N/A na->len = na->maxlen = sizeof (struct sockaddr_in);
2N/A na->buf = (char *)sin;
2N/A sin->sin_family = AF_INET;
2N/A sin->sin_addr = **inaddr;
2N/A sin->sin_port = *servp;
2N/A na++;
2N/A sin++;
2N/A }
2N/A } else if (af == AF_INET6) {
2N/A sin6buf = calloc(num, sizeof (struct sockaddr_in6));
2N/A if (sin6buf == NULL) {
2N/A free(result->n_addrs);
2N/A free(result);
2N/A return (ND_NOMEM);
2N/A }
2N/A
2N/A sin6 = sin6buf;
2N/A for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) {
2N/A if (IN6_IS_ADDR_V4MAPPED(*in6addr))
2N/A continue;
2N/A
2N/A na->len = na->maxlen = sizeof (struct sockaddr_in6);
2N/A na->buf = (char *)sin6;
2N/A sin6->sin6_family = AF_INET6;
2N/A sin6->sin6_addr = **in6addr;
2N/A sin6->sin6_port = *servp;
2N/A na++;
2N/A sin6++;
2N/A }
2N/A }
2N/A *(nd_alist) = result;
2N/A return (ND_OK);
2N/A}
2N/A
2N/A/*
2N/A * Given a hostent and a servent, mallocs and populates
2N/A * a new nd_hostservlist with host and service names.
2N/A *
2N/A * We could be passed in a NULL servent, in which case stringify port.
2N/A */
2N/Aint
2N/Ahsents2ndhostservs(struct hostent *he, struct servent *se,
2N/A ushort_t port, struct nd_hostservlist **hslist)
2N/A{
2N/A struct nd_hostservlist *result;
2N/A struct nd_hostserv *hs;
2N/A int hosts, servs, i, j;
2N/A char **hn, **sn;
2N/A
2N/A if ((result = malloc(sizeof (struct nd_hostservlist))) == 0)
2N/A return (ND_NOMEM);
2N/A
2N/A /*
2N/A * We initialize the counters to 1 rather than zero because
2N/A * we have to count the "official" name as well as the aliases.
2N/A */
2N/A for (hn = he->h_aliases, hosts = 1; hn && *hn; hn++, hosts++) {};
2N/A if (se) {
2N/A for (sn = se->s_aliases, servs = 1; sn && *sn; sn++, servs++) {
2N/A };
2N/A } else
2N/A servs = 1;
2N/A
2N/A if ((hs = calloc(hosts * servs, sizeof (struct nd_hostserv))) == 0) {
2N/A free(result);
2N/A return (ND_NOMEM);
2N/A }
2N/A
2N/A result->h_cnt = servs * hosts;
2N/A result->h_hostservs = hs;
2N/A
2N/A for (i = 0, hn = he->h_aliases; i < hosts; i++) {
2N/A sn = se ? se->s_aliases : NULL;
2N/A
2N/A for (j = 0; j < servs; j++) {
2N/A if (i == 0)
2N/A hs->h_host = strdup(he->h_name);
2N/A else
2N/A hs->h_host = strdup(*hn);
2N/A if (j == 0) {
2N/A if (se)
2N/A hs->h_serv = strdup(se->s_name);
2N/A else {
2N/A /* Convert to a number string */
2N/A char stmp[16];
2N/A
2N/A (void) sprintf(stmp, "%d", port);
2N/A hs->h_serv = strdup(stmp);
2N/A }
2N/A } else
2N/A hs->h_serv = strdup(*sn++);
2N/A
2N/A if ((hs->h_host == 0) || (hs->h_serv == 0)) {
2N/A free(result->h_hostservs);
2N/A free(result);
2N/A return (ND_NOMEM);
2N/A }
2N/A hs++;
2N/A }
2N/A if (i)
2N/A hn++;
2N/A }
2N/A *(hslist) = result;
2N/A return (ND_OK);
2N/A}
2N/A
2N/A/*
2N/A * Process results from nd_addrlist ( returned by netdir_getbyname)
2N/A * into a hostent using buf.
2N/A * *** ASSUMES that nd_addrlist->n_addrs->buf contains IP addresses in
2N/A * sockaddr_in's ***
2N/A */
2N/Aint
2N/Andaddr2hent(int af, const char *nam, struct nd_addrlist *addrs,
2N/A struct hostent *result, char *buffer, int buflen)
2N/A{
2N/A int i, count;
2N/A struct in_addr *addrp;
2N/A struct in6_addr *addr6p;
2N/A char **addrvec;
2N/A struct netbuf *na;
2N/A size_t len;
2N/A
2N/A result->h_name = buffer;
2N/A result->h_addrtype = af;
2N/A result->h_length = (af == AF_INET) ? sizeof (*addrp):
2N/A sizeof (*addr6p);
2N/A
2N/A /*
2N/A * Build addrlist at start of buffer (after name); store the
2N/A * addresses themselves at the end of the buffer.
2N/A */
2N/A len = strlen(nam) + 1;
2N/A addrvec = (char **)ROUND_UP(buffer + len, sizeof (*addrvec));
2N/A result->h_addr_list = addrvec;
2N/A
2N/A if (af == AF_INET) {
2N/A addrp = (struct in_addr *)ROUND_DOWN(buffer + buflen,
2N/A sizeof (*addrp));
2N/A
2N/A count = addrs->n_cnt;
2N/A if ((char *)(&addrvec[count + 1]) > (char *)(&addrp[-count]))
2N/A return (ND_NOMEM);
2N/A
2N/A (void) memcpy(buffer, nam, len);
2N/A
2N/A for (na = addrs->n_addrs, i = 0; i < count; na++, i++) {
2N/A --addrp;
2N/A (void) memcpy(addrp,
2N/A &((struct sockaddr_in *)na->buf)->sin_addr,
2N/A sizeof (*addrp));
2N/A *addrvec++ = (char *)addrp;
2N/A }
2N/A } else {
2N/A addr6p = (struct in6_addr *)ROUND_DOWN(buffer + buflen,
2N/A sizeof (*addr6p));
2N/A
2N/A count = addrs->n_cnt;
2N/A if ((char *)(&addrvec[count + 1]) > (char *)(&addr6p[-count]))
2N/A return (ND_NOMEM);
2N/A
2N/A (void) memcpy(buffer, nam, len);
2N/A
2N/A for (na = addrs->n_addrs, i = 0; i < count; na++, i++) {
2N/A --addr6p;
2N/A (void) memcpy(addr6p,
2N/A &((struct sockaddr_in6 *)na->buf)->sin6_addr,
2N/A sizeof (*addr6p));
2N/A *addrvec++ = (char *)addr6p;
2N/A }
2N/A }
2N/A *addrvec = 0;
2N/A result->h_aliases = addrvec;
2N/A
2N/A return (ND_OK);
2N/A}
2N/A
2N/A/*
2N/A * Process results from nd_addrlist ( returned by netdir_getbyname)
2N/A * into a servent using buf.
2N/A */
2N/Aint
2N/Andaddr2srent(const char *name, const char *proto, ushort_t port,
2N/A struct servent *result, char *buffer, int buflen)
2N/A{
2N/A size_t i;
2N/A char *bufend = (buffer + buflen);
2N/A
2N/A result->s_port = (int)port;
2N/A
2N/A result->s_aliases =
2N/A (char **)ROUND_UP(buffer, sizeof (char *));
2N/A result->s_aliases[0] = NULL;
2N/A buffer = (char *)&result->s_aliases[1];
2N/A result->s_name = buffer;
2N/A i = strlen(name) + 1;
2N/A if ((buffer + i) > bufend)
2N/A return (ND_NOMEM);
2N/A (void) memcpy(buffer, name, i);
2N/A buffer += i;
2N/A
2N/A result->s_proto = buffer;
2N/A i = strlen(proto) + 1;
2N/A if ((buffer + i) > bufend)
2N/A return (ND_NOMEM);
2N/A (void) memcpy(buffer, proto, i);
2N/A buffer += i;
2N/A
2N/A return (ND_OK);
2N/A}
2N/A
2N/A/*
2N/A * Process results from nd_hostservlist ( returned by netdir_getbyaddr)
2N/A * into a hostent using buf.
2N/A * *** ASSUMES that nd_buf->buf is a sockaddr_in ***
2N/A */
2N/Aint
2N/Andhostserv2hent(struct netbuf *nbuf, struct nd_hostservlist *addrs,
2N/A struct hostent *result, char *buffer, int buflen)
2N/A{
2N/A int i, count;
2N/A char *aliasp;
2N/A char **aliasvec;
2N/A struct sockaddr_in *sa;
2N/A struct nd_hostserv *hs;
2N/A const char *la;
2N/A size_t length;
2N/A
2N/A /* First, give the lonely address a specious home in h_addr_list. */
2N/A aliasp = (char *)ROUND_UP(buffer, sizeof (sa->sin_addr));
2N/A sa = (struct sockaddr_in *)nbuf->buf;
2N/A (void) memcpy(aliasp, &(sa->sin_addr), sizeof (sa->sin_addr));
2N/A aliasvec = (char **)ROUND_UP(aliasp + sizeof (sa->sin_addr),
2N/A sizeof (*aliasvec));
2N/A result->h_addr_list = aliasvec;
2N/A *aliasvec++ = aliasp;
2N/A *aliasvec++ = 0;
2N/A
2N/A /*
2N/A * Build h_aliases at start of buffer (after addr and h_addr_list);
2N/A * store the alias strings at the end of the buffer (before h_name).
2N/A */
2N/A
2N/A aliasp = buffer + buflen;
2N/A
2N/A result->h_aliases = aliasvec;
2N/A
2N/A hs = addrs->h_hostservs;
2N/A if (!hs)
2N/A return (ND_NOHOST);
2N/A
2N/A length = strlen(hs->h_host) + 1;
2N/A aliasp -= length;
2N/A if ((char *)(&aliasvec[1]) > aliasp)
2N/A return (ND_NOMEM);
2N/A (void) memcpy(aliasp, hs->h_host, length);
2N/A
2N/A result->h_name = aliasp;
2N/A result->h_addrtype = AF_INET;
2N/A result->h_length = sizeof (sa->sin_addr);
2N/A
2N/A /*
2N/A * Assumption: the netdir nametoaddr_libs
2N/A * sort the vector of (host, serv) pairs in such a way that
2N/A * all pairs with the same host name are contiguous.
2N/A */
2N/A la = hs->h_host;
2N/A count = addrs->h_cnt;
2N/A for (i = 0; i < count; i++, hs++)
2N/A if (strcmp(la, hs->h_host) != 0) {
2N/A size_t len = strlen(hs->h_host) + 1;
2N/A
2N/A aliasp -= len;
2N/A if ((char *)(&aliasvec[2]) > aliasp)
2N/A return (ND_NOMEM);
2N/A (void) memcpy(aliasp, hs->h_host, len);
2N/A *aliasvec++ = aliasp;
2N/A la = hs->h_host;
2N/A }
2N/A *aliasvec = 0;
2N/A
2N/A return (ND_OK);
2N/A}
2N/A
2N/A/*
2N/A * Process results from nd_hostservlist ( returned by netdir_getbyaddr)
2N/A * into a servent using buf.
2N/A */
2N/Aint
2N/Andhostserv2srent(int port, const char *proto, struct nd_hostservlist *addrs,
2N/A struct servent *result, char *buffer, int buflen)
2N/A{
2N/A int i, count;
2N/A char *aliasp;
2N/A char **aliasvec;
2N/A struct nd_hostserv *hs;
2N/A const char *host_cname;
2N/A size_t leni, lenj;
2N/A
2N/A result->s_port = port;
2N/A /*
2N/A * Build s_aliases at start of buffer;
2N/A * store proto and aliases at the end of the buffer (before h_name).
2N/A */
2N/A
2N/A aliasp = buffer + buflen;
2N/A aliasvec = (char **)ROUND_UP(buffer, sizeof (char *));
2N/A
2N/A result->s_aliases = aliasvec;
2N/A
2N/A hs = addrs->h_hostservs;
2N/A if (!hs)
2N/A return (ND_NOHOST);
2N/A host_cname = hs->h_host;
2N/A
2N/A leni = strlen(proto) + 1;
2N/A lenj = strlen(hs->h_serv) + 1;
2N/A if ((char *)(&aliasvec[2]) > (aliasp - leni - lenj))
2N/A return (ND_NOMEM);
2N/A
2N/A aliasp -= leni;
2N/A (void) memcpy(aliasp, proto, leni);
2N/A result->s_proto = aliasp;
2N/A
2N/A aliasp -= lenj;
2N/A (void) memcpy(aliasp, hs->h_serv, lenj);
2N/A result->s_name = aliasp;
2N/A
2N/A /*
2N/A * Assumption: the netdir nametoaddr_libs
2N/A * do a host aliases first and serv aliases next
2N/A * enumeration for creating the list of hostserv
2N/A * structures.
2N/A */
2N/A count = addrs->h_cnt;
2N/A for (i = 0;
2N/A i < count && hs->h_serv && strcmp(hs->h_host, host_cname) == 0;
2N/A i++, hs++) {
2N/A size_t len = strlen(hs->h_serv) + 1;
2N/A
2N/A aliasp -= len;
2N/A if ((char *)(&aliasvec[2]) > aliasp)
2N/A return (ND_NOMEM);
2N/A (void) memcpy(aliasp, hs->h_serv, len);
2N/A *aliasvec++ = aliasp;
2N/A }
2N/A *aliasvec = NULL;
2N/A
2N/A return (ND_OK);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/And2herrno(int nerr)
2N/A{
2N/A switch (nerr) {
2N/A case ND_OK:
2N/A return (0);
2N/A case ND_TRY_AGAIN:
2N/A return (TRY_AGAIN);
2N/A case ND_NO_RECOVERY:
2N/A case ND_BADARG:
2N/A case ND_NOMEM:
2N/A return (NO_RECOVERY);
2N/A case ND_NO_DATA:
2N/A return (NO_DATA);
2N/A case ND_NOHOST:
2N/A case ND_NOSERV:
2N/A return (HOST_NOT_FOUND);
2N/A default:
2N/A return (NO_RECOVERY);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * This is a utility function so that various parts of libnsl can
2N/A * easily send ioctls down to ip.
2N/A *
2N/A */
2N/Aint
2N/Anss_ioctl(int af, int cmd, void *arg)
2N/A{
2N/A int fd;
2N/A char *devpath;
2N/A int retv;
2N/A
2N/A switch (af) {
2N/A case AF_INET6:
2N/A devpath = UDP6DEV;
2N/A break;
2N/A case AF_INET:
2N/A case AF_UNSPEC:
2N/A default:
2N/A devpath = UDPDEV;
2N/A }
2N/A if ((fd = open(devpath, O_RDONLY)) < 0) {
2N/A return (-1);
2N/A }
2N/A while ((retv = ioctl(fd, cmd, arg)) == -1) {
2N/A if (errno != EINTR)
2N/A break;
2N/A }
2N/A (void) close(fd);
2N/A return (retv);
2N/A}
2N/A
2N/Astatic int
2N/Anss_strioctl(int af, int cmd, void *ptr, int ilen)
2N/A{
2N/A struct strioctl str;
2N/A
2N/A str.ic_cmd = cmd;
2N/A str.ic_timout = 0;
2N/A str.ic_len = ilen;
2N/A str.ic_dp = ptr;
2N/A
2N/A return (nss_ioctl(af, I_STR, &str));
2N/A}
2N/A
2N/Astatic struct ifinfo *
2N/Aget_local_info(void)
2N/A{
2N/A int numifs;
2N/A int n;
2N/A char *buf = NULL;
2N/A size_t needed;
2N/A struct lifconf lifc;
2N/A struct lifreq lifreq, *lifr;
2N/A struct lifnum lifn;
2N/A struct ifinfo *localinfo;
2N/A
2N/A lifn.lifn_family = AF_UNSPEC;
2N/A lifn.lifn_flags = 0;
2N/A
2N/Agetifnum:
2N/A if (nss_ioctl(AF_UNSPEC, SIOCGLIFNUM, &lifn) == -1) {
2N/A numifs = MAXIFS;
2N/A } else {
2N/A numifs = lifn.lifn_count;
2N/A }
2N/A
2N/A /*
2N/A * Add a small fudge factor in case interfaces get plumbed between
2N/A * the call to SIOCGLIFNUM and SIOCGLIFCONF.
2N/A */
2N/A needed = (numifs + 4) * sizeof (lifreq);
2N/A if (buf == NULL)
2N/A buf = malloc(needed);
2N/A else
2N/A buf = realloc(buf, needed);
2N/A if (buf == NULL) {
2N/A (void) syslog(LOG_ERR, "n2a get_local_info: malloc failed: %m");
2N/A _nderror = ND_NOMEM;
2N/A return (NULL);
2N/A }
2N/A lifc.lifc_family = AF_UNSPEC;
2N/A lifc.lifc_flags = 0;
2N/A lifc.lifc_len = needed;
2N/A lifc.lifc_buf = buf;
2N/A if (nss_ioctl(AF_UNSPEC, SIOCGLIFCONF, &lifc) == -1) {
2N/A /*
2N/A * IP returns EINVAL if the buffer was too small to fit
2N/A * all of the entries. If that's the case, go back and
2N/A * try again.
2N/A */
2N/A if (errno == EINVAL)
2N/A goto getifnum;
2N/A
2N/A (void) syslog(LOG_ERR, "n2a get_local_info: "
2N/A "ioctl (get interface configuration): %m");
2N/A free(buf);
2N/A _nderror = ND_SYSTEM;
2N/A return (NULL);
2N/A }
2N/A /* LINTED pointer cast */
2N/A lifr = (struct lifreq *)buf;
2N/A numifs = lifc.lifc_len/sizeof (lifreq);
2N/A localinfo = malloc(ifinfosize(numifs));
2N/A if (localinfo == NULL) {
2N/A (void) syslog(LOG_ERR, "n2a get_local_info: malloc failed: %m");
2N/A free(buf);
2N/A _nderror = ND_SYSTEM;
2N/A return (NULL);
2N/A }
2N/A
2N/A /* LINTED pointer cast */
2N/A localinfo->addresses = (struct __ifaddr *)
2N/A ((char *)localinfo + sizeof (struct ifinfo));
2N/A
2N/A for (localinfo->count = 0, n = numifs; n > 0; n--, lifr++) {
2N/A int af;
2N/A
2N/A lifreq = *lifr;
2N/A af = lifreq.lifr_addr.ss_family;
2N/A
2N/A /* Squirrel away the address */
2N/A if (ifassign(lifreq, localinfo->count, IF_ADDR) == 0)
2N/A continue;
2N/A
2N/A if (nss_ioctl(af, SIOCGLIFFLAGS, &lifreq) < 0) {
2N/A (void) syslog(LOG_ERR,
2N/A "n2a get_local_info: "
2N/A "ioctl (get interface flags): %m");
2N/A continue;
2N/A }
2N/A if (!(lifreq.lifr_flags & IFF_UP))
2N/A continue;
2N/A
2N/A if (nss_ioctl(af, SIOCGLIFNETMASK, &lifreq) < 0) {
2N/A (void) syslog(LOG_ERR,
2N/A "n2a get_local_info: "
2N/A "ioctl (get interface netmask): %m");
2N/A continue;
2N/A }
2N/A
2N/A if (ifassign(lifreq, localinfo->count, IF_MASK) == 0)
2N/A continue;
2N/A
2N/A localinfo->count++;
2N/A }
2N/A
2N/A free(buf);
2N/A return (localinfo);
2N/A}
2N/A
2N/Astatic int
2N/A__inet_ifassign(sa_family_t af, struct __ifaddr *ifa, __ifaddr_type type,
2N/A void *addr) {
2N/A switch (type) {
2N/A case IF_ADDR:
2N/A ifa->af = af;
2N/A if (af == AF_INET6) {
2N/A ifa->addr.in6 = *(struct in6_addr *)addr;
2N/A } else {
2N/A ifa->addr.in4 = *(struct in_addr *)addr;
2N/A }
2N/A break;
2N/A case IF_MASK:
2N/A if (ifa->af == af) {
2N/A if (af == AF_INET6) {
2N/A ifa->mask.in6 = *(struct in6_addr *)addr;
2N/A } else {
2N/A ifa->mask.in4 = *(struct in_addr *)addr;
2N/A }
2N/A } else {
2N/A return (0);
2N/A }
2N/A break;
2N/A default:
2N/A return (0);
2N/A }
2N/A
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * Some higher-level routines for determining if an address is
2N/A * on a local network.
2N/A *
2N/A * __inet_get_local_interfaces() - get an opaque handle with
2N/A * with a list of local interfaces
2N/A * __inet_address_is_local() - return 1 if an address is
2N/A * on a local network; 0 otherwise
2N/A * __inet_free_local_interfaces() - free handle that was
2N/A * returned by __inet_get_local_interfaces()
2N/A *
2N/A * A typical calling sequence is:
2N/A *
2N/A * p = __inet_get_local_interfaces();
2N/A * if (__inet_address_is_local(p, inaddr)) {
2N/A * ...
2N/A * }
2N/A * __inet_free_local_interfaces(p);
2N/A */
2N/A
2N/A/*
2N/A * Return an opaque pointer to a list of configured interfaces.
2N/A */
2N/Avoid *
2N/A__inet_get_local_interfaces(void)
2N/A{
2N/A return (get_local_info());
2N/A}
2N/A
2N/A/*
2N/A * Free memory allocated by inet_local_interfaces().
2N/A */
2N/Avoid
2N/A__inet_free_local_interfaces(void *p)
2N/A{
2N/A free(p);
2N/A}
2N/A
2N/A/*
2N/A * Determine if an address is on a local network.
2N/A *
2N/A * Might have made sense to use SIOCTONLINK, except that it doesn't
2N/A * handle matching on IPv4 network addresses.
2N/A */
2N/Aint
2N/A__inet_address_is_local_af(void *p, sa_family_t af, void *addr) {
2N/A
2N/A struct ifinfo *localinfo = (struct ifinfo *)p;
2N/A int i, a;
2N/A struct in_addr v4addr;
2N/A
2N/A if (localinfo == 0)
2N/A return (0);
2N/A
2N/A if (af == AF_INET6 && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)addr)) {
2N/A IN6_V4MAPPED_TO_INADDR((struct in6_addr *)addr, &v4addr);
2N/A af = AF_INET;
2N/A addr = (void *)&v4addr;
2N/A }
2N/A
2N/A for (i = 0; i < localinfo->count; i++) {
2N/A if (ifaf(i) == af) {
2N/A if (af == AF_INET6) {
2N/A struct in6_addr *a6 = (struct in6_addr *)addr;
2N/A for (a = 0; a < sizeof (a6->s6_addr); a++) {
2N/A if ((a6->s6_addr[a] &
2N/A ifmask6(i).s6_addr[a]) !=
2N/A (ifaddr6(i).s6_addr[a] &
2N/A ifmask6(i).s6_addr[a]))
2N/A break;
2N/A }
2N/A if (a >= sizeof (a6->s6_addr))
2N/A return (1);
2N/A } else {
2N/A if ((((struct in_addr *)addr)->s_addr &
2N/A ifmask4(i).s_addr) ==
2N/A (ifaddr4(i).s_addr &
2N/A ifmask4(i).s_addr))
2N/A return (1);
2N/A }
2N/A }
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/A__inet_address_is_local(void *p, struct in_addr addr)
2N/A{
2N/A return (__inet_address_is_local_af(p, AF_INET, &addr));
2N/A}
2N/A
2N/Aint
2N/A__inet_uaddr_is_local(void *p, struct netconfig *nc, char *uaddr)
2N/A{
2N/A struct netbuf *taddr;
2N/A sa_family_t af;
2N/A int ret;
2N/A
2N/A taddr = uaddr2taddr(nc, uaddr);
2N/A if (taddr == 0)
2N/A return (0);
2N/A
2N/A af = ((struct sockaddr *)taddr->buf)->sa_family;
2N/A
2N/A ret = __inet_address_is_local_af(p, af, (af == AF_INET6) ?
2N/A (void *)&((struct sockaddr_in6 *)taddr->buf)->sin6_addr :
2N/A (void *)&((struct sockaddr_in *)taddr->buf)->sin_addr);
2N/A
2N/A netdir_free(taddr, ND_ADDR);
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/Aint
2N/A__inet_address_count(void *p)
2N/A{
2N/A struct ifinfo *lp = (struct ifinfo *)p;
2N/A
2N/A if (lp != 0) {
2N/A return (lp->count);
2N/A } else {
2N/A return (0);
2N/A }
2N/A}
2N/A
2N/Auint32_t
2N/A__inet_get_addr(void *p, int n)
2N/A{
2N/A struct ifinfo *localinfo = (struct ifinfo *)p;
2N/A
2N/A if (localinfo == 0 || n >= localinfo->count || ifaf(n) != AF_INET)
2N/A return (0);
2N/A
2N/A return (ifaddr4(n).s_addr);
2N/A}
2N/A
2N/Auint32_t
2N/A__inet_get_network(void *p, int n)
2N/A{
2N/A struct ifinfo *localinfo = (struct ifinfo *)p;
2N/A
2N/A if (localinfo == 0 || n >= localinfo->count || ifaf(n) != AF_INET)
2N/A return (0);
2N/A
2N/A return (ifaddr4(n).s_addr & ifmask4(n).s_addr);
2N/A}
2N/A
2N/Achar *
2N/A__inet_get_uaddr(void *p, struct netconfig *nc, int n)
2N/A{
2N/A struct ifinfo *localinfo = (struct ifinfo *)p;
2N/A char *uaddr;
2N/A struct sockaddr_in sin4;
2N/A struct sockaddr_in6 sin6;
2N/A struct netbuf nb;
2N/A
2N/A if (localinfo == 0 || nc == 0 || n >= localinfo->count)
2N/A return (0);
2N/A
2N/A if (ifaf(n) == AF_INET6) {
2N/A if (strcmp(NC_INET6, nc->nc_protofmly) != 0)
2N/A return (0);
2N/A (void) memset(&sin6, 0, sizeof (sin6));
2N/A sin6.sin6_family = AF_INET6;
2N/A sin6.sin6_addr = ifaddr6(n);
2N/A nb.buf = (char *)&sin6;
2N/A nb.len = sizeof (sin6);
2N/A } else {
2N/A if (strcmp(NC_INET, nc->nc_protofmly) != 0)
2N/A return (0);
2N/A (void) memset(&sin4, 0, sizeof (sin4));
2N/A sin4.sin_family = AF_INET;
2N/A sin4.sin_addr = ifaddr4(n);
2N/A nb.buf = (char *)&sin4;
2N/A nb.len = sizeof (sin4);
2N/A }
2N/A
2N/A nb.maxlen = nb.len;
2N/A
2N/A uaddr = taddr2uaddr(nc, &nb);
2N/A return (uaddr);
2N/A}
2N/A
2N/Achar *
2N/A__inet_get_networka(void *p, int n)
2N/A{
2N/A struct ifinfo *localinfo = (struct ifinfo *)p;
2N/A
2N/A if (localinfo == 0 || n >= localinfo->count)
2N/A return (0);
2N/A
2N/A if (ifaf(n) == AF_INET6) {
2N/A char buf[INET6_ADDRSTRLEN];
2N/A struct in6_addr in6;
2N/A int i;
2N/A
2N/A for (i = 0; i < sizeof (in6.s6_addr); i++) {
2N/A in6.s6_addr[i] = ifaddr6(n).s6_addr[i] &
2N/A ifmask6(n).s6_addr[i];
2N/A }
2N/A return (strdup(inet_ntop(AF_INET6, &in6, buf, sizeof (buf))));
2N/A } else {
2N/A struct in_addr in4;
2N/A
2N/A in4.s_addr = ifaddr4(n).s_addr & ifmask4(n).s_addr;
2N/A return (strdup(inet_ntoa(in4)));
2N/A }
2N/A}
2N/A
2N/Astatic int
2N/Ain_list(struct in_addr *addrs, int n, struct in_addr a)
2N/A{
2N/A int i;
2N/A
2N/A for (i = 0; i < n; i++) {
2N/A if (addrs[i].s_addr == a.s_addr)
2N/A return (1);
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Agetbroadcastnets(struct netconfig *tp, struct in_addr **addrs)
2N/A{
2N/A struct ifconf ifc;
2N/A struct ifreq ifreq, *ifr;
2N/A struct sockaddr_in *sin;
2N/A struct in_addr a;
2N/A int fd;
2N/A int n, i, numifs;
2N/A char *buf;
2N/A int use_loopback = 0;
2N/A
2N/A _nderror = ND_SYSTEM;
2N/A fd = open(tp->nc_device, O_RDONLY);
2N/A if (fd < 0) {
2N/A (void) syslog(LOG_ERR,
2N/A "broadcast: open to get interface configuration: %m");
2N/A return (0);
2N/A }
2N/A if (ioctl(fd, SIOCGIFNUM, (char *)&numifs) < 0)
2N/A numifs = MAXIFS;
2N/A buf = malloc(numifs * sizeof (struct ifreq));
2N/A if (buf == NULL) {
2N/A (void) syslog(LOG_ERR, "broadcast: malloc failed: %m");
2N/A (void) close(fd);
2N/A return (0);
2N/A }
2N/A *addrs = malloc(numifs * sizeof (struct in_addr));
2N/A if (*addrs == NULL) {
2N/A (void) syslog(LOG_ERR, "broadcast: malloc failed: %m");
2N/A free(buf);
2N/A (void) close(fd);
2N/A return (0);
2N/A }
2N/A ifc.ifc_len = numifs * (int)sizeof (struct ifreq);
2N/A ifc.ifc_buf = buf;
2N/A /*
2N/A * Ideally, this ioctl should also tell me, how many bytes were
2N/A * finally allocated, but it doesnt.
2N/A */
2N/A if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0) {
2N/A (void) syslog(LOG_ERR,
2N/A "broadcast: ioctl (get interface configuration): %m");
2N/A free(buf);
2N/A free(*addrs);
2N/A (void) close(fd);
2N/A return (0);
2N/A }
2N/A
2N/Aretry:
2N/A /* LINTED pointer cast */
2N/A ifr = (struct ifreq *)buf;
2N/A for (i = 0, n = ifc.ifc_len / (int)sizeof (struct ifreq);
2N/A n > 0; n--, ifr++) {
2N/A ifreq = *ifr;
2N/A if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
2N/A (void) syslog(LOG_ERR, "broadcast: "
2N/A "ioctl (get interface flags): %m");
2N/A continue;
2N/A }
2N/A if (!(ifreq.ifr_flags & IFF_UP) ||
2N/A (ifr->ifr_addr.sa_family != AF_INET))
2N/A continue;
2N/A if (ifreq.ifr_flags & IFF_BROADCAST) {
2N/A /* LINTED pointer cast */
2N/A sin = (struct sockaddr_in *)&ifr->ifr_addr;
2N/A if (ioctl(fd, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
2N/A /* May not work with other implementation */
2N/A a = _inet_makeaddr(
2N/A inet_netof(sin->sin_addr),
2N/A INADDR_ANY);
2N/A if (!in_list(*addrs, i, a))
2N/A (*addrs)[i++] = a;
2N/A } else {
2N/A /* LINTED pointer cast */
2N/A a = ((struct sockaddr_in *)
2N/A &ifreq.ifr_addr)->sin_addr;
2N/A if (!in_list(*addrs, i, a))
2N/A (*addrs)[i++] = a;
2N/A }
2N/A continue;
2N/A }
2N/A if (use_loopback && (ifreq.ifr_flags & IFF_LOOPBACK)) {
2N/A /* LINTED pointer cast */
2N/A sin = (struct sockaddr_in *)&ifr->ifr_addr;
2N/A a = sin->sin_addr;
2N/A if (!in_list(*addrs, i, a))
2N/A (*addrs)[i++] = a;
2N/A continue;
2N/A }
2N/A if (ifreq.ifr_flags & IFF_POINTOPOINT) {
2N/A if (ioctl(fd, SIOCGIFDSTADDR, (char *)&ifreq) < 0)
2N/A continue;
2N/A /* LINTED pointer cast */
2N/A a = ((struct sockaddr_in *)
2N/A &ifreq.ifr_addr)->sin_addr;
2N/A if (!in_list(*addrs, i, a))
2N/A (*addrs)[i++] = a;
2N/A continue;
2N/A }
2N/A }
2N/A if (i == 0 && !use_loopback) {
2N/A use_loopback = 1;
2N/A goto retry;
2N/A }
2N/A free(buf);
2N/A (void) close(fd);
2N/A if (i)
2N/A _nderror = ND_OK;
2N/A else
2N/A free(*addrs);
2N/A return (i);
2N/A}
2N/A
2N/A/*
2N/A * This is lifted straight from libsocket/inet/inet_mkaddr.c.
2N/A * Copied here to avoid our dependency on libsocket. More importantly,
2N/A * to make sure partially static apps that use libnsl, but not
2N/A * libsocket, don't get screwed up.
2N/A * If you understand the above paragraph, try to get rid of
2N/A * this copy of inet_makeaddr; if you don;t, leave it alone.
2N/A *
2N/A * Formulate an Internet address from network + host. Used in
2N/A * building addresses stored in the ifnet structure.
2N/A */
2N/Astatic struct in_addr
2N/A_inet_makeaddr(in_addr_t net, in_addr_t host)
2N/A{
2N/A in_addr_t addr;
2N/A struct in_addr inaddr;
2N/A
2N/A if (net < 128)
2N/A addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
2N/A else if (net < 65536)
2N/A addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
2N/A else if (net < 16777216L)
2N/A addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
2N/A else
2N/A addr = net | host;
2N/A inaddr.s_addr = htonl(addr);
2N/A return (inaddr);
2N/A}
2N/A
2N/A/*
2N/A * Routine to read the default configuration file and check if SORT_ADDRS
2N/A * is set to NO or FALSE. This routine is called by order_haddrlist_af()
2N/A * to determine if the addresses need to be sorted.
2N/A */
2N/Astatic boolean_t
2N/A_read_nsw_file(void)
2N/A{
2N/A char defval[LINESIZE];
2N/A FILE *defl;
2N/A boolean_t nosort = B_FALSE;
2N/A
2N/A
2N/A do {
2N/A defl = fopen(__NSW_DEFAULT_FILE, "rF");
2N/A } while ((defl == NULL) && (errno == EINTR));
2N/A
2N/A if (defl == NULL)
2N/A return (B_FALSE);
2N/A
2N/A while (fgets(defval, sizeof (defval), defl) != NULL) {
2N/A if ((strncmp(DONT_SORT, defval, sizeof (DONT_SORT) - 1) == 0) ||
2N/A (strncmp(DONT_SORT2, defval,
2N/A sizeof (DONT_SORT2) - 1) == 0)) {
2N/A nosort = B_TRUE;
2N/A break;
2N/A }
2N/A }
2N/A (void) fclose(defl);
2N/A return (nosort);
2N/A}