xref: /openbsd-src/regress/lib/libc/asr/bin/getnameinfo.c (revision a9e850503858625db8c9b559960909d88f57d4ab)
1*a9e85050Seric /*	$OpenBSD: getnameinfo.c,v 1.2 2018/12/15 15:16:12 eric Exp $	*/
24c1e55dcSeric /*
34c1e55dcSeric  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
44c1e55dcSeric  *
54c1e55dcSeric  * Permission to use, copy, modify, and distribute this software for any
64c1e55dcSeric  * purpose with or without fee is hereby granted, provided that the above
74c1e55dcSeric  * copyright notice and this permission notice appear in all copies.
84c1e55dcSeric  *
94c1e55dcSeric  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
104c1e55dcSeric  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
114c1e55dcSeric  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
124c1e55dcSeric  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
134c1e55dcSeric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
144c1e55dcSeric  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
154c1e55dcSeric  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
164c1e55dcSeric  */
174c1e55dcSeric 
184c1e55dcSeric #include <err.h>
194c1e55dcSeric #include <errno.h>
204c1e55dcSeric #include <getopt.h>
214c1e55dcSeric #include <stdio.h>
224c1e55dcSeric #include <stdlib.h>
234c1e55dcSeric #include <string.h>
244c1e55dcSeric 
254c1e55dcSeric #include "common.h"
264c1e55dcSeric 
274c1e55dcSeric static void
usage(void)284c1e55dcSeric usage(void)
294c1e55dcSeric {
304c1e55dcSeric 	extern const char * __progname;
314c1e55dcSeric 
324c1e55dcSeric 	fprintf(stderr, "usage: %s [-DFHNSe] [-p portno] <addr...>\n", __progname);
334c1e55dcSeric 	exit(1);
344c1e55dcSeric }
354c1e55dcSeric 
364c1e55dcSeric int
main(int argc,char * argv[])374c1e55dcSeric main(int argc, char *argv[])
384c1e55dcSeric {
394c1e55dcSeric 	char			 serv[1024];
404c1e55dcSeric 	char			 host[1024];
414c1e55dcSeric 	const char		 *e;
424c1e55dcSeric 	int			 i, ch, flags = 0, port = 0;
434c1e55dcSeric 	struct sockaddr_storage	 ss;
444c1e55dcSeric 	struct sockaddr		*sa;
454c1e55dcSeric 
464c1e55dcSeric 	sa = (struct sockaddr*)&ss;
474c1e55dcSeric 
48*a9e85050Seric 	while((ch = getopt(argc, argv, "DFHNR:Saep:")) !=  -1) {
494c1e55dcSeric 		switch(ch) {
504c1e55dcSeric 		case 'D':
514c1e55dcSeric 			flags |= NI_DGRAM;
524c1e55dcSeric 			break;
534c1e55dcSeric 		case 'F':
544c1e55dcSeric 			flags |= NI_NOFQDN;
554c1e55dcSeric 			break;
564c1e55dcSeric 		case 'H':
574c1e55dcSeric 			flags |= NI_NUMERICHOST;
584c1e55dcSeric 			break;
594c1e55dcSeric 		case 'N':
604c1e55dcSeric 			flags |= NI_NAMEREQD;
614c1e55dcSeric 			break;
62*a9e85050Seric 		case 'R':
63*a9e85050Seric 			parseresopt(optarg);
64*a9e85050Seric 			break;
654c1e55dcSeric 		case 'S':
664c1e55dcSeric 			flags |= NI_NUMERICSERV;
674c1e55dcSeric 			break;
684c1e55dcSeric 		case 'e':
694c1e55dcSeric 			long_err += 1;
704c1e55dcSeric 			break;
714c1e55dcSeric 		case 'p':
724c1e55dcSeric 			port = strtonum(optarg, 0, 65535, &e);
734c1e55dcSeric 			if (e)
744c1e55dcSeric 				usage();
754c1e55dcSeric 			break;
764c1e55dcSeric 		default:
774c1e55dcSeric 			usage();
784c1e55dcSeric 			/* NOTREACHED */
794c1e55dcSeric 		}
804c1e55dcSeric 	}
814c1e55dcSeric 	argc -= optind;
824c1e55dcSeric 	argv += optind;
834c1e55dcSeric 
844c1e55dcSeric 	for(i = 0; i < argc; i++) {
854c1e55dcSeric 
864c1e55dcSeric 		if (i)
874c1e55dcSeric 			printf("\n");
884c1e55dcSeric 		printf("===> \"%s\"\n", argv[i]);
894c1e55dcSeric 
904c1e55dcSeric 		if (sockaddr_from_str(sa, AF_UNSPEC, argv[i]) == -1) {
914c1e55dcSeric 			printf("   => invalid address\n");
924c1e55dcSeric 			continue;
934c1e55dcSeric 		}
944c1e55dcSeric 
954c1e55dcSeric 		if (sa->sa_family == PF_INET)
964c1e55dcSeric 			((struct sockaddr_in *)sa)->sin_port = htons(port);
974c1e55dcSeric 		else if (sa->sa_family == PF_INET6)
984c1e55dcSeric 			((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
994c1e55dcSeric 
1004c1e55dcSeric 		errno = 0;
1014c1e55dcSeric 		h_errno = 0;
1024c1e55dcSeric 		gai_errno = 0;
1034c1e55dcSeric 		rrset_errno = 0;
1044c1e55dcSeric 
1054c1e55dcSeric 		gai_errno = getnameinfo(sa, sa->sa_len, host, sizeof host, serv,
1064c1e55dcSeric 		    sizeof serv, flags);
1074c1e55dcSeric 
1084c1e55dcSeric 		if (gai_errno == 0)
1094c1e55dcSeric 			printf("   %s:%s\n", host, serv);
1104c1e55dcSeric 		print_errors();
1114c1e55dcSeric 
1124c1e55dcSeric 	}
1134c1e55dcSeric 
1144c1e55dcSeric 	return (0);
1154c1e55dcSeric }
116