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