More updates to RPC.
[wrappi.git] / daemon / wrappid.c
1 /* wrappi
2  * Copyright (C) 2011-2012 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <rpc/xdr.h>
24
25 #include "wrappi.h"
26
27 /* XXX XDR code should be in its own mini-library.
28  * OR we should export a way to serialize function calls.
29  */
30 #include "internal.h"
31
32 static void main_loop (void);
33
34 static wrap_h *w;
35
36 int
37 main (int argc, char *argv[])
38 {
39   /* Command line XXX */
40
41   w = wrap_create ();
42   if (!w) {
43     fprintf (stderr, "could not allocate wrappi handle\n");
44     exit (EXIT_FAILURE);
45   }
46
47   wrap_connect (w);
48   if (wrap_error (w))
49     exit (EXIT_FAILURE);
50
51   main_loop ();
52
53   wrap_close (w);
54
55   exit (EXIT_SUCCESS);
56 }
57
58 static void
59 main_loop (void)
60 {
61   XDR xdr;
62   xdrproc_t xdrp_args;
63   xdrproc_t xdrp_ret;
64   struct wrap_int_message_header hdr;
65   struct wrap_int_message_error err;
66   size_t i;
67
68   for (;;) {
69     /* Receive the request header. */
70     xdrstdio_create (&xdr, stdin, XDR_DECODE);
71     memset (&hdr, 0, sizeof hdr);
72     if (!wrap_int_xdr_message_header (&xdr, &hdr)) {
73       fprintf (stderr, "error receiving request header\n");
74       return;
75     }
76
77     if (hdr.magic != WRAP_INT_PROTO_MAGIC) {
78       fprintf (stderr, "error in request: unexpected magic (%x)\n",
79                hdr.magic);
80     return;
81     }
82     if (hdr.protocol != WRAP_INT_PROTOCOL) {
83       fprintf (stderr, "error in request: unexpected protocol number (%d)\n",
84                hdr.protocol);
85       return;
86     }
87     if (hdr.type != WRAP_INT_PROTO_TYPE_REQUEST) {
88       fprintf (stderr, "error in request: unexpected type (%d)\n",
89                hdr.type);
90       return;
91     }
92
93     /* Convert the proc name to the internal entry point. */
94     /* XXX We're going to use gperf here, as we do in libguestfs. */
95     for (i = 0; i < wrap_int_nr_procs; ++i) {
96       if (STREQ (wrap_int_proc_table[i].name, hdr.proc))
97         goto found;
98     }
99     fprintf (stderr, "unknown proc name in request: %s\n", hdr.proc);
100     exit (EXIT_FAILURE);
101
102   found:
103     xdrp_args = wrap_int_proc_table[i].xdr_args;
104     assert (xdrp_args);
105     xdrp_ret = wrap_int_proc_table[i].xdr_ret;
106     assert (xdrp_ret);
107
108     /* Receive the arguments. */
109     
110
111
112
113   }
114 }