1 1.8 christos /* $NetBSD: getnameinfo.c,v 1.8 2025/02/06 20:59:00 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.8 christos __RCSID("$NetBSD: getnameinfo.c,v 1.8 2025/02/06 20:59:00 christos Exp $"); 35 1.1 christos #endif 36 1.1 christos 37 1.1 christos #include <sys/types.h> 38 1.4 riastrad 39 1.1 christos #include <sys/socket.h> 40 1.4 riastrad 41 1.4 riastrad #include <arpa/inet.h> 42 1.1 christos #include <netinet/in.h> 43 1.8 christos #include <netatalk/at.h> 44 1.8 christos #include <sys/un.h> 45 1.8 christos #include <net/if_dl.h> 46 1.8 christos #include <arpa/inet.h> 47 1.1 christos 48 1.1 christos #include <assert.h> 49 1.1 christos #include <err.h> 50 1.1 christos #include <errno.h> 51 1.1 christos #include <netdb.h> 52 1.1 christos #include <stdbool.h> 53 1.1 christos #include <stdio.h> 54 1.4 riastrad #include <stdlib.h> 55 1.1 christos #include <string.h> 56 1.1 christos #include <unistd.h> 57 1.1 christos 58 1.8 christos #include "support.h" 59 1.8 christos 60 1.1 christos /* 61 1.1 christos * getnameinfo: Resolve IP addresses and ports to hostnames and service names, 62 1.1 christos * similar to the getnameinfo function in the standard library. 63 1.1 christos * 64 1.1 christos * usage: 65 1.8 christos * getnameinfo [-46FHNnrSu] [-f family] [-p port] <IP-address> 66 1.1 christos * 67 1.1 christos * -4: Restrict lookup to IPv4 addresses only 68 1.1 christos * -6: Restrict lookup to IPv6 addresses only 69 1.8 christos * -F: Suppress the fully-qualified domain name (FQDN) 70 1.8 christos * -f: Specify address family to look up 71 1.2 wiz * -H: Display only the hostname, omitting the service name 72 1.2 wiz * -N: Display the numeric service name instead of the service name 73 1.1 christos * -n: Display the numeric host address instead of the hostname 74 1.2 wiz * -p: Specify the port number to be used in the lookup 75 1.2 wiz * -r: Ensure that the name is returned (error if no name is found) 76 1.1 christos * -S: Display only the service name, omitting the hostname 77 1.2 wiz * -u: Use UDP instead of the default TCP 78 1.1 christos */ 79 1.1 christos 80 1.4 riastrad static void usage(void) __dead; 81 1.4 riastrad static void print_result(bool, bool, char *, char *); 82 1.1 christos static in_port_t get_port(const char *); 83 1.8 christos static uint8_t get_family_from_address(const char *); 84 1.4 riastrad static uint8_t get_family(const char *); 85 1.8 christos static void parse_atalk(const char *, struct sockaddr_at *); 86 1.1 christos 87 1.1 christos int 88 1.1 christos main(int argc, char **argv) 89 1.1 christos { 90 1.3 christos socklen_t hostlen = NI_MAXHOST, servlen = NI_MAXSERV, addrlen; 91 1.3 christos char hostname[NI_MAXHOST], service[NI_MAXSERV]; 92 1.1 christos bool hostname_only = false, service_only = false; 93 1.3 christos uint8_t family = AF_UNSPEC; 94 1.1 christos int flags = 0; 95 1.4 riastrad char *address = NULL; 96 1.1 christos in_port_t port = 0; 97 1.1 christos struct sockaddr_storage addr_st; 98 1.1 christos struct sockaddr_in *addr_in; 99 1.1 christos struct sockaddr_in6 *addr_in6; 100 1.8 christos struct sockaddr_un *addr_un; 101 1.8 christos struct sockaddr_dl *addr_dl; 102 1.1 christos int ch; 103 1.1 christos int error; 104 1.1 christos 105 1.1 christos setprogname(argv[0]); 106 1.1 christos 107 1.8 christos while ((ch = getopt(argc, argv, "46Ff:HNnp:rSu")) != -1) { 108 1.1 christos switch (ch) { 109 1.1 christos case '4': 110 1.1 christos if (family != AF_UNSPEC) 111 1.8 christos goto opt46f; 112 1.1 christos family = AF_INET; 113 1.1 christos break; 114 1.1 christos case '6': 115 1.1 christos if (family != AF_UNSPEC) 116 1.8 christos goto opt46f; 117 1.1 christos family = AF_INET6; 118 1.1 christos break; 119 1.1 christos case 'r': 120 1.1 christos flags |= NI_NAMEREQD; 121 1.1 christos break; 122 1.1 christos case 'u': 123 1.1 christos flags |= NI_DGRAM; 124 1.1 christos break; 125 1.1 christos case 'f': 126 1.8 christos if (family != AF_UNSPEC) 127 1.8 christos goto opt46f; 128 1.8 christos family = get_family(optarg); 129 1.8 christos break; 130 1.8 christos case 'F': 131 1.1 christos flags |= NI_NOFQDN; 132 1.1 christos break; 133 1.1 christos case 'n': 134 1.1 christos flags |= NI_NUMERICHOST; 135 1.1 christos break; 136 1.1 christos case 'N': 137 1.1 christos flags |= NI_NUMERICSERV; 138 1.1 christos break; 139 1.1 christos case 'H': 140 1.1 christos if (service_only) 141 1.1 christos goto optHS; 142 1.1 christos hostname_only = true; 143 1.1 christos break; 144 1.1 christos case 'S': 145 1.1 christos if (hostname_only) 146 1.1 christos goto optHS; 147 1.1 christos service_only = true; 148 1.1 christos break; 149 1.1 christos case 'p': 150 1.1 christos port = get_port(optarg); 151 1.1 christos break; 152 1.1 christos case '?': 153 1.1 christos default: 154 1.1 christos usage(); 155 1.1 christos } 156 1.1 christos } 157 1.1 christos 158 1.1 christos argc -= optind; 159 1.1 christos argv += optind; 160 1.1 christos 161 1.1 christos if (argc > 1) { 162 1.1 christos warnx("Too many addresses"); 163 1.1 christos usage(); 164 1.1 christos } 165 1.1 christos 166 1.4 riastrad if (argc == 0 && (hostname_only || 167 1.4 riastrad (!hostname_only && !service_only))) { 168 1.1 christos warnx("No IP address provided"); 169 1.1 christos usage(); 170 1.1 christos } 171 1.1 christos 172 1.1 christos if (port == 0 && (service_only || 173 1.4 riastrad (!hostname_only && !service_only))) { 174 1.1 christos warnx("No port number provided"); 175 1.1 christos usage(); 176 1.1 christos } 177 1.1 christos 178 1.1 christos if (argc == 1) { 179 1.1 christos address = argv[0]; 180 1.1 christos if (family == AF_UNSPEC) 181 1.8 christos family = get_family_from_address(address); 182 1.1 christos } 183 1.1 christos 184 1.8 christos memset(&addr_st, 0, sizeof(addr_st)); 185 1.8 christos 186 1.1 christos switch (family) { 187 1.8 christos case AF_APPLETALK: 188 1.8 christos parse_atalk(address, (struct sockaddr_at *)&addr_st); 189 1.8 christos addrlen = sizeof(struct sockaddr_at *); 190 1.8 christos break; 191 1.1 christos case AF_INET: 192 1.1 christos addr_in = (struct sockaddr_in *)&addr_st; 193 1.1 christos addr_in->sin_family = family; 194 1.1 christos addr_in->sin_port = htons(port); 195 1.6 riastrad if (address == NULL) { 196 1.6 riastrad addr_in->sin_addr.s_addr = INADDR_ANY; 197 1.6 riastrad } else if (inet_pton(family, address, &addr_in->sin_addr) 198 1.8 christos == 0) 199 1.8 christos errx(EXIT_FAILURE, "Invalid IPv4 address: %s", address); 200 1.3 christos addrlen = sizeof(*addr_in); 201 1.1 christos break; 202 1.1 christos case AF_INET6: 203 1.1 christos addr_in6 = (struct sockaddr_in6 *)&addr_st; 204 1.1 christos addr_in6->sin6_family = family; 205 1.1 christos addr_in6->sin6_port = htons(port); 206 1.6 riastrad if (address == NULL) { 207 1.6 riastrad addr_in6->sin6_addr = 208 1.6 riastrad (struct in6_addr)IN6ADDR_ANY_INIT; 209 1.6 riastrad } else if (inet_pton(family, address, &addr_in6->sin6_addr) 210 1.8 christos == 0) 211 1.8 christos errx(EXIT_FAILURE, "Invalid IPv6 address: %s", address); 212 1.3 christos addrlen = sizeof(*addr_in6); 213 1.1 christos break; 214 1.8 christos case AF_LINK: 215 1.8 christos addr_dl = (struct sockaddr_dl *)&addr_st; 216 1.8 christos addr_dl->sdl_len = sizeof(addr_st); 217 1.8 christos link_addr(address, addr_dl); 218 1.8 christos addrlen = addr_dl->sdl_len; 219 1.8 christos break; 220 1.8 christos case AF_LOCAL: 221 1.8 christos addr_un = (struct sockaddr_un *)&addr_st; 222 1.8 christos addr_un->sun_family = family; 223 1.8 christos if (strlen(address) >= sizeof(addr_un->sun_path)) 224 1.8 christos errx(EXIT_FAILURE, "Invalid AF_LOCAL address: %s", 225 1.8 christos address); 226 1.8 christos (void)strncpy(addr_un->sun_path, address, 227 1.8 christos sizeof(addr_un->sun_path)); 228 1.8 christos addrlen = sizeof(*addr_un); 229 1.8 christos break; 230 1.1 christos default: 231 1.8 christos errx(EXIT_FAILURE, "Unsupported family %d", family); 232 1.1 christos } 233 1.1 christos 234 1.1 christos if (hostname_only) 235 1.1 christos servlen = 0; 236 1.1 christos else if (service_only) 237 1.1 christos hostlen = 0; 238 1.1 christos 239 1.3 christos error = getnameinfo((struct sockaddr *)&addr_st, addrlen, 240 1.1 christos hostname, hostlen, service, servlen, flags); 241 1.4 riastrad if (error) 242 1.1 christos errx(EXIT_FAILURE, "%s", gai_strerror(error)); 243 1.1 christos 244 1.1 christos print_result(hostname_only, service_only, hostname, service); 245 1.8 christos 246 1.5 riastrad fflush(stdout); 247 1.5 riastrad return ferror(stdout) ? EXIT_FAILURE : EXIT_SUCCESS; 248 1.8 christos opt46f: 249 1.8 christos warnx("Options -4, -6, -f cannot be used together"); 250 1.1 christos usage(); 251 1.1 christos optHS: 252 1.1 christos warnx("Options -H and -S cannot be used together"); 253 1.1 christos usage(); 254 1.1 christos } 255 1.1 christos 256 1.8 christos static void 257 1.8 christos parse_atalk(const char *address, struct sockaddr_at *addr_at) 258 1.8 christos { 259 1.8 christos int net, node, port; 260 1.8 christos 261 1.8 christos if (sscanf(address, "%d:%d:%d", &net, &node, &port) != 3) 262 1.8 christos badat: errx(EXIT_FAILURE, "Invalid appletalk address: %s", address); 263 1.8 christos 264 1.8 christos if (net < 0 || net > 0xFFFF || node < 0 || node > 0xFF || 265 1.8 christos port < 0 || port > 0xFFFF) 266 1.8 christos goto badat; 267 1.8 christos addr_at->sat_family = AF_APPLETALK; 268 1.8 christos addr_at->sat_addr.s_net = htons((u_short)net); 269 1.8 christos addr_at->sat_addr.s_node = (u_char)node; 270 1.8 christos addr_at->sat_port = htons((u_short)port); 271 1.8 christos } 272 1.8 christos 273 1.1 christos static uint8_t 274 1.8 christos get_family_from_address(const char *address) 275 1.1 christos { 276 1.1 christos struct in_addr ipv4_addr; 277 1.1 christos struct in6_addr ipv6_addr; 278 1.1 christos 279 1.1 christos if (inet_pton(AF_INET, address, &ipv4_addr) == 1) 280 1.1 christos return AF_INET; 281 1.1 christos 282 1.1 christos if (inet_pton(AF_INET6, address, &ipv6_addr) == 1) 283 1.1 christos return AF_INET6; 284 1.1 christos 285 1.1 christos errx(EXIT_FAILURE, "Invalid addrsss %s", address); 286 1.1 christos } 287 1.1 christos 288 1.8 christos static uint8_t 289 1.8 christos get_family(const char* str) 290 1.8 christos { 291 1.8 christos int fam; 292 1.8 christos if (!parse_af(str, &fam) || fam <= 0 || fam > 255) 293 1.8 christos errx(EXIT_FAILURE, "Invalid family %s", str); 294 1.8 christos return (int8_t)fam; 295 1.8 christos } 296 1.8 christos 297 1.1 christos static in_port_t 298 1.1 christos get_port(const char *port_str) 299 1.1 christos { 300 1.1 christos int r; 301 1.4 riastrad 302 1.4 riastrad const intmax_t port = strtoi(port_str, NULL, 0, 0, 65535, &r); 303 1.1 christos if (r) 304 1.1 christos errc(EXIT_FAILURE, r, "Invalid port number %s", port_str); 305 1.1 christos 306 1.1 christos return (in_port_t)port; 307 1.1 christos } 308 1.1 christos 309 1.1 christos static void 310 1.4 riastrad print_result(bool hostname_only, bool service_only, 311 1.4 riastrad char *hostname, char *service) 312 1.1 christos { 313 1.4 riastrad 314 1.1 christos if (hostname_only) 315 1.5 riastrad printf("%s\n", hostname); 316 1.1 christos else if (service_only) 317 1.5 riastrad printf("%s\n", service); 318 1.1 christos else 319 1.5 riastrad printf("%s %s\n", hostname, service); 320 1.1 christos } 321 1.1 christos 322 1.1 christos static void __dead 323 1.1 christos usage(void) 324 1.1 christos { 325 1.4 riastrad 326 1.1 christos (void)fprintf(stderr, "Usage: %s", getprogname()); 327 1.7 riastrad (void)fprintf(stderr, " [-46fHNnrSu] [-p port] [<IP-address>]\n"); 328 1.1 christos exit(EXIT_FAILURE); 329 1.1 christos } 330