Home | History | Annotate | Line # | Download | only in getnameinfo
getnameinfo.c revision 1.2
      1 /* $NetBSD: getnameinfo.c,v 1.2 2025/01/27 19:12:11 wiz 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.2 2025/01/27 19:12:11 wiz Exp $");
     35 #endif
     36 
     37 #include <sys/types.h>
     38 #include <sys/socket.h>
     39 #include <netinet/in.h>
     40 #include <arpa/inet.h>
     41 
     42 #include <assert.h>
     43 #include <err.h>
     44 #include <errno.h>
     45 #include <netdb.h>
     46 #include <stdlib.h>
     47 #include <stdbool.h>
     48 #include <stdio.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 
     52 /*
     53  * getnameinfo: Resolve IP addresses and ports to hostnames and service names,
     54  * similar to the getnameinfo function in the standard library.
     55  *
     56  * usage:
     57  *   getnameinfo [-46fHNnrSu] [-p port] <IP-address>
     58  *
     59  *   -4: Restrict lookup to IPv4 addresses only
     60  *   -6: Restrict lookup to IPv6 addresses only
     61  *   -f: Suppress the fully-qualified domain name (FQDN)
     62  *   -H: Display only the hostname, omitting the service name
     63  *   -N: Display the numeric service name instead of the service name
     64  *   -n: Display the numeric host address instead of the hostname
     65  *   -p: Specify the port number to be used in the lookup
     66  *   -r: Ensure that the name is returned (error if no name is found)
     67  *   -S: Display only the service name, omitting the hostname
     68  *   -u: Use UDP instead of the default TCP
     69  */
     70 
     71 
     72 static void	usage(void) __dead;
     73 static void	print_result(int, int, char *, char *);
     74 static in_port_t	get_port(const char *);
     75 static uint8_t	get_family(const char *);
     76 
     77 int
     78 main(int argc, char **argv)
     79 {
     80 	int hostlen = NI_MAXHOST, servlen = NI_MAXSERV;
     81 	char hostname[hostlen], service[servlen];
     82 	bool hostname_only = false, service_only = false;
     83 	int family = AF_UNSPEC;
     84 	int flags = 0;
     85 	char *address = NULL;
     86 	in_port_t port = 0;
     87 	struct sockaddr_storage addr_st;
     88 	struct sockaddr_in *addr_in;
     89 	struct sockaddr_in6 *addr_in6;
     90 	int addr_stlen;
     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 == 0 && service_only == 0))) {
    152 		warnx("No IP address provided");
    153 		usage();
    154 	}
    155 
    156 	if (port == 0 && (service_only ||
    157 	    (hostname_only == 0 && service_only == 0))) {
    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 (inet_pton(family, address, &addr_in->sin_addr) == 0) {
    174 			warnx("Invalid IPv4 address: %s", address);
    175 			return EXIT_FAILURE;
    176 		}
    177 		addr_stlen = sizeof(struct sockaddr_in);
    178 		break;
    179 	case AF_INET6:
    180 		addr_in6 = (struct sockaddr_in6 *)&addr_st;
    181 		addr_in6->sin6_family = family;
    182 		addr_in6->sin6_port = htons(port);
    183 		if (inet_pton(family, address, &addr_in6->sin6_addr) == 0) {
    184 			warnx("Invalid IPv6 address: %s", address);
    185 			return EXIT_FAILURE;
    186 		}
    187 		addr_stlen = sizeof(struct sockaddr_in6);
    188 		break;
    189 	default:
    190 		warnx("Unsupported family %d", family);
    191 		return EXIT_FAILURE;
    192 	}
    193 
    194 	if (hostname_only)
    195 		servlen = 0;
    196 	else if (service_only)
    197 		hostlen = 0;
    198 
    199 	error = getnameinfo((struct sockaddr *)&addr_st, addr_stlen,
    200 	    hostname, hostlen, service, servlen, flags);
    201 	if (error)
    202 		errx(EXIT_FAILURE, "%s", gai_strerror(error));
    203 
    204 	print_result(hostname_only, service_only, hostname, service);
    205 
    206 	return EXIT_SUCCESS;
    207 opt46:
    208 	warnx("Options -4 and -6 cannot be used together");
    209 	usage();
    210 optHS:
    211 	warnx("Options -H and -S cannot be used together");
    212 	usage();
    213 }
    214 
    215 static uint8_t
    216 get_family(const char* address)
    217 {
    218 	struct in_addr ipv4_addr;
    219 	struct in6_addr ipv6_addr;
    220 
    221 	if (inet_pton(AF_INET, address, &ipv4_addr) == 1)
    222 		return AF_INET;
    223 
    224 	if (inet_pton(AF_INET6, address, &ipv6_addr) == 1)
    225 		return AF_INET6;
    226 
    227 	errx(EXIT_FAILURE, "Invalid addrsss %s", address);
    228 }
    229 
    230 static in_port_t
    231 get_port(const char *port_str)
    232 {
    233 	int r;
    234 	intmax_t port = strtoi(port_str, NULL, 0, 0, 65535, &r);
    235 	if (r)
    236 		errc(EXIT_FAILURE, r, "Invalid port number %s", port_str);
    237 
    238 	return (in_port_t)port;
    239 }
    240 
    241 static void
    242 print_result(int hostname_only, int service_only, char *hostname, char *service)
    243 {
    244 	int n;
    245 	if (hostname_only)
    246 		n = printf("%s\n", hostname);
    247 	else if (service_only)
    248 		n = printf("%s\n", service);
    249 	else
    250 		n = printf("%s %s\n", hostname, service);
    251 	if (n < 0)
    252 		err(EXIT_FAILURE, "printf");
    253 }
    254 
    255 
    256 static void __dead
    257 usage(void)
    258 {
    259 	(void)fprintf(stderr, "Usage: %s", getprogname());
    260 	(void)fprintf(stderr, " [-46fHNnrSu] [-p port] <IP-address>\n");
    261 	exit(EXIT_FAILURE);
    262 }
    263