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