]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nss_files/files-hosts.c
* nss/nss_files/files-network.c: Pass empty for new DB_LOOKUP args.
[thirdparty/glibc.git] / nss / nss_files / files-hosts.c
1 /* Hosts file parser in nss_files module.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
19
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <arpa/nameser.h>
23 #include <netdb.h>
24 #include <resolv.h>
25
26
27 /* Get implementation for some internal functions. */
28 #include "../resolv/mapv4v6addr.h"
29 #include "../resolv/mapv4v6hostent.h"
30
31
32 #define ENTNAME hostent
33 #define DATABASE "hosts"
34
35 #define ENTDATA hostent_data
36 struct hostent_data
37 {
38 unsigned char host_addr[16]; /* IPv4 or IPv6 address. */
39 char *h_addr_ptrs[2]; /* Points to that and null terminator. */
40 };
41
42 #define TRAILING_LIST_MEMBER h_aliases
43 #define TRAILING_LIST_SEPARATOR_P isspace
44 #include "files-parse.c"
45 LINE_PARSER
46 ("#",
47 {
48 char *addr;
49
50 STRING_FIELD (addr, isspace, 1);
51
52 /* Parse address. */
53 if ((_res.options & RES_USE_INET6)
54 && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
55 {
56 result->h_addrtype = AF_INET6;
57 result->h_length = IN6ADDRSZ;
58 }
59 else if (inet_pton (AF_INET, addr, entdata->host_addr) > 0)
60 {
61 if (_res.options & RES_USE_INET6)
62 {
63 map_v4v6_address ((char *) entdata->host_addr,
64 (char *) entdata->host_addr);
65 result->h_addrtype = AF_INET6;
66 result->h_length = IN6ADDRSZ;
67 }
68 else
69 {
70 result->h_addrtype = AF_INET;
71 result->h_length = INADDRSZ;
72 }
73 }
74 else
75 /* Illegal address: ignore line. */
76 return 0;
77
78 /* Store a pointer to the address in the expected form. */
79 entdata->h_addr_ptrs[0] = entdata->host_addr;
80 entdata->h_addr_ptrs[1] = NULL;
81 result->h_addr_list = entdata->h_addr_ptrs;
82
83 /* If we need the host entry in IPv6 form change it now. */
84 if (_res.options & RES_USE_INET6)
85 {
86 char *bufptr = data->linebuffer;
87 int buflen = (char *) data + datalen - bufptr;
88 map_v4v6_hostent (result, &bufptr, &buflen);
89 }
90
91 STRING_FIELD (result->h_name, isspace, 1);
92 })
93
94 #include "files-XXX.c"
95
96 DB_LOOKUP (hostbyname, ,,
97 LOOKUP_NAME (h_name, h_aliases),
98 const char *name)
99
100 DB_LOOKUP (hostbyaddr, ,,
101 {
102 if (result->h_addrtype == type && result->h_length == len &&
103 ! memcmp (addr, result->h_addr_list[0], len))
104 break;
105 }, const char *addr, int len, int type)