Blame scripts/gen-rrtypes.py

Packit 6c4009
#!/usr/bin/python3
Packit 6c4009
# Generate DNS RR type constants for resolv header files.
Packit 6c4009
# Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
# This file is part of the GNU C Library.
Packit 6c4009
#
Packit 6c4009
# The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
# modify it under the terms of the GNU Lesser General Public
Packit 6c4009
# License as published by the Free Software Foundation; either
Packit 6c4009
# version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
#
Packit 6c4009
# The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
# Lesser General Public License for more details.
Packit 6c4009
#
Packit 6c4009
# You should have received a copy of the GNU Lesser General Public
Packit 6c4009
# License along with the GNU C Library; if not, see
Packit 6c4009
# <http://www.gnu.org/licenses/>.
Packit 6c4009
Packit 6c4009
"""Generate DNS RR type constants for resolv header files.
Packit 6c4009
Packit 6c4009
resolv/arpa/nameser.h and resolv/arpa/nameser_compat.h contain lists
Packit 6c4009
of RR type constants.  This script downloads the current definitions
Packit 6c4009
from the IANA DNS Parameters protocol registry and translates it into
Packit 6c4009
the two different lists.
Packit 6c4009
Packit 6c4009
Two lists are written to standard output.  The first one contains enum
Packit 6c4009
constants for resolv/arpa/nameser.h.  The second one lists the
Packit 6c4009
preprocessor macros for resolv/arpa/nameser_compat.h.
Packit 6c4009
Packit 6c4009
"""
Packit 6c4009
Packit 6c4009
# URL of the IANA registry.
Packit 6c4009
source = "http://www.iana.org/assignments/dns-parameters/dns-parameters-4.csv"
Packit 6c4009
Packit 6c4009
import collections
Packit 6c4009
import csv
Packit 6c4009
import io
Packit 6c4009
import urllib.request
Packit 6c4009
Packit 6c4009
Type = collections.namedtuple("Type", "name number comment")
Packit 6c4009
Packit 6c4009
def get_types(source):
Packit 6c4009
    for row in csv.reader(io.TextIOWrapper(urllib.request.urlopen(source))):
Packit 6c4009
        if row[0] in ('TYPE', 'Unassigned', 'Private use', 'Reserved'):
Packit 6c4009
            continue
Packit 6c4009
        name, number, comment = row[:3]
Packit 6c4009
        if name == '*':
Packit 6c4009
            name = 'ANY'
Packit 6c4009
            comment = 'request for all cached records'
Packit 6c4009
        number = int(number)
Packit 6c4009
        yield Type(name, number, comment)
Packit 6c4009
Packit 6c4009
types = list(get_types(source))
Packit 6c4009
Packit 6c4009
print("// enum constants for resolv/arpa/nameser.h")
Packit 6c4009
print()
Packit 6c4009
for typ in types:
Packit 6c4009
    name = typ.name.replace("-", "_").lower()
Packit 6c4009
    print("    ns_t_{0} = {1.number},".format(name, typ))
Packit 6c4009
print()
Packit 6c4009
Packit 6c4009
print("// macro aliases resolv/arpa/nameser_compat.h")
Packit 6c4009
print()
Packit 6c4009
for typ in types:
Packit 6c4009
    name = typ.name.replace("-", "_")
Packit 6c4009
    print("#define T_{0} ns_t_{1}".format(name.upper(), name.lower()))
Packit 6c4009
print()