xref: /openbsd-src/regress/lib/libc/asr/bin/gethostnamadr.c (revision a9e850503858625db8c9b559960909d88f57d4ab)
1*a9e85050Seric /*	$OpenBSD: gethostnamadr.c,v 1.3 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 #include <sys/types.h>
184c1e55dcSeric #include <sys/socket.h>
194c1e55dcSeric #include <netinet/in.h>
204c1e55dcSeric #include <arpa/inet.h>
214c1e55dcSeric 
224c1e55dcSeric #include <err.h>
234c1e55dcSeric #include <errno.h>
244c1e55dcSeric #include <getopt.h>
254c1e55dcSeric #include <stdio.h>
264c1e55dcSeric #include <stdlib.h>
274c1e55dcSeric #include <string.h>
284c1e55dcSeric 
294c1e55dcSeric #include "common.h"
304c1e55dcSeric 
314c1e55dcSeric static void
usage(void)324c1e55dcSeric usage(void)
334c1e55dcSeric {
344c1e55dcSeric 	extern const char * __progname;
354c1e55dcSeric 
364c1e55dcSeric 	fprintf(stderr, "usage: %s [-46e] <host...>\n", __progname);
374c1e55dcSeric 	exit(1);
384c1e55dcSeric }
394c1e55dcSeric 
404c1e55dcSeric int
main(int argc,char * argv[])414c1e55dcSeric main(int argc, char *argv[])
424c1e55dcSeric {
430549f880Seric 	int			 i, ch, aflag, family = AF_INET;
444c1e55dcSeric 	struct hostent		*h;
454c1e55dcSeric 	char			*host;
464c1e55dcSeric 	char			 addr[16];
474c1e55dcSeric 	int			 addrlen;
484c1e55dcSeric 
490549f880Seric 	aflag = 0;
50*a9e85050Seric 	while((ch = getopt(argc, argv, "46R:ae")) !=  -1) {
514c1e55dcSeric 		switch(ch) {
524c1e55dcSeric 		case '4':
534c1e55dcSeric 			family = AF_INET;
544c1e55dcSeric 			break;
554c1e55dcSeric 		case '6':
564c1e55dcSeric 			family = AF_INET6;
574c1e55dcSeric 			break;
58*a9e85050Seric 		case 'R':
59*a9e85050Seric 			parseresopt(optarg);
60*a9e85050Seric 			break;
610549f880Seric 		case 'a':
620549f880Seric 			aflag = 1;
630549f880Seric 			break;
644c1e55dcSeric 		case 'e':
654c1e55dcSeric 			long_err += 1;
664c1e55dcSeric 			break;
674c1e55dcSeric 		default:
684c1e55dcSeric 			usage();
694c1e55dcSeric 			/* NOTREACHED */
704c1e55dcSeric 		}
714c1e55dcSeric 	}
724c1e55dcSeric 	argc -= optind;
734c1e55dcSeric 	argv += optind;
744c1e55dcSeric 
754c1e55dcSeric 	for(i = 0; i < argc; i++) {
764c1e55dcSeric 
774c1e55dcSeric 		if (i)
784c1e55dcSeric 			printf("\n");
794c1e55dcSeric 		printf("===> \"%s\"\n", argv[i]);
804c1e55dcSeric 		host = gethostarg(argv[i]);
814c1e55dcSeric 
820549f880Seric 		if (aflag && addr_from_str(addr, &family, &addrlen, host) == -1)
830549f880Seric 			errx(1, "bad address");
844c1e55dcSeric 
854c1e55dcSeric 		errno = 0;
864c1e55dcSeric 		h_errno = 0;
874c1e55dcSeric 		gai_errno = 0;
884c1e55dcSeric 		rrset_errno = 0;
894c1e55dcSeric 
900549f880Seric 		if (aflag == 0)
914c1e55dcSeric 			h = gethostbyname2(host, family);
924c1e55dcSeric 		else
930549f880Seric 			h = gethostbyaddr(addr, addrlen, family);
944c1e55dcSeric 		if (h)
954c1e55dcSeric 			print_hostent(h);
964c1e55dcSeric 		print_errors();
974c1e55dcSeric 	}
984c1e55dcSeric 
994c1e55dcSeric 	return (0);
1004c1e55dcSeric }
1014c1e55dcSeric 
1024c1e55dcSeric int
addr_from_str(char * addr,int * family,int * len,const char * src)1034c1e55dcSeric addr_from_str(char *addr, int *family, int *len, const char *src)
1044c1e55dcSeric {
1054c1e55dcSeric 	if (inet_pton(AF_INET6, src, addr) == 1) {
1064c1e55dcSeric 		*family = AF_INET6;
1074c1e55dcSeric 		*len = 16;
1084c1e55dcSeric 		return (0);
1094c1e55dcSeric 	}
1104c1e55dcSeric 	if (inet_pton(AF_INET, src, addr) == 1) {
1114c1e55dcSeric 		*family = AF_INET;
1124c1e55dcSeric 		*len = 4;
1134c1e55dcSeric 		return (0);
1144c1e55dcSeric 	}
1154c1e55dcSeric 	return (-1);
1164c1e55dcSeric }
117