xref: /titanic_41/usr/src/cmd/ipf/lib/common/gethost.c (revision b509e89b2befbaa42939abad9da1d7f5a8c6aaae)
1 /*
2  * Copyright (C) 1993-2005  by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
7  * Use is subject to license terms.
8  */
9 
10 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11 
12 #include "ipf.h"
13 
14 int gethost(name, hostp, use_inet6)
15 char *name;
16 i6addr_t *hostp;
17 int use_inet6;
18 {
19 	struct addrinfo hints, *ai;
20 	struct netent *n;
21 	int error;
22 
23 	if (!strcmp(name, "test.host.dots")) {
24 		hostp->in4.s_addr = htonl(0xfedcba98);
25 		return 0;
26 	}
27 
28 	if (!strcmp(name, "<thishost>"))
29 		name = thishost;
30 
31 	bzero(&hints, sizeof (hints));
32 	if (use_inet6 == 0)
33 		hints.ai_family = AF_INET;
34 	else
35 		hints.ai_family = AF_INET6;
36 
37 	error = getaddrinfo(name, NULL, &hints, &ai);
38 
39 	if ((error == 0) && (ai != NULL) && (ai->ai_addr != NULL)) {
40 		switch (ai->ai_family)
41 		{
42 			case AF_INET:
43 				hostp->in4 = ((struct sockaddr_in *)
44 				    ai->ai_addr)->sin_addr;
45 				break;
46 			case AF_INET6:
47 				hostp->in6 = ((struct sockaddr_in6 *)
48 				    ai->ai_addr)->sin6_addr;
49 				break;
50 			default:
51 				break;
52 		}
53 		freeaddrinfo(ai);
54 		return 0;
55 	}
56 
57 	if (ai != NULL)
58 		freeaddrinfo(ai);
59 
60 	if (use_inet6 == 0) {
61 		n = getnetbyname(name);
62 		if (n != NULL) {
63 			hostp->in4.s_addr = htonl(n->n_net);
64 			return 0;
65 		}
66 	}
67 	return -1;
68 }
69