1*4c1e55dcSeric /* $OpenBSD: getnameinfo.c,v 1.1.1.1 2012/07/13 17:49:54 eric Exp $ */ 2*4c1e55dcSeric /* 3*4c1e55dcSeric * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> 4*4c1e55dcSeric * 5*4c1e55dcSeric * Permission to use, copy, modify, and distribute this software for any 6*4c1e55dcSeric * purpose with or without fee is hereby granted, provided that the above 7*4c1e55dcSeric * copyright notice and this permission notice appear in all copies. 8*4c1e55dcSeric * 9*4c1e55dcSeric * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10*4c1e55dcSeric * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*4c1e55dcSeric * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12*4c1e55dcSeric * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*4c1e55dcSeric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*4c1e55dcSeric * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15*4c1e55dcSeric * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16*4c1e55dcSeric */ 17*4c1e55dcSeric 18*4c1e55dcSeric #include <err.h> 19*4c1e55dcSeric #include <errno.h> 20*4c1e55dcSeric #include <getopt.h> 21*4c1e55dcSeric #include <stdio.h> 22*4c1e55dcSeric #include <stdlib.h> 23*4c1e55dcSeric #include <string.h> 24*4c1e55dcSeric 25*4c1e55dcSeric #include "common.h" 26*4c1e55dcSeric 27*4c1e55dcSeric static void 28*4c1e55dcSeric usage(void) 29*4c1e55dcSeric { 30*4c1e55dcSeric extern const char * __progname; 31*4c1e55dcSeric 32*4c1e55dcSeric fprintf(stderr, "usage: %s [-DFHNSe] [-p portno] <addr...>\n", __progname); 33*4c1e55dcSeric exit(1); 34*4c1e55dcSeric } 35*4c1e55dcSeric 36*4c1e55dcSeric int 37*4c1e55dcSeric main(int argc, char *argv[]) 38*4c1e55dcSeric { 39*4c1e55dcSeric char serv[1024]; 40*4c1e55dcSeric char host[1024]; 41*4c1e55dcSeric const char *e; 42*4c1e55dcSeric int i, ch, flags = 0, port = 0; 43*4c1e55dcSeric struct sockaddr_storage ss; 44*4c1e55dcSeric struct sockaddr *sa; 45*4c1e55dcSeric 46*4c1e55dcSeric sa = (struct sockaddr*)&ss; 47*4c1e55dcSeric 48*4c1e55dcSeric while((ch = getopt(argc, argv, "DFHNSaep:")) != -1) { 49*4c1e55dcSeric switch(ch) { 50*4c1e55dcSeric case 'D': 51*4c1e55dcSeric flags |= NI_DGRAM; 52*4c1e55dcSeric break; 53*4c1e55dcSeric case 'F': 54*4c1e55dcSeric flags |= NI_NOFQDN; 55*4c1e55dcSeric break; 56*4c1e55dcSeric case 'H': 57*4c1e55dcSeric flags |= NI_NUMERICHOST; 58*4c1e55dcSeric break; 59*4c1e55dcSeric case 'N': 60*4c1e55dcSeric flags |= NI_NAMEREQD; 61*4c1e55dcSeric break; 62*4c1e55dcSeric case 'S': 63*4c1e55dcSeric flags |= NI_NUMERICSERV; 64*4c1e55dcSeric break; 65*4c1e55dcSeric case 'e': 66*4c1e55dcSeric long_err += 1; 67*4c1e55dcSeric break; 68*4c1e55dcSeric case 'p': 69*4c1e55dcSeric port = strtonum(optarg, 0, 65535, &e); 70*4c1e55dcSeric if (e) 71*4c1e55dcSeric usage(); 72*4c1e55dcSeric break; 73*4c1e55dcSeric default: 74*4c1e55dcSeric usage(); 75*4c1e55dcSeric /* NOTREACHED */ 76*4c1e55dcSeric } 77*4c1e55dcSeric } 78*4c1e55dcSeric argc -= optind; 79*4c1e55dcSeric argv += optind; 80*4c1e55dcSeric 81*4c1e55dcSeric for(i = 0; i < argc; i++) { 82*4c1e55dcSeric 83*4c1e55dcSeric if (i) 84*4c1e55dcSeric printf("\n"); 85*4c1e55dcSeric printf("===> \"%s\"\n", argv[i]); 86*4c1e55dcSeric 87*4c1e55dcSeric if (sockaddr_from_str(sa, AF_UNSPEC, argv[i]) == -1) { 88*4c1e55dcSeric printf(" => invalid address\n"); 89*4c1e55dcSeric continue; 90*4c1e55dcSeric } 91*4c1e55dcSeric 92*4c1e55dcSeric if (sa->sa_family == PF_INET) 93*4c1e55dcSeric ((struct sockaddr_in *)sa)->sin_port = htons(port); 94*4c1e55dcSeric else if (sa->sa_family == PF_INET6) 95*4c1e55dcSeric ((struct sockaddr_in6 *)sa)->sin6_port = htons(port); 96*4c1e55dcSeric 97*4c1e55dcSeric errno = 0; 98*4c1e55dcSeric h_errno = 0; 99*4c1e55dcSeric gai_errno = 0; 100*4c1e55dcSeric rrset_errno = 0; 101*4c1e55dcSeric 102*4c1e55dcSeric gai_errno = getnameinfo(sa, sa->sa_len, host, sizeof host, serv, 103*4c1e55dcSeric sizeof serv, flags); 104*4c1e55dcSeric 105*4c1e55dcSeric if (gai_errno == 0) 106*4c1e55dcSeric printf(" %s:%s\n", host, serv); 107*4c1e55dcSeric print_errors(); 108*4c1e55dcSeric 109*4c1e55dcSeric } 110*4c1e55dcSeric 111*4c1e55dcSeric return (0); 112*4c1e55dcSeric } 113