1 /* $OpenBSD: getaddrinfo.c,v 1.2 2013/03/28 09:36:03 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 #include <sys/types.h> 18 #include <sys/socket.h> 19 20 #include <netinet/in.h> 21 22 #include <err.h> 23 #include <errno.h> 24 #include <getopt.h> 25 #include <netdb.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 30 #include "common.h" 31 32 static void 33 usage(void) 34 { 35 extern const char * __progname; 36 37 fprintf(stderr, "usage: %s [-CHSPe] [-f family] [-p proto] " 38 "[-s servname]\n [-t socktype] <host...>\n", __progname); 39 exit(1); 40 } 41 42 int 43 main(int argc, char *argv[]) 44 { 45 struct addrinfo *ai, *res, hints; 46 char *servname = NULL, *host; 47 int i, ch; 48 49 memset(&hints, 0, sizeof hints); 50 51 while((ch = getopt(argc, argv, "CFHPSef:p:s:t:")) != -1) { 52 switch(ch) { 53 case 'C': 54 hints.ai_flags |= AI_CANONNAME; 55 break; 56 case 'F': 57 hints.ai_flags |= AI_FQDN; 58 break; 59 case 'H': 60 hints.ai_flags |= AI_NUMERICHOST; 61 break; 62 case 'P': 63 hints.ai_flags |= AI_PASSIVE; 64 break; 65 case 'S': 66 hints.ai_flags |= AI_NUMERICSERV; 67 break; 68 case 'e': 69 long_err += 1; 70 break; 71 case 'f': 72 if (!strcmp(optarg, "inet")) 73 hints.ai_family = AF_INET; 74 else if (!strcmp(optarg, "inet6")) 75 hints.ai_family = AF_INET6; 76 else 77 usage(); 78 break; 79 case 'p': 80 if (!strcmp(optarg, "udp")) 81 hints.ai_protocol = IPPROTO_UDP; 82 else if (!strcmp(optarg, "tcp")) 83 hints.ai_protocol = IPPROTO_TCP; 84 else if (!strcmp(optarg, "icmp")) 85 hints.ai_protocol = IPPROTO_ICMP; 86 else if (!strcmp(optarg, "icmpv6")) 87 hints.ai_protocol = IPPROTO_ICMPV6; 88 else 89 usage(); 90 break; 91 case 's': 92 servname = optarg; 93 break; 94 case 't': 95 if (!strcmp(optarg, "stream")) 96 hints.ai_socktype = SOCK_STREAM; 97 else if (!strcmp(optarg, "dgram")) 98 hints.ai_socktype = SOCK_DGRAM; 99 else if (!strcmp(optarg, "raw")) 100 hints.ai_socktype = SOCK_RAW; 101 else 102 usage(); 103 break; 104 default: 105 usage(); 106 /* NOTREACHED */ 107 } 108 } 109 argc -= optind; 110 argv += optind; 111 112 for(i = 0; i < argc; i++) { 113 114 if (i) 115 printf("\n"); 116 printf("===> \"%s\"\n", argv[i]); 117 host = gethostarg(argv[i]); 118 119 errno = 0; 120 h_errno = 0; 121 gai_errno = 0; 122 rrset_errno = 0; 123 124 gai_errno = getaddrinfo(host, servname, &hints, &ai); 125 126 print_errors(); 127 if (gai_errno == 0) { 128 for (res = ai; res; res = res->ai_next) 129 print_addrinfo(res); 130 freeaddrinfo(ai); 131 } 132 } 133 134 return (0); 135 } 136