messaging4: Change irpc_servers_by_name to NTSTATUS
[Samba.git] / source4 / lib / messaging / pymessaging.c
blob1e9588c6737bbb5a3febd403e49ef6df8526be5b
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 return PyArg_ParseTuple(object, "KII", &server_id->pid, &server_id->task_id, &server_id->vnn);
55 } else {
56 int pid, task_id;
57 if (!PyArg_ParseTuple(object, "KI", &pid, &task_id))
58 return false;
59 *server_id = cluster_id(pid, task_id);
60 return true;
64 typedef struct {
65 PyObject_HEAD
66 TALLOC_CTX *mem_ctx;
67 struct imessaging_context *msg_ctx;
68 } imessaging_Object;
70 static PyObject *py_imessaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
72 struct tevent_context *ev;
73 const char *kwnames[] = { "own_id", "lp_ctx", NULL };
74 PyObject *own_id = Py_None;
75 PyObject *py_lp_ctx = Py_None;
76 imessaging_Object *ret;
77 struct loadparm_context *lp_ctx;
79 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO:connect",
80 discard_const_p(char *, kwnames), &own_id, &py_lp_ctx)) {
81 return NULL;
84 ret = PyObject_New(imessaging_Object, &imessaging_Type);
85 if (ret == NULL)
86 return NULL;
88 ret->mem_ctx = talloc_new(NULL);
90 lp_ctx = lpcfg_from_py_object(ret->mem_ctx, py_lp_ctx);
91 if (lp_ctx == NULL) {
92 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to interpret loadparm_context");
93 talloc_free(ret->mem_ctx);
94 return NULL;
97 ev = s4_event_context_init(ret->mem_ctx);
99 if (own_id != Py_None) {
100 struct server_id server_id;
102 if (!server_id_from_py(own_id, &server_id))
103 return NULL;
105 ret->msg_ctx = imessaging_init(ret->mem_ctx,
106 lp_ctx,
107 server_id,
108 ev, true);
109 } else {
110 ret->msg_ctx = imessaging_client_init(ret->mem_ctx,
111 lp_ctx,
112 ev);
115 if (ret->msg_ctx == NULL) {
116 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to create a messaging context");
117 talloc_free(ret->mem_ctx);
118 return NULL;
121 return (PyObject *)ret;
124 static void py_imessaging_dealloc(PyObject *self)
126 imessaging_Object *iface = (imessaging_Object *)self;
127 talloc_free(iface->msg_ctx);
128 self->ob_type->tp_free(self);
131 static PyObject *py_imessaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
133 imessaging_Object *iface = (imessaging_Object *)self;
134 uint32_t msg_type;
135 DATA_BLOB data;
136 PyObject *target;
137 NTSTATUS status;
138 struct server_id server;
139 const char *kwnames[] = { "target", "msg_type", "data", NULL };
140 int length;
142 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send",
143 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
145 return NULL;
148 data.length = length;
150 if (!server_id_from_py(target, &server))
151 return NULL;
153 status = imessaging_send(iface->msg_ctx, server, msg_type, &data);
154 if (NT_STATUS_IS_ERR(status)) {
155 PyErr_SetNTSTATUS(status);
156 return NULL;
159 Py_RETURN_NONE;
162 static void py_msg_callback_wrapper(struct imessaging_context *msg, void *private_data,
163 uint32_t msg_type,
164 struct server_id server_id, DATA_BLOB *data)
166 PyObject *py_server_id, *callback = (PyObject *)private_data;
168 struct server_id *p_server_id = talloc(NULL, struct server_id);
169 if (!p_server_id) {
170 PyErr_NoMemory();
171 return;
173 *p_server_id = server_id;
175 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
176 talloc_unlink(NULL, p_server_id);
178 PyObject_CallFunction(callback, discard_const_p(char, "i(O)s#"), msg_type,
179 py_server_id,
180 data->data, data->length);
183 static PyObject *py_imessaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
185 imessaging_Object *iface = (imessaging_Object *)self;
186 int msg_type = -1;
187 PyObject *callback;
188 NTSTATUS status;
189 const char *kwnames[] = { "callback", "msg_type", NULL };
191 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register",
192 discard_const_p(char *, kwnames), &callback, &msg_type)) {
193 return NULL;
196 Py_INCREF(callback);
198 if (msg_type == -1) {
199 uint32_t msg_type32 = msg_type;
200 status = imessaging_register_tmp(iface->msg_ctx, callback,
201 py_msg_callback_wrapper, &msg_type32);
202 msg_type = msg_type32;
203 } else {
204 status = imessaging_register(iface->msg_ctx, callback,
205 msg_type, py_msg_callback_wrapper);
207 if (NT_STATUS_IS_ERR(status)) {
208 PyErr_SetNTSTATUS(status);
209 return NULL;
212 return PyLong_FromLong(msg_type);
215 static PyObject *py_imessaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
217 imessaging_Object *iface = (imessaging_Object *)self;
218 int msg_type = -1;
219 PyObject *callback;
220 const char *kwnames[] = { "callback", "msg_type", NULL };
222 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
223 discard_const_p(char *, kwnames), &callback, &msg_type)) {
224 return NULL;
227 imessaging_deregister(iface->msg_ctx, msg_type, callback);
229 Py_DECREF(callback);
231 Py_RETURN_NONE;
234 static PyObject *py_irpc_servers_byname(PyObject *self, PyObject *args, PyObject *kwargs)
236 imessaging_Object *iface = (imessaging_Object *)self;
237 char *server_name;
238 unsigned i, num_ids;
239 struct server_id *ids;
240 PyObject *pylist;
241 TALLOC_CTX *mem_ctx = talloc_new(NULL);
242 NTSTATUS status;
244 if (!mem_ctx) {
245 PyErr_NoMemory();
246 return NULL;
249 if (!PyArg_ParseTuple(args, "s", &server_name)) {
250 TALLOC_FREE(mem_ctx);
251 return NULL;
254 status = irpc_servers_byname(iface->msg_ctx, mem_ctx, server_name,
255 &num_ids, &ids);
256 if (!NT_STATUS_IS_OK(status)) {
257 TALLOC_FREE(mem_ctx);
258 PyErr_SetString(PyExc_KeyError, "No such name");
259 return NULL;
262 pylist = PyList_New(num_ids);
263 if (pylist == NULL) {
264 TALLOC_FREE(mem_ctx);
265 PyErr_NoMemory();
266 return NULL;
268 for (i = 0; i < num_ids; i++) {
269 PyObject *py_server_id;
270 struct server_id *p_server_id = talloc(NULL, struct server_id);
271 if (!p_server_id) {
272 PyErr_NoMemory();
273 return NULL;
275 *p_server_id = ids[i];
277 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
278 if (!py_server_id) {
279 return NULL;
281 PyList_SetItem(pylist, i, py_server_id);
282 talloc_unlink(NULL, p_server_id);
284 TALLOC_FREE(mem_ctx);
285 return pylist;
288 static PyObject *py_irpc_all_servers(PyObject *self, PyObject *args, PyObject *kwargs)
290 imessaging_Object *iface = (imessaging_Object *)self;
291 PyObject *pylist;
292 int i;
293 struct irpc_name_records *records;
294 TALLOC_CTX *mem_ctx = talloc_new(NULL);
295 if (!mem_ctx) {
296 PyErr_NoMemory();
297 return NULL;
300 records = irpc_all_servers(iface->msg_ctx, mem_ctx);
301 if (records == NULL) {
302 return NULL;
305 pylist = PyList_New(records->num_records);
306 if (pylist == NULL) {
307 TALLOC_FREE(mem_ctx);
308 PyErr_NoMemory();
309 return NULL;
311 for (i = 0; i < records->num_records; i++) {
312 PyObject *py_name_record
313 = py_return_ndr_struct("samba.dcerpc.irpc",
314 "name_record",
315 records->names[i],
316 records->names[i]);
317 if (!py_name_record) {
318 return NULL;
320 PyList_SetItem(pylist, i,
321 py_name_record);
323 TALLOC_FREE(mem_ctx);
324 return pylist;
327 static PyMethodDef py_imessaging_methods[] = {
328 { "send", (PyCFunction)py_imessaging_send, METH_VARARGS|METH_KEYWORDS,
329 "S.send(target, msg_type, data) -> None\nSend a message" },
330 { "register", (PyCFunction)py_imessaging_register, METH_VARARGS|METH_KEYWORDS,
331 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
332 { "deregister", (PyCFunction)py_imessaging_deregister, METH_VARARGS|METH_KEYWORDS,
333 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
334 { "irpc_servers_byname", (PyCFunction)py_irpc_servers_byname, METH_VARARGS,
335 "S.irpc_servers_byname(name) -> list\nGet list of server_id values that are registered for a particular name" },
336 { "irpc_all_servers", (PyCFunction)py_irpc_all_servers, METH_NOARGS,
337 "S.irpc_servers_byname() -> list\nGet list of all registered names and the associated server_id values" },
338 { NULL, NULL, 0, NULL }
341 static PyObject *py_imessaging_server_id(PyObject *obj, void *closure)
343 imessaging_Object *iface = (imessaging_Object *)obj;
344 PyObject *py_server_id;
345 struct server_id server_id = imessaging_get_server_id(iface->msg_ctx);
346 struct server_id *p_server_id = talloc(NULL, struct server_id);
347 if (!p_server_id) {
348 PyErr_NoMemory();
349 return NULL;
351 *p_server_id = server_id;
353 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
354 talloc_unlink(NULL, p_server_id);
356 return py_server_id;
359 static PyGetSetDef py_imessaging_getset[] = {
360 { discard_const_p(char, "server_id"), py_imessaging_server_id, NULL,
361 discard_const_p(char, "local server id") },
362 { NULL },
366 PyTypeObject imessaging_Type = {
367 PyObject_HEAD_INIT(NULL) 0,
368 .tp_name = "messaging.Messaging",
369 .tp_basicsize = sizeof(imessaging_Object),
370 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
371 .tp_new = py_imessaging_connect,
372 .tp_dealloc = py_imessaging_dealloc,
373 .tp_methods = py_imessaging_methods,
374 .tp_getset = py_imessaging_getset,
375 .tp_doc = "Messaging(own_id=None)\n" \
376 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n"
379 void initmessaging(void)
381 PyObject *mod;
383 if (PyType_Ready(&imessaging_Type) < 0)
384 return;
386 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
387 if (mod == NULL)
388 return;
390 Py_INCREF((PyObject *)&imessaging_Type);
391 PyModule_AddObject(mod, "Messaging", (PyObject *)&imessaging_Type);