1 /* $NetBSD: gethost.c,v 1.5 2014/06/29 05:06:46 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
gethost(family,name,hostp)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 memset(hostp, 0, sizeof(*hostp));
23 if (!strcmp(name, "test.host.dots")) {
24 if (family == AF_INET) {
25 hostp->in4.s_addr = htonl(0xfedcba98);
26 }
27 #ifdef USE_INET6
28 if (family == AF_INET6) {
29 hostp->i6[0] = htonl(0xfe80aa55);
30 hostp->i6[1] = htonl(0x12345678);
31 hostp->i6[2] = htonl(0x5a5aa5a5);
32 hostp->i6[3] = htonl(0xfedcba98);
33 }
34 #endif
35 return 0;
36 }
37
38 if (!strcmp(name, "<thishost>"))
39 name = thishost;
40
41 if (family == AF_INET) {
42 h = gethostbyname(name);
43 if (h != NULL) {
44 if ((h->h_addr != NULL) &&
45 (h->h_length == sizeof(addr))) {
46 bcopy(h->h_addr, (char *)&addr, sizeof(addr));
47 hostp->in4.s_addr = addr;
48 return 0;
49 }
50 }
51
52 n = getnetbyname(name);
53 if (n != NULL) {
54 hostp->in4.s_addr = htonl(n->n_net & 0xffffffff);
55 return 0;
56 }
57 }
58 #ifdef USE_INET6
59 if (family == AF_INET6) {
60 struct addrinfo hints, *res;
61 struct sockaddr_in6 *sin6;
62
63 memset(&hints, 0, sizeof(hints));
64 hints.ai_family = PF_INET6;
65
66 getaddrinfo(name, NULL, &hints, &res);
67 if (res != NULL) {
68 sin6 = (struct sockaddr_in6 *)res->ai_addr;
69 hostp->in6 = sin6->sin6_addr;
70 freeaddrinfo(res);
71 return 0;
72 }
73 }
74 #endif
75 return -1;
76 }
77