MOUNT: describe mount version 1
[deb_libnfs.git] / lib / pdu.c
CommitLineData
84004dbf
RS
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*/
a8a1b858
M
17#ifdef WIN32
18#include "win32_compat.h"
19#ifndef MSG_DONTWAIT
6874f61e 20#define MSG_DONTWAIT 0
a8a1b858 21#endif
6874f61e 22#else
84004dbf 23#include <strings.h>
a8a1b858 24#endif/*WIN32*/
6874f61e
RS
25
26#include <stdio.h>
98f5fee8 27#include <stdlib.h>
a669857d 28#include <errno.h>
98f5fee8 29#include <rpc/rpc.h>
84004dbf
RS
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) {
84004dbf
RS
43 return NULL;
44 }
45
46 pdu = malloc(sizeof(struct rpc_pdu));
47 if (pdu == NULL) {
1896d37b 48 rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure");
84004dbf
RS
49 return NULL;
50 }
ea98629a 51 memset(pdu, 0, sizeof(struct rpc_pdu));
84004dbf
RS
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);
a1992412
RS
59 if (rpc->is_udp == 0) {
60 xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */
61 }
84004dbf 62
ea98629a 63 memset(&msg, 0, sizeof(struct rpc_msg));
84004dbf
RS
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) {
1896d37b 74 rpc_set_error(rpc, "xdr_callmsg failed");
84004dbf
RS
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);
df5af25f 97
84004dbf
RS
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
a669857d
RS
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
84004dbf
RS
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
84004dbf
RS
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
ea98629a 151 memset(&msg, 0, sizeof(struct rpc_msg));
84004dbf
RS
152 msg.acpted_rply.ar_verf = _null_auth;
153 if (pdu->xdr_decode_bufsize > 0) {
1b9917b8
RS
154 if (pdu->xdr_decode_buf != NULL) {
155 free(pdu->xdr_decode_buf);
156 }
84004dbf
RS
157 pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize);
158 if (pdu->xdr_decode_buf == NULL) {
1896d37b 159 rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply");
84004dbf
RS
160 pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data);
161 return 0;
162 }
ea98629a 163 memset(pdu->xdr_decode_buf, 0, pdu->xdr_decode_bufsize);
84004dbf
RS
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) {
1896d37b 169 rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply");
84004dbf
RS
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;
d678b73e 212 int pos, recordmarker = 0;
84004dbf 213 unsigned int xid;
d678b73e 214 char *reasbuf = NULL;
84004dbf 215
ea98629a 216 memset(&xdr, 0, sizeof(XDR));
84004dbf
RS
217
218 xdrmem_create(&xdr, buf, size, XDR_DECODE);
a669857d
RS
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 }
d678b73e
RS
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 }
84004dbf 233 }
d678b73e
RS
234
235 /* reassembly */
236 if (recordmarker != 0 && rpc->fragments != NULL) {
237 struct rpc_fragment *fragment;
183451cf 238 uint64_t total = size - 4;
d678b73e
RS
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
84004dbf
RS
262 pos = xdr_getpos(&xdr);
263 if (xdr_int(&xdr, (int *)&xid) == 0) {
1896d37b 264 rpc_set_error(rpc, "xdr_int reading xid failed");
84004dbf 265 xdr_destroy(&xdr);
d678b73e
RS
266 if (reasbuf != NULL) {
267 free(reasbuf);
268 }
84004dbf
RS
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 }
a669857d
RS
277 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
278 SLIST_REMOVE(&rpc->waitpdu, pdu);
279 }
84004dbf 280 if (rpc_process_reply(rpc, pdu, &xdr) != 0) {
1896d37b 281 rpc_set_error(rpc, "rpc_procdess_reply failed");
84004dbf
RS
282 }
283 xdr_destroy(&xdr);
a669857d
RS
284 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
285 rpc_free_pdu(rpc, pdu);
286 }
d678b73e
RS
287 if (reasbuf != NULL) {
288 free(reasbuf);
289 }
84004dbf
RS
290 return 0;
291 }
1896d37b 292 rpc_set_error(rpc, "No matching pdu found for xid:%d", xid);
84004dbf 293 xdr_destroy(&xdr);
d678b73e
RS
294 if (reasbuf != NULL) {
295 free(reasbuf);
296 }
84004dbf
RS
297 return -1;
298}
299