2 Unix SMB/CIFS implementation.
4 Samba internal messaging functions
6 Copyright (C) Andrew Tridgell 2004
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/events/events.h"
25 #include "system/dir.h"
26 #include "system/filesys.h"
27 #include "system/time.h"
29 #include "dlinklist.h"
30 #include "lib/socket/socket.h"
31 #include "librpc/gen_ndr/ndr_irpc.h"
32 #include "lib/messaging/irpc.h"
34 /* change the message version with any incompatible changes in the protocol */
35 #define MESSAGING_VERSION 1
37 struct messaging_context {
39 struct socket_context *sock;
40 const char *base_path;
42 struct dispatch_fn *dispatch;
43 struct messaging_rec *pending;
44 struct irpc_list *irpc;
45 struct idr_context *idr;
48 struct event_context *ev;
53 /* we have a linked list of dispatch handlers that this messaging
54 server can deal with */
56 struct dispatch_fn *next, *prev;
59 void (*fn)(struct messaging_context *msg, void *private,
60 uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
63 /* an individual message */
64 struct messaging_rec {
65 struct messaging_rec *next, *prev;
66 struct messaging_context *msg;
69 struct messaging_header {
81 static void irpc_handler(struct messaging_context *, void *,
82 uint32_t, uint32_t, DATA_BLOB *);
86 A useful function for testing the message system.
88 static void ping_message(struct messaging_context *msg, void *private,
89 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
91 DEBUG(1,("INFO: Received PING message from server %u [%.*s]\n",
92 (uint_t)src, data->length, data->data?(const char *)data->data:""));
93 messaging_send(msg, src, MSG_PONG, data);
97 return the path to a messaging socket
99 static char *messaging_path(struct messaging_context *msg, uint32_t server_id)
101 return talloc_asprintf(msg, "%s/msg.%u", msg->base_path, (unsigned)server_id);
105 dispatch a fully received message
107 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
109 struct dispatch_fn *d, *next;
110 for (d=msg->dispatch;d;d=next) {
112 if (d->msg_type == rec->header->msg_type) {
114 data.data = rec->packet.data + sizeof(*rec->header);
115 data.length = rec->header->length;
116 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
119 rec->header->length = 0;
124 try to send the message
126 static NTSTATUS try_send(struct messaging_rec *rec)
128 struct messaging_context *msg = rec->msg;
133 /* we send with privileges so messages work from any context */
134 priv = root_privileges();
135 status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, rec->path, 0);
142 handle a socket write event
144 static void messaging_send_handler(struct messaging_context *msg)
146 while (msg->pending) {
147 struct messaging_rec *rec = msg->pending;
149 status = try_send(rec);
150 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
153 if (!NT_STATUS_IS_OK(status)) {
154 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
155 rec->header->from, rec->header->to, rec->header->msg_type,
158 DLIST_REMOVE(msg->pending, rec);
161 if (msg->pending == NULL) {
162 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
167 handle a new incoming packet
169 static void messaging_recv_handler(struct messaging_context *msg)
171 struct messaging_rec *rec;
176 /* see how many bytes are in the next packet */
177 status = socket_pending(msg->sock, &msize);
178 if (!NT_STATUS_IS_OK(status)) {
179 DEBUG(0,("socket_pending failed in messaging - %s\n",
184 packet = data_blob_talloc(msg, NULL, msize);
185 if (packet.data == NULL) {
186 /* assume this is temporary and retry */
190 status = socket_recv(msg->sock, packet.data, msize, &msize, 0);
191 if (!NT_STATUS_IS_OK(status)) {
192 data_blob_free(&packet);
196 if (msize < sizeof(*rec->header)) {
197 DEBUG(0,("messaging: bad message of size %d\n", msize));
198 data_blob_free(&packet);
202 rec = talloc(msg, struct messaging_rec);
204 smb_panic("Unable to allocate messaging_rec");
207 talloc_steal(rec, packet.data);
209 rec->path = msg->path;
210 rec->header = (struct messaging_header *)packet.data;
211 rec->packet = packet;
213 if (msize != sizeof(*rec->header) + rec->header->length) {
214 DEBUG(0,("messaging: bad message header size %d should be %d\n",
215 rec->header->length, msize - sizeof(*rec->header)));
220 messaging_dispatch(msg, rec);
226 handle a socket event
228 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
229 uint16_t flags, void *private)
231 struct messaging_context *msg = talloc_get_type(private,
232 struct messaging_context);
233 if (flags & EVENT_FD_WRITE) {
234 messaging_send_handler(msg);
236 if (flags & EVENT_FD_READ) {
237 messaging_recv_handler(msg);
243 Register a dispatch function for a particular message type.
245 void messaging_register(struct messaging_context *msg, void *private,
247 void (*fn)(struct messaging_context *, void *, uint32_t, uint32_t, DATA_BLOB *))
249 struct dispatch_fn *d;
251 d = talloc(msg, struct dispatch_fn);
252 d->msg_type = msg_type;
253 d->private = private;
255 DLIST_ADD(msg->dispatch, d);
259 De-register the function for a particular message type.
261 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
263 struct dispatch_fn *d, *next;
265 for (d = msg->dispatch; d; d = next) {
267 if (d->msg_type == msg_type &&
268 d->private == private) {
269 DLIST_REMOVE(msg->dispatch, d);
277 Send a message to a particular server
279 NTSTATUS messaging_send(struct messaging_context *msg, uint32_t server,
280 uint32_t msg_type, DATA_BLOB *data)
282 struct messaging_rec *rec;
284 size_t dlength = data?data->length:0;
286 rec = talloc(msg, struct messaging_rec);
288 return NT_STATUS_NO_MEMORY;
291 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
292 if (rec->packet.data == NULL) {
294 return NT_STATUS_NO_MEMORY;
298 rec->header = (struct messaging_header *)rec->packet.data;
299 rec->header->version = MESSAGING_VERSION;
300 rec->header->msg_type = msg_type;
301 rec->header->from = msg->server_id;
302 rec->header->to = server;
303 rec->header->length = dlength;
305 memcpy(rec->packet.data + sizeof(*rec->header),
306 data->data, dlength);
309 rec->path = messaging_path(msg, server);
310 talloc_steal(rec, rec->path);
312 if (msg->pending != NULL) {
313 status = STATUS_MORE_ENTRIES;
315 status = try_send(rec);
318 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
319 if (msg->pending == NULL) {
320 EVENT_FD_WRITEABLE(msg->event.fde);
322 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
332 Send a message to a particular server, with the message containing a single pointer
334 NTSTATUS messaging_send_ptr(struct messaging_context *msg, uint32_t server,
335 uint32_t msg_type, void *ptr)
339 blob.data = (void *)&ptr;
340 blob.length = sizeof(void *);
342 return messaging_send(msg, server, msg_type, &blob);
347 destroy the messaging context
349 static int messaging_destructor(void *ptr)
351 struct messaging_context *msg = ptr;
357 create the listening socket and setup the dispatcher
359 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id,
360 struct event_context *ev)
362 struct messaging_context *msg;
366 msg = talloc(mem_ctx, struct messaging_context);
371 /* create the messaging directory if needed */
372 path = smbd_tmp_path(msg, "messaging");
376 msg->base_path = smbd_tmp_path(msg, "messaging");
377 msg->path = messaging_path(msg, server_id);
378 msg->server_id = server_id;
379 msg->dispatch = NULL;
381 msg->idr = idr_init(msg);
384 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
385 if (!NT_STATUS_IS_OK(status)) {
390 /* by stealing here we ensure that the socket is cleaned up (and even
392 talloc_steal(msg, msg->sock);
394 status = socket_listen(msg->sock, msg->path, 0, 50, 0);
395 if (!NT_STATUS_IS_OK(status)) {
396 DEBUG(0,("Unable to setup messaging listener for '%s'\n", msg->path));
401 /* it needs to be non blocking for sends */
402 set_blocking(socket_get_fd(msg->sock), False);
404 msg->event.ev = talloc_reference(msg, ev);
405 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
406 EVENT_FD_READ, messaging_handler, msg);
408 talloc_set_destructor(msg, messaging_destructor);
410 messaging_register(msg, NULL, MSG_PING, ping_message);
411 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
418 a list of registered irpc server functions
421 struct irpc_list *next, *prev;
423 const struct dcerpc_interface_table *table;
431 register a irpc server function
433 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
434 const struct dcerpc_interface_table *table,
435 int callnum, irpc_function_t fn, void *private)
437 struct irpc_list *irpc;
439 /* override an existing handler, if any */
440 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
441 if (irpc->table == table && irpc->callnum == callnum) {
446 irpc = talloc(msg_ctx, struct irpc_list);
447 NT_STATUS_HAVE_NO_MEMORY(irpc);
448 DLIST_ADD(msg_ctx->irpc, irpc);
452 irpc->callnum = callnum;
454 irpc->private = private;
455 GUID_from_string(irpc->table->uuid, &irpc->uuid);
462 handle an incoming irpc reply message
464 static void irpc_handler_reply(struct messaging_context *msg_ctx,
465 struct ndr_pull *ndr, struct irpc_header *header)
467 struct irpc_request *irpc;
469 irpc = idr_find(msg_ctx->idr, header->callid);
470 if (irpc == NULL) return;
472 /* parse the reply data */
473 irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(ndr, NDR_OUT, irpc->r);
474 if (NT_STATUS_IS_OK(irpc->status)) {
475 irpc->status = header->status;
478 if (irpc->async.fn) {
479 irpc->async.fn(irpc);
485 handle an incoming irpc request message
487 static void irpc_handler_request(struct messaging_context *msg_ctx,
488 struct ndr_pull *ndr, struct irpc_header *header,
494 struct irpc_message m;
495 struct ndr_push *push;
498 for (i=msg_ctx->irpc; i; i=i->next) {
499 if (GUID_equal(&i->uuid, &header->uuid) &&
500 i->table->if_version == header->if_version &&
501 i->callnum == header->callnum) {
507 /* no registered handler for this message */
511 /* allocate space for the structure */
512 r = talloc_zero_size(ndr, i->table->calls[header->callnum].struct_size);
513 if (r == NULL) goto failed;
515 /* parse the request data */
516 status = i->table->calls[i->callnum].ndr_pull(ndr, NDR_IN, r);
517 if (!NT_STATUS_IS_OK(status)) goto failed;
521 m.private = i->private;
522 header->status = i->fn(&m, r);
524 /* setup the reply */
525 push = ndr_push_init_ctx(ndr);
526 if (push == NULL) goto failed;
528 header->flags |= IRPC_FLAG_REPLY;
530 /* construct the packet */
531 status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, header);
532 if (!NT_STATUS_IS_OK(status)) goto failed;
534 status = i->table->calls[i->callnum].ndr_push(push, NDR_OUT, r);
535 if (!NT_STATUS_IS_OK(status)) goto failed;
537 /* send the reply message */
538 packet = ndr_push_blob(push);
539 status = messaging_send(msg_ctx, src, MSG_IRPC, &packet);
540 if (!NT_STATUS_IS_OK(status)) goto failed;
543 /* nothing to clean up */
548 handle an incoming irpc message
550 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
551 uint32_t msg_type, uint32_t src, DATA_BLOB *packet)
553 struct irpc_header header;
554 struct ndr_pull *ndr;
557 ndr = ndr_pull_init_blob(packet, msg_ctx);
558 if (ndr == NULL) goto failed;
560 status = ndr_pull_irpc_header(ndr, NDR_BUFFERS|NDR_SCALARS, &header);
561 if (!NT_STATUS_IS_OK(status)) goto failed;
563 if (header.flags & IRPC_FLAG_REPLY) {
564 irpc_handler_reply(msg_ctx, ndr, &header);
566 irpc_handler_request(msg_ctx, ndr, &header, src);
575 destroy a irpc request
577 static int irpc_destructor(void *ptr)
579 struct irpc_request *irpc = talloc_get_type(ptr, struct irpc_request);
580 idr_remove(irpc->msg_ctx->idr, irpc->callid);
585 timeout a irpc request
587 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
588 struct timeval t, void *private)
590 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
591 irpc->status = NT_STATUS_IO_TIMEOUT;
593 if (irpc->async.fn) {
594 irpc->async.fn(irpc);
600 make a irpc call - async send
602 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
604 const struct dcerpc_interface_table *table,
605 int callnum, void *r)
607 struct irpc_header header;
608 struct ndr_push *ndr;
611 struct irpc_request *irpc;
613 irpc = talloc(msg_ctx, struct irpc_request);
614 if (irpc == NULL) goto failed;
616 irpc->msg_ctx = msg_ctx;
618 irpc->callnum = callnum;
619 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
620 if (irpc->callid == -1) goto failed;
623 irpc->async.fn = NULL;
625 talloc_set_destructor(irpc, irpc_destructor);
627 /* setup the header */
628 status = GUID_from_string(table->uuid, &header.uuid);
629 if (!NT_STATUS_IS_OK(status)) goto failed;
631 header.if_version = table->if_version;
632 header.callid = irpc->callid;
633 header.callnum = callnum;
635 header.status = NT_STATUS_OK;
637 /* construct the irpc packet */
638 ndr = ndr_push_init_ctx(irpc);
639 if (ndr == NULL) goto failed;
641 status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
642 if (!NT_STATUS_IS_OK(status)) goto failed;
644 status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
645 if (!NT_STATUS_IS_OK(status)) goto failed;
648 packet = ndr_push_blob(ndr);
649 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
650 if (!NT_STATUS_IS_OK(status)) goto failed;
652 event_add_timed(msg_ctx->event.ev, irpc,
653 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
665 wait for a irpc reply
667 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
670 NT_STATUS_HAVE_NO_MEMORY(irpc);
671 while (!irpc->done) {
672 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
673 return NT_STATUS_CONNECTION_DISCONNECTED;
676 status = irpc->status;
682 perform a synchronous irpc request
684 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
686 const struct dcerpc_interface_table *table,
687 int callnum, void *r)
689 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
691 return irpc_call_recv(irpc);