aboutsummaryrefslogtreecommitdiff
path: root/sbin/ipf/libipf/gethost.c
blob: aefdbbae9fb3335e06e6fe96a797820aff7a3ec9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*	$FreeBSD$	*/

/*
 * Copyright (C) 2012 by Darren Reed.
 *
 * See the IPFILTER.LICENCE file for details on licencing.
 *
 * $Id$
 */

#include "ipf.h"

int
gethost(int family, char *name, i6addr_t *hostp)
{
	struct hostent *h;
	struct netent *n;
	u_32_t addr;

	bzero(hostp, sizeof(*hostp));
	if (!strcmp(name, "test.host.dots")) {
		if (family == AF_INET) {
			hostp->in4.s_addr = htonl(0xfedcba98);
		}
#ifdef USE_INET6
		if (family == AF_INET6) {
			hostp->i6[0] = htonl(0xfe80aa55);
			hostp->i6[1] = htonl(0x12345678);
			hostp->i6[2] = htonl(0x5a5aa5a5);
			hostp->i6[3] = htonl(0xfedcba98);
		}
#endif
		return (0);
	}

	if (!strcmp(name, "<thishost>"))
		name = thishost;

	if (family == AF_INET) {
		h = gethostbyname(name);
		if (h != NULL) {
			if ((h->h_addr != NULL) &&
			    (h->h_length == sizeof(addr))) {
				bcopy(h->h_addr, (char *)&addr, sizeof(addr));
				hostp->in4.s_addr = addr;
				return (0);
			}
		}

		n = getnetbyname(name);
		if (n != NULL) {
			hostp->in4.s_addr = htonl(n->n_net & 0xffffffff);
			return (0);
		}
	}
#ifdef USE_INET6
	if (family == AF_INET6) {
		struct addrinfo hints, *res;
		struct sockaddr_in6 *sin6;

		bzero((char *)&hints, sizeof(hints));
		hints.ai_family = PF_INET6;

		getaddrinfo(name, NULL, &hints, &res);
		if (res != NULL) {
			sin6 = (struct sockaddr_in6 *)res->ai_addr;
			hostp->in6 = sin6->sin6_addr;
			freeaddrinfo(res);
			return (0);
		}
	}
#endif
	return (-1);
}