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