X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=python3%2Fhtp.c;fp=python3%2Fhtp.c;h=ec4ebabce26d0ebe36f008c7e87f27a0aea4bd82;hb=173e0e9efec5ae690cc157fe238113fcd814895e;hp=0000000000000000000000000000000000000000;hpb=188cd02daf85ef68a832deab4fcbf0daaf2d4573;p=ashd.git diff --git a/python3/htp.c b/python3/htp.c new file mode 100644 index 0000000..ec4ebab --- /dev/null +++ b/python3/htp.c @@ -0,0 +1,83 @@ +/* + ashd - A Sane HTTP Daemon + Copyright (C) 2008 Fredrik Tolf + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include + +#include +#include + +static PyObject *p_recvfd(PyObject *self, PyObject *args) +{ + int fd, ret; + char *data; + size_t dlen; + PyObject *ro; + + fd = 0; + if(!PyArg_ParseTuple(args, "|i", &fd)) + return(NULL); + Py_BEGIN_ALLOW_THREADS; + ret = recvfd(fd, &data, &dlen); + Py_END_ALLOW_THREADS; + if(ret < 0) { + if(errno == 0) + return(Py_BuildValue("OO", Py_None, Py_None)); + PyErr_SetFromErrno(PyExc_OSError); + return(NULL); + } + ro = Py_BuildValue("Ni", PyBytes_FromStringAndSize(data, dlen), ret); + free(data); + return(ro); +} + +static PyObject *p_sendfd(PyObject *self, PyObject *args) +{ + int sock, fd, ret; + Py_buffer data; + + if(!PyArg_ParseTuple(args, "iiy*", &sock, &fd, &data)) + return(NULL); + Py_BEGIN_ALLOW_THREADS; + ret = sendfd(sock, fd, data.buf, data.len); + Py_END_ALLOW_THREADS; + PyBuffer_Release(&data); + if(ret < 0) { + PyErr_SetFromErrno(PyExc_OSError); + return(NULL); + } + Py_RETURN_NONE; +} + +static PyMethodDef methods[] = { + {"recvfd", p_recvfd, METH_VARARGS, "Receive a datagram and a file descriptor"}, + {"sendfd", p_sendfd, METH_VARARGS, "Send a datagram and a file descriptor"}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef module = { + PyModuleDef_HEAD_INIT, + .m_name = "htlib", + .m_size = -1, + .m_methods = methods, +}; + +PyMODINIT_FUNC PyInit_htlib(void) +{ + return(PyModule_Create(&module)); +}