r3014: got rid of the old intra-smbd messaging system in preparation for the new one
[bbaumbach/samba-autobuild/.git] / source4 / utils / ndrdump.c
index d02d993604fc23bddd05ae20a86774beda3e1e4d..8c4f1fef77721233f1cb6b290f1938b36b0c89f3 100644 (file)
@@ -40,6 +40,10 @@ static const struct dcerpc_interface_call *find_function(
        const char *function)
 {
        int i;
+       if (isdigit(function[0])) {
+               i = strtol(function, NULL, 0);
+               return &p->calls[i];
+       }
        for (i=0;i<p->num_calls;i++) {
                if (strcmp(p->calls[i].name, function) == 0) {
                        break;
@@ -52,20 +56,18 @@ static const struct dcerpc_interface_call *find_function(
        return &p->calls[i];
 }
 
-static void usage(void)
-{
-       printf("Usage: ndrdump <pipe> <function> <inout> <filename>\n");
-}
-
 
 static void show_pipes(void)
 {
        int i;
-       usage();
        printf("\nYou must specify a pipe\n");
        printf("known pipes are:\n");
        for (i=0;dcerpc_pipes[i];i++) {
-               printf("\t%s\n", dcerpc_pipes[i]->name);
+               if(dcerpc_pipes[i]->helpstring) {
+                       printf("\t%s - %s\n", dcerpc_pipes[i]->name, dcerpc_pipes[i]->helpstring);
+               } else {
+                       printf("\t%s\n", dcerpc_pipes[i]->name);
+               }
        }
        exit(1);
 }
@@ -73,7 +75,6 @@ static void show_pipes(void)
 static void show_functions(const struct dcerpc_interface_table *p)
 {
        int i;
-       usage();
        printf("\nYou must specify a function\n");
        printf("known functions on '%s' are:\n", p->name);
        for (i=0;i<p->num_calls;i++) {
@@ -82,7 +83,7 @@ static void show_functions(const struct dcerpc_interface_table *p)
        exit(1);
 }
 
-int main(int argc, char *argv[])
+ int main(int argc, const char *argv[])
 {
        const struct dcerpc_interface_table *p;
        const struct dcerpc_interface_call *f;
@@ -93,32 +94,47 @@ int main(int argc, char *argv[])
        struct ndr_pull *ndr;
        TALLOC_CTX *mem_ctx;
        int flags;
+       poptContext pc;
        NTSTATUS status;
        void *st;
-       struct ndr_print pr;
+       int opt;
+       struct ndr_print *pr;
+       struct poptOption long_options[] = {
+               POPT_AUTOHELP
+               POPT_TABLEEND
+       };
 
        DEBUGLEVEL = 10;
 
-       setup_logging("smbtorture", DEBUG_STDOUT);
+       setup_logging("ndrdump", DEBUG_STDOUT);
+
+       pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
+       
+       poptSetOtherOptionHelp(pc, "<pipe> <function> <inout> <filename>");
 
-       if (argc < 2) {
+       while ((opt = poptGetNextOpt(pc)) != -1) {
+       }
+
+       pipe_name = poptGetArg(pc);
+
+       if (!pipe_name) {
+               poptPrintUsage(pc, stderr, 0);
                show_pipes();
                exit(1);
        }
 
-       pipe_name = argv[1];
-
        p = find_pipe(pipe_name);
 
-       if (argc < 5) {
+       function = poptGetArg(pc);
+       inout = poptGetArg(pc);
+       filename = poptGetArg(pc);
+
+       if (!function || !inout || !filename) {
+               poptPrintUsage(pc, stderr, 0);
                show_functions(p);
                exit(1);
        }
 
-       function = argv[2];
-       inout = argv[3];
-       filename = argv[4];
-
        if (strcmp(inout, "in") == 0 ||
            strcmp(inout, "request") == 0) {
                flags = NDR_IN;
@@ -161,12 +177,13 @@ int main(int argc, char *argv[])
 
        if (ndr->offset != ndr->data_size) {
                printf("WARNING! %d unread bytes\n", ndr->data_size - ndr->offset);
+               dump_data(0, ndr->data+ndr->offset, ndr->data_size - ndr->offset);
        }
 
-       pr.mem_ctx = mem_ctx;
-       pr.print = ndr_print_debug_helper;
-       pr.depth = 1;
-       f->ndr_print(&pr, function, flags, st);
+       pr = talloc_p(NULL, struct ndr_print);
+       pr->print = ndr_print_debug_helper;
+       pr->depth = 1;
+       f->ndr_print(pr, function, flags, st);
 
        if (!NT_STATUS_IS_OK(status) ||
            ndr->offset != ndr->data_size) {
@@ -175,6 +192,10 @@ int main(int argc, char *argv[])
        }
 
        printf("dump OK\n");
+
+       talloc_free(pr);
+       
+       poptFreeContext(pc);
        
        return 0;
 }