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
23/*
24 * Copyright (c) 2018 Peter Tribble.
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
38static int
39puthostent(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 */
90int
91dogetipnodes(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		rc = EXC_ENUM_NOT_SUPPORTED;
103	} else {
104		for (; *list != NULL; list++) {
105			if (strchr(*list, ':') != 0) {
106				af = AF_INET6;
107				len = sizeof (in6);
108				addr = &in6;
109			} else {
110				af = AF_INET;
111				len = sizeof (in4);
112				addr = &in4;
113			}
114			if (inet_pton(af, *list, addr) == 1)
115				hp = getipnodebyaddr(addr, len, af, &err_ret);
116			else
117				hp = getipnodebyname(*list, AF_INET6,
118				    AI_V4MAPPED|AI_ALL, &err_ret);
119			if (hp == NULL)
120				rc = EXC_NAME_NOT_FOUND;
121			else
122				(void) puthostent(hp, stdout);
123		}
124	}
125
126	return (rc);
127}
128