155714Skris/* $NetBSD: getaddrinfo.c,v 1.4 2014/04/22 02:23:03 ginsbach Exp $ */ 255714Skris 355714Skris/*- 455714Skris * Copyright (c) 2013 The NetBSD Foundation, Inc. 555714Skris * All rights reserved. 655714Skris * 755714Skris * This code is derived from software contributed to The NetBSD Foundation 8280304Sjkim * by Taylor R. Campbell. 955714Skris * 1055714Skris * Redistribution and use in source and binary forms, with or without 1155714Skris * modification, are permitted provided that the following conditions 1255714Skris * are met: 1355714Skris * 1. Redistributions of source code must retain the above copyright 1455714Skris * notice, this list of conditions and the following disclaimer. 15280304Sjkim * 2. Redistributions in binary form must reproduce the above copyright 1655714Skris * notice, this list of conditions and the following disclaimer in the 1755714Skris * documentation and/or other materials provided with the distribution. 1855714Skris * 1955714Skris * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2055714Skris * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2155714Skris * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22280304Sjkim * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2355714Skris * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2455714Skris * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2555714Skris * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2655714Skris * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2755714Skris * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2855714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2955714Skris * POSSIBILITY OF SUCH DAMAGE. 3055714Skris */ 3155714Skris 3255714Skris#include <sys/types.h> 3355714Skris#include <sys/socket.h> 3455714Skris 3555714Skris#include <assert.h> 3655714Skris#include <err.h> 37280304Sjkim#include <errno.h> 3855714Skris#include <limits.h> 3955714Skris#include <netdb.h> 40280304Sjkim#include <stdbool.h> 4155714Skris#include <stdint.h> 4255714Skris#include <stdlib.h> 4355714Skris#include <stdio.h> 4455714Skris#include <string.h> 4555714Skris#include <unistd.h> 4655714Skris#include <util.h> 4755714Skris 4855714Skris#include "tables.h" 4955714Skris 5055714Skrisstatic void usage(void) __dead; 5155714Skrisstatic void printaddrinfo(struct addrinfo *); 52280304Sjkimstatic bool parse_af(const char *, int *); 5355714Skrisstatic bool parse_protocol(const char *, int *); 5455714Skrisstatic bool parse_socktype(const char *, int *); 5555714Skrisstatic bool parse_numeric_tabular(const char *, int *, const char *const *, 5655714Skris size_t); 5755714Skris 5855714Skrisint 5955714Skrismain(int argc, char **argv) 60280304Sjkim{ 6155714Skris static const struct addrinfo zero_addrinfo; 62280304Sjkim struct addrinfo hints = zero_addrinfo; 6368651Skris struct addrinfo *addrinfo; 6455714Skris const char *hostname = NULL, *service = NULL; 6555714Skris int ch; 6655714Skris int error; 6755714Skris 68280304Sjkim setprogname(argv[0]); 69280304Sjkim 70280304Sjkim hints.ai_family = AF_UNSPEC; 7155714Skris hints.ai_socktype = 0; 72280304Sjkim hints.ai_protocol = 0; 73280304Sjkim hints.ai_flags = 0; 7455714Skris 75280304Sjkim while ((ch = getopt(argc, argv, "cf:nNp:Ps:t:")) != -1) { 76280304Sjkim switch (ch) { 77280304Sjkim case 'c': 78280304Sjkim hints.ai_flags |= AI_CANONNAME; 79280304Sjkim break; 80280304Sjkim 81238405Sjkim case 'f': 82280304Sjkim if (!parse_af(optarg, &hints.ai_family)) { 83194206Ssimon warnx("invalid address family: %s", optarg); 84280304Sjkim usage(); 85109998Smarkm } 86160814Ssimon break; 87109998Smarkm 88280304Sjkim case 'n': 8955714Skris hints.ai_flags |= AI_NUMERICHOST; 9055714Skris break; 9155714Skris 9255714Skris case 'N': 9355714Skris hints.ai_flags |= AI_NUMERICSERV; 9455714Skris break; 95 96 case 's': 97 service = optarg; 98 break; 99 100 case 'p': 101 if (!parse_protocol(optarg, &hints.ai_protocol)) { 102 warnx("invalid protocol: %s", optarg); 103 usage(); 104 } 105 break; 106 107 case 'P': 108 hints.ai_flags |= AI_PASSIVE; 109 break; 110 111 case 't': 112 if (!parse_socktype(optarg, &hints.ai_socktype)) { 113 warnx("invalid socket type: %s", optarg); 114 usage(); 115 } 116 break; 117 118 case '?': 119 default: 120 usage(); 121 } 122 } 123 124 argc -= optind; 125 argv += optind; 126 127 if (!((argc == 1) || ((argc == 0) && (hints.ai_flags & AI_PASSIVE)))) 128 usage(); 129 if (argc == 1) 130 hostname = argv[0]; 131 132 if (service != NULL) { 133 char *p; 134 135 if ((p = strchr(service, '/')) != NULL) { 136 if (hints.ai_protocol != 0) { 137 warnx("protocol already specified"); 138 usage(); 139 } 140 *p = '\0'; 141 p++; 142 143 if (!parse_protocol(p, &hints.ai_protocol)) { 144 warnx("invalid protocol: %s", p); 145 usage(); 146 } 147 } 148 } 149 150 error = getaddrinfo(hostname, service, &hints, &addrinfo); 151 if (error) 152 errx(1, "%s", gai_strerror(error)); 153 154 if ((hints.ai_flags & AI_CANONNAME) && (addrinfo != NULL)) { 155 if (printf("canonname %s\n", addrinfo->ai_canonname) < 0) 156 err(1, "printf"); 157 } 158 159 printaddrinfo(addrinfo); 160 161 freeaddrinfo(addrinfo); 162 163 return 0; 164} 165 166static void __dead 167usage(void) 168{ 169 170 (void)fprintf(stderr, "Usage: %s", getprogname()); 171 (void)fprintf(stderr, 172 " [-f <family>] [-p <protocol>] [-t <socktype>] [-s <service>]\n"); 173 (void)fprintf(stderr, " [-cnNP] [<hostname>]\n"); 174 exit(1); 175} 176 177static bool 178parse_af(const char *string, int *afp) 179{ 180 181 return parse_numeric_tabular(string, afp, address_families, 182 __arraycount(address_families)); 183} 184 185static bool 186parse_protocol(const char *string, int *protop) 187{ 188 struct protoent *protoent; 189 char *end; 190 long value; 191 192 errno = 0; 193 value = strtol(string, &end, 0); 194 if ((string[0] == '\0') || (*end != '\0')) 195 goto numeric_failed; 196 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN))) 197 goto numeric_failed; 198 if ((value > INT_MAX) || (value < INT_MIN)) 199 goto numeric_failed; 200 201 *protop = value; 202 return true; 203 204numeric_failed: 205 protoent = getprotobyname(string); 206 if (protoent == NULL) 207 goto protoent_failed; 208 209 *protop = protoent->p_proto; 210 return true; 211 212protoent_failed: 213 return false; 214} 215 216static bool 217parse_socktype(const char *string, int *typep) 218{ 219 220 return parse_numeric_tabular(string, typep, socket_types, 221 __arraycount(socket_types)); 222} 223 224static bool 225parse_numeric_tabular(const char *string, int *valuep, 226 const char *const *table, size_t n) 227{ 228 char *end; 229 long value; 230 size_t i; 231 232 assert((uintmax_t)n <= (uintmax_t)INT_MAX); 233 234 errno = 0; 235 value = strtol(string, &end, 0); 236 if ((string[0] == '\0') || (*end != '\0')) 237 goto numeric_failed; 238 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN))) 239 goto numeric_failed; 240 if ((value > INT_MAX) || (value < INT_MIN)) 241 goto numeric_failed; 242 243 *valuep = value; 244 return true; 245 246numeric_failed: 247 for (i = 0; i < n; i++) 248 if ((table[i] != NULL) && (strcmp(string, table[i]) == 0)) 249 break; 250 if (i == n) 251 goto table_failed; 252 *valuep = i; 253 return true; 254 255table_failed: 256 return false; 257} 258 259static void 260printaddrinfo(struct addrinfo *addrinfo) 261{ 262 struct addrinfo *ai; 263 char buf[1024]; 264 int n; 265 struct protoent *protoent; 266 267 for (ai = addrinfo; ai != NULL; ai = ai->ai_next) { 268 /* Print the socket type. */ 269 if ((ai->ai_socktype >= 0) && 270 ((size_t)ai->ai_socktype < __arraycount(socket_types)) && 271 (socket_types[ai->ai_socktype] != NULL)) 272 n = printf("%s", socket_types[ai->ai_socktype]); 273 else 274 n = printf("%d", ai->ai_socktype); 275 if (n < 0) 276 err(1, "printf"); 277 278 /* Print the address family. */ 279 if ((ai->ai_family >= 0) && 280 ((size_t)ai->ai_family < __arraycount(address_families)) && 281 (address_families[ai->ai_family] != NULL)) 282 n = printf(" %s", address_families[ai->ai_family]); 283 else 284 n = printf(" %d", ai->ai_family); 285 if (n < 0) 286 err(1, "printf"); 287 288 /* Print the protocol number. */ 289 protoent = getprotobynumber(ai->ai_protocol); 290 if (protoent == NULL) 291 n = printf(" %d", ai->ai_protocol); 292 else 293 n = printf(" %s", protoent->p_name); 294 if (n < 0) 295 err(1, "printf"); 296 297 /* Format the sockaddr. */ 298 switch (ai->ai_family) { 299 case AF_INET: 300 case AF_INET6: 301 n = sockaddr_snprintf(buf, sizeof(buf), " %a %p", 302 ai->ai_addr); 303 break; 304 305 default: 306 n = sockaddr_snprintf(buf, sizeof(buf), 307 "%a %p %I %F %R %S", ai->ai_addr); 308 } 309 310 /* 311 * Check for sockaddr_snprintf failure. 312 * 313 * XXX sockaddr_snprintf's error reporting is botched 314 * -- man page says it sets errno, but if getnameinfo 315 * fails, errno is not where it reports the error... 316 */ 317 if (n < 0) { 318 warnx("sockaddr_snprintf failed"); 319 continue; 320 } 321 if (sizeof(buf) <= (size_t)n) 322 warnx("truncated sockaddr_snprintf output"); 323 324 /* Print the formatted sockaddr. */ 325 if (printf("%s\n", buf) < 0) 326 err(1, "printf"); 327 } 328} 329