Skip to content

Commit 6fa6c59

Browse files
committed
added interfaces to fd polling
1 parent 9e83d73 commit 6fa6c59

File tree

4 files changed

+140
-2
lines changed

4 files changed

+140
-2
lines changed

fd_poller.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

getdns.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,9 @@ static struct PyMethodDef getdns_methods[] = {
12901290
{ "context_set_edns_version", (PyCFunction)context_set_edns_version, METH_KEYWORDS },
12911291
{ "context_set_edns_do_bit", (PyCFunction)context_set_edns_do_bit, METH_KEYWORDS },
12921292
{ "context_get_api_information", (PyCFunction)context_get_api_information, METH_KEYWORDS },
1293+
{ "context_fd", (PyCFunction)context_fd, METH_KEYWORDS },
1294+
{ "context_get_num_pending_requests", (PyCFunction)context_get_num_pending_requests, METH_KEYWORDS },
1295+
{ "context_process_async", (PyCFunction)context_process_async, METH_KEYWORDS },
12931296
{ 0, 0, 0 }
12941297
};
12951298

pygetdns.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ PyObject *decode_getdns_response(struct getdns_dict *);
3939
PyObject *decode_getdns_replies_tree_response(struct getdns_dict *response);
4040
PyObject *getFullResponse(struct getdns_dict *dict);
4141
char *reverse_address(struct getdns_bindata *address_data);
42-
42+
PyObject *context_fd(PyObject *self, PyObject *args, PyObject *keywds);
43+
PyObject *context_get_num_pending_requests(PyObject *self, PyObject *args, PyObject *keywds);
44+
PyObject *context_process_async(PyObject *self, PyObject *args, PyObject *keywds);
4345

4446
typedef struct pygetdns_libevent_callback_data {
4547
char *callback_func;

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
include_dirs = [ '/usr/local/include', ],
1616
libraries = [ 'ldns', 'getdns', 'getdns_ext_event' ],
1717
library_dirs = [ '/usr/local/lib' ],
18-
sources = [ 'getdns.c', 'pygetdns_util.c' ],
18+
sources = [ 'getdns.c', 'pygetdns_util.c', 'fd_poller.c' ],
1919
runtime_library_dirs = [ '/usr/local/lib' ]
2020
)
2121

0 commit comments

Comments
 (0)