|
| 1 | +/** |
| 2 | + * |
| 3 | + * \file fd_poller.c |
| 4 | + * @brief pygetdns support for file descriptor polling |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | + |
| 9 | +/* |
| 10 | + * Copyright (c) 2014, Verisign, Inc. |
| 11 | + * All rights reserved. |
| 12 | + * |
| 13 | + * Redistribution and use in source and binary forms, with or without |
| 14 | + * modification, are permitted provided that the following conditions are met: |
| 15 | + * * Redistributions of source code must retain the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer. |
| 17 | + * * Redistributions in binary form must reproduce the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer in the |
| 19 | + * documentation and/or other materials provided with the distribution. |
| 20 | + * * Neither the name of the <organization> nor the |
| 21 | + * names of its contributors may be used to endorse or promote products |
| 22 | + * derived from this software without specific prior written permission. |
| 23 | + * |
| 24 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 25 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | + * DISCLAIMED. IN NO EVENT SHALL Verisign, Include. BE LIABLE FOR ANY |
| 28 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 31 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | + */ |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +#include <Python.h> |
| 39 | +#include <arpa/inet.h> |
| 40 | +#include <stdio.h> |
| 41 | +#include <string.h> |
| 42 | +#include <getdns/getdns.h> |
| 43 | +#include <getdns/getdns_ext_libevent.h> |
| 44 | +#include <event2/event.h> |
| 45 | +#include "pygetdns.h" |
| 46 | + |
| 47 | + |
| 48 | +PyObject * |
| 49 | +context_fd(PyObject *self, PyObject *args, PyObject *keywds) |
| 50 | +{ |
| 51 | + static char *kwlist[] = { |
| 52 | + "context", |
| 53 | + 0 |
| 54 | + }; |
| 55 | + |
| 56 | + PyObject *context_capsule; |
| 57 | + struct getdns_context *context; |
| 58 | + int fd; |
| 59 | + FILE *t; |
| 60 | + |
| 61 | + if (!PyArg_ParseTupleAndKeywords(args, keywds, "O", kwlist, |
| 62 | + &context_capsule)) { |
| 63 | + PyErr_SetString(getdns_error, GETDNS_RETURN_INVALID_PARAMETER_TEXT); |
| 64 | + return NULL; |
| 65 | + } |
| 66 | + context = PyCapsule_GetPointer(context_capsule, "context"); |
| 67 | + if ((fd = getdns_context_fd(context)) < 0) { |
| 68 | + PyErr_SetString(getdns_error, GETDNS_RETURN_INVALID_PARAMETER_TEXT); |
| 69 | + return NULL; |
| 70 | + } |
| 71 | + if ((t = fdopen(fd, "r")) == NULL) { |
| 72 | + PyErr_SetString(getdns_error, GETDNS_RETURN_GENERIC_ERROR_TEXT); |
| 73 | + return NULL; |
| 74 | + } |
| 75 | + return (PyFile_FromFile(t, "<context>", "r", NULL)); |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +PyObject * |
| 80 | +context_get_num_pending_requests(PyObject *self, PyObject *args, PyObject *keywds) |
| 81 | +{ |
| 82 | + static char *kwlist[] = { |
| 83 | + "context", |
| 84 | + "timeout", |
| 85 | + 0 |
| 86 | + }; |
| 87 | + |
| 88 | + PyObject *context_capsule; |
| 89 | + struct getdns_context *context; |
| 90 | + uint64_t timeout = 0; |
| 91 | + struct timeval tv; |
| 92 | + uint32_t n_requests; |
| 93 | + |
| 94 | + if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|L", kwlist, |
| 95 | + &context_capsule, &timeout)) { |
| 96 | + PyErr_SetString(getdns_error, GETDNS_RETURN_INVALID_PARAMETER_TEXT); |
| 97 | + return NULL; |
| 98 | + } |
| 99 | + context = PyCapsule_GetPointer(context_capsule, "context"); |
| 100 | + if (timeout) { |
| 101 | + tv.tv_sec = timeout / 1000; /* timeout is expressed in milliseconds */ |
| 102 | + tv.tv_usec = (timeout % 1000) * 1000; |
| 103 | + } |
| 104 | + return (PyInt_FromLong((long)getdns_context_get_num_pending_requests(context, &tv))); |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +PyObject * |
| 109 | +context_process_async(PyObject *self, PyObject *args, PyObject *keywds) |
| 110 | +{ |
| 111 | + static char *kwlist[] = { |
| 112 | + "context", |
| 113 | + 0 |
| 114 | + }; |
| 115 | + |
| 116 | + PyObject *context_capsule; |
| 117 | + struct getdns_context *context; |
| 118 | + getdns_return_t ret; |
| 119 | + char err_buf[256]; |
| 120 | + |
| 121 | + if (!PyArg_ParseTupleAndKeywords(args, keywds, "O", kwlist, |
| 122 | + &context_capsule)) { |
| 123 | + PyErr_SetString(getdns_error, GETDNS_RETURN_INVALID_PARAMETER_TEXT); |
| 124 | + return NULL; |
| 125 | + } |
| 126 | + context = PyCapsule_GetPointer(context_capsule, "context"); |
| 127 | + if ((ret = getdns_context_process_async(context)) != GETDNS_RETURN_GOOD) { |
| 128 | + getdns_strerror(ret, err_buf, sizeof err_buf); |
| 129 | + PyErr_SetString(getdns_error, err_buf); |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + return Py_None; |
| 133 | +} |
0 commit comments