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 "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"
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");
50 *server_id
= *pytalloc_get_type(object
, struct server_id
);
53 if (PyTuple_Size(object
) == 3) {
54 unsigned long long pid
;
57 if (!PyArg_ParseTuple(object
, "KII", &pid
, &task_id
, &vnn
)) {
61 server_id
->task_id
= task_id
;
65 unsigned long long pid
;
67 if (!PyArg_ParseTuple(object
, "KI", &pid
, &task_id
))
69 *server_id
= cluster_id(pid
, task_id
);
77 struct imessaging_context
*msg_ctx
;
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
)) {
94 ret
= PyObject_New(imessaging_Object
, &imessaging_Type
);
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
);
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
))
115 ret
->msg_ctx
= imessaging_init(ret
->mem_ctx
,
120 ret
->msg_ctx
= imessaging_client_init(ret
->mem_ctx
,
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
);
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
;
148 struct server_id server
;
149 const char *kwnames
[] = { "target", "msg_type", "data", NULL
};
152 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Ois#:send",
153 discard_const_p(char *, kwnames
), &target
, &msg_type
, &data
.data
, &length
)) {
158 data
.length
= length
;
160 if (!server_id_from_py(target
, &server
))
163 status
= imessaging_send(iface
->msg_ctx
, server
, msg_type
, &data
);
164 if (NT_STATUS_IS_ERR(status
)) {
165 PyErr_SetNTSTATUS(status
);
172 static void py_msg_callback_wrapper(struct imessaging_context
*msg
, void *private_data
,
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
);
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
,
190 data
->data
, data
->length
);
193 static PyObject
*py_imessaging_register(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
195 imessaging_Object
*iface
= (imessaging_Object
*)self
;
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
)) {
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
;
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
);
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
;
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
)) {
237 imessaging_deregister(iface
->msg_ctx
, msg_type
, callback
);
244 static PyObject
*py_irpc_add_name(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
246 imessaging_Object
*iface
= (imessaging_Object
*)self
;
250 if (!PyArg_ParseTuple(args
, "s", &server_name
)) {
254 status
= irpc_add_name(iface
->msg_ctx
, server_name
);
255 if (!NT_STATUS_IS_OK(status
)) {
256 PyErr_SetNTSTATUS(status
);
263 static PyObject
*py_irpc_servers_byname(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
265 imessaging_Object
*iface
= (imessaging_Object
*)self
;
268 struct server_id
*ids
;
270 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
278 if (!PyArg_ParseTuple(args
, "s", &server_name
)) {
279 TALLOC_FREE(mem_ctx
);
283 status
= irpc_servers_byname(iface
->msg_ctx
, mem_ctx
, server_name
,
285 if (!NT_STATUS_IS_OK(status
)) {
286 TALLOC_FREE(mem_ctx
);
287 PyErr_SetString(PyExc_KeyError
, "No such name");
291 pylist
= PyList_New(num_ids
);
292 if (pylist
== NULL
) {
293 TALLOC_FREE(mem_ctx
);
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
);
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
);
310 PyList_SetItem(pylist
, i
, py_server_id
);
311 talloc_unlink(NULL
, p_server_id
);
313 TALLOC_FREE(mem_ctx
);
317 static PyObject
*py_irpc_all_servers(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
319 imessaging_Object
*iface
= (imessaging_Object
*)self
;
322 struct irpc_name_records
*records
;
323 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
329 records
= irpc_all_servers(iface
->msg_ctx
, mem_ctx
);
330 if (records
== NULL
) {
334 pylist
= PyList_New(records
->num_records
);
335 if (pylist
== NULL
) {
336 TALLOC_FREE(mem_ctx
);
340 for (i
= 0; i
< records
->num_records
; i
++) {
341 PyObject
*py_name_record
342 = py_return_ndr_struct("samba.dcerpc.irpc",
346 if (!py_name_record
) {
349 PyList_SetItem(pylist
, i
,
352 TALLOC_FREE(mem_ctx
);
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
);
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
);
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") },
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)
417 if (PyType_Ready(&imessaging_Type
) < 0)
420 mod
= Py_InitModule3("messaging", NULL
, "Internal RPC");
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
));