]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ipfilter/lib/gethost.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ipfilter / lib / gethost.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * $Id$
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         bzero(hostp, 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                 bzero((char *)&hints, 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 }