xref: /illumos-gate/usr/src/cmd/ipf/lib/gethost.c (revision d7574e9a)
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 #include "ipf.h"
11 
12 int gethost(name, hostp, use_inet6)
13 char *name;
14 i6addr_t *hostp;
15 int use_inet6;
16 {
17 	struct addrinfo hints, *ai;
18 	struct netent *n;
19 	int error;
20 
21 	if (!strcmp(name, "test.host.dots")) {
22 		hostp->in4.s_addr = htonl(0xfedcba98);
23 		return 0;
24 	}
25 
26 	if (!strcmp(name, "<thishost>"))
27 		name = thishost;
28 
29 	bzero(&hints, sizeof (hints));
30 	if (use_inet6 == 0)
31 		hints.ai_family = AF_INET;
32 	else
33 		hints.ai_family = AF_INET6;
34 
35 	error = getaddrinfo(name, NULL, &hints, &ai);
36 
37 	if ((error == 0) && (ai != NULL) && (ai->ai_addr != NULL)) {
38 		switch (ai->ai_family)
39 		{
40 			case AF_INET:
41 				hostp->in4 = ((struct sockaddr_in *)
42 				    ai->ai_addr)->sin_addr;
43 				break;
44 			case AF_INET6:
45 				hostp->in6 = ((struct sockaddr_in6 *)
46 				    ai->ai_addr)->sin6_addr;
47 				break;
48 			default:
49 				break;
50 		}
51 		freeaddrinfo(ai);
52 		return 0;
53 	}
54 
55 	if (ai != NULL)
56 		freeaddrinfo(ai);
57 
58 	if (use_inet6 == 0) {
59 		n = getnetbyname(name);
60 		if (n != NULL) {
61 			hostp->in4.s_addr = htonl(n->n_net);
62 			return 0;
63 		}
64 	}
65 	return -1;
66 }
67