The branch, master has been updated via 468fa95 s4 dns: Reduce test output noise by upping log level for dns_name_packet logging via e5ebda4 s4 dns: Add a first test case via ef4bda5 s4 dns: Turn on internal DNS server during testing via bd8aafc socket_wrapper: Added python interface to socket_wrapper from f7c8af7 samba.tests.dsdb: Import TestCase from samba.tests.
http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit 468fa95df2a682c39bd46c8e451961994b91f3c4 Author: Kai Blin <k...@samba.org> Date: Tue Nov 15 08:38:27 2011 +0100 s4 dns: Reduce test output noise by upping log level for dns_name_packet logging Autobuild-User: Kai Blin <k...@samba.org> Autobuild-Date: Thu Nov 17 20:10:05 CET 2011 on sn-devel-104 commit e5ebda41563ed6152bae9ffcf873eeee679878de Author: Kai Blin <k...@samba.org> Date: Fri Nov 11 00:32:09 2011 +0100 s4 dns: Add a first test case commit ef4bda5c767215f09c035192587a9a29e8a3754a Author: Kai Blin <k...@samba.org> Date: Tue Nov 8 00:34:01 2011 +0100 s4 dns: Turn on internal DNS server during testing commit bd8aafc530ba473acefd53665b73a47d1ebbb3a5 Author: Amitay Isaacs <ami...@gmail.com> Date: Fri Oct 14 17:24:16 2011 +1100 socket_wrapper: Added python interface to socket_wrapper The socket_wrapper does not support setting blocking flag or timeouts on the sockets. To use socket module in python, use from samba import socket Signed-off-by: Kai Blin <k...@samba.org> ----------------------------------------------------------------------- Summary of changes: lib/socket_wrapper/py_socket_wrapper.c | 876 +++++++++++++++++++++++++++ lib/socket_wrapper/socket.py | 52 ++ lib/socket_wrapper/wscript_build | 10 + selftest/target/Samba4.pm | 2 +- source4/dns_server/dns_server.c | 9 +- source4/scripting/python/samba/tests/dns.py | 117 ++++ source4/selftest/tests.py | 3 + 7 files changed, 1065 insertions(+), 4 deletions(-) create mode 100644 lib/socket_wrapper/py_socket_wrapper.c create mode 100644 lib/socket_wrapper/socket.py create mode 100644 source4/scripting/python/samba/tests/dns.py Changeset truncated at 500 lines: diff --git a/lib/socket_wrapper/py_socket_wrapper.c b/lib/socket_wrapper/py_socket_wrapper.c new file mode 100644 index 0000000..405a43a --- /dev/null +++ b/lib/socket_wrapper/py_socket_wrapper.c @@ -0,0 +1,876 @@ +/* + * Copyright (C) Amitay Isaacs 2011 + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the author nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/* + Python interface to socket wrapper library. + + Passes all socket communication over unix domain sockets if the environment + variable SOCKET_WRAPPER_DIR is set. +*/ + +#include <Python.h> +#include <pytalloc.h> +#include "replace/replace.h" +#include "system/network.h" +#include "socket_wrapper.h" + +#ifndef Py_RETURN_NONE +#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None +#endif + +#ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */ +#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) +#endif + +#ifndef PY_CHECK_TYPE +#define PY_CHECK_TYPE(type, var, fail) \ + if (!PyObject_TypeCheck(var, type)) {\ + PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \ + fail; \ + } +#endif + +staticforward PyTypeObject PySocket; + +static PyObject *py_socket_error; + +void initsocket_wrapper(void); + +static PyObject *py_socket_addr_to_tuple(struct sockaddr *addr, socklen_t len) +{ + char host[256]; + char service[8]; + int status; + PyObject *pyaddr; + + status = getnameinfo(addr, len, host, 255, service, 7, NI_NUMERICHOST|NI_NUMERICSERV); + if (status < 0) { + PyErr_SetString(py_socket_error, gai_strerror(status)); + return NULL; + } + + pyaddr = PyTuple_New(2); + if (pyaddr == NULL) { + return PyErr_NoMemory(); + } + + PyTuple_SetItem(pyaddr, 0, PyString_FromString(host)); + PyTuple_SetItem(pyaddr, 1, PyInt_FromLong(atoi(service))); + + return pyaddr; +} + +static bool py_socket_tuple_to_addr(PyObject *pyaddr, struct sockaddr *addr, socklen_t *len) +{ + const char *host; + char *service; + in_port_t port; + struct addrinfo *ainfo; + int status; + + if (!PyTuple_Check(pyaddr)) { + PyErr_SetString(PyExc_TypeError, "Expected a tuple"); + return false; + } + + if (!PyArg_ParseTuple(pyaddr, "sH", &host, &port)) { + return false; + } + + service = talloc_asprintf(NULL, "%d", port); + if (service == NULL) { + PyErr_NoMemory(); + return false; + } + + status = getaddrinfo(host, service, NULL, &ainfo); + if (status < 0) { + talloc_free(service); + PyErr_SetString(py_socket_error, gai_strerror(status)); + return false; + } + + talloc_free(service); + + memcpy(addr, ainfo->ai_addr, sizeof(struct sockaddr)); + *len = ainfo->ai_addrlen; + + freeaddrinfo(ainfo); + return true; +} + + +static PyObject *py_socket_accept(pytalloc_Object *self, PyObject *args) +{ + int *sock, *new_sock; + struct sockaddr addr; + socklen_t addrlen; + PyObject *pysocket; + PyObject *pyaddr; + PyObject *pyret; + + sock = pytalloc_get_ptr(self); + + new_sock = talloc_zero(NULL, int); + if (new_sock == NULL) { + return PyErr_NoMemory(); + } + + *new_sock = swrap_accept(*sock, &addr, &addrlen); + if (*new_sock < 0) { + return PyErr_SetFromErrno(py_socket_error); + } + + if ((pysocket = pytalloc_steal(&PySocket, new_sock)) == NULL) { + return PyErr_NoMemory(); + } + + pyret = PyTuple_New(2); + if (pyret == NULL) { + Py_DECREF(pysocket); + return PyErr_NoMemory(); + } + + pyaddr = py_socket_addr_to_tuple(&addr, addrlen); + if (pyaddr == NULL) { + Py_DECREF(pysocket); + Py_DECREF(pysocket); + return NULL; + } + + PyTuple_SetItem(pyret, 0, pysocket); + PyTuple_SetItem(pyret, 1, pyaddr); + return pyret; +} + +static PyObject *py_socket_bind(pytalloc_Object *self, PyObject *args) +{ + PyObject *pyaddr; + int *sock; + int status; + struct sockaddr addr; + socklen_t addrlen; + + if (!PyArg_ParseTuple(args, "O:bind", &pyaddr)) { + return NULL; + } + + if (!py_socket_tuple_to_addr(pyaddr, &addr, &addrlen)) { + return NULL; + } + + sock = pytalloc_get_ptr(self); + + status = swrap_bind(*sock, &addr, addrlen); + if (status < 0) { + PyErr_SetString(py_socket_error, "Unable to bind"); + return NULL; + } + + Py_RETURN_NONE; +} + +static PyObject *py_socket_close(pytalloc_Object *self, PyObject *args) +{ + int *sock; + int status; + + sock = pytalloc_get_ptr(self); + + status = swrap_close(*sock); + if (status < 0) { + return PyErr_SetFromErrno(py_socket_error); + } + + Py_RETURN_NONE; +} + +static PyObject *py_socket_connect(pytalloc_Object *self, PyObject *args) +{ + int *sock; + PyObject *pyaddr; + struct sockaddr addr; + socklen_t addrlen; + int status; + + if (!PyArg_ParseTuple(args, "O:connect", &pyaddr)) { + return NULL; + } + + if (!py_socket_tuple_to_addr(pyaddr, &addr, &addrlen)) { + return NULL; + } + + sock = pytalloc_get_ptr(self); + + status = swrap_connect(*sock, &addr, addrlen); + if (status < 0) { + PyErr_SetFromErrno(py_socket_error); + return NULL; + } + + Py_RETURN_NONE; +} + +static PyObject *py_socket_connect_ex(pytalloc_Object *self, PyObject *args) +{ + int *sock; + PyObject *pyaddr; + struct sockaddr addr; + socklen_t addrlen; + int status; + + if (!PyArg_ParseTuple(args, "O:connect", &pyaddr)) { + return NULL; + } + + if (!py_socket_tuple_to_addr(pyaddr, &addr, &addrlen)) { + return NULL; + } + + sock = pytalloc_get_ptr(self); + + status = swrap_connect(*sock, &addr, addrlen); + if (status < 0) { + return Py_BuildValue("%d", errno); + } + + return Py_BuildValue("%d", 0); +} + +static PyObject *py_socket_dup(pytalloc_Object *self, PyObject *args) +{ + int *sock, *new_sock; + PyObject *pysocket; + + sock = pytalloc_get_ptr(self); + + new_sock = talloc_zero(NULL, int); + if (new_sock == NULL) { + return PyErr_NoMemory(); + } + + *new_sock = swrap_dup(*sock); + if (*new_sock < 0) { + return PyErr_SetFromErrno(py_socket_error); + } + + pysocket = pytalloc_steal(&PySocket, new_sock); + if (pysocket == NULL) { + return PyErr_NoMemory(); + } + + return pysocket; +} + +static PyObject *py_socket_dup2(pytalloc_Object *self, PyObject *args) +{ + int *sock, *new_sock; + PyObject *pysocket; + int status; + + if (!PyArg_ParseTuple(args, "O", &pysocket)) { + return NULL; + } + + PY_CHECK_TYPE(&PySocket, pysocket, return NULL); + + sock = pytalloc_get_ptr(self); + new_sock = pytalloc_get_ptr(pysocket); + + status = swrap_dup2(*sock, *new_sock); + if (status < 0) { + return PyErr_SetFromErrno(py_socket_error); + } + + Py_RETURN_NONE; +} + +static PyObject *py_socket_fileno(pytalloc_Object *self, PyObject *args) +{ + PyErr_SetString(py_socket_error, "Not Supported"); + return NULL; +} + +static PyObject *py_socket_getpeername(pytalloc_Object *self, PyObject *args) +{ + int *sock; + struct sockaddr addr; + socklen_t addrlen; + int status; + PyObject *pyaddr; + + sock = pytalloc_get_ptr(self); + + status = swrap_getpeername(*sock, &addr, &addrlen); + if (status < 0) { + return PyErr_SetFromErrno(py_socket_error); + } + + pyaddr = py_socket_addr_to_tuple(&addr, addrlen); + + return pyaddr; +} + +static PyObject *py_socket_getsockname(pytalloc_Object *self, PyObject *args) +{ + int *sock; + struct sockaddr addr; + socklen_t addrlen; + int status; + PyObject *pyaddr; + + sock = pytalloc_get_ptr(self); + + status = swrap_getsockname(*sock, &addr, &addrlen); + if (status < 0) { + return PyErr_SetFromErrno(py_socket_error); + } + + pyaddr = py_socket_addr_to_tuple(&addr, addrlen); + + return pyaddr; +} + +static PyObject *py_socket_getsockopt(pytalloc_Object *self, PyObject *args) +{ + int level, optname; + int *sock; + socklen_t optlen = -1, newlen; + int optval; + bool is_integer = false; + char *buffer; + PyObject *pyret; + int status; + + if (!PyArg_ParseTuple(args, "ii|i:getsockopt", &level, &optname, &optlen)) { + return NULL; + } + + if (optlen < 0) { + optlen = sizeof(int); + is_integer = true; + } + + buffer = talloc_zero_array(NULL, char, optlen); + if (buffer == NULL) { + return PyErr_NoMemory(); + } + + sock = pytalloc_get_ptr(self); + + status = swrap_getsockopt(*sock, level, optname, (void *)buffer, &newlen); + if (status < 0) { + talloc_free(buffer); + return PyErr_SetFromErrno(py_socket_error); + } + + if (is_integer) { + optval = *(int *)buffer; + pyret = PyInt_FromLong(optval); + } else { + pyret = PyString_FromStringAndSize(buffer, optlen); + } + + talloc_free(buffer); + + return pyret; +} + +static PyObject *py_socket_gettimeout(pytalloc_Object *self, PyObject *args) +{ + PyErr_SetString(py_socket_error, "Not Supported"); + return NULL; +} + +static PyObject *py_socket_listen(pytalloc_Object *self, PyObject *args) +{ + int backlog; + int *sock; + int status; + + if (!PyArg_ParseTuple(args, "i:listen", &backlog)) { + return NULL; + } + + sock = pytalloc_get_ptr(self); + + status = swrap_listen(*sock, backlog); + if (status < 0) { + return PyErr_SetFromErrno(py_socket_error); + } + + Py_RETURN_NONE; +} + +static PyObject *py_socket_makefile(pytalloc_Object *self, PyObject *args) +{ + PyErr_SetString(py_socket_error, "Not Supported"); + return NULL; +} + +static PyObject *py_socket_read(pytalloc_Object *self, PyObject *args) +{ + int bufsize, len; + int *sock; + char *buffer; + PyObject *pyret; + + if (!PyArg_ParseTuple(args, "i:read", &bufsize)) { + return NULL; + } + + buffer = talloc_zero_array(NULL, char, bufsize); + if (buffer == NULL) { + return PyErr_NoMemory(); + } + + sock = pytalloc_get_ptr(self); + + len = swrap_read(*sock, buffer, bufsize); + if (len < 0) { + return PyErr_SetFromErrno(py_socket_error); + } + + pyret = PyString_FromStringAndSize(buffer, len); + + talloc_free(buffer); + + return pyret; +} + +static PyObject *py_socket_recv(pytalloc_Object *self, PyObject *args) +{ + int bufsize, flags, len; + int *sock; + char *buffer; + PyObject *pyret; + + if (!PyArg_ParseTuple(args, "ii:recv", &bufsize, &flags)) { + return NULL; + } + + buffer = talloc_zero_array(NULL, char, bufsize); + if (buffer == NULL) { + return PyErr_NoMemory(); + } + + sock = pytalloc_get_ptr(self); + + len = swrap_recv(*sock, buffer, bufsize, flags); + if (len < 0) { + return PyErr_SetFromErrno(py_socket_error); + } -- Samba Shared Repository