getnameinfo.c revision 1.3 1 1.3 christos /* $NetBSD: getnameinfo.c,v 1.3 2025/02/01 13:42:57 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Attaullah Ansari.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in
17 1.1 christos * the documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 1.1 christos * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 1.1 christos * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 1.1 christos * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 1.1 christos * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 1.1 christos * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 1.1 christos * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 1.1 christos * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 1.1 christos * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/cdefs.h>
33 1.1 christos #ifndef lint
34 1.3 christos __RCSID("$NetBSD: getnameinfo.c,v 1.3 2025/02/01 13:42:57 christos Exp $");
35 1.1 christos #endif
36 1.1 christos
37 1.1 christos #include <sys/types.h>
38 1.1 christos #include <sys/socket.h>
39 1.1 christos #include <netinet/in.h>
40 1.1 christos #include <arpa/inet.h>
41 1.1 christos
42 1.1 christos #include <assert.h>
43 1.1 christos #include <err.h>
44 1.1 christos #include <errno.h>
45 1.1 christos #include <netdb.h>
46 1.1 christos #include <stdlib.h>
47 1.1 christos #include <stdbool.h>
48 1.1 christos #include <stdio.h>
49 1.1 christos #include <string.h>
50 1.1 christos #include <unistd.h>
51 1.1 christos
52 1.1 christos /*
53 1.1 christos * getnameinfo: Resolve IP addresses and ports to hostnames and service names,
54 1.1 christos * similar to the getnameinfo function in the standard library.
55 1.1 christos *
56 1.1 christos * usage:
57 1.2 wiz * getnameinfo [-46fHNnrSu] [-p port] <IP-address>
58 1.1 christos *
59 1.1 christos * -4: Restrict lookup to IPv4 addresses only
60 1.1 christos * -6: Restrict lookup to IPv6 addresses only
61 1.1 christos * -f: Suppress the fully-qualified domain name (FQDN)
62 1.2 wiz * -H: Display only the hostname, omitting the service name
63 1.2 wiz * -N: Display the numeric service name instead of the service name
64 1.1 christos * -n: Display the numeric host address instead of the hostname
65 1.2 wiz * -p: Specify the port number to be used in the lookup
66 1.2 wiz * -r: Ensure that the name is returned (error if no name is found)
67 1.1 christos * -S: Display only the service name, omitting the hostname
68 1.2 wiz * -u: Use UDP instead of the default TCP
69 1.1 christos */
70 1.1 christos
71 1.1 christos
72 1.1 christos static void usage(void) __dead;
73 1.1 christos static void print_result(int, int, char *, char *);
74 1.1 christos static in_port_t get_port(const char *);
75 1.1 christos static uint8_t get_family(const char *);
76 1.1 christos
77 1.1 christos int
78 1.1 christos main(int argc, char **argv)
79 1.1 christos {
80 1.3 christos socklen_t hostlen = NI_MAXHOST, servlen = NI_MAXSERV, addrlen;
81 1.3 christos char hostname[NI_MAXHOST], service[NI_MAXSERV];
82 1.1 christos bool hostname_only = false, service_only = false;
83 1.3 christos uint8_t family = AF_UNSPEC;
84 1.1 christos int flags = 0;
85 1.1 christos char *address = NULL;
86 1.1 christos in_port_t port = 0;
87 1.1 christos struct sockaddr_storage addr_st;
88 1.1 christos struct sockaddr_in *addr_in;
89 1.1 christos struct sockaddr_in6 *addr_in6;
90 1.1 christos int ch;
91 1.1 christos int error;
92 1.1 christos
93 1.1 christos setprogname(argv[0]);
94 1.1 christos
95 1.1 christos while ((ch = getopt(argc, argv, "46rufnNHSp:")) != -1) {
96 1.1 christos switch (ch) {
97 1.1 christos case '4':
98 1.1 christos if (family != AF_UNSPEC)
99 1.1 christos goto opt46;
100 1.1 christos family = AF_INET;
101 1.1 christos break;
102 1.1 christos case '6':
103 1.1 christos if (family != AF_UNSPEC)
104 1.1 christos goto opt46;
105 1.1 christos family = AF_INET6;
106 1.1 christos break;
107 1.1 christos case 'r':
108 1.1 christos flags |= NI_NAMEREQD;
109 1.1 christos break;
110 1.1 christos case 'u':
111 1.1 christos flags |= NI_DGRAM;
112 1.1 christos break;
113 1.1 christos case 'f':
114 1.1 christos flags |= NI_NOFQDN;
115 1.1 christos break;
116 1.1 christos case 'n':
117 1.1 christos flags |= NI_NUMERICHOST;
118 1.1 christos break;
119 1.1 christos case 'N':
120 1.1 christos flags |= NI_NUMERICSERV;
121 1.1 christos break;
122 1.1 christos case 'H':
123 1.1 christos if (service_only)
124 1.1 christos goto optHS;
125 1.1 christos hostname_only = true;
126 1.1 christos break;
127 1.1 christos case 'S':
128 1.1 christos if (hostname_only)
129 1.1 christos goto optHS;
130 1.1 christos service_only = true;
131 1.1 christos break;
132 1.1 christos case 'p':
133 1.1 christos port = get_port(optarg);
134 1.1 christos break;
135 1.1 christos case '?':
136 1.1 christos default:
137 1.1 christos usage();
138 1.1 christos }
139 1.1 christos }
140 1.1 christos
141 1.1 christos argc -= optind;
142 1.1 christos argv += optind;
143 1.1 christos
144 1.1 christos if (argc > 1) {
145 1.1 christos warnx("Too many addresses");
146 1.1 christos usage();
147 1.1 christos }
148 1.1 christos
149 1.1 christos if (argc == 0 && (hostname_only ||
150 1.1 christos (hostname_only == 0 && service_only == 0))) {
151 1.1 christos warnx("No IP address provided");
152 1.1 christos usage();
153 1.1 christos }
154 1.1 christos
155 1.1 christos if (port == 0 && (service_only ||
156 1.1 christos (hostname_only == 0 && service_only == 0))) {
157 1.1 christos warnx("No port number provided");
158 1.1 christos usage();
159 1.1 christos }
160 1.1 christos
161 1.1 christos if (argc == 1) {
162 1.1 christos address = argv[0];
163 1.1 christos if (family == AF_UNSPEC)
164 1.1 christos family = get_family(address);
165 1.1 christos }
166 1.1 christos
167 1.1 christos switch (family) {
168 1.1 christos case AF_INET:
169 1.1 christos addr_in = (struct sockaddr_in *)&addr_st;
170 1.1 christos addr_in->sin_family = family;
171 1.1 christos addr_in->sin_port = htons(port);
172 1.1 christos if (inet_pton(family, address, &addr_in->sin_addr) == 0) {
173 1.1 christos warnx("Invalid IPv4 address: %s", address);
174 1.1 christos return EXIT_FAILURE;
175 1.1 christos }
176 1.3 christos addrlen = sizeof(*addr_in);
177 1.1 christos break;
178 1.1 christos case AF_INET6:
179 1.1 christos addr_in6 = (struct sockaddr_in6 *)&addr_st;
180 1.1 christos addr_in6->sin6_family = family;
181 1.1 christos addr_in6->sin6_port = htons(port);
182 1.1 christos if (inet_pton(family, address, &addr_in6->sin6_addr) == 0) {
183 1.1 christos warnx("Invalid IPv6 address: %s", address);
184 1.1 christos return EXIT_FAILURE;
185 1.1 christos }
186 1.3 christos addrlen = sizeof(*addr_in6);
187 1.1 christos break;
188 1.1 christos default:
189 1.1 christos warnx("Unsupported family %d", family);
190 1.1 christos return EXIT_FAILURE;
191 1.1 christos }
192 1.1 christos
193 1.1 christos if (hostname_only)
194 1.1 christos servlen = 0;
195 1.1 christos else if (service_only)
196 1.1 christos hostlen = 0;
197 1.1 christos
198 1.3 christos error = getnameinfo((struct sockaddr *)&addr_st, addrlen,
199 1.1 christos hostname, hostlen, service, servlen, flags);
200 1.1 christos if (error)
201 1.1 christos errx(EXIT_FAILURE, "%s", gai_strerror(error));
202 1.1 christos
203 1.1 christos print_result(hostname_only, service_only, hostname, service);
204 1.1 christos
205 1.1 christos return EXIT_SUCCESS;
206 1.1 christos opt46:
207 1.1 christos warnx("Options -4 and -6 cannot be used together");
208 1.1 christos usage();
209 1.1 christos optHS:
210 1.1 christos warnx("Options -H and -S cannot be used together");
211 1.1 christos usage();
212 1.1 christos }
213 1.1 christos
214 1.1 christos static uint8_t
215 1.1 christos get_family(const char* address)
216 1.1 christos {
217 1.1 christos struct in_addr ipv4_addr;
218 1.1 christos struct in6_addr ipv6_addr;
219 1.1 christos
220 1.1 christos if (inet_pton(AF_INET, address, &ipv4_addr) == 1)
221 1.1 christos return AF_INET;
222 1.1 christos
223 1.1 christos if (inet_pton(AF_INET6, address, &ipv6_addr) == 1)
224 1.1 christos return AF_INET6;
225 1.1 christos
226 1.1 christos errx(EXIT_FAILURE, "Invalid addrsss %s", address);
227 1.1 christos }
228 1.1 christos
229 1.1 christos static in_port_t
230 1.1 christos get_port(const char *port_str)
231 1.1 christos {
232 1.1 christos int r;
233 1.1 christos intmax_t port = strtoi(port_str, NULL, 0, 0, 65535, &r);
234 1.1 christos if (r)
235 1.1 christos errc(EXIT_FAILURE, r, "Invalid port number %s", port_str);
236 1.1 christos
237 1.1 christos return (in_port_t)port;
238 1.1 christos }
239 1.1 christos
240 1.1 christos static void
241 1.1 christos print_result(int hostname_only, int service_only, char *hostname, char *service)
242 1.1 christos {
243 1.1 christos int n;
244 1.1 christos if (hostname_only)
245 1.1 christos n = printf("%s\n", hostname);
246 1.1 christos else if (service_only)
247 1.1 christos n = printf("%s\n", service);
248 1.1 christos else
249 1.1 christos n = printf("%s %s\n", hostname, service);
250 1.1 christos if (n < 0)
251 1.1 christos err(EXIT_FAILURE, "printf");
252 1.1 christos }
253 1.1 christos
254 1.1 christos
255 1.1 christos static void __dead
256 1.1 christos usage(void)
257 1.1 christos {
258 1.1 christos (void)fprintf(stderr, "Usage: %s", getprogname());
259 1.2 wiz (void)fprintf(stderr, " [-46fHNnrSu] [-p port] <IP-address>\n");
260 1.1 christos exit(EXIT_FAILURE);
261 1.1 christos }
262