1/*	$NetBSD: stdhosts.c,v 1.19 2009/10/20 00:51:14 snj Exp $	 */
2
3/*
4 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30#ifndef lint
31__RCSID("$NetBSD: stdhosts.c,v 1.19 2009/10/20 00:51:14 snj Exp $");
32#endif
33
34#include <sys/types.h>
35#include <sys/socket.h>
36#include <netinet/in.h>
37#include <arpa/inet.h>
38#include <ctype.h>
39#include <err.h>
40#include <limits.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45#include <netdb.h>
46
47#include "protos.h"
48
49__dead static void	usage(void);
50
51int
52main(int argc, char *argv[])
53{
54	struct in_addr host_addr;
55	FILE	*data_file;
56	size_t	 line_no;
57	size_t	 len;
58	char	*line, *k, *v, *addr_string;
59	const char *fname;
60	int	 ch;
61	int	 af = 1 << 4;	/*IPv4*/
62	struct addrinfo hints, *res;
63
64	addr_string = NULL;		/* XXX gcc -Wuninitialized */
65
66	while ((ch = getopt(argc, argv, "n")) != -1) {
67		switch (ch) {
68		case 'n':
69			af |= 1 << 6;	/*IPv6*/
70			break;
71		default:
72			usage();
73			/* NOTREACHED */
74		}
75	}
76	argc -= optind;
77	argv += optind;
78
79	if (argc > 1)
80		usage();
81
82	if (argc == 1) {
83		fname = argv[0];
84		data_file = fopen(fname, "r");
85		if (data_file == NULL)
86			err(1, "%s", fname);
87	} else {
88		fname = "<stdin>";
89		data_file = stdin;
90	}
91
92	line_no = 0;
93	for (;
94	    (line = fparseln(data_file, &len, &line_no, NULL,
95		FPARSELN_UNESCALL));
96	    free(line)) {
97		if (len == 0)
98			continue;
99
100		v = line;
101		for (k = v; *v && !isspace((unsigned char)*v); v++)
102			;
103		while (*v && isspace((unsigned char)*v))
104			*v++ = '\0';
105
106		memset(&hints, 0, sizeof(hints));
107		hints.ai_socktype = SOCK_DGRAM;		/*dummy*/
108		hints.ai_flags = AI_NUMERICHOST;
109
110		if ((af & (1 << 4)) != 0 && inet_aton(k, &host_addr) == 1 &&
111		    (addr_string = inet_ntoa(host_addr)) != NULL) {
112			/* IPv4 */
113			printf("%s %s\n", addr_string, v);
114		} else if ((af & (1 << 6)) != 0 &&
115			   getaddrinfo(k, "0", &hints, &res) == 0) {
116			/* IPv6, with scope extension permitted */
117			freeaddrinfo(res);
118			printf("%s %s\n", k, v);
119		} else
120			warnx("%s line %lu: syntax error", fname,
121			    (unsigned long)line_no);
122	}
123
124	exit(0);
125}
126
127static void
128usage(void)
129{
130
131	fprintf(stderr, "usage: %s [-n] [file]\n", getprogname());
132	exit(1);
133}
134