Combine generator subdirectories into one.
[wrappi.git] / lib / proto-xdr-request.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 <assert.h>
24 #include <rpc/xdr.h>
25
26 #include "wrappi.h"
27 #include "internal.h"
28 #include "proto-xdr.h"
29
30 void
31 wrap_int_make_request_xdr (wrap_h *w, int proc_nr, const void *args, void *ret)
32 {
33   XDR xdr;
34   xdrproc_t args_xdrproc;
35   xdrproc_t ret_xdrproc;
36   struct wrap_int_message_header hdr;
37   struct wrap_int_message_error err;
38
39   memset (&hdr, 0, sizeof hdr);
40
41   hdr.proc = (char *) wrap_int_proc_table[proc_nr].name;
42   assert (hdr.proc);
43   args_xdrproc = wrap_int_proc_table[proc_nr].args_xdrproc;
44   ret_xdrproc = wrap_int_proc_table[proc_nr].ret_xdrproc;
45
46   /* Construct the header. */
47   w->serial++;
48
49   hdr.magic = WRAP_INT_PROTO_MAGIC;
50   hdr.protocol = WRAP_INT_PROTOCOL;
51   hdr.serial = w->serial;
52   hdr.type = WRAP_INT_PROTO_TYPE_REQUEST;
53
54   /* Send the header. */
55   xdrstdio_create (&xdr, w->wfp, XDR_ENCODE);
56   if (!wrap_int_xdr_message_header (&xdr, &hdr)) {
57     set_error ("error sending request header");
58     xdr_destroy (&xdr);
59     return;
60   }
61
62   /* Send the request arguments. */
63   if (!args_xdrproc (&xdr, (void *) args)) {
64     set_error ("error sending request arguments");
65     xdr_destroy (&xdr);
66     return;
67   }
68   xdr_destroy (&xdr);
69
70   /* Receive the reply header. */
71   xdrstdio_create (&xdr, w->rfp, XDR_DECODE);
72   memset (&hdr, 0, sizeof hdr);
73   if (!wrap_int_xdr_message_header (&xdr, &hdr)) {
74     set_error ("error receiving reply header");
75     xdr_destroy (&xdr);
76     return;
77   }
78
79   /* Check the reply header. */
80   if (hdr.magic != WRAP_INT_PROTO_MAGIC) {
81     set_error ("error in reply: unexpected magic (%x)", hdr.magic);
82     xdr_destroy (&xdr);
83     xdr_free ((xdrproc_t) wrap_int_xdr_message_header, (void *) &hdr);
84     return;
85   }
86   if (hdr.protocol != WRAP_INT_PROTOCOL) {
87     set_error ("error in reply: unexpected protocol number (%d)", hdr.protocol);
88     xdr_destroy (&xdr);
89     xdr_free ((xdrproc_t) wrap_int_xdr_message_header, (void *) &hdr);
90     return;
91   }
92   if (hdr.serial != w->serial) {
93     set_error ("error in reply: unexpected serial (%d)", hdr.serial);
94     xdr_destroy (&xdr);
95     xdr_free ((xdrproc_t) wrap_int_xdr_message_header, (void *) &hdr);
96     return;
97   }
98
99   switch (hdr.type) {
100   case WRAP_INT_PROTO_TYPE_REPLY:
101     /* Receive the return value. */
102     if (!ret_xdrproc (&xdr, (void *) ret)) {
103       set_error ("error receiving return value");
104       xdr_destroy (&xdr);
105       xdr_free ((xdrproc_t) wrap_int_xdr_message_header, (void *) &hdr);
106       return;
107     }
108     break;
109
110   case WRAP_INT_PROTO_TYPE_ERROR:
111     /* Receive the error message etc. */
112     memset (&err, 0, sizeof err);
113     if (!wrap_int_xdr_message_error (&xdr, &err)) {
114       set_error ("error receiving error message");
115       xdr_destroy (&xdr);
116       xdr_free ((xdrproc_t) wrap_int_xdr_message_header, (void *) &hdr);
117       return;
118     }
119
120     /* XXX errno should be converted to an integer here */
121     /* XXX func should be set, but it's not static! */
122     set_error ("%s", err.error_message);
123     xdr_free ((xdrproc_t) wrap_int_xdr_message_error, (void *) &err);
124     xdr_free ((xdrproc_t) wrap_int_xdr_message_header, (void *) &hdr);
125
126     break;
127
128   default:
129     set_error ("error in reply: unexpected type (%x)", hdr.type);
130     xdr_destroy (&xdr);
131     xdr_free ((xdrproc_t) wrap_int_xdr_message_header, (void *) &hdr);
132     return;
133   }
134
135   xdr_destroy (&xdr);
136   xdr_free ((xdrproc_t) wrap_int_xdr_message_header, (void *) &hdr);
137 }