1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" 23*7c478bd9Sstevel@tonic-gate 24*7c478bd9Sstevel@tonic-gate /* 25*7c478bd9Sstevel@tonic-gate * Copyright (c) 1994-1999, by Sun Microsystems, Inc. 26*7c478bd9Sstevel@tonic-gate */ 27*7c478bd9Sstevel@tonic-gate 28*7c478bd9Sstevel@tonic-gate #include <stdio.h> 29*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 32*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 33*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <netdb.h> 36*7c478bd9Sstevel@tonic-gate #include "getent.h" 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate static int 39*7c478bd9Sstevel@tonic-gate puthostent(const struct hostent *hp, FILE *fp) 40*7c478bd9Sstevel@tonic-gate { 41*7c478bd9Sstevel@tonic-gate char **p; 42*7c478bd9Sstevel@tonic-gate int rc = 0; 43*7c478bd9Sstevel@tonic-gate char obuf[INET6_ADDRSTRLEN]; 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate if (hp == NULL) { 46*7c478bd9Sstevel@tonic-gate return (1); 47*7c478bd9Sstevel@tonic-gate } 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate for (p = hp->h_addr_list; *p != 0; p++) { 50*7c478bd9Sstevel@tonic-gate void *addr; 51*7c478bd9Sstevel@tonic-gate struct in_addr in4; 52*7c478bd9Sstevel@tonic-gate int af; 53*7c478bd9Sstevel@tonic-gate const char *res; 54*7c478bd9Sstevel@tonic-gate char **q; 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate if (hp->h_addrtype == AF_INET6) { 57*7c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)*p)) { 58*7c478bd9Sstevel@tonic-gate IN6_V4MAPPED_TO_INADDR((struct in6_addr *)*p, 59*7c478bd9Sstevel@tonic-gate &in4); 60*7c478bd9Sstevel@tonic-gate af = AF_INET; 61*7c478bd9Sstevel@tonic-gate addr = &in4; 62*7c478bd9Sstevel@tonic-gate } else { 63*7c478bd9Sstevel@tonic-gate af = AF_INET6; 64*7c478bd9Sstevel@tonic-gate addr = *p; 65*7c478bd9Sstevel@tonic-gate } 66*7c478bd9Sstevel@tonic-gate } else { 67*7c478bd9Sstevel@tonic-gate af = AF_INET; 68*7c478bd9Sstevel@tonic-gate addr = *p; 69*7c478bd9Sstevel@tonic-gate } 70*7c478bd9Sstevel@tonic-gate res = inet_ntop(af, addr, obuf, sizeof (obuf)); 71*7c478bd9Sstevel@tonic-gate if (res == 0) { 72*7c478bd9Sstevel@tonic-gate rc = 1; 73*7c478bd9Sstevel@tonic-gate continue; 74*7c478bd9Sstevel@tonic-gate } 75*7c478bd9Sstevel@tonic-gate if (fprintf(fp, "%s\t%s", res, hp->h_name) == EOF) 76*7c478bd9Sstevel@tonic-gate rc = 1; 77*7c478bd9Sstevel@tonic-gate for (q = hp->h_aliases; q && *q; q++) { 78*7c478bd9Sstevel@tonic-gate if (fprintf(fp, " %s", *q) == EOF) 79*7c478bd9Sstevel@tonic-gate rc = 1; 80*7c478bd9Sstevel@tonic-gate } 81*7c478bd9Sstevel@tonic-gate if (putc('\n', fp) == EOF) 82*7c478bd9Sstevel@tonic-gate rc = 1; 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate return (rc); 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate /* 88*7c478bd9Sstevel@tonic-gate * getipnodebyname/addr - get entries from ipnodes database 89*7c478bd9Sstevel@tonic-gate */ 90*7c478bd9Sstevel@tonic-gate int 91*7c478bd9Sstevel@tonic-gate dogetipnodes(const char **list) 92*7c478bd9Sstevel@tonic-gate { 93*7c478bd9Sstevel@tonic-gate struct hostent *hp; 94*7c478bd9Sstevel@tonic-gate int rc = EXC_SUCCESS; 95*7c478bd9Sstevel@tonic-gate struct in6_addr in6; 96*7c478bd9Sstevel@tonic-gate struct in_addr in4; 97*7c478bd9Sstevel@tonic-gate int af, len; 98*7c478bd9Sstevel@tonic-gate void *addr; 99*7c478bd9Sstevel@tonic-gate int err_ret; 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate if (list == NULL || *list == NULL) { 102*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, 103*7c478bd9Sstevel@tonic-gate "Enumeration not supported on ipnodes\n"); 104*7c478bd9Sstevel@tonic-gate } else { 105*7c478bd9Sstevel@tonic-gate for (; *list != NULL; list++) { 106*7c478bd9Sstevel@tonic-gate if (strchr(*list, ':') != 0) { 107*7c478bd9Sstevel@tonic-gate af = AF_INET6; 108*7c478bd9Sstevel@tonic-gate len = sizeof (in6); 109*7c478bd9Sstevel@tonic-gate addr = &in6; 110*7c478bd9Sstevel@tonic-gate } else { 111*7c478bd9Sstevel@tonic-gate af = AF_INET; 112*7c478bd9Sstevel@tonic-gate len = sizeof (in4); 113*7c478bd9Sstevel@tonic-gate addr = &in4; 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate if (inet_pton(af, *list, addr) == 1) 116*7c478bd9Sstevel@tonic-gate hp = getipnodebyaddr(addr, len, af, &err_ret); 117*7c478bd9Sstevel@tonic-gate else 118*7c478bd9Sstevel@tonic-gate hp = getipnodebyname( 119*7c478bd9Sstevel@tonic-gate *list, 120*7c478bd9Sstevel@tonic-gate AF_INET6, 121*7c478bd9Sstevel@tonic-gate AI_V4MAPPED|AI_ALL, 122*7c478bd9Sstevel@tonic-gate &err_ret); 123*7c478bd9Sstevel@tonic-gate if (hp == NULL) 124*7c478bd9Sstevel@tonic-gate rc = EXC_NAME_NOT_FOUND; 125*7c478bd9Sstevel@tonic-gate else 126*7c478bd9Sstevel@tonic-gate (void) puthostent(hp, stdout); 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate return (rc); 131*7c478bd9Sstevel@tonic-gate } 132