1/* Networks file parser in nss_files module.
2 Copyright (C) 1996-2020 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <netdb.h>
22#include <stdint.h>
23#include <nss.h>
24
25NSS_DECLARE_MODULE_FUNCTIONS (files)
26
27#define ENTNAME netent
28#define DATABASE "networks"
29#define NEED_H_ERRNO
30
31struct netent_data {};
32
33#define TRAILING_LIST_MEMBER n_aliases
34#define TRAILING_LIST_SEPARATOR_P isspace
35#include "files-parse.c"
36LINE_PARSER
37("#",
38 {
39 char *addr;
40 char *cp;
41 int n = 1;
42
43 STRING_FIELD (result->n_name, isspace, 1);
44
45 STRING_FIELD (addr, isspace, 1);
46 /* 'inet_network' does not add zeroes at the end if the network number
47 does not four byte values. We add them outselves if necessary. */
48 cp = strchr (addr, '.');
49 if (cp != NULL)
50 {
51 ++n;
52 cp = strchr (cp + 1, '.');
53 if (cp != NULL)
54 {
55 ++n;
56 cp = strchr (cp + 1, '.');
57 if (cp != NULL)
58 ++n;
59 }
60 }
61 if (n < 4)
62 {
63 char *newp = (char *) alloca (strlen (addr) + (4 - n) * 2 + 1);
64 cp = stpcpy (newp, addr);
65 do
66 {
67 *cp++ = '.';
68 *cp++ = '0';
69 }
70 while (++n < 4);
71 *cp = '\0';
72 addr = newp;
73 }
74 result->n_net = inet_network (addr);
75 result->n_addrtype = AF_INET;
76
77 })
78
79#include "files-XXX.c"
80
81DB_LOOKUP (netbyname, ,,,
82 LOOKUP_NAME_CASE (n_name, n_aliases),
83 const char *name)
84
85DB_LOOKUP (netbyaddr, ,,,
86 {
87 if ((type == AF_UNSPEC || result->n_addrtype == type)
88 && result->n_net == net)
89 /* Bingo! */
90 break;
91 }, uint32_t net, int type)
92