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/>.
23 #include "python/py3compat.h"
25 #include "python/modules.h"
26 #include "libcli/util/pyerrors.h"
27 #include "librpc/rpc/pyrpc_util.h"
28 #include "librpc/ndr/libndr.h"
29 #include "lib/messaging/messaging.h"
30 #include "lib/messaging/irpc.h"
31 #include "lib/events/events.h"
32 #include "cluster/cluster.h"
33 #include "param/param.h"
34 #include "param/pyparam.h"
35 #include "librpc/rpc/dcerpc.h"
36 #include "librpc/gen_ndr/server_id.h"
38 #include "messaging_internal.h"
41 extern PyTypeObject imessaging_Type;
43 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
45 Py_ssize_t tuple_size;
47 if (!PyTuple_Check(object)) {
48 if (!py_check_dcerpc_type(object, "samba.dcerpc.server_id", "server_id")) {
50 PyErr_SetString(PyExc_ValueError, "Expected tuple or server_id");
53 *server_id = *pytalloc_get_type(object, struct server_id);
57 tuple_size = PyTuple_Size(object);
58 if (tuple_size == 3) {
59 unsigned long long pid;
62 if (!PyArg_ParseTuple(object, "Kii", &pid, &task_id, &vnn)) {
66 server_id->task_id = task_id;
69 } else if (tuple_size == 2) {
70 unsigned long long pid;
72 if (!PyArg_ParseTuple(object, "Ki", &pid, &task_id))
74 *server_id = cluster_id(pid, task_id);
76 } else if (tuple_size == 1) {
77 unsigned long long pid = getpid();
79 if (!PyArg_ParseTuple(object, "i", &task_id))
81 *server_id = cluster_id(pid, task_id);
84 PyErr_SetString(PyExc_ValueError, "Expected tuple containing one, two, or three elements");
92 struct imessaging_context *msg_ctx;
95 static PyObject *py_imessaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
97 struct tevent_context *ev;
98 const char *kwnames[] = { "own_id", "lp_ctx", NULL };
99 PyObject *own_id = Py_None;
100 PyObject *py_lp_ctx = Py_None;
101 imessaging_Object *ret;
102 struct loadparm_context *lp_ctx;
104 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO",
105 discard_const_p(char *, kwnames), &own_id, &py_lp_ctx)) {
109 ret = PyObject_New(imessaging_Object, &imessaging_Type);
113 ret->mem_ctx = talloc_new(NULL);
115 lp_ctx = lpcfg_from_py_object(ret->mem_ctx, py_lp_ctx);
116 if (lp_ctx == NULL) {
117 PyErr_SetString(PyExc_RuntimeError, "unable to interpret loadparm_context");
118 talloc_free(ret->mem_ctx);
122 ev = s4_event_context_init(ret->mem_ctx);
124 if (own_id != Py_None) {
125 struct server_id server_id;
127 if (!server_id_from_py(own_id, &server_id))
130 ret->msg_ctx = imessaging_init(ret->mem_ctx,
135 ret->msg_ctx = imessaging_client_init(ret->mem_ctx,
140 if (ret->msg_ctx == NULL) {
141 PyErr_SetString(PyExc_RuntimeError, "unable to create a messaging context");
142 talloc_free(ret->mem_ctx);
146 return (PyObject *)ret;
149 static void py_imessaging_dealloc(PyObject *self)
151 imessaging_Object *iface = (imessaging_Object *)self;
152 talloc_free(iface->msg_ctx);
153 self->ob_type->tp_free(self);
156 static PyObject *py_imessaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
158 imessaging_Object *iface = (imessaging_Object *)self;
163 struct server_id server;
164 const char *kwnames[] = { "target", "msg_type", "data", NULL };
167 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OIs#:send",
168 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
173 data.length = length;
175 if (!server_id_from_py(target, &server))
178 status = imessaging_send(iface->msg_ctx, server, msg_type, &data);
179 if (NT_STATUS_IS_ERR(status)) {
180 PyErr_SetNTSTATUS(status);
187 static void py_msg_callback_wrapper(struct imessaging_context *msg,
190 struct server_id server_id,
195 PyObject *py_server_id, *callback_and_tuple = (PyObject *)private_data;
196 PyObject *callback, *py_private;
198 struct server_id *p_server_id = NULL;
201 DBG_WARNING("Received %zu fds, ignoring message\n", num_fds);
205 p_server_id = talloc(NULL, struct server_id);
210 *p_server_id = server_id;
212 if (!PyArg_ParseTuple(callback_and_tuple, "OO",
218 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
219 talloc_unlink(NULL, p_server_id);
221 PyObject_CallFunction(callback, discard_const_p(char, "OiOs#"),
225 data->data, data->length);
228 static PyObject *py_imessaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
230 imessaging_Object *iface = (imessaging_Object *)self;
232 PyObject *callback_and_context;
234 const char *kwnames[] = { "callback_and_context", "msg_type", NULL };
236 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register",
237 discard_const_p(char *, kwnames),
238 &callback_and_context, &msg_type)) {
241 if (!PyTuple_Check(callback_and_context)
242 || PyTuple_Size(callback_and_context) != 2) {
243 PyErr_SetString(PyExc_ValueError, "Expected tuple of size 2 for callback_and_context");
247 Py_INCREF(callback_and_context);
249 if (msg_type == -1) {
250 uint32_t msg_type32 = msg_type;
251 status = imessaging_register_tmp(iface->msg_ctx, callback_and_context,
252 py_msg_callback_wrapper, &msg_type32);
253 msg_type = msg_type32;
255 status = imessaging_register(iface->msg_ctx, callback_and_context,
256 msg_type, py_msg_callback_wrapper);
258 if (NT_STATUS_IS_ERR(status)) {
259 Py_DECREF(callback_and_context);
260 PyErr_SetNTSTATUS(status);
264 return PyLong_FromLong(msg_type);
267 static PyObject *py_imessaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
269 imessaging_Object *iface = (imessaging_Object *)self;
272 const char *kwnames[] = { "callback", "msg_type", NULL };
275 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
276 discard_const_p(char *, kwnames), &callback, &msg_type)) {
280 removed = imessaging_deregister(iface->msg_ctx, msg_type, callback);
281 while (removed-- > 0) {
288 static void simple_timer_handler(struct tevent_context *ev,
289 struct tevent_timer *te,
290 struct timeval current_time,
296 static PyObject *py_imessaging_loop_once(PyObject *self, PyObject *args, PyObject *kwargs)
298 imessaging_Object *iface = (imessaging_Object *)self;
301 struct timeval next_event;
302 struct tevent_timer *timer = NULL;
303 const char *kwnames[] = { "timeout", NULL };
305 TALLOC_CTX *frame = talloc_stackframe();
307 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "d",
308 discard_const_p(char *, kwnames), &offset)) {
316 next_event = tevent_timeval_current_ofs(seconds, (int)(offset*1000000));
318 timer = tevent_add_timer(iface->msg_ctx->ev, frame, next_event, simple_timer_handler,
327 tevent_loop_once(iface->msg_ctx->ev);
334 static PyObject *py_irpc_add_name(PyObject *self, PyObject *args)
336 imessaging_Object *iface = (imessaging_Object *)self;
340 if (!PyArg_ParseTuple(args, "s", &server_name)) {
344 status = irpc_add_name(iface->msg_ctx, server_name);
345 if (!NT_STATUS_IS_OK(status)) {
346 PyErr_SetNTSTATUS(status);
353 static PyObject *py_irpc_remove_name(PyObject *self, PyObject *args)
355 imessaging_Object *iface = (imessaging_Object *)self;
358 if (!PyArg_ParseTuple(args, "s", &server_name)) {
362 irpc_remove_name(iface->msg_ctx, server_name);
367 static PyObject *py_irpc_servers_byname(PyObject *self, PyObject *args)
369 imessaging_Object *iface = (imessaging_Object *)self;
372 struct server_id *ids;
374 TALLOC_CTX *mem_ctx = talloc_new(NULL);
382 if (!PyArg_ParseTuple(args, "s", &server_name)) {
383 TALLOC_FREE(mem_ctx);
387 status = irpc_servers_byname(iface->msg_ctx, mem_ctx, server_name,
389 if (!NT_STATUS_IS_OK(status)) {
390 TALLOC_FREE(mem_ctx);
391 PyErr_SetString(PyExc_KeyError, "No such name");
395 pylist = PyList_New(num_ids);
396 if (pylist == NULL) {
397 TALLOC_FREE(mem_ctx);
401 for (i = 0; i < num_ids; i++) {
402 PyObject *py_server_id;
403 struct server_id *p_server_id = talloc(NULL, struct server_id);
408 *p_server_id = ids[i];
410 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
414 PyList_SetItem(pylist, i, py_server_id);
415 talloc_unlink(NULL, p_server_id);
417 TALLOC_FREE(mem_ctx);
421 static PyObject *py_irpc_all_servers(PyObject *self,
422 PyObject *Py_UNUSED(ignored))
424 imessaging_Object *iface = (imessaging_Object *)self;
427 struct irpc_name_records *records;
428 TALLOC_CTX *mem_ctx = talloc_new(NULL);
434 records = irpc_all_servers(iface->msg_ctx, mem_ctx);
435 if (records == NULL) {
439 pylist = PyList_New(records->num_records);
440 if (pylist == NULL) {
441 TALLOC_FREE(mem_ctx);
445 for (i = 0; i < records->num_records; i++) {
446 PyObject *py_name_record
447 = py_return_ndr_struct("samba.dcerpc.irpc",
451 if (!py_name_record) {
454 PyList_SetItem(pylist, i,
457 TALLOC_FREE(mem_ctx);
461 static PyMethodDef py_imessaging_methods[] = {
462 { "send", PY_DISCARD_FUNC_SIG(PyCFunction, py_imessaging_send),
463 METH_VARARGS|METH_KEYWORDS,
464 "S.send(target, msg_type, data) -> None\nSend a message" },
465 { "register", PY_DISCARD_FUNC_SIG(PyCFunction, py_imessaging_register),
466 METH_VARARGS|METH_KEYWORDS,
467 "S.register((callback, context), msg_type=None) -> msg_type\nRegister a message handler. "
468 "The callback and context must be supplied as a two-element tuple." },
469 { "deregister", PY_DISCARD_FUNC_SIG(PyCFunction,
470 py_imessaging_deregister),
471 METH_VARARGS|METH_KEYWORDS,
472 "S.deregister((callback, context), msg_type) -> None\nDeregister a message handler "
473 "The callback and context must be supplied as the exact same two-element tuple "
474 "as was used as registration time." },
475 { "loop_once", PY_DISCARD_FUNC_SIG(PyCFunction,
476 py_imessaging_loop_once),
477 METH_VARARGS|METH_KEYWORDS,
478 "S.loop_once(timeout) -> None\n"
479 "Loop on the internal event context until we get an event "
480 "(which might be a message calling the callback), "
481 "timeout after timeout seconds (if not 0)" },
482 { "irpc_add_name", (PyCFunction)py_irpc_add_name, METH_VARARGS,
483 "S.irpc_add_name(name) -> None\n"
484 "Add this context to the list of server_id values that "
485 "are registered for a particular name" },
486 { "irpc_remove_name", (PyCFunction)py_irpc_remove_name, METH_VARARGS,
487 "S.irpc_remove_name(name) -> None\n"
488 "Remove this context from the list of server_id values that "
489 "are registered for a particular name" },
490 { "irpc_servers_byname", (PyCFunction)py_irpc_servers_byname, METH_VARARGS,
491 "S.irpc_servers_byname(name) -> list\nGet list of server_id values that are registered for a particular name" },
492 { "irpc_all_servers", (PyCFunction)py_irpc_all_servers, METH_NOARGS,
493 "S.irpc_all_servers() -> list\n"
494 "Get list of all registered names and the associated server_id values" },
495 { NULL, NULL, 0, NULL }
498 static PyObject *py_imessaging_server_id(PyObject *obj, void *closure)
500 imessaging_Object *iface = (imessaging_Object *)obj;
501 PyObject *py_server_id;
502 struct server_id server_id = imessaging_get_server_id(iface->msg_ctx);
503 struct server_id *p_server_id = talloc(NULL, struct server_id);
508 *p_server_id = server_id;
510 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
511 talloc_unlink(NULL, p_server_id);
516 static PyGetSetDef py_imessaging_getset[] = {
518 .name = discard_const_p(char, "server_id"),
519 .get = py_imessaging_server_id,
520 .doc = discard_const_p(char, "local server id")
526 PyTypeObject imessaging_Type = {
527 PyVarObject_HEAD_INIT(NULL, 0)
528 .tp_name = "messaging.Messaging",
529 .tp_basicsize = sizeof(imessaging_Object),
530 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
531 .tp_new = py_imessaging_connect,
532 .tp_dealloc = py_imessaging_dealloc,
533 .tp_methods = py_imessaging_methods,
534 .tp_getset = py_imessaging_getset,
535 .tp_doc = "Messaging(own_id=None, lp_ctx=None)\n" \
536 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n"
539 static struct PyModuleDef moduledef = {
540 PyModuleDef_HEAD_INIT,
541 .m_name = "messaging",
542 .m_doc = "Internal RPC",
547 MODULE_INIT_FUNC(messaging)
551 if (PyType_Ready(&imessaging_Type) < 0)
554 mod = PyModule_Create(&moduledef);
558 Py_INCREF((PyObject *)&imessaging_Type);
559 PyModule_AddObject(mod, "Messaging", (PyObject *)&imessaging_Type);
560 PyModule_AddObject(mod, "IRPC_CALL_TIMEOUT", PyLong_FromLong(IRPC_CALL_TIMEOUT));
561 PyModule_AddObject(mod, "IRPC_CALL_TIMEOUT_INF", PyLong_FromLong(IRPC_CALL_TIMEOUT_INF));