Prepare for new upstream release.
[deb_libnfs.git] / lib / pdu.c
CommitLineData
dabf4152
AM
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16*/
5670ec6e
AM
17#ifdef WIN32
18#include "win32_compat.h"
19#ifndef MSG_DONTWAIT
20#define MSG_DONTWAIT 0
21#endif
22#else
23#include <strings.h>
24#endif/*WIN32*/
dabf4152
AM
25
26#include <stdio.h>
dabf4152
AM
27#include <stdlib.h>
28#include <errno.h>
29#include <rpc/rpc.h>
30#include <rpc/xdr.h>
31#include <rpc/rpc_msg.h>
32#include "slist.h"
33#include "libnfs.h"
34#include "libnfs-raw.h"
35#include "libnfs-private.h"
36
37struct 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)
38{
39 struct rpc_pdu *pdu;
40 struct rpc_msg msg;
41
42 if (rpc == NULL) {
43 return NULL;
44 }
45
46 pdu = malloc(sizeof(struct rpc_pdu));
47 if (pdu == NULL) {
48 rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure");
49 return NULL;
50 }
5670ec6e 51 memset(pdu, 0, sizeof(struct rpc_pdu));
dabf4152
AM
52 pdu->xid = rpc->xid++;
53 pdu->cb = cb;
54 pdu->private_data = private_data;
55 pdu->xdr_decode_fn = xdr_decode_fn;
56 pdu->xdr_decode_bufsize = xdr_decode_bufsize;
57
58 xdrmem_create(&pdu->xdr, rpc->encodebuf, rpc->encodebuflen, XDR_ENCODE);
59 if (rpc->is_udp == 0) {
60 xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */
61 }
62
5670ec6e 63 memset(&msg, 0, sizeof(struct rpc_msg));
dabf4152
AM
64 msg.rm_xid = pdu->xid;
65 msg.rm_direction = CALL;
66 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
67 msg.rm_call.cb_prog = program;
68 msg.rm_call.cb_vers = version;
69 msg.rm_call.cb_proc = procedure;
70 msg.rm_call.cb_cred = rpc->auth->ah_cred;
71 msg.rm_call.cb_verf = rpc->auth->ah_verf;
72
73 if (xdr_callmsg(&pdu->xdr, &msg) == 0) {
74 rpc_set_error(rpc, "xdr_callmsg failed");
75 xdr_destroy(&pdu->xdr);
76 free(pdu);
77 return NULL;
78 }
79
80 return pdu;
81}
82
83void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu)
84{
85 if (pdu->outdata.data != NULL) {
86 free(pdu->outdata.data);
87 pdu->outdata.data = NULL;
88 }
89
90 if (pdu->xdr_decode_buf != NULL) {
91 xdr_free(pdu->xdr_decode_fn, pdu->xdr_decode_buf);
92 free(pdu->xdr_decode_buf);
93 pdu->xdr_decode_buf = NULL;
94 }
95
96 xdr_destroy(&pdu->xdr);
97
98 free(pdu);
99}
100
101
102int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
103{
104 int size, recordmarker;
105
106 size = xdr_getpos(&pdu->xdr);
107
108 /* for udp we dont queue, we just send it straight away */
109 if (rpc->is_udp != 0) {
110 if (sendto(rpc->fd, rpc->encodebuf, size, MSG_DONTWAIT, rpc->udp_dest, sizeof(struct sockaddr_in)) < 0) {
111 rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno));
112 rpc_free_pdu(rpc, pdu);
113 return -1;
114 }
115 SLIST_ADD_END(&rpc->waitpdu, pdu);
116 return 0;
117 }
118
119 /* write recordmarker */
120 xdr_setpos(&pdu->xdr, 0);
121 recordmarker = (size - 4) | 0x80000000;
122 xdr_int(&pdu->xdr, &recordmarker);
123
124 pdu->outdata.size = size;
125 pdu->outdata.data = malloc(pdu->outdata.size);
126 if (pdu->outdata.data == NULL) {
127 rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n");
128 rpc_free_pdu(rpc, pdu);
129 return -1;
130 }
131
132 memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size);
133 SLIST_ADD_END(&rpc->outqueue, pdu);
134
135 return 0;
136}
137
138int rpc_get_pdu_size(char *buf)
139{
140 uint32_t size;
141
142 size = ntohl(*(uint32_t *)buf);
143
dabf4152
AM
144 return (size & 0x7fffffff) + 4;
145}
146
147static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr)
148{
149 struct rpc_msg msg;
150
5670ec6e 151 memset(&msg, 0, sizeof(struct rpc_msg));
dabf4152
AM
152 msg.acpted_rply.ar_verf = _null_auth;
153 if (pdu->xdr_decode_bufsize > 0) {
154 if (pdu->xdr_decode_buf != NULL) {
155 free(pdu->xdr_decode_buf);
156 }
157 pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize);
158 if (pdu->xdr_decode_buf == NULL) {
159 rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply");
160 pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data);
161 return 0;
162 }
5670ec6e 163 memset(pdu->xdr_decode_buf, 0, pdu->xdr_decode_bufsize);
dabf4152
AM
164 }
165 msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf;
166 msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn;
167
168 if (xdr_replymsg(xdr, &msg) == 0) {
169 rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply");
170 pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data);
171 if (pdu->xdr_decode_buf != NULL) {
172 free(pdu->xdr_decode_buf);
173 pdu->xdr_decode_buf = NULL;
174 }
175 return 0;
176 }
177 if (msg.rm_reply.rp_stat != MSG_ACCEPTED) {
178 pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data);
179 return 0;
180 }
181 switch (msg.rm_reply.rp_acpt.ar_stat) {
182 case SUCCESS:
183 pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data);
184 break;
185 case PROG_UNAVAIL:
186 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data);
187 break;
188 case PROG_MISMATCH:
189 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data);
190 break;
191 case PROC_UNAVAIL:
192 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data);
193 break;
194 case GARBAGE_ARGS:
195 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data);
196 break;
197 case SYSTEM_ERR:
198 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data);
199 break;
200 default:
201 pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data);
202 break;
203 }
204
205 return 0;
206}
207
208int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size)
209{
210 struct rpc_pdu *pdu;
211 XDR xdr;
5670ec6e 212 int pos, recordmarker = 0;
dabf4152 213 unsigned int xid;
5670ec6e 214 char *reasbuf = NULL;
dabf4152 215
5670ec6e 216 memset(&xdr, 0, sizeof(XDR));
dabf4152
AM
217
218 xdrmem_create(&xdr, buf, size, XDR_DECODE);
219 if (rpc->is_udp == 0) {
220 if (xdr_int(&xdr, &recordmarker) == 0) {
221 rpc_set_error(rpc, "xdr_int reading recordmarker failed");
222 xdr_destroy(&xdr);
223 return -1;
224 }
5670ec6e
AM
225 if (!(recordmarker&0x80000000)) {
226 xdr_destroy(&xdr);
227 if (rpc_add_fragment(rpc, buf+4, size-4) != 0) {
228 rpc_set_error(rpc, "Failed to queue fragment for reassembly.");
229 return -1;
230 }
231 return 0;
232 }
dabf4152 233 }
5670ec6e
AM
234
235 /* reassembly */
236 if (recordmarker != 0 && rpc->fragments != NULL) {
237 struct rpc_fragment *fragment;
238 uint64_t total = size - 4;
239 char *ptr;
240
241 xdr_destroy(&xdr);
242 for (fragment = rpc->fragments; fragment; fragment = fragment->next) {
243 total += fragment->size;
244 }
245
246 reasbuf = malloc(total);
247 if (reasbuf == NULL) {
248 rpc_set_error(rpc, "Failed to reassemble PDU");
249 rpc_free_all_fragments(rpc);
250 return -1;
251 }
252 ptr = reasbuf;
253 for (fragment = rpc->fragments; fragment; fragment = fragment->next) {
254 memcpy(ptr, fragment->data, fragment->size);
255 ptr += fragment->size;
256 }
257 memcpy(ptr, buf + 4, size - 4);
258 xdrmem_create(&xdr, reasbuf, total, XDR_DECODE);
259 rpc_free_all_fragments(rpc);
260 }
261
dabf4152
AM
262 pos = xdr_getpos(&xdr);
263 if (xdr_int(&xdr, (int *)&xid) == 0) {
264 rpc_set_error(rpc, "xdr_int reading xid failed");
265 xdr_destroy(&xdr);
5670ec6e
AM
266 if (reasbuf != NULL) {
267 free(reasbuf);
268 }
dabf4152
AM
269 return -1;
270 }
271 xdr_setpos(&xdr, pos);
272
273 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
274 if (pdu->xid != xid) {
275 continue;
276 }
277 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
278 SLIST_REMOVE(&rpc->waitpdu, pdu);
279 }
280 if (rpc_process_reply(rpc, pdu, &xdr) != 0) {
281 rpc_set_error(rpc, "rpc_procdess_reply failed");
282 }
283 xdr_destroy(&xdr);
284 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
285 rpc_free_pdu(rpc, pdu);
286 }
5670ec6e
AM
287 if (reasbuf != NULL) {
288 free(reasbuf);
289 }
dabf4152
AM
290 return 0;
291 }
292 rpc_set_error(rpc, "No matching pdu found for xid:%d", xid);
293 xdr_destroy(&xdr);
5670ec6e
AM
294 if (reasbuf != NULL) {
295 free(reasbuf);
296 }
dabf4152
AM
297 return -1;
298}
299