Merge branch 'ronnie'
[ccan] / ccan / nfs / pdu.c
1 /* 
2    Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
3    
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8    
9    This program 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
12    GNU General Public License for more details.
13    
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdio.h>
19 #include <strings.h>
20 #include <rpc/xdr.h>
21 #include <rpc/rpc_msg.h>
22 #include "dlinklist.h"
23 #include "nfs.h"
24 #include "libnfs-raw.h"
25 #include "libnfs-private.h"
26
27 struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, xdrproc_t xdr_decode_fn, int xdr_decode_bufsize)
28 {
29         struct rpc_pdu *pdu;
30         struct rpc_msg msg;
31
32         if (rpc == NULL) {
33                 printf("trying to allocate rpc pdu on NULL context\n");
34                 return NULL;
35         }
36
37         pdu = malloc(sizeof(struct rpc_pdu));
38         if (pdu == NULL) {
39                 printf("Failed to allocate pdu structure\n");
40                 return NULL;
41         }
42         bzero(pdu, sizeof(struct rpc_pdu));
43         pdu->xid                = rpc->xid++;
44         pdu->cb                 = cb;
45         pdu->private_data       = private_data;
46         pdu->xdr_decode_fn      = xdr_decode_fn;
47         pdu->xdr_decode_bufsize = xdr_decode_bufsize;
48
49         xdrmem_create(&pdu->xdr, rpc->encodebuf, rpc->encodebuflen, XDR_ENCODE);
50         xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */
51
52         bzero(&msg, sizeof(struct rpc_msg));
53         msg.rm_xid = pdu->xid;
54         msg.rm_direction = CALL;
55         msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
56         msg.rm_call.cb_prog = program;
57         msg.rm_call.cb_vers = version;
58         msg.rm_call.cb_proc = procedure;
59         msg.rm_call.cb_cred = rpc->auth->ah_cred;
60         msg.rm_call.cb_verf = rpc->auth->ah_verf;
61
62         if (xdr_callmsg(&pdu->xdr, &msg) == 0) {
63                 printf("xdr_callmsg failed\n");
64                 xdr_destroy(&pdu->xdr);
65                 free(pdu);
66                 return NULL;
67         }
68
69         return pdu;
70 }
71
72 void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu)
73 {
74         if (pdu->outdata.data != NULL) {
75                 free(pdu->outdata.data);
76                 pdu->outdata.data = NULL;
77         }
78
79         if (pdu->xdr_decode_buf != NULL) {
80                 xdr_free(pdu->xdr_decode_fn, pdu->xdr_decode_buf);
81                 free(pdu->xdr_decode_buf);
82                 pdu->xdr_decode_buf = NULL;
83         }
84
85         xdr_destroy(&pdu->xdr);
86         free(pdu);
87 }
88
89
90 int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
91 {
92         int size, recordmarker;
93
94         size = xdr_getpos(&pdu->xdr);
95
96         /* write recordmarker */
97         xdr_setpos(&pdu->xdr, 0);
98         recordmarker = (size - 4) | 0x80000000;
99         xdr_int(&pdu->xdr, &recordmarker);
100
101         pdu->outdata.size = size;
102         pdu->outdata.data = malloc(pdu->outdata.size);
103         if (pdu->outdata.data == NULL) {
104                 rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n");
105                 rpc_free_pdu(rpc, pdu);
106                 return -1;
107         }
108
109         memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size);
110         DLIST_ADD_END(rpc->outqueue, pdu, NULL);
111
112         return 0;
113 }
114
115 int rpc_get_pdu_size(char *buf)
116 {
117         uint32_t size;
118
119         size = ntohl(*(uint32_t *)buf);
120
121         if ((size & 0x80000000) == 0) {
122                 printf("cant handle oncrpc fragments\n");
123                 return -1;
124         }
125
126         return (size & 0x7fffffff) + 4;
127 }
128
129 static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr)
130 {
131         struct rpc_msg msg;
132
133         bzero(&msg, sizeof(struct rpc_msg));
134         msg.acpted_rply.ar_verf = _null_auth;
135         if (pdu->xdr_decode_bufsize > 0) {
136                 pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize);
137                 if (pdu->xdr_decode_buf == NULL) {
138                         printf("xdr_replymsg failed in portmap_getport_reply\n");
139                         pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data);
140                         return 0;
141                 }
142                 bzero(pdu->xdr_decode_buf, pdu->xdr_decode_bufsize);
143         }
144         msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf;
145         msg.acpted_rply.ar_results.proc  = pdu->xdr_decode_fn;
146
147         if (xdr_replymsg(xdr, &msg) == 0) {
148                 printf("xdr_replymsg failed in portmap_getport_reply\n");
149                 pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data);
150                 if (pdu->xdr_decode_buf != NULL) {
151                         free(pdu->xdr_decode_buf);
152                         pdu->xdr_decode_buf = NULL;
153                 }
154                 return 0;
155         }
156         if (msg.rm_reply.rp_stat != MSG_ACCEPTED) {
157                 pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data);
158                 return 0;
159         }
160         switch (msg.rm_reply.rp_acpt.ar_stat) {
161         case SUCCESS:
162                 pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data);
163                 break;
164         case PROG_UNAVAIL:
165                 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data);
166                 break;
167         case PROG_MISMATCH:
168                 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data);
169                 break;
170         case PROC_UNAVAIL:
171                 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data);
172                 break;
173         case GARBAGE_ARGS:
174                 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data);
175                 break;
176         case SYSTEM_ERR:
177                 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data);
178                 break;
179         default:
180                 pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data);
181                 break;
182         }
183
184         return 0;
185 }
186
187 int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size)
188 {
189         struct rpc_pdu *pdu;
190         XDR xdr;
191         int pos, recordmarker;
192         unsigned int xid;
193
194         bzero(&xdr, sizeof(XDR));
195
196         xdrmem_create(&xdr, buf, size, XDR_DECODE);
197         if (xdr_int(&xdr, &recordmarker) == 0) {
198                 printf("xdr_int reading recordmarker failed\n");
199                 xdr_destroy(&xdr);
200                 return -1;
201         }
202         pos = xdr_getpos(&xdr);
203         if (xdr_int(&xdr, (int *)&xid) == 0) {
204                 printf("xdr_int reading xid failed\n");
205                 xdr_destroy(&xdr);
206                 return -1;
207         }
208         xdr_setpos(&xdr, pos);
209
210         for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
211                 if (pdu->xid != xid) {
212                         continue;
213                 }
214                 DLIST_REMOVE(rpc->waitpdu, pdu);
215                 if (rpc_process_reply(rpc, pdu, &xdr) != 0) {
216                         printf("rpc_procdess_reply failed\n");
217                 }
218                 xdr_destroy(&xdr);
219                 rpc_free_pdu(rpc, pdu);
220                 return 0;
221         }
222         printf("No matching pdu found for xid:%d\n", xid);
223         xdr_destroy(&xdr);
224         return -1;
225 }
226