Avoid the use of PyAPI_DATA, which is for internal Python API's.
[metze/samba/wip.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/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_id.h"
35
36 extern PyTypeObject messaging_Type;
37
38 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
39 {
40         if (!PyTuple_Check(object)) {
41                 PyErr_SetString(PyExc_ValueError, "Expected tuple");
42                 return false;
43         }
44
45         if (PyTuple_Size(object) == 3) {
46                 return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
47         } else {
48                 int id, id2;
49                 if (!PyArg_ParseTuple(object, "ii", &id, &id2))
50                         return false;
51                 *server_id = cluster_id(id, id2);
52                 return true;
53         }
54 }
55
56 typedef struct {
57         PyObject_HEAD
58         TALLOC_CTX *mem_ctx;
59         struct messaging_context *msg_ctx;
60 } messaging_Object;
61
62 PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
63 {
64         struct tevent_context *ev;
65         const char *kwnames[] = { "own_id", "messaging_path", NULL };
66         PyObject *own_id = Py_None;
67         const char *messaging_path = NULL;
68         messaging_Object *ret;
69
70         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect", 
71                 discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
72                 return NULL;
73         }
74
75         ret = PyObject_New(messaging_Object, &messaging_Type);
76         if (ret == NULL)
77                 return NULL;
78
79         ret->mem_ctx = talloc_new(NULL);
80
81         ev = s4_event_context_init(ret->mem_ctx);
82
83         if (messaging_path == NULL) {
84                 messaging_path = lpcfg_messaging_path(ret->mem_ctx,
85                                                                    py_default_loadparm_context(ret->mem_ctx));
86         } else {
87                 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
88         }
89
90         if (own_id != Py_None) {
91                 struct server_id server_id;
92
93                 if (!server_id_from_py(own_id, &server_id)) 
94                         return NULL;
95
96                 ret->msg_ctx = messaging_init(ret->mem_ctx, 
97                                             messaging_path,
98                                             server_id,
99                                             ev);
100         } else {
101                 ret->msg_ctx = messaging_client_init(ret->mem_ctx, 
102                                             messaging_path,
103                                             ev);
104         }
105
106         if (ret->msg_ctx == NULL) {
107                 PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context");
108                 talloc_free(ret->mem_ctx);
109                 return NULL;
110         }
111
112         return (PyObject *)ret;
113 }
114
115 static void py_messaging_dealloc(PyObject *self)
116 {
117         messaging_Object *iface = (messaging_Object *)self;
118         talloc_free(iface->msg_ctx);
119         PyObject_Del(self);
120 }
121
122 static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
123 {
124         messaging_Object *iface = (messaging_Object *)self;
125         uint32_t msg_type;
126         DATA_BLOB data;
127         PyObject *target;
128         NTSTATUS status;
129         struct server_id server;
130         const char *kwnames[] = { "target", "msg_type", "data", NULL };
131         int length;
132
133         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send", 
134                 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
135
136                 return NULL;
137         }
138
139         data.length = length;
140
141         if (!server_id_from_py(target, &server))
142                 return NULL;
143
144         status = messaging_send(iface->msg_ctx, server, msg_type, &data);
145         if (NT_STATUS_IS_ERR(status)) {
146                 PyErr_SetNTSTATUS(status);
147                 return NULL;
148         }
149
150         Py_RETURN_NONE;
151 }
152
153 static void py_msg_callback_wrapper(struct messaging_context *msg, void *private_data,
154                                uint32_t msg_type, 
155                                struct server_id server_id, DATA_BLOB *data)
156 {
157         PyObject *callback = (PyObject *)private_data;
158
159         PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type, 
160                               server_id.id, server_id.id2, server_id.node, 
161                               data->data, data->length);
162 }
163
164 static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
165 {
166         messaging_Object *iface = (messaging_Object *)self;
167         int msg_type = -1;
168         PyObject *callback;
169         NTSTATUS status;
170         const char *kwnames[] = { "callback", "msg_type", NULL };
171         
172         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register", 
173                 discard_const_p(char *, kwnames), &callback, &msg_type)) {
174                 return NULL;
175         }
176
177         Py_INCREF(callback);
178
179         if (msg_type == -1) {
180                 uint32_t msg_type32 = msg_type;
181                 status = messaging_register_tmp(iface->msg_ctx, callback,
182                                                 py_msg_callback_wrapper, &msg_type32);
183                 msg_type = msg_type32;
184         } else {
185                 status = messaging_register(iface->msg_ctx, callback,
186                                     msg_type, py_msg_callback_wrapper);
187         }
188         if (NT_STATUS_IS_ERR(status)) {
189                 PyErr_SetNTSTATUS(status);
190                 return NULL;
191         }
192
193         return PyLong_FromLong(msg_type);
194 }
195
196 static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
197 {
198         messaging_Object *iface = (messaging_Object *)self;
199         int msg_type = -1;
200         PyObject *callback;
201         const char *kwnames[] = { "callback", "msg_type", NULL };
202
203         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
204                 discard_const_p(char *, kwnames), &callback, &msg_type)) {
205                 return NULL;
206         }
207
208         messaging_deregister(iface->msg_ctx, msg_type, callback);
209
210         Py_DECREF(callback);
211
212         Py_RETURN_NONE;
213 }
214
215 static PyMethodDef py_messaging_methods[] = {
216         { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS, 
217                 "S.send(target, msg_type, data) -> None\nSend a message" },
218         { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
219                 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
220         { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
221                 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
222         { NULL, NULL, 0, NULL }
223 };
224
225 static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
226 {
227         messaging_Object *iface = (messaging_Object *)obj;
228         struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
229
230         return Py_BuildValue("(iii)", server_id.id, server_id.id2, 
231                              server_id.node);
232 }
233
234 static PyGetSetDef py_messaging_getset[] = {
235         { discard_const_p(char, "server_id"), py_messaging_server_id, NULL, 
236           discard_const_p(char, "local server id") },
237         { NULL },
238 };
239
240
241 PyTypeObject messaging_Type = {
242         PyObject_HEAD_INIT(NULL) 0,
243         .tp_name = "messaging.Messaging",
244         .tp_basicsize = sizeof(messaging_Object),
245         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
246         .tp_new = py_messaging_connect,
247         .tp_dealloc = py_messaging_dealloc,
248         .tp_methods = py_messaging_methods,
249         .tp_getset = py_messaging_getset,
250         .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \
251                   "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
252                   "If no path is specified, the default path from smb.conf will be used."
253 };
254
255 void initmessaging(void)
256 {
257         PyObject *mod;
258
259         if (PyType_Ready(&messaging_Type) < 0)
260                 return;
261
262         mod = Py_InitModule3("messaging", NULL, "Internal RPC");
263         if (mod == NULL)
264                 return;
265
266         Py_INCREF((PyObject *)&messaging_Type);
267         PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);
268 }