1 1.2 christos /* $NetBSD: h_hostent.c,v 1.2 2014/01/09 02:18:10 christos Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation 8 1.1 christos * by Christos Zoulas. 9 1.1 christos * 10 1.1 christos * Redistribution and use in source and binary forms, with or without 11 1.1 christos * modification, are permitted provided that the following conditions 12 1.1 christos * are met: 13 1.1 christos * 1. Redistributions of source code must retain the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer. 15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 christos * notice, this list of conditions and the following disclaimer in the 17 1.1 christos * documentation and/or other materials provided with the distribution. 18 1.1 christos * 19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 30 1.1 christos */ 31 1.1 christos #include <sys/cdefs.h> 32 1.2 christos __RCSID("$NetBSD: h_hostent.c,v 1.2 2014/01/09 02:18:10 christos Exp $"); 33 1.1 christos 34 1.1 christos #include <stdio.h> 35 1.1 christos #include <string.h> 36 1.1 christos #include <nsswitch.h> 37 1.1 christos #include <netdb.h> 38 1.1 christos #include <stdint.h> 39 1.1 christos #include <stdlib.h> 40 1.1 christos #include <unistd.h> 41 1.1 christos #include <err.h> 42 1.1 christos 43 1.1 christos #include <netinet/in.h> 44 1.1 christos #include <sys/types.h> 45 1.1 christos #include <arpa/nameser.h> 46 1.1 christos #include <arpa/inet.h> 47 1.1 christos 48 1.1 christos #include "hostent.h" 49 1.1 christos 50 1.2 christos extern const char *__res_conf_name; 51 1.2 christos 52 1.1 christos static void 53 1.1 christos phostent(const struct hostent *h) 54 1.1 christos { 55 1.1 christos size_t i; 56 1.1 christos char buf[1024]; 57 1.1 christos const int af = h->h_length == NS_INADDRSZ ? AF_INET : AF_INET6; 58 1.1 christos 59 1.1 christos printf("name=%s, length=%d, addrtype=%d, aliases=[", 60 1.1 christos h->h_name, h->h_length, h->h_addrtype); 61 1.1 christos 62 1.1 christos for (i = 0; h->h_aliases[i]; i++) 63 1.1 christos printf("%s%s", i == 0 ? "" : " ", h->h_aliases[i]); 64 1.1 christos 65 1.1 christos printf("] addr_list=["); 66 1.1 christos 67 1.1 christos for (i = 0; h->h_addr_list[i]; i++) 68 1.1 christos printf("%s%s", i == 0 ? "" : " ", inet_ntop(af, 69 1.1 christos h->h_addr_list[i], buf, (socklen_t)sizeof(buf))); 70 1.1 christos 71 1.1 christos printf("]\n"); 72 1.1 christos } 73 1.1 christos 74 1.1 christos static void 75 1.1 christos usage(void) 76 1.1 christos { 77 1.1 christos (void)fprintf(stderr, "Usage: %s [-f <hostsfile>] " 78 1.1 christos "[-t <any|dns|nis|files>] " 79 1.1 christos "[-46a] <name|address>\n", getprogname()); 80 1.1 christos exit(EXIT_FAILURE); 81 1.1 christos } 82 1.1 christos 83 1.1 christos static void 84 1.1 christos getby(int (*f)(void *, void *, va_list), struct getnamaddr *info, ...) 85 1.1 christos { 86 1.1 christos va_list ap; 87 1.1 christos int e; 88 1.1 christos 89 1.1 christos va_start(ap, info); 90 1.1 christos e = (*f)(info, NULL, ap); 91 1.1 christos va_end(ap); 92 1.1 christos switch (e) { 93 1.1 christos case NS_SUCCESS: 94 1.1 christos phostent(info->hp); 95 1.1 christos break; 96 1.1 christos default: 97 1.1 christos printf("error %d\n", e); 98 1.1 christos break; 99 1.1 christos } 100 1.1 christos } 101 1.1 christos 102 1.1 christos static void 103 1.1 christos geta(struct hostent *hp) { 104 1.1 christos if (hp == NULL) 105 1.1 christos printf("error %d\n", h_errno); 106 1.1 christos else 107 1.1 christos phostent(hp); 108 1.1 christos } 109 1.1 christos 110 1.1 christos int 111 1.1 christos main(int argc, char *argv[]) 112 1.1 christos { 113 1.1 christos int (*f)(void *, void *, va_list) = NULL; 114 1.1 christos const char *type = "any"; 115 1.1 christos int c, af, e, byaddr, len; 116 1.1 christos struct hostent hent; 117 1.1 christos struct getnamaddr info; 118 1.1 christos char buf[4096]; 119 1.1 christos 120 1.1 christos af = AF_INET; 121 1.1 christos byaddr = 0; 122 1.1 christos len = 0; 123 1.1 christos info.hp = &hent; 124 1.1 christos info.buf = buf; 125 1.1 christos info.buflen = sizeof(buf); 126 1.1 christos info.he = &e; 127 1.1 christos 128 1.2 christos while ((c = getopt(argc, argv, "46af:r:t:")) != -1) { 129 1.1 christos switch (c) { 130 1.1 christos case '4': 131 1.1 christos af = AF_INET; 132 1.1 christos break; 133 1.1 christos case '6': 134 1.1 christos af = AF_INET6; 135 1.1 christos break; 136 1.1 christos case 'a': 137 1.1 christos byaddr++; 138 1.1 christos break; 139 1.2 christos case 'f': 140 1.2 christos _hf_sethostsfile(optarg); 141 1.2 christos break; 142 1.2 christos case 'r': 143 1.2 christos __res_conf_name = optarg; 144 1.2 christos break; 145 1.1 christos case 't': 146 1.1 christos type = optarg; 147 1.1 christos break; 148 1.1 christos default: 149 1.1 christos usage(); 150 1.1 christos } 151 1.1 christos } 152 1.1 christos 153 1.1 christos argc -= optind; 154 1.1 christos argv += optind; 155 1.1 christos 156 1.1 christos if (argc != 1) 157 1.1 christos usage(); 158 1.1 christos 159 1.1 christos switch (*type) { 160 1.1 christos case 'a': 161 1.1 christos break; 162 1.1 christos case 'd': 163 1.1 christos f = byaddr ? _dns_gethtbyaddr : _dns_gethtbyname; 164 1.1 christos break; 165 1.1 christos #ifdef YP 166 1.1 christos case 'n': 167 1.1 christos f = byaddr ? _yp_gethtbyaddr : _yp_gethtbyname; 168 1.1 christos break; 169 1.1 christos #endif 170 1.1 christos case 'f': 171 1.1 christos f = byaddr ? _hf_gethtbyaddr : _hf_gethtbyname; 172 1.1 christos break; 173 1.1 christos default: 174 1.1 christos errx(EXIT_FAILURE, "Unknown db type `%s'", type); 175 1.1 christos } 176 1.1 christos 177 1.1 christos if (byaddr) { 178 1.1 christos struct in6_addr addr; 179 1.1 christos af = strchr(*argv, ':') ? AF_INET6 : AF_INET; 180 1.1 christos len = af == AF_INET ? NS_INADDRSZ : NS_IN6ADDRSZ; 181 1.1 christos if (inet_pton(af, *argv, &addr) == -1) 182 1.1 christos err(EXIT_FAILURE, "Can't parse `%s'", *argv); 183 1.1 christos if (*type == 'a') 184 1.1 christos geta(gethostbyaddr((const char *)&addr, len, af)); 185 1.1 christos else 186 1.1 christos getby(f, &info, &addr, len, af); 187 1.1 christos } else { 188 1.1 christos if (*type == 'a') 189 1.1 christos geta(gethostbyname2(*argv, af)); 190 1.1 christos else 191 1.1 christos getby(f, &info, *argv, len, af); 192 1.1 christos } 193 1.1 christos 194 1.1 christos return 0; 195 1.1 christos } 196