pydnspp.cc 32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15

16
// We want a lot of different parts of the DNS API in the python
17 18 19 20 21 22 23
// module, but not one big 10000-line file.
// So we split it up in several 'mini-modules'
// These would be the same as a single module, except for
// the init function, which has to be modified to a unique
// name initModulePart_<name>, and return true/false instead of
// NULL/*mod
//
24 25
// The big init function is split up into a separate initModulePart function
// for each class we add.
26 27 28

#define PY_SSIZE_T_CLEAN
#include <Python.h>
29
#include <structmember.h>
30

31 32 33
#include <dns/message.h>
#include <dns/opcode.h>
#include <dns/tsig.h>
34 35
#include <util/python/pycppwrapper_util.h>

36
#include "pydnspp_common.h"
37

38 39 40 41
#include "edns_python.h"
#include "message_python.h"
#include "messagerenderer_python.h"
#include "name_python.h"
42
#include "nsec3hash_python.h"
43 44 45 46 47 48 49 50 51 52
#include "opcode_python.h"
#include "pydnspp_common.h"
#include "pydnspp_towire.h"
#include "question_python.h"
#include "rcode_python.h"
#include "rdata_python.h"
#include "rrclass_python.h"
#include "rrset_python.h"
#include "rrttl_python.h"
#include "rrtype_python.h"
53
#include "serial_python.h"
54 55 56 57 58
#include "tsigerror_python.h"
#include "tsigkey_python.h"
#include "tsig_python.h"
#include "tsig_rdata_python.h"
#include "tsigrecord_python.h"
59

60
using namespace isc::dns;
61
using namespace isc::dns::python;
62
using namespace isc::util::python;
63 64

