rrclass_python.cc 13.6 KB
Newer Older
1
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// 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.

// $Id$

#include <dns/rrclass.h>
using namespace isc::dns;

//
// Declaration of the custom exceptions
// Initialization and addition of these go in the initModulePart
// function at the end of this file
//
static PyObject* po_InvalidRRClass;
static PyObject* po_IncompleteRRClass;

//
// Definition of the classes
//

// For each class, we need a struct, a helper functions (init, destroy,
// and static wrappers around the methods we export), a list of methods,
// and a type description

//
// RRClass
//

40
// The s_* Class simply covers one instantiation of the object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
typedef struct {
    PyObject_HEAD
    RRClass* rrclass;
} s_RRClass;

//
// We declare the functions here, the definitions are below
// the type definition of the object, since both can use the other
//

// General creation and destruction
static int RRClass_init(s_RRClass* self, PyObject* args);
static void RRClass_destroy(s_RRClass* self);

// These are the functions we export
static PyObject* RRClass_toText(s_RRClass* self);
// This is a second version of toText, we need one where the argument
// is a PyObject*, for the str() function in python.
static PyObject* RRClass_str(PyObject* self);
static PyObject* RRClass_toWire(s_RRClass* self, PyObject* args);
static PyObject* RRClass_getCode(s_RRClass* self);
static PyObject* RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op);

Jelte Jansen's avatar
Jelte Jansen committed
64
65
66
67
68
69
70
71
// Static function for direct class creation
static PyObject* RRClass_IN(s_RRClass *self);
static PyObject* RRClass_CH(s_RRClass *self);
static PyObject* RRClass_HS(s_RRClass *self);
static PyObject* RRClass_NONE(s_RRClass *self);
static PyObject* RRClass_ANY(s_RRClass *self);


72
73
74
75
76
77
78
// This list contains the actual set of functions we have in
// python. Each entry has
// 1. Python method name
// 2. Our static function here
// 3. Argument type
// 4. Documentation
static PyMethodDef RRClass_methods[] = {
Jelte Jansen's avatar
Jelte Jansen committed
79
80
81
82
83
84
85
86
87
88
89
    { "to_text", (PyCFunction)RRClass_toText, METH_NOARGS,
      "Returns the string representation" },
    { "to_wire", (PyCFunction)RRClass_toWire, METH_VARARGS,
      "Converts the RRClass object to wire format.\n"
      "The argument can be either a MessageRenderer or an object that "
      "implements the sequence interface. If the object is mutable "
      "(for instance a bytearray()), the wire data is added in-place.\n"
      "If it is not (for instance a bytes() object), a new object is "
      "returned" },
    { "get_code", (PyCFunction)RRClass_getCode, METH_NOARGS,
      "Returns the class code as an integer" },
Jelte Jansen's avatar
Jelte Jansen committed
90
91
92
93
94
    { "IN", (PyCFunction)RRClass_IN, METH_NOARGS | METH_STATIC, "Creates an IN RRClass" },
    { "CH", (PyCFunction)RRClass_CH, METH_NOARGS | METH_STATIC, "Creates a CH RRClass" },
    { "HS", (PyCFunction)RRClass_HS, METH_NOARGS | METH_STATIC, "Creates an HS RRClass" },
    { "NONE", (PyCFunction)RRClass_NONE, METH_NOARGS | METH_STATIC, "Creates a NONE RRClass" },
    { "ANY", (PyCFunction)RRClass_ANY, METH_NOARGS | METH_STATIC, "Creates an ANY RRClass" },
95
96
97
98
99
100
101
102
103
    { NULL, NULL, 0, NULL }
};

// This defines the complete type for reflection in python and
// parsing of PyObject* to s_RRClass
// Most of the functions are not actually implemented and NULL here.
static PyTypeObject rrclass_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "libdns_python.RRClass",
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    sizeof(s_RRClass),                  // tp_basicsize
    0,                                  // tp_itemsize
    (destructor)RRClass_destroy,        // tp_dealloc
    NULL,                               // tp_print
    NULL,                               // tp_getattr
    NULL,                               // tp_setattr
    NULL,                               // tp_reserved
    NULL,                               // tp_repr
    NULL,                               // tp_as_number
    NULL,                               // tp_as_sequence
    NULL,                               // tp_as_mapping
    NULL,                               // tp_hash 
    NULL,                               // tp_call
    RRClass_str,                        // tp_str
    NULL,                               // tp_getattro
    NULL,                               // tp_setattro
    NULL,                               // tp_as_buffer
    Py_TPFLAGS_DEFAULT,                 // tp_flags
