python: Converted ashd.htlib to Python 3 API.
[ashd.git] / python / htp.c
CommitLineData
4942dacd
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <Python.h>
20#include <errno.h>
21
e8b7861a
FT
22#include <ashd/utils.h>
23#include <ashd/proc.h>
4942dacd
FT
24
25static PyObject *p_recvfd(PyObject *self, PyObject *args)
26{
27 int fd, ret;
28 char *data;
29 size_t dlen;
30 PyObject *ro;
31
32 fd = 0;
33 if(!PyArg_ParseTuple(args, "|i", &fd))
34 return(NULL);
b9f22456
FT
35 Py_BEGIN_ALLOW_THREADS;
36 ret = recvfd(fd, &data, &dlen);
37 Py_END_ALLOW_THREADS;
38 if(ret < 0) {
4942dacd
FT
39 if(errno == 0)
40 return(Py_BuildValue("OO", Py_None, Py_None));
41 PyErr_SetFromErrno(PyExc_OSError);
42 return(NULL);
43 }
5fc54882 44 ro = Py_BuildValue("Ni", PyBytes_FromStringAndSize(data, dlen), ret);
4942dacd
FT
45 free(data);
46 return(ro);
47}
48
49static PyObject *p_sendfd(PyObject *self, PyObject *args)
50{
b9f22456 51 int sock, fd, ret;
5fc54882 52 Py_buffer data;
4942dacd 53
5fc54882 54 if(!PyArg_ParseTuple(args, "iiy*", &sock, &fd, &data))
4942dacd 55 return(NULL);
b9f22456 56 Py_BEGIN_ALLOW_THREADS;
5fc54882 57 ret = sendfd(sock, fd, data.buf, data.len);
b9f22456 58 Py_END_ALLOW_THREADS;
5fc54882 59 PyBuffer_Release(&data);
b9f22456 60 if(ret < 0) {
4942dacd
FT
61 PyErr_SetFromErrno(PyExc_OSError);
62 return(NULL);
63 }
64 Py_RETURN_NONE;
65}
66
67static PyMethodDef methods[] = {
68 {"recvfd", p_recvfd, METH_VARARGS, "Receive a datagram and a file descriptor"},
69 {"sendfd", p_sendfd, METH_VARARGS, "Send a datagram and a file descriptor"},
70 {NULL, NULL, 0, NULL}
71};
72
5fc54882
FT
73static struct PyModuleDef module = {
74 PyModuleDef_HEAD_INIT,
75 .m_name = "htlib",
76 .m_size = -1,
77 .m_methods = methods,
78};
79
80PyMODINIT_FUNC PyInit_htlib(void)
4942dacd 81{
5fc54882 82 return(PyModule_Create(&module));
4942dacd 83}