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/>.
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 PyAPI_DATA(PyTypeObject) messaging_Type;
38 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
40 if (!PyTuple_Check(object)) {
41 PyErr_SetString(PyExc_ValueError, "Expected tuple");
45 if (PyTuple_Size(object) == 3) {
46 return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
49 if (!PyArg_ParseTuple(object, "ii", &id, &id2))
51 *server_id = cluster_id(id, id2);
59 struct messaging_context *msg_ctx;
62 PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
64 struct tevent_context *ev;
65 const char *kwnames[] = { "own_id", "messaging_path", NULL };
66 PyObject *own_id = Py_None;
67 const char *messaging_path = NULL;
68 messaging_Object *ret;
70 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect",
71 discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
75 ret = PyObject_New(messaging_Object, &messaging_Type);
79 ret->mem_ctx = talloc_new(NULL);
81 ev = s4_event_context_init(ret->mem_ctx);
83 if (messaging_path == NULL) {
84 messaging_path = lpcfg_messaging_path(ret->mem_ctx,
85 py_default_loadparm_context(ret->mem_ctx));
87 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
90 if (own_id != Py_None) {
91 struct server_id server_id;
93 if (!server_id_from_py(own_id, &server_id))
96 ret->msg_ctx = messaging_init(ret->mem_ctx,
101 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
106 if (ret->msg_ctx == NULL) {
107 PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context");
108 talloc_free(ret->mem_ctx);
112 return (PyObject *)ret;
115 static void py_messaging_dealloc(PyObject *self)
117 messaging_Object *iface = (messaging_Object *)self;
118 talloc_free(iface->msg_ctx);
122 static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
124 messaging_Object *iface = (messaging_Object *)self;
129 struct server_id server;
130 const char *kwnames[] = { "target", "msg_type", "data", NULL };
133 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send",
134 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
139 data.length = length;
141 if (!server_id_from_py(target, &server))
144 status = messaging_send(iface->msg_ctx, server, msg_type, &data);
145 if (NT_STATUS_IS_ERR(status)) {
146 PyErr_SetNTSTATUS(status);
153 static void py_msg_callback_wrapper(struct messaging_context *msg, void *private_data,
155 struct server_id server_id, DATA_BLOB *data)
157 PyObject *callback = (PyObject *)private_data;
159 PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type,
160 server_id.id, server_id.id2, server_id.node,
161 data->data, data->length);
164 static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
166 messaging_Object *iface = (messaging_Object *)self;
170 const char *kwnames[] = { "callback", "msg_type", NULL };
172 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register",
173 discard_const_p(char *, kwnames), &callback, &msg_type)) {
179 if (msg_type == -1) {
180 uint32_t msg_type32 = msg_type;
181 status = messaging_register_tmp(iface->msg_ctx, callback,
182 py_msg_callback_wrapper, &msg_type32);
183 msg_type = msg_type32;
185 status = messaging_register(iface->msg_ctx, callback,
186 msg_type, py_msg_callback_wrapper);
188 if (NT_STATUS_IS_ERR(status)) {
189 PyErr_SetNTSTATUS(status);
193 return PyLong_FromLong(msg_type);
196 static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
198 messaging_Object *iface = (messaging_Object *)self;
201 const char *kwnames[] = { "callback", "msg_type", NULL };
203 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
204 discard_const_p(char *, kwnames), &callback, &msg_type)) {
208 messaging_deregister(iface->msg_ctx, msg_type, callback);
215 static PyMethodDef py_messaging_methods[] = {
216 { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS,
217 "S.send(target, msg_type, data) -> None\nSend a message" },
218 { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
219 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
220 { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
221 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
222 { NULL, NULL, 0, NULL }
225 static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
227 messaging_Object *iface = (messaging_Object *)obj;
228 struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
230 return Py_BuildValue("(iii)", server_id.id, server_id.id2,
234 static PyGetSetDef py_messaging_getset[] = {
235 { discard_const_p(char, "server_id"), py_messaging_server_id, NULL,
236 discard_const_p(char, "local server id") },
241 PyTypeObject messaging_Type = {
242 PyObject_HEAD_INIT(NULL) 0,
243 .tp_name = "messaging.Messaging",
244 .tp_basicsize = sizeof(messaging_Object),
245 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
246 .tp_new = py_messaging_connect,
247 .tp_dealloc = py_messaging_dealloc,
248 .tp_methods = py_messaging_methods,
249 .tp_getset = py_messaging_getset,
250 .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \
251 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
252 "If no path is specified, the default path from smb.conf will be used."
255 void initmessaging(void)
259 if (PyType_Ready(&messaging_Type) < 0)
262 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
266 Py_INCREF((PyObject *)&messaging_Type);
267 PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);