Jelte Jansen's avatar
Jelte Jansen committed
122
123
124
125
    "The RRClass class encapsulates DNS resource record classes.\n"
    "This class manages the 16-bit integer class codes in quite a straightforward"
    "way.  The only non trivial task is to handle textual representations of"
    "RR classes, such as \"IN\", \"CH\", or \"CLASS65534\".",
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    NULL,                               // tp_traverse
    NULL,                               // tp_clear
    (richcmpfunc)RRClass_richcmp,       // tp_richcompare
    0,                                  // tp_weaklistoffset
    NULL,                               // tp_iter
    NULL,                               // tp_iternext
    RRClass_methods,                    // tp_methods
    NULL,                               // tp_members
    NULL,                               // tp_getset
    NULL,                               // tp_base
    NULL,                               // tp_dict
    NULL,                               // tp_descr_get
    NULL,                               // tp_descr_set
    0,                                  // tp_dictoffset
    (initproc)RRClass_init,             // tp_init
    NULL,                               // tp_alloc
    PyType_GenericNew,                  // tp_new
    NULL,                               // tp_free
    NULL,                               // tp_is_gc
    NULL,                               // tp_bases
    NULL,                               // tp_mro
    NULL,                               // tp_cache
    NULL,                               // tp_subclasses
    NULL,                               // tp_weaklist
150
151
    // Note: not sure if the following are correct.  Added them just to
    // make the compiler happy.
152
153
    NULL,                               // tp_del
    0                                   // tp_version_tag
154
155
156
};

static int
157
RRClass_init(s_RRClass* self, PyObject* args) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    const char* s;
    unsigned int i;
    PyObject* bytes = NULL;
    // The constructor argument can be a string ("IN"), an integer (1),
    // or a sequence of numbers between 0 and 255 (wire code)

    // Note that PyArg_ParseType can set PyError, and we need to clear
    // that if we try several like here. Otherwise the *next* python
    // call will suddenly appear to throw an exception.
    // (the way to do exceptions is to set PyErr and return -1)
    try {
        if (PyArg_ParseTuple(args, "s", &s)) {
            self->rrclass = new RRClass(s);
            return 0;
        } else if (PyArg_ParseTuple(args, "I", &i)) {
            PyErr_Clear();
            if (i > 65535) {
                PyErr_SetString(po_InvalidRRClass, "Class number too high");
                return -1;
            }
            self->rrclass = new RRClass(i);
            return 0;
        } else if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
            uint8_t data[2];
            int result = readDataFromSequence(data, 2, bytes);
            if (result != 0) {
                return result;
            }
            InputBuffer ib(data, 2);
            self->rrclass = new RRClass(ib);
            PyErr_Clear();
            return 0;
        }
191
192
    // Incomplete is never thrown, a type error would have already been raised
    //when we try to read the 2 bytes above
193
194
195
196
197
198
199
200
201
202
203
204
    } catch (InvalidRRClass ic) {
        PyErr_Clear();
        PyErr_SetString(po_InvalidRRClass, ic.what());
        return -1;
    }
    PyErr_Clear();
    PyErr_SetString(PyExc_TypeError,
                    "no valid type in constructor argument");
    return -1;
}

static void
205
RRClass_destroy(s_RRClass* self) {
206
207
208
209
210
211
212
    if (self->rrclass != NULL)
        delete self->rrclass;
    self->rrclass = NULL;
    Py_TYPE(self)->tp_free(self);
}

static PyObject*
213
RRClass_toText(s_RRClass* self) {
214
215
216
217
218
    // Py_BuildValue makes python objects from native data
    return Py_BuildValue("s", self->rrclass->toText().c_str());
}

static PyObject*
219
RRClass_str(PyObject* self) {
220
221
222
223
224
    // Simply call the to_text method we already defined
    return PyObject_CallMethod(self, (char*)"to_text", (char*)"");
}

static PyObject*
225
RRClass_toWire(s_RRClass* self, PyObject* args) {
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    PyObject* bytes;
    s_MessageRenderer* mr;
    
    if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
        PyObject* bytes_o = bytes;
        
        OutputBuffer buffer(2);
        self->rrclass->toWire(buffer);
        PyObject* n = PyBytes_FromStringAndSize((const char*) buffer.getData(), buffer.getLength());
        PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
        // We need to release the object we temporarily created here
        // to prevent memory leak
        Py_DECREF(n);
        return result;
    } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, (PyObject**) &mr)) {
        self->rrclass->toWire(*mr->messagerenderer);
        // If we return NULL it is seen as an error, so use this for
        // None returns
        Py_RETURN_NONE;
    }
    PyErr_Clear();
    PyErr_SetString(PyExc_TypeError,
                    "toWire argument must be a sequence object or a MessageRenderer");
    return NULL;
}