namespace {
65 66 67 68 69 70 71

bool
initModulePart_EDNS(PyObject* mod) {
    // After the type has been initialized, we initialize any exceptions
    // that are defined in the wrapper for this class, and add constants
    // to the type, if any

72
    if (!initClass(edns_type, "EDNS", mod)) {
73 74 75 76 77 78 79 80 81 82
        return (false);
    }
    addClassVariable(edns_type, "SUPPORTED_VERSION",
                     Py_BuildValue("B", EDNS::SUPPORTED_VERSION));

    return (true);
}

bool
initModulePart_Message(PyObject* mod) {
83
    if (!initClass(message_type, "Message", mod)) {
84 85
        return (false);
    }
86

87 88 89 90 91 92 93 94 95 96 97
    try {
        //
        // Constant class variables
        //

        // Parse mode
        installClassVariable(message_type, "PARSE",
                             Py_BuildValue("I", Message::PARSE));
        installClassVariable(message_type, "RENDER",
                             Py_BuildValue("I", Message::RENDER));

98
        // Parse options
99 100
        installClassVariable(message_type, "PARSE_DEFAULT",
                             Py_BuildValue("I", Message::PARSE_DEFAULT));
101 102 103
        installClassVariable(message_type, "PRESERVE_ORDER",
                             Py_BuildValue("I", Message::PRESERVE_ORDER));

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        // Header flags
        installClassVariable(message_type, "HEADERFLAG_QR",
                             Py_BuildValue("I", Message::HEADERFLAG_QR));
        installClassVariable(message_type, "HEADERFLAG_AA",
                             Py_BuildValue("I", Message::HEADERFLAG_AA));
        installClassVariable(message_type, "HEADERFLAG_TC",
                             Py_BuildValue("I", Message::HEADERFLAG_TC));
        installClassVariable(message_type, "HEADERFLAG_RD",
                             Py_BuildValue("I", Message::HEADERFLAG_RD));
        installClassVariable(message_type, "HEADERFLAG_RA",
                             Py_BuildValue("I", Message::HEADERFLAG_RA));
        installClassVariable(message_type, "HEADERFLAG_AD",
                             Py_BuildValue("I", Message::HEADERFLAG_AD));
        installClassVariable(message_type, "HEADERFLAG_CD",
                             Py_BuildValue("I", Message::HEADERFLAG_CD));

        // Sections
        installClassVariable(message_type, "SECTION_QUESTION",
                             Py_BuildValue("I", Message::SECTION_QUESTION));
        installClassVariable(message_type, "SECTION_ANSWER",
                             Py_BuildValue("I", Message::SECTION_ANSWER));
        installClassVariable(message_type, "SECTION_AUTHORITY",
                             Py_BuildValue("I", Message::SECTION_AUTHORITY));
        installClassVariable(message_type, "SECTION_ADDITIONAL",
                             Py_BuildValue("I", Message::SECTION_ADDITIONAL));

        // Protocol constant
        installClassVariable(message_type, "DEFAULT_MAX_UDPSIZE",
                             Py_BuildValue("I", Message::DEFAULT_MAX_UDPSIZE));

        /* Class-specific exceptions */
        po_MessageTooShort =
            PyErr_NewException("pydnspp.MessageTooShort", NULL, NULL);
        PyObjectContainer(po_MessageTooShort).installToModule(
            mod, "MessageTooShort");
        po_InvalidMessageSection =
            PyErr_NewException("pydnspp.InvalidMessageSection", NULL, NULL);
        PyObjectContainer(po_InvalidMessageSection).installToModule(
            mod, "InvalidMessageSection");
        po_InvalidMessageOperation =
            PyErr_NewException("pydnspp.InvalidMessageOperation", NULL, NULL);
        PyObjectContainer(po_InvalidMessageOperation).installToModule(
            mod, "InvalidMessageOperation");
        po_InvalidMessageUDPSize =
            PyErr_NewException("pydnspp.InvalidMessageUDPSize", NULL, NULL);
        PyObjectContainer(po_InvalidMessageUDPSize).installToModule(
            mod, "InvalidMessageUDPSize");
        po_DNSMessageBADVERS =
            PyErr_NewException("pydnspp.DNSMessageBADVERS", NULL, NULL);
        PyObjectContainer(po_DNSMessageBADVERS).installToModule(
            mod, "DNSMessageBADVERS");
155 156 157 158
        po_UnknownNSEC3HashAlgorithm =
            PyErr_NewException("pydnspp.UnknownNSEC3HashAlgorithm", NULL, NULL);
        PyObjectContainer(po_UnknownNSEC3HashAlgorithm).installToModule(
            mod, "UnknownNSEC3HashAlgorithm");
159 160 161 162 163 164 165 166 167 168 169
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in Message initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in Message initialization");
        return (false);
    }
170 171 172 173 174 175

    return (true);
}

bool
initModulePart_MessageRenderer(PyObject* mod) {
176
    if (!initClass(messagerenderer_type, "MessageRenderer", mod)) {
177 178 179 180 181 182 183 184 185 186 187 188 189
        return (false);
    }

    addClassVariable(messagerenderer_type, "CASE_INSENSITIVE",
                     Py_BuildValue("I", MessageRenderer::CASE_INSENSITIVE));
    addClassVariable(messagerenderer_type, "CASE_SENSITIVE",
                     Py_BuildValue("I", MessageRenderer::CASE_SENSITIVE));


    return (true);
}

bool
190
initModulePart_NameComparisonResult(PyObject* mod) {
191 192
    if (!initClass(name_comparison_result_type,
                   "NameComparisonResult", mod)) {
193 194 195 196 197 198 199 200 201 202 203
        return (false);
    }

    // Add the enums to the module
    po_NameRelation = Py_BuildValue("{i:s,i:s,i:s,i:s}",
        NameComparisonResult::SUPERDOMAIN, "SUPERDOMAIN",
        NameComparisonResult::SUBDOMAIN, "SUBDOMAIN",
        NameComparisonResult::EQUAL, "EQUAL",
        NameComparisonResult::COMMONANCESTOR, "COMMONANCESTOR");
    addClassVariable(name_comparison_result_type, "NameRelation",
                     po_NameRelation);
204 205 206 207 208 209 210 211 212
    // Add the constants themselves too
    addClassVariable(name_comparison_result_type, "SUPERDOMAIN",
                     Py_BuildValue("I", NameComparisonResult::SUPERDOMAIN));
    addClassVariable(name_comparison_result_type, "SUBDOMAIN",
                     Py_BuildValue("I", NameComparisonResult::SUBDOMAIN));
    addClassVariable(name_comparison_result_type, "EQUAL",
                     Py_BuildValue("I", NameComparisonResult::EQUAL));
    addClassVariable(name_comparison_result_type, "COMMONANCESTOR",
                     Py_BuildValue("I", NameComparisonResult::COMMONANCESTOR));
213

214 215
    return (true);
}
216

217 218
bool
initModulePart_Name(PyObject* mod) {
219
    if (!initClass(name_type, "Name", mod)) {
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        return (false);
    }

    // Add the constants to the module
    addClassVariable(name_type, "MAX_WIRE",
                     Py_BuildValue("I", Name::MAX_WIRE));
    addClassVariable(name_type, "MAX_LABELS",
                     Py_BuildValue("I", Name::MAX_LABELS));
    addClassVariable(name_type, "MAX_LABELLEN",
                     Py_BuildValue("I", Name::MAX_LABELLEN));
    addClassVariable(name_type, "MAX_COMPRESS_POINTER",
                     Py_BuildValue("I", Name::MAX_COMPRESS_POINTER));
    addClassVariable(name_type, "COMPRESS_POINTER_MARK8",
                     Py_BuildValue("I", Name::COMPRESS_POINTER_MARK8));
    addClassVariable(name_type, "COMPRESS_POINTER_MARK16",
                     Py_BuildValue("I", Name::COMPRESS_POINTER_MARK16));

    addClassVariable(name_type, "ROOT_NAME",
                     createNameObject(Name::ROOT_NAME()));

    // Add the exceptions to the module
241 242 243
    try {
        po_EmptyLabel = PyErr_NewException("pydnspp.EmptyLabel", NULL, NULL);
        PyObjectContainer(po_EmptyLabel).installToModule(mod, "EmptyLabel");
244

245 246
        po_TooLongName = PyErr_NewException("pydnspp.TooLongName", NULL, NULL);
        PyObjectContainer(po_TooLongName).installToModule(mod, "TooLongName");
247

248 249
        po_TooLongLabel = PyErr_NewException("pydnspp.TooLongLabel", NULL, NULL);
        PyObjectContainer(po_TooLongLabel).installToModule(mod, "TooLongLabel");
250

251 252
        po_BadLabelType = PyErr_NewException("pydnspp.BadLabelType", NULL, NULL);
        PyObjectContainer(po_BadLabelType).installToModule(mod, "BadLabelType");
253

254 255
        po_BadEscape = PyErr_NewException("pydnspp.BadEscape", NULL, NULL);
        PyObjectContainer(po_BadEscape).installToModule(mod, "BadEscape");
256

257 258 259
        po_IncompleteName = PyErr_NewException("pydnspp.IncompleteName", NULL,
                                               NULL);
        PyObjectContainer(po_IncompleteName).installToModule(mod, "IncompleteName");
260

261 262 263 264
        po_InvalidBufferPosition =
            PyErr_NewException("pydnspp.InvalidBufferPosition", NULL, NULL);
        PyObjectContainer(po_InvalidBufferPosition).installToModule(
            mod, "InvalidBufferPosition");
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        // This one could have gone into the message_python.cc file, but is
        // already needed here.
        po_DNSMessageFORMERR = PyErr_NewException("pydnspp.DNSMessageFORMERR",
                                                  NULL, NULL);
        PyObjectContainer(po_DNSMessageFORMERR).installToModule(
            mod, "DNSMessageFORMERR");
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in Name initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in Name initialization");
        return (false);
    }
283 284 285 286 287 288

    return (true);
}

bool
initModulePart_Opcode(PyObject* mod) {
289
    if (!initClass(opcode_type, "Opcode", mod)) {
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        return (false);
    }

    addClassVariable(opcode_type, "QUERY_CODE",
                     Py_BuildValue("h", Opcode::QUERY_CODE));
    addClassVariable(opcode_type, "IQUERY_CODE",
                     Py_BuildValue("h", Opcode::IQUERY_CODE));
    addClassVariable(opcode_type, "STATUS_CODE",
                     Py_BuildValue("h", Opcode::STATUS_CODE));
    addClassVariable(opcode_type, "RESERVED3_CODE",
                     Py_BuildValue("h", Opcode::RESERVED3_CODE));
    addClassVariable(opcode_type, "NOTIFY_CODE",
                     Py_BuildValue("h", Opcode::NOTIFY_CODE));
    addClassVariable(opcode_type, "UPDATE_CODE",
                     Py_BuildValue("h", Opcode::UPDATE_CODE));
    addClassVariable(opcode_type, "RESERVED6_CODE",
                     Py_BuildValue("h", Opcode::RESERVED6_CODE));
    addClassVariable(opcode_type, "RESERVED7_CODE",
                     Py_BuildValue("h", Opcode::RESERVED7_CODE));
    addClassVariable(opcode_type, "RESERVED8_CODE",
                     Py_BuildValue("h", Opcode::RESERVED8_CODE));
    addClassVariable(opcode_type, "RESERVED9_CODE",
                     Py_BuildValue("h", Opcode::RESERVED9_CODE));
    addClassVariable(opcode_type, "RESERVED10_CODE",
                     Py_BuildValue("h", Opcode::RESERVED10_CODE));
    addClassVariable(opcode_type, "RESERVED11_CODE",
                     Py_BuildValue("h", Opcode::RESERVED11_CODE));
    addClassVariable(opcode_type, "RESERVED12_CODE",
                     Py_BuildValue("h", Opcode::RESERVED12_CODE));
    addClassVariable(opcode_type, "RESERVED13_CODE",
                     Py_BuildValue("h", Opcode::RESERVED13_CODE));
    addClassVariable(opcode_type, "RESERVED14_CODE",
                     Py_BuildValue("h", Opcode::RESERVED14_CODE));
    addClassVariable(opcode_type, "RESERVED15_CODE",
                     Py_BuildValue("h", Opcode::RESERVED15_CODE));

    return (true);
}

bool
initModulePart_Question(PyObject* mod) {
331
    return (initClass(question_type, "Question", mod));
332 333 334 335
}

bool
initModulePart_Rcode(PyObject* mod) {
336
    if (!initClass(rcode_type, "Rcode", mod)) {
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        return (false);
    }

    addClassVariable(rcode_type, "NOERROR_CODE",
                     Py_BuildValue("h", Rcode::NOERROR_CODE));
    addClassVariable(rcode_type, "FORMERR_CODE",
                     Py_BuildValue("h", Rcode::FORMERR_CODE));
    addClassVariable(rcode_type, "SERVFAIL_CODE",
                     Py_BuildValue("h", Rcode::SERVFAIL_CODE));
    addClassVariable(rcode_type, "NXDOMAIN_CODE",
                     Py_BuildValue("h", Rcode::NXDOMAIN_CODE));
    addClassVariable(rcode_type, "NOTIMP_CODE",
                     Py_BuildValue("h", Rcode::NOTIMP_CODE));
    addClassVariable(rcode_type, "REFUSED_CODE",
                     Py_BuildValue("h", Rcode::REFUSED_CODE));
    addClassVariable(rcode_type, "YXDOMAIN_CODE",
                     Py_BuildValue("h", Rcode::YXDOMAIN_CODE));
    addClassVariable(rcode_type, "YXRRSET_CODE",
                     Py_BuildValue("h", Rcode::YXRRSET_CODE));
    addClassVariable(rcode_type, "NXRRSET_CODE",
                     Py_BuildValue("h", Rcode::NXRRSET_CODE));
    addClassVariable(rcode_type, "NOTAUTH_CODE",
                     Py_BuildValue("h", Rcode::NOTAUTH_CODE));
    addClassVariable(rcode_type, "NOTZONE_CODE",
                     Py_BuildValue("h", Rcode::NOTZONE_CODE));
    addClassVariable(rcode_type, "RESERVED11_CODE",
                     Py_BuildValue("h", Rcode::RESERVED11_CODE));
    addClassVariable(rcode_type, "RESERVED12_CODE",
                     Py_BuildValue("h", Rcode::RESERVED12_CODE));
    addClassVariable(rcode_type, "RESERVED13_CODE",
                     Py_BuildValue("h", Rcode::RESERVED13_CODE));
    addClassVariable(rcode_type, "RESERVED14_CODE",
                     Py_BuildValue("h", Rcode::RESERVED14_CODE));
    addClassVariable(rcode_type, "RESERVED15_CODE",
                     Py_BuildValue("h", Rcode::RESERVED15_CODE));
    addClassVariable(rcode_type, "BADVERS_CODE",
                     Py_BuildValue("h", Rcode::BADVERS_CODE));

    return (true);
}

bool
initModulePart_Rdata(PyObject* mod) {
380
    if (!initClass(rdata_type, "Rdata", mod)) {
381 382 383 384
        return (false);
    }

    // Add the exceptions to the class
385
    try {
386 387
        po_InvalidRdataLength =
            PyErr_NewException("pydnspp.InvalidRdataLength", NULL, NULL);
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
        PyObjectContainer(po_InvalidRdataLength).installToModule(
            mod, "InvalidRdataLength");

        po_InvalidRdataText = PyErr_NewException("pydnspp.InvalidRdataText",
                                                 NULL, NULL);
        PyObjectContainer(po_InvalidRdataText).installToModule(
            mod, "InvalidRdataText");

        po_CharStringTooLong = PyErr_NewException("pydnspp.CharStringTooLong",
                                                  NULL, NULL);
        PyObjectContainer(po_CharStringTooLong).installToModule(
            mod, "CharStringTooLong");
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in Rdata initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in Rdata initialization");
        return (false);
    }
411 412 413 414 415 416

    return (true);
}

bool
initModulePart_RRClass(PyObject* mod) {
417 418 419 420
    if (!initClass(rrclass_type, "RRClass", mod)) {
        return (false);
    }

421 422 423 424 425
    try {
        po_InvalidRRClass = PyErr_NewException("pydnspp.InvalidRRClass",
                                               NULL, NULL);
        PyObjectContainer(po_InvalidRRClass).installToModule(
            mod, "InvalidRRClass");
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
        po_IncompleteRRClass = PyErr_NewException("pydnspp.IncompleteRRClass",
                                                  NULL, NULL);
        PyObjectContainer(po_IncompleteRRClass).installToModule(
            mod, "IncompleteRRClass");
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in RRClass initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in RRClass initialization");
        return (false);
    }
442 443 444 445 446 447

    return (true);
}

bool
initModulePart_RRset(PyObject* mod) {
448
    if (!initClass(rrset_type, "RRset", mod)) {
449 450
        return (false);
    }
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465
    try {
        po_EmptyRRset = PyErr_NewException("pydnspp.EmptyRRset", NULL, NULL);
        PyObjectContainer(po_EmptyRRset).installToModule(mod, "EmptyRRset");
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in RRset initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in RRset initialization");
        return (false);
    }
466 467 468 469 470 471

    return (true);
}

bool
initModulePart_RRTTL(PyObject* mod) {
472 473 474 475
    if (!initClass(rrttl_type, "RRTTL", mod)) {
        return (false);
    }

476 477 478 479 480
    try {
        po_InvalidRRTTL = PyErr_NewException("pydnspp.InvalidRRTTL",
                                             NULL, NULL);
        PyObjectContainer(po_InvalidRRTTL).installToModule(mod,
                                                           "InvalidRRTTL");
481

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
        po_IncompleteRRTTL = PyErr_NewException("pydnspp.IncompleteRRTTL",
                                                NULL, NULL);
        PyObjectContainer(po_IncompleteRRTTL).installToModule(
            mod, "IncompleteRRTTL");
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in RRTTL initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in RRTTL initialization");
        return (false);
    }
497 498 499 500 501 502

    return (true);
}

bool
initModulePart_RRType(PyObject* mod) {
503 504 505 506
    if (!initClass(rrtype_type, "RRType", mod)) {
        return (false);
    }

507 508 509 510 511
    try {
        po_InvalidRRType = PyErr_NewException("pydnspp.InvalidRRType",
                                              NULL, NULL);
        PyObjectContainer(po_InvalidRRType).installToModule(mod,
                                                            "InvalidRRType");
512

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
        po_IncompleteRRType = PyErr_NewException("pydnspp.IncompleteRRType",
                                                 NULL, NULL);
        PyObjectContainer(po_IncompleteRRType).installToModule(
            mod, "IncompleteRRType");
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in RRType initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in RRType initialization");
        return (false);
    }
528 529 530 531

    return (true);
}

532 533
bool
initModulePart_Serial(PyObject* mod) {
534
    return (initClass(serial_type, "Serial", mod));
535 536
}

537 538
bool
initModulePart_TSIGError(PyObject* mod) {
539
    if (!initClass(tsigerror_type, "TSIGError", mod)) {
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
        return (false);
    }

    try {
        // Constant class variables
        // Error codes (bare values)
        installClassVariable(tsigerror_type, "BAD_SIG_CODE",
                             Py_BuildValue("H", TSIGError::BAD_SIG_CODE));
        installClassVariable(tsigerror_type, "BAD_KEY_CODE",
                             Py_BuildValue("H", TSIGError::BAD_KEY_CODE));
        installClassVariable(tsigerror_type, "BAD_TIME_CODE",
                             Py_BuildValue("H", TSIGError::BAD_TIME_CODE));

        // Error codes (constant objects)
        installClassVariable(tsigerror_type, "NOERROR",
                             createTSIGErrorObject(TSIGError::NOERROR()));
        installClassVariable(tsigerror_type, "FORMERR",
                             createTSIGErrorObject(TSIGError::FORMERR()));
        installClassVariable(tsigerror_type, "SERVFAIL",
                             createTSIGErrorObject(TSIGError::SERVFAIL()));
        installClassVariable(tsigerror_type, "NXDOMAIN",
                             createTSIGErrorObject(TSIGError::NXDOMAIN()));
        installClassVariable(tsigerror_type, "NOTIMP",
                             createTSIGErrorObject(TSIGError::NOTIMP()));
        installClassVariable(tsigerror_type, "REFUSED",
                             createTSIGErrorObject(TSIGError::REFUSED()));
        installClassVariable(tsigerror_type, "YXDOMAIN",
                             createTSIGErrorObject(TSIGError::YXDOMAIN()));
        installClassVariable(tsigerror_type, "YXRRSET",
                             createTSIGErrorObject(TSIGError::YXRRSET()));
        installClassVariable(tsigerror_type, "NXRRSET",
                             createTSIGErrorObject(TSIGError::NXRRSET()));
        installClassVariable(tsigerror_type, "NOTAUTH",
                             createTSIGErrorObject(TSIGError::NOTAUTH()));
        installClassVariable(tsigerror_type, "NOTZONE",
                             createTSIGErrorObject(TSIGError::NOTZONE()));
        installClassVariable(tsigerror_type, "RESERVED11",
                             createTSIGErrorObject(TSIGError::RESERVED11()));
        installClassVariable(tsigerror_type, "RESERVED12",
                             createTSIGErrorObject(TSIGError::RESERVED12()));
        installClassVariable(tsigerror_type, "RESERVED13",
                             createTSIGErrorObject(TSIGError::RESERVED13()));
        installClassVariable(tsigerror_type, "RESERVED14",
                             createTSIGErrorObject(TSIGError::RESERVED14()));
        installClassVariable(tsigerror_type, "RESERVED15",
                             createTSIGErrorObject(TSIGError::RESERVED15()));
        installClassVariable(tsigerror_type, "BAD_SIG",
                             createTSIGErrorObject(TSIGError::BAD_SIG()));
        installClassVariable(tsigerror_type, "BAD_KEY",
                             createTSIGErrorObject(TSIGError::BAD_KEY()));
        installClassVariable(tsigerror_type, "BAD_TIME",
                             createTSIGErrorObject(TSIGError::BAD_TIME()));
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in TSIGError initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in TSIGError initialization");
        return (false);
    }

    return (true);
}

bool
initModulePart_TSIGKey(PyObject* mod) {
609
    if (!initClass(tsigkey_type, "TSIGKey", mod)) {
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
        return (false);
    }

    try {
        // Constant class variables
        installClassVariable(tsigkey_type, "HMACMD5_NAME",
                             createNameObject(TSIGKey::HMACMD5_NAME()));
        installClassVariable(tsigkey_type, "HMACSHA1_NAME",
                             createNameObject(TSIGKey::HMACSHA1_NAME()));
        installClassVariable(tsigkey_type, "HMACSHA256_NAME",
                             createNameObject(TSIGKey::HMACSHA256_NAME()));
        installClassVariable(tsigkey_type, "HMACSHA224_NAME",
                             createNameObject(TSIGKey::HMACSHA224_NAME()));
        installClassVariable(tsigkey_type, "HMACSHA384_NAME",
                             createNameObject(TSIGKey::HMACSHA384_NAME()));
        installClassVariable(tsigkey_type, "HMACSHA512_NAME",
                             createNameObject(TSIGKey::HMACSHA512_NAME()));
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in TSIGKey initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in TSIGKey initialization");
        return (false);
    }

    return (true);
}

bool
initModulePart_TSIGKeyRing(PyObject* mod) {
644
    if (!initClass(tsigkeyring_type, "TSIGKeyRing", mod)) {
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
        return (false);
    }

    addClassVariable(tsigkeyring_type, "SUCCESS",
                     Py_BuildValue("I", TSIGKeyRing::SUCCESS));
    addClassVariable(tsigkeyring_type, "EXIST",
                     Py_BuildValue("I", TSIGKeyRing::EXIST));
    addClassVariable(tsigkeyring_type, "NOTFOUND",
                     Py_BuildValue("I", TSIGKeyRing::NOTFOUND));

    return (true);
}

bool
initModulePart_TSIGContext(PyObject* mod) {
660
    if (!initClass(tsigcontext_type, "TSIGContext", mod)) {
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
        return (false);
    }

    try {
        // Class specific exceptions
        po_TSIGContextError = PyErr_NewException("pydnspp.TSIGContextError",
                                                 po_IscException, NULL);
        PyObjectContainer(po_TSIGContextError).installToModule(
            mod, "TSIGContextError");

        // Constant class variables
        installClassVariable(tsigcontext_type, "STATE_INIT",
                             Py_BuildValue("I", TSIGContext::INIT));
        installClassVariable(tsigcontext_type, "STATE_SENT_REQUEST",
                             Py_BuildValue("I", TSIGContext::SENT_REQUEST));
        installClassVariable(tsigcontext_type, "STATE_RECEIVED_REQUEST",
                             Py_BuildValue("I", TSIGContext::RECEIVED_REQUEST));
        installClassVariable(tsigcontext_type, "STATE_SENT_RESPONSE",
                             Py_BuildValue("I", TSIGContext::SENT_RESPONSE));
        installClassVariable(tsigcontext_type, "STATE_VERIFIED_RESPONSE",
                             Py_BuildValue("I",
                                           TSIGContext::VERIFIED_RESPONSE));

        installClassVariable(tsigcontext_type, "DEFAULT_FUDGE",
                             Py_BuildValue("H", TSIGContext::DEFAULT_FUDGE));
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in TSIGContext initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in TSIGContext initialization");
        return (false);
    }

    return (true);
}

bool
initModulePart_TSIG(PyObject* mod) {
703
    return (initClass(tsig_type, "TSIG", mod));
704 705 706 707
}

bool
initModulePart_TSIGRecord(PyObject* mod) {
708
    if (!initClass(tsigrecord_type, "TSIGRecord", mod)) {
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
        return (false);
    }

    try {
        // Constant class variables
        installClassVariable(tsigrecord_type, "TSIG_TTL",
                             Py_BuildValue("I", 0));
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in TSIGRecord initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (false);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in TSIGRecord initialization");
        return (false);
    }

    return (true);
}

731
PyModuleDef pydnspp = {
732
    { PyObject_HEAD_INIT(NULL) NULL, 0, NULL},
Jelte Jansen's avatar
Jelte Jansen committed
733
    "pydnspp",
Jelte Jansen's avatar
Jelte Jansen committed
734 735 736 737 738
    "Python bindings for the classes in the isc::dns namespace.\n\n"
    "These bindings match the original C++ API as closely as possible, "
    "but are not complete. Some classes are unnecessary (InputBuffer "
    "and OutputBuffer for instance), and others may be necessary, but "
    "were not up to now.",
739 740 741 742 743 744 745
    -1,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};
746
}
747 748

