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