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