1#!/usr/bin/python3
2# Generate DNS RR type constants for resolv header files.
3# Copyright (C) 2016-2022 Free Software Foundation, Inc.
4# This file is part of the GNU C Library.
5#
6# The GNU C Library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# The GNU C Library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with the GNU C Library; if not, see
18# <https://www.gnu.org/licenses/>.
19
20"""Generate DNS RR type constants for resolv header files.
21
22resolv/arpa/nameser.h and resolv/arpa/nameser_compat.h contain lists
23of RR type constants.  This script downloads the current definitions
24from the IANA DNS Parameters protocol registry and translates it into
25the two different lists.
26
27Two lists are written to standard output.  The first one contains enum
28constants for resolv/arpa/nameser.h.  The second one lists the
29preprocessor macros for resolv/arpa/nameser_compat.h.
30
31"""
32
33# URL of the IANA registry.
34source = "http://www.iana.org/assignments/dns-parameters/dns-parameters-4.csv"
35
36import collections
37import csv
38import io
39import urllib.request
40
41Type = collections.namedtuple("Type", "name number comment")
42
43def get_types(source):
44    for row in csv.reader(io.TextIOWrapper(urllib.request.urlopen(source))):
45        if row[0] in ('TYPE', 'Unassigned', 'Private use', 'Reserved'):
46            continue
47        name, number, comment = row[:3]
48        if name == '*':
49            name = 'ANY'
50            comment = 'request for all cached records'
51        number = int(number)
52        yield Type(name, number, comment)
53
54types = list(get_types(source))
55
56print("// enum constants for resolv/arpa/nameser.h")
57print()
58for typ in types:
59    name = typ.name.replace("-", "_").lower()
60    print("    ns_t_{0} = {1.number},".format(name, typ))
61print()
62
63print("// macro aliases resolv/arpa/nameser_compat.h")
64print()
65for typ in types:
66    name = typ.name.replace("-", "_")
67    print("#define T_{0} ns_t_{1}".format(name.upper(), name.lower()))
68print()
69