Home | History | Annotate | Line # | Download | only in getnameinfo
getnameinfo.c revision 1.3
      1 /* $NetBSD: getnameinfo.c,v 1.3 2025/02/01 13:42:57 christos 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.3 2025/02/01 13:42:57 christos 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 	socklen_t hostlen = NI_MAXHOST, servlen = NI_MAXSERV, addrlen;
     81 	char hostname[NI_MAXHOST], service[NI_MAXSERV];
     82 	bool hostname_only = false, service_only = false;
     83 	uint8_t 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 ch;
     91 	int error;
     92 
     93 	setprogname(argv[0]);
     94 
     95 	while ((ch = getopt(argc, argv, "46rufnNHSp:")) != -1) {
     96 		switch (ch) {
     97 		case '4':
     98 			if (family != AF_UNSPEC)
     99 				goto opt46;
    100 			family = AF_INET;
    101 			break;
    102 		case '6':
    103 			if (family != AF_UNSPEC)
    104 				goto opt46;
    105 			family = AF_INET6;
    106 			break;
    107 		case 'r':
    108 			flags |= NI_NAMEREQD;
    109 			break;
    110 		case 'u':
    111 			flags |= NI_DGRAM;
    112 			break;
    113 		case 'f':
    114 			flags |= NI_NOFQDN;
    115 			break;
    116 		case 'n':
    117 			flags |= NI_NUMERICHOST;
    118 			break;
    119 		case 'N':
    120 			flags |= NI_NUMERICSERV;
    121 			break;
    122 		case 'H':
    123 			if (service_only)
    124 				goto optHS;
    125 			hostname_only = true;
    126 			break;
    127 		case 'S':
    128 			if (hostname_only)
    129 				goto optHS;
    130 			service_only = true;
    131 			break;
    132 		case 'p':
    133 			port = get_port(optarg);
    134 			break;
    135 		case '?':
    136 		default:
    137 			usage();
    138 		}
    139 	}
    140 
    141 	argc -= optind;
    142 	argv += optind;
    143 
    144 	if (argc > 1) {
    145 		warnx("Too many addresses");
    146 		usage();
    147 	}
    148 
    149 	if (argc == 0 && (hostname_only ||
    150 	    (hostname_only == 0 && service_only == 0))) {
    151 		warnx("No IP address provided");
    152 		usage();
    153 	}
    154 
    155 	if (port == 0 && (service_only ||
    156 	    (hostname_only == 0 && service_only == 0))) {
    157 		warnx("No port number provided");
    158 		usage();
    159 	}
    160 
    161 	if (argc == 1) {
    162 		address = argv[0];
    163 		if (family == AF_UNSPEC)
    164 			family = get_family(address);
    165 	}
    166 
    167 	switch (family) {
    168 	case AF_INET:
    169 		addr_in = (struct sockaddr_in *)&addr_st;
    170 		addr_in->sin_family = family;
    171 		addr_in->sin_port = htons(port);
    172 		if (inet_pton(family, address, &addr_in->sin_addr) == 0) {
    173 			warnx("Invalid IPv4 address: %s", address);
    174 			return EXIT_FAILURE;
    175 		}
    176 		addrlen = sizeof(*addr_in);
    177 		break;
    178 	case AF_INET6:
    179 		addr_in6 = (struct sockaddr_in6 *)&addr_st;
    180 		addr_in6->sin6_family = family;
    181 		addr_in6->sin6_port = htons(port);
    182 		if (inet_pton(family, address, &addr_in6->sin6_addr) == 0) {
    183 			warnx("Invalid IPv6 address: %s", address);
    184 			return EXIT_FAILURE;
    185 		}
    186 		addrlen = sizeof(*addr_in6);
    187 		break;
    188 	default:
    189 		warnx("Unsupported family %d", family);
    190 		return EXIT_FAILURE;
    191 	}
    192 
    193 	if (hostname_only)
    194 		servlen = 0;
    195 	else if (service_only)
    196 		hostlen = 0;
    197 
    198 	error = getnameinfo((struct sockaddr *)&addr_st, addrlen,
    199 	    hostname, hostlen, service, servlen, flags);
    200 	if (error)
    201 		errx(EXIT_FAILURE, "%s", gai_strerror(error));
    202 
    203 	print_result(hostname_only, service_only, hostname, service);
    204 
    205 	return EXIT_SUCCESS;
    206 opt46:
    207 	warnx("Options -4 and -6 cannot be used together");
    208 	usage();
    209 optHS:
    210 	warnx("Options -H and -S cannot be used together");
    211 	usage();
    212 }
    213 
    214 static uint8_t
    215 get_family(const char* address)
    216 {
    217 	struct in_addr ipv4_addr;
    218 	struct in6_addr ipv6_addr;
    219 
    220 	if (inet_pton(AF_INET, address, &ipv4_addr) == 1)
    221 		return AF_INET;
    222 
    223 	if (inet_pton(AF_INET6, address, &ipv6_addr) == 1)
    224 		return AF_INET6;
    225 
    226 	errx(EXIT_FAILURE, "Invalid addrsss %s", address);
    227 }
    228 
    229 static in_port_t
    230 get_port(const char *port_str)
    231 {
    232 	int r;
    233 	intmax_t port = strtoi(port_str, NULL, 0, 0, 65535, &r);
    234 	if (r)
    235 		errc(EXIT_FAILURE, r, "Invalid port number %s", port_str);
    236 
    237 	return (in_port_t)port;
    238 }
    239 
    240 static void
    241 print_result(int hostname_only, int service_only, char *hostname, char *service)
    242 {
    243 	int n;
    244 	if (hostname_only)
    245 		n = printf("%s\n", hostname);
    246 	else if (service_only)
    247 		n = printf("%s\n", service);
    248 	else
    249 		n = printf("%s %s\n", hostname, service);
    250 	if (n < 0)
    251 		err(EXIT_FAILURE, "printf");
    252 }
    253 
    254 
    255 static void __dead
    256 usage(void)
    257 {
    258 	(void)fprintf(stderr, "Usage: %s", getprogname());
    259 	(void)fprintf(stderr, " [-46fHNnrSu] [-p port] <IP-address>\n");
    260 	exit(EXIT_FAILURE);
    261 }
    262