1 1.2 mrg /* $NetBSD: h_gai.c,v 1.2 2019/02/03 10:48:47 mrg Exp $ */ 2 1.1 pgoyette 3 1.1 pgoyette /* 4 1.1 pgoyette * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project. 5 1.1 pgoyette * All rights reserved. 6 1.1 pgoyette * 7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without 8 1.1 pgoyette * modification, are permitted provided that the following conditions 9 1.1 pgoyette * are met: 10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright 11 1.1 pgoyette * notice, this list of conditions and the following disclaimer. 12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the 14 1.1 pgoyette * documentation and/or other materials provided with the distribution. 15 1.1 pgoyette * 3. Neither the name of the project nor the names of its contributors 16 1.1 pgoyette * may be used to endorse or promote products derived from this software 17 1.1 pgoyette * without specific prior written permission. 18 1.1 pgoyette * 19 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 1.1 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 1.1 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 pgoyette * SUCH DAMAGE. 30 1.1 pgoyette */ 31 1.1 pgoyette 32 1.1 pgoyette #include <netdb.h> 33 1.1 pgoyette #include <stdio.h> 34 1.1 pgoyette #include <stdlib.h> 35 1.1 pgoyette #include <string.h> 36 1.1 pgoyette #include <unistd.h> 37 1.1 pgoyette 38 1.1 pgoyette #include <sys/types.h> 39 1.1 pgoyette #include <sys/socket.h> 40 1.1 pgoyette 41 1.1 pgoyette #include <netinet/in.h> 42 1.1 pgoyette 43 1.1 pgoyette #include <arpa/inet.h> 44 1.1 pgoyette 45 1.1 pgoyette struct addrinfo ai; 46 1.1 pgoyette 47 1.1 pgoyette char host[NI_MAXHOST]; 48 1.1 pgoyette char serv[NI_MAXSERV]; 49 1.1 pgoyette int vflag = 0; 50 1.1 pgoyette 51 1.1 pgoyette static void usage(void); 52 1.1 pgoyette static void print1(const char *, const struct addrinfo *, char *, char *); 53 1.1 pgoyette int main(int, char *[]); 54 1.1 pgoyette 55 1.1 pgoyette static void 56 1.1 pgoyette usage() 57 1.1 pgoyette { 58 1.1 pgoyette fprintf(stderr, "usage: test [-f family] [-s socktype] [-p proto] [-DPRSv46] host serv\n"); 59 1.1 pgoyette } 60 1.1 pgoyette 61 1.1 pgoyette static void 62 1.1 pgoyette print1(const char *title, const struct addrinfo *res, char *h, char *s) 63 1.1 pgoyette { 64 1.1 pgoyette const char *start, *end; 65 1.1 pgoyette int error; 66 1.1 pgoyette const int niflag = NI_NUMERICHOST; 67 1.1 pgoyette 68 1.1 pgoyette if (res->ai_addr) { 69 1.1 pgoyette error = getnameinfo(res->ai_addr, res->ai_addr->sa_len, 70 1.1 pgoyette host, sizeof(host), serv, sizeof(serv), 71 1.1 pgoyette niflag); 72 1.1 pgoyette h = host; 73 1.1 pgoyette s = serv; 74 1.1 pgoyette } else 75 1.1 pgoyette error = 0; 76 1.1 pgoyette 77 1.1 pgoyette if (vflag) { 78 1.1 pgoyette start = "\t"; 79 1.1 pgoyette end = "\n"; 80 1.1 pgoyette } else { 81 1.1 pgoyette start = " "; 82 1.1 pgoyette end = ""; 83 1.1 pgoyette } 84 1.1 pgoyette printf("%s%s", title, end); 85 1.1 pgoyette printf("%sflags 0x%x%s", start, res->ai_flags, end); 86 1.1 pgoyette printf("%sfamily %d%s", start, res->ai_family, end); 87 1.1 pgoyette printf("%ssocktype %d%s", start, res->ai_socktype, end); 88 1.1 pgoyette printf("%sprotocol %d%s", start, res->ai_protocol, end); 89 1.1 pgoyette printf("%saddrlen %d%s", start, res->ai_addrlen, end); 90 1.1 pgoyette if (error) 91 1.1 pgoyette printf("%serror %d%s", start, error, end); 92 1.1 pgoyette else { 93 1.1 pgoyette printf("%shost %s%s", start, h, end); 94 1.1 pgoyette printf("%sserv %s%s", start, s, end); 95 1.1 pgoyette } 96 1.1 pgoyette #if 0 97 1.1 pgoyette if (res->ai_canonname) 98 1.1 pgoyette printf("%scname \"%s\"%s", start, res->ai_canonname, end); 99 1.1 pgoyette #endif 100 1.1 pgoyette if (!vflag) 101 1.1 pgoyette printf("\n"); 102 1.1 pgoyette 103 1.1 pgoyette } 104 1.1 pgoyette 105 1.1 pgoyette int 106 1.1 pgoyette main(int argc, char *argv[]) 107 1.1 pgoyette { 108 1.1 pgoyette struct addrinfo *res; 109 1.1 pgoyette int error, i; 110 1.1 pgoyette char *p, *q; 111 1.1 pgoyette extern int optind; 112 1.1 pgoyette extern char *optarg; 113 1.1 pgoyette int c; 114 1.2 mrg char nbuf[14]; 115 1.1 pgoyette 116 1.1 pgoyette memset(&ai, 0, sizeof(ai)); 117 1.1 pgoyette ai.ai_family = PF_UNSPEC; 118 1.1 pgoyette ai.ai_flags |= AI_CANONNAME; 119 1.1 pgoyette while ((c = getopt(argc, argv, "Df:p:PRs:Sv46")) != -1) { 120 1.1 pgoyette switch (c) { 121 1.1 pgoyette case 'D': 122 1.1 pgoyette ai.ai_socktype = SOCK_DGRAM; 123 1.1 pgoyette break; 124 1.1 pgoyette case 'f': 125 1.1 pgoyette ai.ai_family = atoi(optarg); 126 1.1 pgoyette break; 127 1.1 pgoyette case 'p': 128 1.1 pgoyette ai.ai_protocol = atoi(optarg); 129 1.1 pgoyette break; 130 1.1 pgoyette case 'P': 131 1.1 pgoyette ai.ai_flags |= AI_PASSIVE; 132 1.1 pgoyette break; 133 1.1 pgoyette case 'R': 134 1.1 pgoyette ai.ai_socktype = SOCK_RAW; 135 1.1 pgoyette break; 136 1.1 pgoyette case 's': 137 1.1 pgoyette ai.ai_socktype = atoi(optarg); 138 1.1 pgoyette break; 139 1.1 pgoyette case 'S': 140 1.1 pgoyette ai.ai_socktype = SOCK_STREAM; 141 1.1 pgoyette break; 142 1.1 pgoyette case 'v': 143 1.1 pgoyette vflag++; 144 1.1 pgoyette break; 145 1.1 pgoyette case '4': 146 1.1 pgoyette ai.ai_family = PF_INET; 147 1.1 pgoyette break; 148 1.1 pgoyette case '6': 149 1.1 pgoyette ai.ai_family = PF_INET6; 150 1.1 pgoyette break; 151 1.1 pgoyette default: 152 1.1 pgoyette usage(); 153 1.1 pgoyette exit(1); 154 1.1 pgoyette } 155 1.1 pgoyette } 156 1.1 pgoyette argc -= optind; 157 1.1 pgoyette argv += optind; 158 1.1 pgoyette 159 1.1 pgoyette if (argc != 2){ 160 1.1 pgoyette usage(); 161 1.1 pgoyette exit(1); 162 1.1 pgoyette } 163 1.1 pgoyette 164 1.1 pgoyette p = *argv[0] ? argv[0] : NULL; 165 1.1 pgoyette q = *argv[1] ? argv[1] : NULL; 166 1.1 pgoyette 167 1.1 pgoyette strncpy(nbuf, "(empty)", sizeof(nbuf)); 168 1.1 pgoyette print1("arg:", &ai, p ? p : nbuf , q ? q : nbuf); 169 1.1 pgoyette 170 1.1 pgoyette error = getaddrinfo(p, q, &ai, &res); 171 1.1 pgoyette if (error) { 172 1.1 pgoyette printf("%s\n", gai_strerror(error)); 173 1.1 pgoyette exit(1); 174 1.1 pgoyette } 175 1.1 pgoyette 176 1.1 pgoyette i = 1; 177 1.1 pgoyette do { 178 1.1 pgoyette snprintf(nbuf, sizeof(nbuf), "ai%d:", i); 179 1.1 pgoyette print1(nbuf, res, NULL, NULL); 180 1.1 pgoyette 181 1.1 pgoyette i++; 182 1.1 pgoyette } while ((res = res->ai_next) != NULL); 183 1.1 pgoyette printf("\n"); 184 1.1 pgoyette 185 1.1 pgoyette exit(0); 186 1.1 pgoyette } 187