s3-net: Add encoding=<CP> to 'net printing dump'.
[Samba.git] / source4 / lib / messaging / pymessaging.c
blob358d205b53b53039bfeacb51e7d58a370fe275b3
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 "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_id4.h"
36 void initmessaging(void);
38 extern PyTypeObject messaging_Type;
40 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
42 if (!PyTuple_Check(object)) {
43 PyErr_SetString(PyExc_ValueError, "Expected tuple");
44 return false;
47 if (PyTuple_Size(object) == 3) {
48 return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
49 } else {
50 int id, id2;
51 if (!PyArg_ParseTuple(object, "ii", &id, &id2))
52 return false;
53 *server_id = cluster_id(id, id2);
54 return true;
58 typedef struct {
59 PyObject_HEAD
60 TALLOC_CTX *mem_ctx;
61 struct messaging_context *msg_ctx;
62 } messaging_Object;
64 static PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
66 struct tevent_context *ev;
67 const char *kwnames[] = { "own_id", "messaging_path", NULL };
68 PyObject *own_id = Py_None;
69 const char *messaging_path = NULL;
70 messaging_Object *ret;
72 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect",
73 discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
74 return NULL;
77 ret = PyObject_New(messaging_Object, &messaging_Type);
78 if (ret == NULL)
79 return NULL;
81 ret->mem_ctx = talloc_new(NULL);
83 ev = s4_event_context_init(ret->mem_ctx);
85 if (messaging_path == NULL) {
86 messaging_path = lpcfg_messaging_path(ret->mem_ctx,
87 py_default_loadparm_context(ret->mem_ctx));
88 } else {
89 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
92 if (own_id != Py_None) {
93 struct server_id server_id;
95 if (!server_id_from_py(own_id, &server_id))
96 return NULL;
98 ret->msg_ctx = messaging_init(ret->mem_ctx,
99 messaging_path,
100 server_id,
101 ev);
102 } else {
103 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
104 messaging_path,
105 ev);
108 if (ret->msg_ctx == NULL) {
109 PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context");
110 talloc_free(ret->mem_ctx);
111 return NULL;
114 return (PyObject *)ret;
117 static void py_messaging_dealloc(PyObject *self)
119 messaging_Object *iface = (messaging_Object *)self;
120 talloc_free(iface->msg_ctx);
121 self->ob_type->tp_free(self);
124 static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
126 messaging_Object *iface = (messaging_Object *)self;
127 uint32_t msg_type;
128 DATA_BLOB data;
129 PyObject *target;
130 NTSTATUS status;
131 struct server_id server;
132 const char *kwnames[] = { "target", "msg_type", "data", NULL };
133 int length;
135 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send",
136 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
138 return NULL;
141 data.length = length;
143 if (!server_id_from_py(target, &server))
144 return NULL;
146 status = messaging_send(iface->msg_ctx, server, msg_type, &data);
147 if (NT_STATUS_IS_ERR(status)) {
148 PyErr_SetNTSTATUS(status);
149 return NULL;
152 Py_RETURN_NONE;
155 static void py_msg_callback_wrapper(struct messaging_context *msg, void *private_data,
156 uint32_t msg_type,
157 struct server_id server_id, DATA_BLOB *data)
159 PyObject *callback = (PyObject *)private_data;
161 PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type,
162 server_id.id, server_id.id2, server_id.node,
163 data->data, data->length);
166 static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
168 messaging_Object *iface = (messaging_Object *)self;
169 int msg_type = -1;
170 PyObject *callback;
171 NTSTATUS status;
172 const char *kwnames[] = { "callback", "msg_type", NULL };
174 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register",
175 discard_const_p(char *, kwnames), &callback, &msg_type)) {
176 return NULL;
179 Py_INCREF(callback);
181 if (msg_type == -1) {
182 uint32_t msg_type32 = msg_type;
183 status = messaging_register_tmp(iface->msg_ctx, callback,
184 py_msg_callback_wrapper, &msg_type32);
185 msg_type = msg_type32;
186 } else {
187 status = messaging_register(iface->msg_ctx, callback,
188 msg_type, py_msg_callback_wrapper);
190 if (NT_STATUS_IS_ERR(status)) {
191 PyErr_SetNTSTATUS(status);
192 return NULL;
195 return PyLong_FromLong(msg_type);
198 static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
200 messaging_Object *iface = (messaging_Object *)self;
201 int msg_type = -1;
202 PyObject *callback;
203 const char *kwnames[] = { "callback", "msg_type", NULL };
205 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
206 discard_const_p(char *, kwnames), &callback, &msg_type)) {
207 return NULL;
210 messaging_deregister(iface->msg_ctx, msg_type, callback);
212 Py_DECREF(callback);
214 Py_RETURN_NONE;
217 static PyMethodDef py_messaging_methods[] = {
218 { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS,
219 "S.send(target, msg_type, data) -> None\nSend a message" },
220 { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
221 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
222 { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
223 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
224 { NULL, NULL, 0, NULL }
227 static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
229 messaging_Object *iface = (messaging_Object *)obj;
230 struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
232 return Py_BuildValue("(iii)", server_id.id, server_id.id2,
233 server_id.node);
236 static PyGetSetDef py_messaging_getset[] = {
237 { discard_const_p(char, "server_id"), py_messaging_server_id, NULL,
238 discard_const_p(char, "local server id") },
239 { NULL },
243 PyTypeObject messaging_Type = {
244 PyObject_HEAD_INIT(NULL) 0,
245 .tp_name = "messaging.Messaging",
246 .tp_basicsize = sizeof(messaging_Object),
247 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
248 .tp_new = py_messaging_connect,
249 .tp_dealloc = py_messaging_dealloc,
250 .tp_methods = py_messaging_methods,
251 .tp_getset = py_messaging_getset,
252 .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \
253 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
254 "If no path is specified, the default path from smb.conf will be used."
257 void initmessaging(void)
259 PyObject *mod;
261 if (PyType_Ready(&messaging_Type) < 0)
262 return;
264 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
265 if (mod == NULL)
266 return;
268 Py_INCREF((PyObject *)&messaging_Type);
269 PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);