mirror of
https://github.com/jorisvink/kore
synced 2025-03-09 20:49:01 -04:00
expose connection address to python.
This commit is contained in:
parent
ed9a34ce95
commit
add6d724e3
@ -59,9 +59,11 @@ static PyMethodDef pyconnection_methods[] = {
|
||||
};
|
||||
|
||||
static PyObject *pyconnection_get_fd(struct pyconnection *, void *);
|
||||
static PyObject *pyconnection_get_addr(struct pyconnection *, void *);
|
||||
|
||||
static PyGetSetDef pyconnection_getset[] = {
|
||||
GETTER("fd", pyconnection_get_fd),
|
||||
GETTER("addr", pyconnection_get_addr),
|
||||
GETTER(NULL, NULL),
|
||||
};
|
||||
|
||||
|
30
src/python.c
30
src/python.c
@ -696,6 +696,36 @@ pyconnection_get_fd(struct pyconnection *pyc, void *closure)
|
||||
return (fd);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
pyconnection_get_addr(struct pyconnection *pyc, void *closure)
|
||||
{
|
||||
void *ptr;
|
||||
PyObject *result;
|
||||
char addr[INET6_ADDRSTRLEN];
|
||||
|
||||
switch (pyc->c->addrtype) {
|
||||
case AF_INET:
|
||||
ptr = &pyc->c->addr.ipv4.sin_addr;
|
||||
break;
|
||||
case AF_INET6:
|
||||
ptr = &pyc->c->addr.ipv6.sin6_addr;
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_RuntimeError, "invalid addrtype");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (inet_ntop(pyc->c->addrtype, ptr, addr, sizeof(addr)) == NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "inet_ntop failed");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((result = PyUnicode_FromString(addr)) == NULL)
|
||||
return (PyErr_NoMemory());
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
pyhttp_request_alloc(struct http_request *req)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user