xref: /netbsd-src/external/bsd/ipf/dist/lib/gethost.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: gethost.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * Id: gethost.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $
9  */
10 
11 #include "ipf.h"
12 
13 int gethost(family, name, hostp)
14 	int family;
15 	char *name;
16 	i6addr_t *hostp;
17 {
18 	struct hostent *h;
19 	struct netent *n;
20 	u_32_t addr;
21 
22 	if (!strcmp(name, "test.host.dots")) {
23 		if (family == AF_INET) {
24 			hostp->in4.s_addr = htonl(0xfedcba98);
25 		}
26 #ifdef USE_INET6
27 		if (family == AF_INET6) {
28 			hostp->i6[0] = 0xfe80aa55;
29 			hostp->i6[1] = 0x12345678;
30 			hostp->i6[2] = 0x5a5aa5a5;
31 			hostp->i6[3] = 0xfedcba98;
32 		}
33 #endif
34 		return 0;
35 	}
36 
37 	if (!strcmp(name, "<thishost>"))
38 		name = thishost;
39 
40 	if (family == AF_INET) {
41 		h = gethostbyname(name);
42 		if (h != NULL) {
43 			if ((h->h_addr != NULL) &&
44 			    (h->h_length == sizeof(addr))) {
45 				bcopy(h->h_addr, (char *)&addr, sizeof(addr));
46 				hostp->in4.s_addr = addr;
47 				return 0;
48 			}
49 		}
50 
51 		n = getnetbyname(name);
52 		if (n != NULL) {
53 			hostp->in4.s_addr = htonl(n->n_net & 0xffffffff);
54 			return 0;
55 		}
56 	}
57 #ifdef USE_INET6
58 	if (family == AF_INET6) {
59 		struct addrinfo hints, *res;
60 		struct sockaddr_in6 *sin6;
61 
62 		bzero((char *)&hints, sizeof(hints));
63 		hints.ai_family = PF_INET6;
64 
65 		getaddrinfo(name, NULL, &hints, &res);
66 		if (res != NULL) {
67 			sin6 = (struct sockaddr_in6 *)res->ai_addr;
68 			hostp->in6 = sin6->sin6_addr;
69 			freeaddrinfo(res);
70 			return 0;
71 		}
72 	}
73 #endif
74 	return -1;
75 }
76