xref: /illumos-gate/usr/src/cmd/ipf/lib/gethost.c (revision f3ac6781)
1ab25eeb5Syz /*
2ab25eeb5Syz  * Copyright (C) 1993-2005  by Darren Reed.
39b4c7145Sjojemann  *
4ab25eeb5Syz  * See the IPFILTER.LICENCE file for details on licencing.
59b4c7145Sjojemann  *
69b4c7145Sjojemann  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
79b4c7145Sjojemann  * Use is subject to license terms.
89b4c7145Sjojemann  */
99b4c7145Sjojemann 
107c478bd9Sstevel@tonic-gate #include "ipf.h"
117c478bd9Sstevel@tonic-gate 
gethost(name,hostp,use_inet6)129b4c7145Sjojemann int gethost(name, hostp, use_inet6)
137c478bd9Sstevel@tonic-gate char *name;
149b4c7145Sjojemann i6addr_t *hostp;
159b4c7145Sjojemann int use_inet6;
167c478bd9Sstevel@tonic-gate {
179b4c7145Sjojemann 	struct addrinfo hints, *ai;
18ab25eeb5Syz 	struct netent *n;
199b4c7145Sjojemann 	int error;
207c478bd9Sstevel@tonic-gate 
21ab25eeb5Syz 	if (!strcmp(name, "test.host.dots")) {
229b4c7145Sjojemann 		hostp->in4.s_addr = htonl(0xfedcba98);
23ab25eeb5Syz 		return 0;
24ab25eeb5Syz 	}
25ab25eeb5Syz 
267c478bd9Sstevel@tonic-gate 	if (!strcmp(name, "<thishost>"))
277c478bd9Sstevel@tonic-gate 		name = thishost;
287c478bd9Sstevel@tonic-gate 
299b4c7145Sjojemann 	bzero(&hints, sizeof (hints));
309b4c7145Sjojemann 	if (use_inet6 == 0)
319b4c7145Sjojemann 		hints.ai_family = AF_INET;
329b4c7145Sjojemann 	else
339b4c7145Sjojemann 		hints.ai_family = AF_INET6;
349b4c7145Sjojemann 
359b4c7145Sjojemann 	error = getaddrinfo(name, NULL, &hints, &ai);
369b4c7145Sjojemann 
379b4c7145Sjojemann 	if ((error == 0) && (ai != NULL) && (ai->ai_addr != NULL)) {
389b4c7145Sjojemann 		switch (ai->ai_family)
399b4c7145Sjojemann 		{
409b4c7145Sjojemann 			case AF_INET:
419b4c7145Sjojemann 				hostp->in4 = ((struct sockaddr_in *)
429b4c7145Sjojemann 				    ai->ai_addr)->sin_addr;
439b4c7145Sjojemann 				break;
449b4c7145Sjojemann 			case AF_INET6:
459b4c7145Sjojemann 				hostp->in6 = ((struct sockaddr_in6 *)
469b4c7145Sjojemann 				    ai->ai_addr)->sin6_addr;
479b4c7145Sjojemann 				break;
489b4c7145Sjojemann 			default:
499b4c7145Sjojemann 				break;
507c478bd9Sstevel@tonic-gate 		}
519b4c7145Sjojemann 		freeaddrinfo(ai);
529b4c7145Sjojemann 		return 0;
537c478bd9Sstevel@tonic-gate 	}
54ab25eeb5Syz 
559b4c7145Sjojemann 	if (ai != NULL)
569b4c7145Sjojemann 		freeaddrinfo(ai);
579b4c7145Sjojemann 
589b4c7145Sjojemann 	if (use_inet6 == 0) {
599b4c7145Sjojemann 		n = getnetbyname(name);
609b4c7145Sjojemann 		if (n != NULL) {
619b4c7145Sjojemann 			hostp->in4.s_addr = htonl(n->n_net);
629b4c7145Sjojemann 			return 0;
639b4c7145Sjojemann 		}
64ab25eeb5Syz 	}
657c478bd9Sstevel@tonic-gate 	return -1;
667c478bd9Sstevel@tonic-gate }
67