pymessaging: Add support for irpc_add_name
[Samba.git] / source4 / lib / messaging / pymessaging.c
blob5c20c186fafa426db08574e01b705e0050b6f945
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 "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/messaging/irpc.h"
30 #include "lib/events/events.h"
31 #include "cluster/cluster.h"
32 #include "param/param.h"
33 #include "param/pyparam.h"
34 #include "librpc/rpc/dcerpc.h"
35 #include "librpc/gen_ndr/server_id.h"
36 #include <pytalloc.h>
38 void initmessaging(void);
40 extern PyTypeObject imessaging_Type;
42 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
44 if (!PyTuple_Check(object)) {
45 if (!py_check_dcerpc_type(object, "samba.dcerpc.server_id", "server_id")) {
47 PyErr_SetString(PyExc_ValueError, "Expected tuple or server_id");
48 return false;
50 *server_id = *pytalloc_get_type(object, struct server_id);
51 return true;
53 if (PyTuple_Size(object) == 3) {
54 unsigned long long pid;
55 int task_id, vnn;
57 if (!PyArg_ParseTuple(object, "KII", &pid, &task_id, &vnn)) {
58 return false;
60 server_id->pid = pid;
61 server_id->task_id = task_id;
62 server_id->vnn = vnn;
63 return true;
64 } else {
65 unsigned long long pid;
66 int task_id;
67 if (!PyArg_ParseTuple(object, "KI", &pid, &task_id))
68 return false;
69 *server_id = cluster_id(pid, task_id);
70 return true;
74 typedef struct {
75 PyObject_HEAD
76 TALLOC_CTX *mem_ctx;
77 struct imessaging_context *msg_ctx;
78 } imessaging_Object;
80 static PyObject *py_imessaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
82 struct tevent_context *ev;
83 const char *kwnames[] = { "own_id", "lp_ctx", NULL };
84 PyObject *own_id = Py_None;
85 PyObject *py_lp_ctx = Py_None;
86 imessaging_Object *ret;
87 struct loadparm_context *lp_ctx;
89 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO:connect",
90 discard_const_p(char *, kwnames), &own_id, &py_lp_ctx)) {
91 return NULL;
94 ret = PyObject_New(imessaging_Object, &imessaging_Type);
95 if (ret == NULL)
96 return NULL;
98 ret->mem_ctx = talloc_new(NULL);
100 lp_ctx = lpcfg_from_py_object(ret->mem_ctx, py_lp_ctx);
101 if (lp_ctx == NULL) {
102 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to interpret loadparm_context");
103 talloc_free(ret->mem_ctx);
104 return NULL;
107 ev = s4_event_context_init(ret->mem_ctx);
109 if (own_id != Py_None) {
110 struct server_id server_id;
112 if (!server_id_from_py(own_id, &server_id))
113 return NULL;
115 ret->msg_ctx = imessaging_init(ret->mem_ctx,
116 lp_ctx,
117 server_id,
118 ev);
119 } else {
120 ret->msg_ctx = imessaging_client_init(ret->mem_ctx,
121 lp_ctx,
122 ev);
125 if (ret->msg_ctx == NULL) {
126 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to create a messaging context");
127 talloc_free(ret->mem_ctx);
128 return NULL;
131 return (PyObject *)ret;
134 static void py_imessaging_dealloc(PyObject *self)
136 imessaging_Object *iface = (imessaging_Object *)self;
137 talloc_free(iface->msg_ctx);
138 self->ob_type->tp_free(self);
141 static PyObject *py_imessaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
143 imessaging_Object *iface = (imessaging_Object *)self;
144 uint32_t msg_type;
145 DATA_BLOB data;
146 PyObject *target;
147 NTSTATUS status;
148 struct server_id server;
149 const char *kwnames[] = { "target", "msg_type", "data", NULL };
150 Py_ssize_t length;
152 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send",
153 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
155 return NULL;
158 data.length = length;
160 if (!server_id_from_py(target, &server))
161 return NULL;
163 status = imessaging_send(iface->msg_ctx, server, msg_type, &data);
164 if (NT_STATUS_IS_ERR(status)) {
165 PyErr_SetNTSTATUS(status);
166 return NULL;
169 Py_RETURN_NONE;
172 static void py_msg_callback_wrapper(struct imessaging_context *msg, void *private_data,
173 uint32_t msg_type,
174 struct server_id server_id, DATA_BLOB *data)
176 PyObject *py_server_id, *callback = (PyObject *)private_data;
178 struct server_id *p_server_id = talloc(NULL, struct server_id);
179 if (!p_server_id) {
180 PyErr_NoMemory();
181 return;
183 *p_server_id = server_id;
185 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
186 talloc_unlink(NULL, p_server_id);
188 PyObject_CallFunction(callback, discard_const_p(char, "i(O)s#"), msg_type,
189 py_server_id,
190 data->data, data->length);
193 static PyObject *py_imessaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
195 imessaging_Object *iface = (imessaging_Object *)self;
196 int msg_type = -1;
197 PyObject *callback;
198 NTSTATUS status;
199 const char *kwnames[] = { "callback", "msg_type", NULL };
201 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register",
202 discard_const_p(char *, kwnames), &callback, &msg_type)) {
203 return NULL;
206 Py_INCREF(callback);
208 if (msg_type == -1) {
209 uint32_t msg_type32 = msg_type;
210 status = imessaging_register_tmp(iface->msg_ctx, callback,
211 py_msg_callback_wrapper, &msg_type32);
212 msg_type = msg_type32;
213 } else {
214 status = imessaging_register(iface->msg_ctx, callback,
215 msg_type, py_msg_callback_wrapper);
217 if (NT_STATUS_IS_ERR(status)) {
218 PyErr_SetNTSTATUS(status);
219 return NULL;
222 return PyLong_FromLong(msg_type);
225 static PyObject *py_imessaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
227 imessaging_Object *iface = (imessaging_Object *)self;
228 int msg_type = -1;
229 PyObject *callback;
230 const char *kwnames[] = { "callback", "msg_type", NULL };
232 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
233 discard_const_p(char *, kwnames), &callback, &msg_type)) {
234 return NULL;
237 imessaging_deregister(iface->msg_ctx, msg_type, callback);
239 Py_DECREF(callback);
241 Py_RETURN_NONE;
244 static PyObject *py_irpc_add_name(PyObject *self, PyObject *args, PyObject *kwargs)
246 imessaging_Object *iface = (imessaging_Object *)self;
247 char *server_name;
248 NTSTATUS status;
250 if (!PyArg_ParseTuple(args, "s", &server_name)) {
251 return NULL;
254 status = irpc_add_name(iface->msg_ctx, server_name);
255 if (!NT_STATUS_IS_OK(status)) {
256 PyErr_SetNTSTATUS(status);
257 return NULL;
260 Py_RETURN_NONE;
263 static PyObject *py_irpc_servers_byname(PyObject *self, PyObject *args, PyObject *kwargs)
265 imessaging_Object *iface = (imessaging_Object *)self;
266 char *server_name;
267 unsigned i, num_ids;
268 struct server_id *ids;
269 PyObject *pylist;
270 TALLOC_CTX *mem_ctx = talloc_new(NULL);
271 NTSTATUS status;
273 if (!mem_ctx) {
274 PyErr_NoMemory();
275 return NULL;
278 if (!PyArg_ParseTuple(args, "s", &server_name)) {
279 TALLOC_FREE(mem_ctx);
280 return NULL;
283 status = irpc_servers_byname(iface->msg_ctx, mem_ctx, server_name,
284 &num_ids, &ids);
285 if (!NT_STATUS_IS_OK(status)) {
286 TALLOC_FREE(mem_ctx);
287 PyErr_SetString(PyExc_KeyError, "No such name");
288 return NULL;
291 pylist = PyList_New(num_ids);
292 if (pylist == NULL) {
293 TALLOC_FREE(mem_ctx);
294 PyErr_NoMemory();
295 return NULL;
297 for (i = 0; i < num_ids; i++) {
298 PyObject *py_server_id;
299 struct server_id *p_server_id = talloc(NULL, struct server_id);
300 if (!p_server_id) {
301 PyErr_NoMemory();
302 return NULL;
304 *p_server_id = ids[i];
306 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
307 if (!py_server_id) {
308 return NULL;
310 PyList_SetItem(pylist, i, py_server_id);
311 talloc_unlink(NULL, p_server_id);
313 TALLOC_FREE(mem_ctx);
314 return pylist;
317 static PyObject *py_irpc_all_servers(PyObject *self, PyObject *args, PyObject *kwargs)
319 imessaging_Object *iface = (imessaging_Object *)self;
320 PyObject *pylist;
321 int i;
322 struct irpc_name_records *records;
323 TALLOC_CTX *mem_ctx = talloc_new(NULL);
324 if (!mem_ctx) {
325 PyErr_NoMemory();
326 return NULL;
329 records = irpc_all_servers(iface->msg_ctx, mem_ctx);
330 if (records == NULL) {
331 return NULL;
334 pylist = PyList_New(records->num_records);
335 if (pylist == NULL) {
336 TALLOC_FREE(mem_ctx);
337 PyErr_NoMemory();
338 return NULL;
340 for (i = 0; i < records->num_records; i++) {
341 PyObject *py_name_record
342 = py_return_ndr_struct("samba.dcerpc.irpc",
343 "name_record",
344 records->names[i],
345 records->names[i]);
346 if (!py_name_record) {
347 return NULL;
349 PyList_SetItem(pylist, i,
350 py_name_record);
352 TALLOC_FREE(mem_ctx);
353 return pylist;
356 static PyMethodDef py_imessaging_methods[] = {
357 { "send", (PyCFunction)py_imessaging_send, METH_VARARGS|METH_KEYWORDS,
358 "S.send(target, msg_type, data) -> None\nSend a message" },
359 { "register", (PyCFunction)py_imessaging_register, METH_VARARGS|METH_KEYWORDS,
360 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
361 { "deregister", (PyCFunction)py_imessaging_deregister, METH_VARARGS|METH_KEYWORDS,
362 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
363 { "irpc_add_name", (PyCFunction)py_irpc_add_name, METH_VARARGS,
364 "S.irpc_add_name(name) -> None\n"
365 "Add this context to the list of server_id values that "
366 "are registered for a particular name" },
367 { "irpc_servers_byname", (PyCFunction)py_irpc_servers_byname, METH_VARARGS,
368 "S.irpc_servers_byname(name) -> list\nGet list of server_id values that are registered for a particular name" },
369 { "irpc_all_servers", (PyCFunction)py_irpc_all_servers, METH_NOARGS,
370 "S.irpc_all_servers() -> list\n"
371 "Get list of all registered names and the associated server_id values" },
372 { NULL, NULL, 0, NULL }
375 static PyObject *py_imessaging_server_id(PyObject *obj, void *closure)
377 imessaging_Object *iface = (imessaging_Object *)obj;
378 PyObject *py_server_id;
379 struct server_id server_id = imessaging_get_server_id(iface->msg_ctx);
380 struct server_id *p_server_id = talloc(NULL, struct server_id);
381 if (!p_server_id) {
382 PyErr_NoMemory();
383 return NULL;
385 *p_server_id = server_id;
387 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
388 talloc_unlink(NULL, p_server_id);
390 return py_server_id;
393 static PyGetSetDef py_imessaging_getset[] = {
394 { discard_const_p(char, "server_id"), py_imessaging_server_id, NULL,
395 discard_const_p(char, "local server id") },
396 { NULL },
400 PyTypeObject imessaging_Type = {
401 PyObject_HEAD_INIT(NULL) 0,
402 .tp_name = "messaging.Messaging",
403 .tp_basicsize = sizeof(imessaging_Object),
404 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
405 .tp_new = py_imessaging_connect,
406 .tp_dealloc = py_imessaging_dealloc,
407 .tp_methods = py_imessaging_methods,
408 .tp_getset = py_imessaging_getset,
409 .tp_doc = "Messaging(own_id=None)\n" \
410 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n"
413 void initmessaging(void)
415 PyObject *mod;
417 if (PyType_Ready(&imessaging_Type) < 0)
418 return;
420 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
421 if (mod == NULL)
422 return;
424 Py_INCREF((PyObject *)&imessaging_Type);
425 PyModule_AddObject(mod, "Messaging", (PyObject *)&imessaging_Type);
426 PyModule_AddObject(mod, "IRPC_CALL_TIMEOUT", PyInt_FromLong(IRPC_CALL_TIMEOUT));
427 PyModule_AddObject(mod, "IRPC_CALL_TIMEOUT_INF", PyInt_FromLong(IRPC_CALL_TIMEOUT_INF));