pymessaging: Use the server_id IDL structure rather than a tuple
[kai/samba.git] / source4 / lib / messaging / pymessaging.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
4
5    Based on the equivalent for EJS:
6    Copyright © Andrew Tridgell <tridge@samba.org> 2005
7    
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.
12    
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.
17    
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/>.
20 */
21
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/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>
37
38 void initmessaging(void);
39
40 extern PyTypeObject imessaging_Type;
41
42 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
43 {
44         if (!PyTuple_Check(object)) {
45                 if (!py_check_dcerpc_type(object, "server_id", "server_id")) {
46
47                         PyErr_SetString(PyExc_ValueError, "Expected tuple or server_id");
48                         return false;
49                 }
50                 *server_id = *pytalloc_get_type(object, struct server_id);
51                 return true;
52         }
53         if (PyTuple_Size(object) == 3) {
54                 return PyArg_ParseTuple(object, "iii", &server_id->pid, &server_id->task_id, &server_id->vnn);
55         } else {
56                 int pid, task_id;
57                 if (!PyArg_ParseTuple(object, "ii", &pid, &task_id))
58                         return false;
59                 *server_id = cluster_id(pid, task_id);
60                 return true;
61         }
62 }
63
64 typedef struct {
65         PyObject_HEAD
66         TALLOC_CTX *mem_ctx;
67         struct imessaging_context *msg_ctx;
68 } imessaging_Object;
69
70 static PyObject *py_imessaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
71 {
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;
78
79         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO:connect", 
80                 discard_const_p(char *, kwnames), &own_id, &py_lp_ctx)) {
81                 return NULL;
82         }
83
84         ret = PyObject_New(imessaging_Object, &imessaging_Type);
85         if (ret == NULL)
86                 return NULL;
87
88         ret->mem_ctx = talloc_new(NULL);
89
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;
95         }
96
97         ev = s4_event_context_init(ret->mem_ctx);
98
99         if (own_id != Py_None) {
100                 struct server_id server_id;
101
102                 if (!server_id_from_py(own_id, &server_id)) 
103                         return NULL;
104
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);
113         }
114
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;
119         }
120
121         return (PyObject *)ret;
122 }
123
124 static void py_imessaging_dealloc(PyObject *self)
125 {
126         imessaging_Object *iface = (imessaging_Object *)self;
127         talloc_free(iface->msg_ctx);
128         self->ob_type->tp_free(self);
129 }
130
131 static PyObject *py_imessaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
132 {
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;
141
142         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send", 
143                 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
144
145                 return NULL;
146         }
147
148         data.length = length;
149
150         if (!server_id_from_py(target, &server))
151                 return NULL;
152
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;
157         }
158
159         Py_RETURN_NONE;
160 }
161
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)
165 {
166         PyObject *callback = (PyObject *)private_data;
167
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);
171 }
172
173 static PyObject *py_imessaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
174 {
175         imessaging_Object *iface = (imessaging_Object *)self;
176         int msg_type = -1;
177         PyObject *callback;
178         NTSTATUS status;
179         const char *kwnames[] = { "callback", "msg_type", NULL };
180         
181         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register", 
182                 discard_const_p(char *, kwnames), &callback, &msg_type)) {
183                 return NULL;
184         }
185
186         Py_INCREF(callback);
187
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;
193         } else {
194                 status = imessaging_register(iface->msg_ctx, callback,
195                                     msg_type, py_msg_callback_wrapper);
196         }
197         if (NT_STATUS_IS_ERR(status)) {
198                 PyErr_SetNTSTATUS(status);
199                 return NULL;
200         }
201
202         return PyLong_FromLong(msg_type);
203 }
204
205 static PyObject *py_imessaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
206 {
207         imessaging_Object *iface = (imessaging_Object *)self;
208         int msg_type = -1;
209         PyObject *callback;
210         const char *kwnames[] = { "callback", "msg_type", NULL };
211
212         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
213                 discard_const_p(char *, kwnames), &callback, &msg_type)) {
214                 return NULL;
215         }
216
217         imessaging_deregister(iface->msg_ctx, msg_type, callback);
218
219         Py_DECREF(callback);
220
221         Py_RETURN_NONE;
222 }
223
224 static PyMethodDef py_imessaging_methods[] = {
225         { "send", (PyCFunction)py_imessaging_send, METH_VARARGS|METH_KEYWORDS,
226                 "S.send(target, msg_type, data) -> None\nSend a message" },
227         { "register", (PyCFunction)py_imessaging_register, METH_VARARGS|METH_KEYWORDS,
228                 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
229         { "deregister", (PyCFunction)py_imessaging_deregister, METH_VARARGS|METH_KEYWORDS,
230                 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
231         { NULL, NULL, 0, NULL }
232 };
233
234 static PyObject *py_imessaging_server_id(PyObject *obj, void *closure)
235 {
236         imessaging_Object *iface = (imessaging_Object *)obj;
237         PyObject *py_server_id;
238         struct server_id server_id = imessaging_get_server_id(iface->msg_ctx);
239         struct server_id *p_server_id = talloc(NULL, struct server_id);
240         if (!p_server_id) {
241                 PyErr_NoMemory();
242                 return NULL;
243         }
244         *p_server_id = server_id;
245
246         py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
247         talloc_unlink(NULL, p_server_id);
248
249         return py_server_id;
250 }
251
252 static PyGetSetDef py_imessaging_getset[] = {
253         { discard_const_p(char, "server_id"), py_imessaging_server_id, NULL,
254           discard_const_p(char, "local server id") },
255         { NULL },
256 };
257
258
259 PyTypeObject imessaging_Type = {
260         PyObject_HEAD_INIT(NULL) 0,
261         .tp_name = "messaging.Messaging",
262         .tp_basicsize = sizeof(imessaging_Object),
263         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
264         .tp_new = py_imessaging_connect,
265         .tp_dealloc = py_imessaging_dealloc,
266         .tp_methods = py_imessaging_methods,
267         .tp_getset = py_imessaging_getset,
268         .tp_doc = "Messaging(own_id=None)\n" \
269                   "Create a new object that can be used to communicate with the peers in the specified messaging path.\n"
270 };
271
272 void initmessaging(void)
273 {
274         PyObject *mod;
275
276         if (PyType_Ready(&imessaging_Type) < 0)
277                 return;
278
279         mod = Py_InitModule3("messaging", NULL, "Internal RPC");
280         if (mod == NULL)
281                 return;
282
283         Py_INCREF((PyObject *)&imessaging_Type);
284         PyModule_AddObject(mod, "Messaging", (PyObject *)&imessaging_Type);
285 }