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/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
, "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 return PyArg_ParseTuple(object
, "iii", &server_id
->pid
, &server_id
->task_id
, &server_id
->vnn
);
57 if (!PyArg_ParseTuple(object
, "ii", &pid
, &task_id
))
59 *server_id
= cluster_id(pid
, task_id
);
67 struct imessaging_context
*msg_ctx
;
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
)) {
84 ret
= PyObject_New(imessaging_Object
, &imessaging_Type
);
88 ret
->mem_ctx
= talloc_new(NULL
);
90 lp_ctx
= lpcfg_from_py_object(ret
->mem_ctx
, py_lp_ctx
);
92 PyErr_SetString(PyExc_RuntimeError
, "imessaging_connect unable to interpret loadparm_context");
93 talloc_free(ret
->mem_ctx
);
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
))
105 ret
->msg_ctx
= imessaging_init(ret
->mem_ctx
,
110 ret
->msg_ctx
= imessaging_client_init(ret
->mem_ctx
,
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
);
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
;
138 struct server_id server
;
139 const char *kwnames
[] = { "target", "msg_type", "data", NULL
};
142 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Ois#:send",
143 discard_const_p(char *, kwnames
), &target
, &msg_type
, &data
.data
, &length
)) {
148 data
.length
= length
;
150 if (!server_id_from_py(target
, &server
))
153 status
= imessaging_send(iface
->msg_ctx
, server
, msg_type
, &data
);
154 if (NT_STATUS_IS_ERR(status
)) {
155 PyErr_SetNTSTATUS(status
);
162 static void py_msg_callback_wrapper(struct imessaging_context
*msg
, void *private_data
,
164 struct server_id server_id
, DATA_BLOB
*data
)
166 PyObject
*callback
= (PyObject
*)private_data
;
168 PyObject_CallFunction(callback
, discard_const_p(char, "i(iii)s#"), msg_type
,
169 server_id
.pid
, server_id
.task_id
, server_id
.vnn
,
170 data
->data
, data
->length
);
173 static PyObject
*py_imessaging_register(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
175 imessaging_Object
*iface
= (imessaging_Object
*)self
;
179 const char *kwnames
[] = { "callback", "msg_type", NULL
};
181 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|i:register",
182 discard_const_p(char *, kwnames
), &callback
, &msg_type
)) {
188 if (msg_type
== -1) {
189 uint32_t msg_type32
= msg_type
;
190 status
= imessaging_register_tmp(iface
->msg_ctx
, callback
,
191 py_msg_callback_wrapper
, &msg_type32
);
192 msg_type
= msg_type32
;
194 status
= imessaging_register(iface
->msg_ctx
, callback
,
195 msg_type
, py_msg_callback_wrapper
);
197 if (NT_STATUS_IS_ERR(status
)) {
198 PyErr_SetNTSTATUS(status
);
202 return PyLong_FromLong(msg_type
);
205 static PyObject
*py_imessaging_deregister(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
207 imessaging_Object
*iface
= (imessaging_Object
*)self
;
210 const char *kwnames
[] = { "callback", "msg_type", NULL
};
212 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|i:deregister",
213 discard_const_p(char *, kwnames
), &callback
, &msg_type
)) {
217 imessaging_deregister(iface
->msg_ctx
, msg_type
, callback
);
224 static PyObject
*py_irpc_servers_byname(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
226 imessaging_Object
*iface
= (imessaging_Object
*)self
;
228 struct server_id
*ids
;
231 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
237 if (!PyArg_ParseTuple(args
, "s", &server_name
)) {
238 TALLOC_FREE(mem_ctx
);
242 ids
= irpc_servers_byname(iface
->msg_ctx
, mem_ctx
, server_name
);
245 TALLOC_FREE(mem_ctx
);
246 PyErr_SetString(PyExc_KeyError
, "No such name");
250 for (i
= 0; !server_id_is_disconnected(&ids
[i
]); i
++) {
254 pylist
= PyList_New(i
);
255 if (pylist
== NULL
) {
256 TALLOC_FREE(mem_ctx
);
260 for (i
= 0; !server_id_is_disconnected(&ids
[i
]); i
++) {
261 PyObject
*py_server_id
;
262 struct server_id
*p_server_id
= talloc(NULL
, struct server_id
);
267 *p_server_id
= ids
[i
];
269 py_server_id
= py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id
, p_server_id
);
273 PyList_SetItem(pylist
, i
, py_server_id
);
274 talloc_unlink(NULL
, p_server_id
);
276 TALLOC_FREE(mem_ctx
);
280 static PyObject
*py_irpc_all_servers(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
282 imessaging_Object
*iface
= (imessaging_Object
*)self
;
285 struct irpc_name_records
*records
;
286 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
292 records
= irpc_all_servers(iface
->msg_ctx
, mem_ctx
);
293 if (records
== NULL
) {
297 pylist
= PyList_New(records
->num_records
);
298 if (pylist
== NULL
) {
299 TALLOC_FREE(mem_ctx
);
303 for (i
= 0; i
< records
->num_records
; i
++) {
304 PyObject
*py_name_record
305 = py_return_ndr_struct("samba.dcerpc.irpc",
309 if (!py_name_record
) {
312 PyList_SetItem(pylist
, i
,
315 TALLOC_FREE(mem_ctx
);
319 static PyMethodDef py_imessaging_methods
[] = {
320 { "send", (PyCFunction
)py_imessaging_send
, METH_VARARGS
|METH_KEYWORDS
,
321 "S.send(target, msg_type, data) -> None\nSend a message" },
322 { "register", (PyCFunction
)py_imessaging_register
, METH_VARARGS
|METH_KEYWORDS
,
323 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
324 { "deregister", (PyCFunction
)py_imessaging_deregister
, METH_VARARGS
|METH_KEYWORDS
,
325 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
326 { "irpc_servers_byname", (PyCFunction
)py_irpc_servers_byname
, METH_VARARGS
,
327 "S.irpc_servers_byname(name) -> list\nGet list of server_id values that are registered for a particular name" },
328 { "irpc_all_servers", (PyCFunction
)py_irpc_all_servers
, METH_NOARGS
,
329 "S.irpc_servers_byname() -> list\nGet list of all registered names and the associated server_id values" },
330 { NULL
, NULL
, 0, NULL
}
333 static PyObject
*py_imessaging_server_id(PyObject
*obj
, void *closure
)
335 imessaging_Object
*iface
= (imessaging_Object
*)obj
;
336 PyObject
*py_server_id
;
337 struct server_id server_id
= imessaging_get_server_id(iface
->msg_ctx
);
338 struct server_id
*p_server_id
= talloc(NULL
, struct server_id
);
343 *p_server_id
= server_id
;
345 py_server_id
= py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id
, p_server_id
);
346 talloc_unlink(NULL
, p_server_id
);
351 static PyGetSetDef py_imessaging_getset
[] = {
352 { discard_const_p(char, "server_id"), py_imessaging_server_id
, NULL
,
353 discard_const_p(char, "local server id") },
358 PyTypeObject imessaging_Type
= {
359 PyObject_HEAD_INIT(NULL
) 0,
360 .tp_name
= "messaging.Messaging",
361 .tp_basicsize
= sizeof(imessaging_Object
),
362 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
363 .tp_new
= py_imessaging_connect
,
364 .tp_dealloc
= py_imessaging_dealloc
,
365 .tp_methods
= py_imessaging_methods
,
366 .tp_getset
= py_imessaging_getset
,
367 .tp_doc
= "Messaging(own_id=None)\n" \
368 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n"
371 void initmessaging(void)
375 if (PyType_Ready(&imessaging_Type
) < 0)
378 mod
= Py_InitModule3("messaging", NULL
, "Internal RPC");
382 Py_INCREF((PyObject
*)&imessaging_Type
);
383 PyModule_AddObject(mod
, "Messaging", (PyObject
*)&imessaging_Type
);