static PyObject*
253
RRClass_getCode(s_RRClass* self) {
254
255
256
257
    return Py_BuildValue("I", self->rrclass->getCode());
}

static PyObject* 
258
RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op) {
259
260
    bool c;

261
262
263
264
265
266
267
268
    // Check for null and if the types match. If different type,
    // simply return False
    if (!other ||
        ((PyObject*)self)->ob_type != ((PyObject*)other)->ob_type
       ) {
        Py_RETURN_FALSE;
    }

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    switch (op) {
    case Py_LT:
        c = *self->rrclass < *other->rrclass;
        break;
    case Py_LE:
        c = *self->rrclass < *other->rrclass ||
            *self->rrclass == *other->rrclass;
        break;
    case Py_EQ:
        c = self->rrclass->equals(*other->rrclass);
        break;
    case Py_NE:
        c = self->rrclass->nequals(*other->rrclass);
        break;
    case Py_GT:
        c = *other->rrclass < *self->rrclass;
        break;
    case Py_GE:
        c = *other->rrclass < *self->rrclass ||
            *self->rrclass == *other->rrclass;
        break;
    default:
        assert(0);              // XXX: should trigger an exception
    }
    if (c)
        Py_RETURN_TRUE;
    else
        Py_RETURN_FALSE;
}
Jelte Jansen's avatar
Jelte Jansen committed
298

299
static PyObject* RRClass_IN(s_RRClass *self UNUSED_PARAM) {
Jelte Jansen's avatar
Jelte Jansen committed
300
301
302
303
304
305
306
307
308
309
310
    s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
    if (ret != NULL) {
        ret->rrclass = new RRClass(RRClass::IN());
        if (ret->rrclass == NULL) {
            Py_DECREF(ret);
            return NULL;
        }
    }
    return (PyObject*) ret;
}

311
static PyObject* RRClass_CH(s_RRClass *self UNUSED_PARAM) {
Jelte Jansen's avatar
Jelte Jansen committed
312
313
314
315
316
317
318
319
320
321
322
    s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
    if (ret != NULL) {
        ret->rrclass = new RRClass(RRClass::CH());
        if (ret->rrclass == NULL) {
            Py_DECREF(ret);
            return NULL;
        }
    }
    return (PyObject*) ret;
}

323
static PyObject* RRClass_HS(s_RRClass *self UNUSED_PARAM) {
Jelte Jansen's avatar
Jelte Jansen committed
324
325
326
327
328
329
330
331
332
333
334
    s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
    if (ret != NULL) {
        ret->rrclass = new RRClass(RRClass::HS());
        if (ret->rrclass == NULL) {
            Py_DECREF(ret);
            return NULL;
        }
    }
    return (PyObject*) ret;
}

335
static PyObject* RRClass_NONE(s_RRClass *self UNUSED_PARAM) {
Jelte Jansen's avatar
Jelte Jansen committed
336
337
338
339
340
341
342
343
344
345
346
    s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
    if (ret != NULL) {
        ret->rrclass = new RRClass(RRClass::NONE());
        if (ret->rrclass == NULL) {
            Py_DECREF(ret);
            return NULL;
        }
    }
    return (PyObject*) ret;
}

347
static PyObject* RRClass_ANY(s_RRClass *self UNUSED_PARAM) {
Jelte Jansen's avatar
Jelte Jansen committed
348
349
350
351
352
353
354
355
356
357
    s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
    if (ret != NULL) {
        ret->rrclass = new RRClass(RRClass::ANY());
        if (ret->rrclass == NULL) {
            Py_DECREF(ret);
            return NULL;
        }
    }
    return (PyObject*) ret;
}
358
359
360
361
362
// end of RRClass


// Module Initialization, all statics are initialized here
bool
363
initModulePart_RRClass(PyObject* mod) {
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    // Add the exceptions to the module
    po_InvalidRRClass = PyErr_NewException("libdns_python.InvalidRRClass", NULL, NULL);
    Py_INCREF(po_InvalidRRClass);
    PyModule_AddObject(mod, "InvalidRRClass", po_InvalidRRClass);
    po_IncompleteRRClass = PyErr_NewException("libdns_python.IncompleteRRClass", NULL, NULL);
    Py_INCREF(po_IncompleteRRClass);
    PyModule_AddObject(mod, "IncompleteRRClass", po_IncompleteRRClass);

    // We initialize the static description object with PyType_Ready(),
    // then add it to the module. This is not just a check! (leaving
    // this out results in segmentation faults)
    if (PyType_Ready(&rrclass_type) < 0) {
        return false;
    }
    Py_INCREF(&rrclass_type);
    PyModule_AddObject(mod, "RRClass",
                       (PyObject*) &rrclass_type);
    
    return true;
}