imessaging: Add irpc_all_servers() to list all available servers
authorAndrew Bartlett <abartlet@samba.org>
Mon, 29 Oct 2012 04:32:21 +0000 (15:32 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 30 Oct 2012 21:13:56 +0000 (08:13 +1100)
This is implemented with a tdb_traverse_read(), and will allow a tool
to disover the name and server_id of all Samba processes, as each
process registers itself to recieve messages.

Andrew Bartlett

source4/lib/messaging/irpc.h
source4/lib/messaging/messaging.c
source4/librpc/idl/irpc.idl

index 15f8259e5148f6ea6ef8d42b8dba5e16cce7975c..456d1906e0c3bf747d62320787526bd000923b7d 100644 (file)
@@ -75,6 +75,8 @@ void irpc_binding_handle_add_security_token(struct dcerpc_binding_handle *h,
 
 NTSTATUS irpc_add_name(struct imessaging_context *msg_ctx, const char *name);
 struct server_id *irpc_servers_byname(struct imessaging_context *msg_ctx, TALLOC_CTX *mem_ctx, const char *name);
+struct irpc_name_records *irpc_all_servers(struct imessaging_context *msg_ctx,
+                                          TALLOC_CTX *mem_ctx);
 void irpc_remove_name(struct imessaging_context *msg_ctx, const char *name);
 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status);
 
index 4d69b9424bdfea4eb50f4b148f1d7b756df7bbf2..66188971f30fe19d93aa121114c69fcd6fb0dd99 100644 (file)
@@ -985,6 +985,77 @@ struct server_id *irpc_servers_byname(struct imessaging_context *msg_ctx,
        return ret;
 }
 
+static int all_servers_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
+{
+       struct irpc_name_records *name_records = talloc_get_type(state, struct irpc_name_records);
+       struct irpc_name_record *name_record;
+       int i;
+
+       name_records->names
+               = talloc_realloc(name_records, name_records->names,
+                                struct irpc_name_record *, name_records->num_records+1);
+       if (!name_records->names) {
+               return -1;
+       }
+
+       name_records->names[name_records->num_records] = name_record
+               = talloc(name_records->names,
+                        struct irpc_name_record);
+       if (!name_record) {
+               return -1;
+       }
+
+       name_records->num_records++;
+
+       name_record->name
+               = talloc_strndup(name_record,
+                                (const char *)key.dptr, key.dsize);
+       if (!name_record->name) {
+               return -1;
+       }
+
+       name_record->count = data.dsize / sizeof(struct server_id);
+       name_record->ids = talloc_array(name_record,
+                                       struct server_id,
+                                       name_record->count);
+       if (name_record->ids == NULL) {
+               return -1;
+       }
+       for (i=0;i<name_record->count;i++) {
+               name_record->ids[i] = ((struct server_id *)data.dptr)[i];
+       }
+       return 0;
+}
+
+/*
+  return a list of server ids for a server name
+*/
+struct irpc_name_records *irpc_all_servers(struct imessaging_context *msg_ctx,
+                                          TALLOC_CTX *mem_ctx)
+{
+       struct tdb_wrap *t;
+       int ret;
+       struct irpc_name_records *name_records = talloc_zero(mem_ctx, struct irpc_name_records);
+       if (name_records == NULL) {
+               return NULL;
+       }
+
+       t = irpc_namedb_open(msg_ctx);
+       if (t == NULL) {
+               return NULL;
+       }
+
+       ret = tdb_traverse_read(t->tdb, all_servers_func, name_records);
+       if (ret == -1) {
+               talloc_free(t);
+               return NULL;
+       }
+
+       talloc_free(t);
+
+       return name_records;
+}
+
 /*
   remove a name from a messaging context
 */
index ed331c7fdbb485ad73665645cc43a8f30d3575fa..6a55eef9532ad13ff1454ea0cd708234a0d632bc 100644 (file)
@@ -1,6 +1,6 @@
 #include "idl_types.h"
 
-import "misc.idl", "security.idl", "nbt.idl", "netlogon.idl";
+import "misc.idl", "security.idl", "nbt.idl", "netlogon.idl", "server_id.idl";
 
 /*
   definitions for irpc primitives
@@ -29,6 +29,17 @@ import "misc.idl", "security.idl", "nbt.idl", "netlogon.idl";
                [flag(NDR_ALIGN8)] DATA_BLOB _pad;
        } irpc_header;
 
+       typedef [public] struct {
+               utf8string name;
+               uint32 count;
+               [size_is(count)] server_id ids[*];
+       } irpc_name_record;
+
+       typedef [public] struct {
+               [size_is(num_records)] irpc_name_record *names[*];
+               uint32 num_records;
+       } irpc_name_records;
+
        /******************************************************
          uptime call - supported by all messaging servers
        *******************************************************/