s3-smbldap: move ldap_open_with_timeout out of smb_ldap.h to ads where it lives.
[Samba/gebeck_regimport.git] / source4 / lib / messaging / pymessaging.c
blobd9ad3790f9995421af806213dcef9cf0c97e82c3
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
5 Based on the equivalent for EJS:
6 Copyright © Andrew Tridgell <tridge@samba.org> 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <Python.h>
23 #include "includes.h"
24 #include "scripting/python/modules.h"
25 #include "libcli/util/pyerrors.h"
26 #include "librpc/rpc/pyrpc_util.h"
27 #include "librpc/ndr/libndr.h"
28 #include "lib/messaging/messaging.h"
29 #include "lib/events/events.h"
30 #include "cluster/cluster.h"
31 #include "param/param.h"
32 #include "param/pyparam.h"
33 #include "librpc/rpc/dcerpc.h"
34 #include "librpc/gen_ndr/server_id.h"
36 void initmessaging(void);
38 extern PyTypeObject imessaging_Type;
40 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
42 if (!PyTuple_Check(object)) {
43 PyErr_SetString(PyExc_ValueError, "Expected tuple");
44 return false;
47 if (PyTuple_Size(object) == 3) {
48 return PyArg_ParseTuple(object, "iii", &server_id->pid, &server_id->task_id, &server_id->vnn);
49 } else {
50 int pid, task_id;
51 if (!PyArg_ParseTuple(object, "ii", &pid, &task_id))
52 return false;
53 *server_id = cluster_id(pid, task_id);
54 return true;
58 typedef struct {
59 PyObject_HEAD
60 TALLOC_CTX *mem_ctx;
61 struct imessaging_context *msg_ctx;
62 } imessaging_Object;
64 static PyObject *py_imessaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
66 struct tevent_context *ev;
67 const char *kwnames[] = { "own_id", "lp_ctx", NULL };
68 PyObject *own_id = Py_None;
69 PyObject *py_lp_ctx = Py_None;
70 imessaging_Object *ret;
71 struct loadparm_context *lp_ctx;
73 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO:connect",
74 discard_const_p(char *, kwnames), &own_id, &py_lp_ctx)) {
75 return NULL;
78 ret = PyObject_New(imessaging_Object, &imessaging_Type);
79 if (ret == NULL)
80 return NULL;
82 ret->mem_ctx = talloc_new(NULL);
84 lp_ctx = lpcfg_from_py_object(ret->mem_ctx, py_lp_ctx);
85 if (lp_ctx == NULL) {
86 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to interpret loadparm_context");
87 talloc_free(ret->mem_ctx);
88 return NULL;
91 ev = s4_event_context_init(ret->mem_ctx);
93 if (own_id != Py_None) {
94 struct server_id server_id;
96 if (!server_id_from_py(own_id, &server_id))
97 return NULL;
99 ret->msg_ctx = imessaging_init(ret->mem_ctx,
100 lp_ctx,
101 server_id,
102 ev, true);
103 } else {
104 ret->msg_ctx = imessaging_client_init(ret->mem_ctx,
105 lp_ctx,
106 ev);
109 if (ret->msg_ctx == NULL) {
110 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to create a messaging context");
111 talloc_free(ret->mem_ctx);
112 return NULL;
115 return (PyObject *)ret;
118 static void py_imessaging_dealloc(PyObject *self)
120 imessaging_Object *iface = (imessaging_Object *)self;
121 talloc_free(iface->msg_ctx);
122 self->ob_type->tp_free(self);
125 static PyObject *py_imessaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
127 imessaging_Object *iface = (imessaging_Object *)self;
128 uint32_t msg_type;
129 DATA_BLOB data;
130 PyObject *target;
131 NTSTATUS status;
132 struct server_id server;
133 const char *kwnames[] = { "target", "msg_type", "data", NULL };
134 int length;
136 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send",
137 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
139 return NULL;
142 data.length = length;
144 if (!server_id_from_py(target, &server))
145 return NULL;
147 status = imessaging_send(iface->msg_ctx, server, msg_type, &data);
148 if (NT_STATUS_IS_ERR(status)) {
149 PyErr_SetNTSTATUS(status);
150 return NULL;
153 Py_RETURN_NONE;
156 static void py_msg_callback_wrapper(struct imessaging_context *msg, void *private_data,
157 uint32_t msg_type,
158 struct server_id server_id, DATA_BLOB *data)
160 PyObject *callback = (PyObject *)private_data;
162 PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type,
163 server_id.pid, server_id.task_id, server_id.vnn,
164 data->data, data->length);
167 static PyObject *py_imessaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
169 imessaging_Object *iface = (imessaging_Object *)self;
170 int msg_type = -1;
171 PyObject *callback;
172 NTSTATUS status;
173 const char *kwnames[] = { "callback", "msg_type", NULL };
175 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register",
176 discard_const_p(char *, kwnames), &callback, &msg_type)) {
177 return NULL;
180 Py_INCREF(callback);
182 if (msg_type == -1) {
183 uint32_t msg_type32 = msg_type;
184 status = imessaging_register_tmp(iface->msg_ctx, callback,
185 py_msg_callback_wrapper, &msg_type32);
186 msg_type = msg_type32;
187 } else {
188 status = imessaging_register(iface->msg_ctx, callback,
189 msg_type, py_msg_callback_wrapper);
191 if (NT_STATUS_IS_ERR(status)) {
192 PyErr_SetNTSTATUS(status);
193 return NULL;
196 return PyLong_FromLong(msg_type);
199 static PyObject *py_imessaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
201 imessaging_Object *iface = (imessaging_Object *)self;
202 int msg_type = -1;
203 PyObject *callback;
204 const char *kwnames[] = { "callback", "msg_type", NULL };
206 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
207 discard_const_p(char *, kwnames), &callback, &msg_type)) {
208 return NULL;
211 imessaging_deregister(iface->msg_ctx, msg_type, callback);
213 Py_DECREF(callback);
215 Py_RETURN_NONE;
218 static PyMethodDef py_imessaging_methods[] = {
219 { "send", (PyCFunction)py_imessaging_send, METH_VARARGS|METH_KEYWORDS,
220 "S.send(target, msg_type, data) -> None\nSend a message" },
221 { "register", (PyCFunction)py_imessaging_register, METH_VARARGS|METH_KEYWORDS,
222 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
223 { "deregister", (PyCFunction)py_imessaging_deregister, METH_VARARGS|METH_KEYWORDS,
224 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
225 { NULL, NULL, 0, NULL }
228 static PyObject *py_imessaging_server_id(PyObject *obj, void *closure)
230 imessaging_Object *iface = (imessaging_Object *)obj;
231 struct server_id server_id = imessaging_get_server_id(iface->msg_ctx);
233 return Py_BuildValue("(iii)", server_id.pid, server_id.task_id,
234 server_id.vnn);
237 static PyGetSetDef py_imessaging_getset[] = {
238 { discard_const_p(char, "server_id"), py_imessaging_server_id, NULL,
239 discard_const_p(char, "local server id") },
240 { NULL },
244 PyTypeObject imessaging_Type = {
245 PyObject_HEAD_INIT(NULL) 0,
246 .tp_name = "messaging.Messaging",
247 .tp_basicsize = sizeof(imessaging_Object),
248 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
249 .tp_new = py_imessaging_connect,
250 .tp_dealloc = py_imessaging_dealloc,
251 .tp_methods = py_imessaging_methods,
252 .tp_getset = py_imessaging_getset,
253 .tp_doc = "Messaging(own_id=None, imessaging_path=None)\n" \
254 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
255 "If no path is specified, the default path from smb.conf will be used."
258 void initmessaging(void)
260 PyObject *mod;
262 if (PyType_Ready(&imessaging_Type) < 0)
263 return;
265 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
266 if (mod == NULL)
267 return;
269 Py_INCREF((PyObject *)&imessaging_Type);
270 PyModule_AddObject(mod, "Messaging", (PyObject *)&imessaging_Type);