xref: /openbsd/regress/lib/libc/asr/bin/getnameinfo.c (revision d415bd75)
1 /*	$OpenBSD: getnameinfo.c,v 1.2 2018/12/15 15:16:12 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, "DFHNR:Saep:")) !=  -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 'R':
63 			parseresopt(optarg);
64 			break;
65 		case 'S':
66 			flags |= NI_NUMERICSERV;
67 			break;
68 		case 'e':
69 			long_err += 1;
70 			break;
71 		case 'p':
72 			port = strtonum(optarg, 0, 65535, &e);
73 			if (e)
74 				usage();
75 			break;
76 		default:
77 			usage();
78 			/* NOTREACHED */
79 		}
80 	}
81 	argc -= optind;
82 	argv += optind;
83 
84 	for(i = 0; i < argc; i++) {
85 
86 		if (i)
87 			printf("\n");
88 		printf("===> \"%s\"\n", argv[i]);
89 
90 		if (sockaddr_from_str(sa, AF_UNSPEC, argv[i]) == -1) {
91 			printf("   => invalid address\n");
92 			continue;
93 		}
94 
95 		if (sa->sa_family == PF_INET)
96 			((struct sockaddr_in *)sa)->sin_port = htons(port);
97 		else if (sa->sa_family == PF_INET6)
98 			((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
99 
100 		errno = 0;
101 		h_errno = 0;
102 		gai_errno = 0;
103 		rrset_errno = 0;
104 
105 		gai_errno = getnameinfo(sa, sa->sa_len, host, sizeof host, serv,
106 		    sizeof serv, flags);
107 
108 		if (gai_errno == 0)
109 			printf("   %s:%s\n", host, serv);
110 		print_errors();
111 
112 	}
113 
114 	return (0);
115 }
116