Added a Python extension to send/receive file descriptors using libht.
[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
22#include <utils.h>
23#include <proc.h>
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);
35 if((ret = recvfd(fd, &data, &dlen)) < 0) {
36 if(errno == 0)
37 return(Py_BuildValue("OO", Py_None, Py_None));
38 PyErr_SetFromErrno(PyExc_OSError);
39 return(NULL);
40 }
41 ro = Py_BuildValue("Ni", PyString_FromStringAndSize(data, dlen), ret);
42 free(data);
43 return(ro);
44}
45
46static PyObject *p_sendfd(PyObject *self, PyObject *args)
47{
48 int sock, fd;
49 PyObject *data;
50
51 if(!PyArg_ParseTuple(args, "iiO", &sock, &fd, &data))
52 return(NULL);
53 if(!PyString_Check(data)) {
54 PyErr_SetString(PyExc_TypeError, "datagram must be a string");
55 return(NULL);
56 }
57 if(sendfd(sock, fd, PyString_AsString(data), PyString_Size(data)) < 0) {
58 PyErr_SetFromErrno(PyExc_OSError);
59 return(NULL);
60 }
61 Py_RETURN_NONE;
62}
63
64static PyMethodDef methods[] = {
65 {"recvfd", p_recvfd, METH_VARARGS, "Receive a datagram and a file descriptor"},
66 {"sendfd", p_sendfd, METH_VARARGS, "Send a datagram and a file descriptor"},
67 {NULL, NULL, 0, NULL}
68};
69
70PyMODINIT_FUNC inithtlib(void)
71{
72 Py_InitModule("ashd.htlib", methods);
73}