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