PyMODINIT_FUNC
Jelte Jansen's avatar
Jelte Jansen committed
749
PyInit_pydnspp(void) {
750
    PyObject* mod = PyModule_Create(&pydnspp);
751
    if (mod == NULL) {
Jelte Jansen's avatar
Jelte Jansen committed
752
        return (NULL);
753 754
    }

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    try {
        // Add the exceptions to the class
        po_IscException = PyErr_NewException("pydnspp.IscException", NULL, NULL);
        PyObjectContainer(po_IscException).installToModule(mod, "IscException");

        po_InvalidOperation = PyErr_NewException("pydnspp.InvalidOperation",
                                                 NULL, NULL);
        PyObjectContainer(po_InvalidOperation).installToModule(
            mod, "InvalidOperation");

        po_InvalidParameter = PyErr_NewException("pydnspp.InvalidParameter",
                                                 NULL, NULL);
        PyObjectContainer(po_InvalidParameter).installToModule(
            mod, "InvalidParameter");
    } catch (const std::exception& ex) {
        const std::string ex_what =
            "Unexpected failure in pydnspp initialization: " +
            std::string(ex.what());
        PyErr_SetString(po_IscException, ex_what.c_str());
        return (NULL);
    } catch (...) {
        PyErr_SetString(PyExc_SystemError,
                        "Unexpected failure in pydnspp initialization");
        return (NULL);
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
780

781 782
    // for each part included above, we call its specific initializer

783 784 785 786
    if (!initModulePart_NameComparisonResult(mod)) {
        return (NULL);
    }

787
    if (!initModulePart_Name(mod)) {
Jelte Jansen's avatar
Jelte Jansen committed
788
        return (NULL);
789 790 791
    }

    if (!initModulePart_MessageRenderer(mod)) {
Jelte Jansen's avatar
Jelte Jansen committed
792
        return (NULL);
793 794
    }

795 796 797 798
    if (!initModulePart_NSEC3Hash(mod)) {
        return (NULL);
    }

799
    if (!initModulePart_RRClass(mod)) {
Jelte Jansen's avatar
Jelte Jansen committed
800
        return (NULL);
801 802 803
    }

    if (!initModulePart_RRType(mod)) {
Jelte Jansen's avatar
Jelte Jansen committed
804
        return (NULL);
805 806 807
    }

    if (!initModulePart_RRTTL(mod)) {
Jelte Jansen's avatar
Jelte Jansen committed
808
        return (NULL);
809 810 811
    }

    if (!initModulePart_Rdata(mod)) {
Jelte Jansen's avatar
Jelte Jansen committed
812
        return (NULL);
813 814 815
    }

    if (!initModulePart_RRset(mod)) {
Jelte Jansen's avatar
Jelte Jansen committed
816
        return (NULL);
817 818
    }

819
    if (!initModulePart_Question(mod)) {
Jelte Jansen's avatar
Jelte Jansen committed
820
        return (NULL);
821 822
    }

823 824 825 826
    if (!initModulePart_Opcode(mod)) {
        return (NULL);
    }

827 828 829 830
    if (!initModulePart_Rcode(mod)) {
        return (NULL);
    }

831
    if (!initModulePart_Message(mod)) {
Jelte Jansen's avatar
Jelte Jansen committed
832
        return (NULL);
833 834
    }

JINMEI Tatuya's avatar
JINMEI Tatuya committed
835 836 837 838
    if (!initModulePart_EDNS(mod)) {
        return (NULL);
    }

839 840 841 842
    if (!initModulePart_Serial(mod)) {
        return (NULL);
    }

843 844 845 846 847 848 849 850
    if (!initModulePart_TSIGKey(mod)) {
        return (NULL);
    }

    if (!initModulePart_TSIGKeyRing(mod)) {
        return (NULL);
    }

851 852 853 854
    if (!initModulePart_TSIG(mod)) {
        return (NULL);
    }

855 856 857 858
    if (!initModulePart_TSIGError(mod)) {
        return (NULL);
    }

859 860 861 862
    if (!initModulePart_TSIGRecord(mod)) {
        return (NULL);
    }

863 864 865 866
    if (!initModulePart_TSIGContext(mod)) {
        return (NULL);
    }

Jelte Jansen's avatar
Jelte Jansen committed
867
    return (mod);
868
}