Imported Upstream version 1.9.3
[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*/
ee872606
RRS
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#ifdef AROS
22#include "aros_compat.h"
23#endif
24
5670ec6e
AM
25#ifdef WIN32
26#include "win32_compat.h"
5670ec6e 27#endif
ee872606
RRS
28
29#ifdef HAVE_NETINET_IN_H
30#include <netinet/in.h>
31#endif
32
33#ifdef HAVE_SYS_SOCKET_H
34#include <sys/socket.h>
35#endif
36
37#ifdef HAVE_STRINGS_H
5670ec6e 38#include <strings.h>
ee872606 39#endif
dabf4152
AM
40
41#include <stdio.h>
dabf4152 42#include <stdlib.h>
ee872606
RRS
43#include <string.h>
44#include <assert.h>
dabf4152 45#include <errno.h>
dabf4152 46#include "slist.h"
ee872606 47#include "libnfs-zdr.h"
dabf4152
AM
48#include "libnfs.h"
49#include "libnfs-raw.h"
50#include "libnfs-private.h"
51
ee872606 52struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, zdrproc_t zdr_decode_fn, int zdr_decode_bufsize)
dabf4152
AM
53{
54 struct rpc_pdu *pdu;
55 struct rpc_msg msg;
56
ee872606 57 assert(rpc->magic == RPC_CONTEXT_MAGIC);
dabf4152
AM
58
59 pdu = malloc(sizeof(struct rpc_pdu));
60 if (pdu == NULL) {
61 rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure");
62 return NULL;
63 }
5670ec6e 64 memset(pdu, 0, sizeof(struct rpc_pdu));
dabf4152
AM
65 pdu->xid = rpc->xid++;
66 pdu->cb = cb;
67 pdu->private_data = private_data;
ee872606
RRS
68 pdu->zdr_decode_fn = zdr_decode_fn;
69 pdu->zdr_decode_bufsize = zdr_decode_bufsize;
dabf4152 70
ee872606 71 zdrmem_create(&pdu->zdr, rpc->encodebuf, rpc->encodebuflen, ZDR_ENCODE);
dabf4152 72 if (rpc->is_udp == 0) {
ee872606 73 zdr_setpos(&pdu->zdr, 4); /* skip past the record marker */
dabf4152
AM
74 }
75
5670ec6e 76 memset(&msg, 0, sizeof(struct rpc_msg));
ee872606
RRS
77 msg.xid = pdu->xid;
78 msg.direction = CALL;
79 msg.body.cbody.rpcvers = RPC_MSG_VERSION;
80 msg.body.cbody.prog = program;
81 msg.body.cbody.vers = version;
82 msg.body.cbody.proc = procedure;
83 msg.body.cbody.cred = rpc->auth->ah_cred;
84 msg.body.cbody.verf = rpc->auth->ah_verf;
85
86 if (zdr_callmsg(rpc, &pdu->zdr, &msg) == 0) {
87 rpc_set_error(rpc, "zdr_callmsg failed with %s",
88 rpc_get_error(rpc));
89 zdr_destroy(&pdu->zdr);
dabf4152
AM
90 free(pdu);
91 return NULL;
92 }
93
94 return pdu;
95}
96
ee872606 97void rpc_free_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
dabf4152 98{
ee872606
RRS
99 assert(rpc->magic == RPC_CONTEXT_MAGIC);
100
dabf4152
AM
101 if (pdu->outdata.data != NULL) {
102 free(pdu->outdata.data);
103 pdu->outdata.data = NULL;
104 }
105
ee872606
RRS
106 if (pdu->zdr_decode_buf != NULL) {
107 zdr_free(pdu->zdr_decode_fn, pdu->zdr_decode_buf);
108 free(pdu->zdr_decode_buf);
109 pdu->zdr_decode_buf = NULL;
dabf4152
AM
110 }
111
ee872606 112 zdr_destroy(&pdu->zdr);
dabf4152
AM
113
114 free(pdu);
115}
116
ee872606
RRS
117void rpc_set_next_xid(struct rpc_context *rpc, uint32_t xid)
118{
119 rpc->xid = xid;
120}
dabf4152
AM
121
122int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
123{
124 int size, recordmarker;
125
ee872606
RRS
126 assert(rpc->magic == RPC_CONTEXT_MAGIC);
127
128 size = zdr_getpos(&pdu->zdr);
dabf4152
AM
129
130 /* for udp we dont queue, we just send it straight away */
131 if (rpc->is_udp != 0) {
ee872606 132// XXX add a rpc->udp_dest_sock_size and get rid of sys/socket.h and netinet/in.h
dabf4152
AM
133 if (sendto(rpc->fd, rpc->encodebuf, size, MSG_DONTWAIT, rpc->udp_dest, sizeof(struct sockaddr_in)) < 0) {
134 rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno));
135 rpc_free_pdu(rpc, pdu);
136 return -1;
137 }
138 SLIST_ADD_END(&rpc->waitpdu, pdu);
139 return 0;
140 }
141
142 /* write recordmarker */
ee872606 143 zdr_setpos(&pdu->zdr, 0);
dabf4152 144 recordmarker = (size - 4) | 0x80000000;
ee872606 145 zdr_int(&pdu->zdr, &recordmarker);
dabf4152
AM
146
147 pdu->outdata.size = size;
148 pdu->outdata.data = malloc(pdu->outdata.size);
149 if (pdu->outdata.data == NULL) {
150 rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n");
151 rpc_free_pdu(rpc, pdu);
152 return -1;
153 }
154
155 memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size);
156 SLIST_ADD_END(&rpc->outqueue, pdu);
157
158 return 0;
159}
160
161int rpc_get_pdu_size(char *buf)
162{
163 uint32_t size;
164
165 size = ntohl(*(uint32_t *)buf);
166
dabf4152
AM
167 return (size & 0x7fffffff) + 4;
168}
169
ee872606 170static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, ZDR *zdr)
dabf4152
AM
171{
172 struct rpc_msg msg;
173
ee872606
RRS
174 assert(rpc->magic == RPC_CONTEXT_MAGIC);
175
5670ec6e 176 memset(&msg, 0, sizeof(struct rpc_msg));
ee872606
RRS
177 msg.body.rbody.reply.areply.verf = _null_auth;
178 if (pdu->zdr_decode_bufsize > 0) {
179 if (pdu->zdr_decode_buf != NULL) {
180 free(pdu->zdr_decode_buf);
dabf4152 181 }
ee872606
RRS
182 pdu->zdr_decode_buf = malloc(pdu->zdr_decode_bufsize);
183 if (pdu->zdr_decode_buf == NULL) {
184 rpc_set_error(rpc, "Failed to allocate memory for "
185 "zdr_encode_buf in rpc_process_reply");
186 pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate "
187 "buffer for decoding of ZDR reply",
188 pdu->private_data);
dabf4152
AM
189 return 0;
190 }
ee872606 191 memset(pdu->zdr_decode_buf, 0, pdu->zdr_decode_bufsize);
dabf4152 192 }
ee872606
RRS
193 msg.body.rbody.reply.areply.reply_data.results.where = pdu->zdr_decode_buf;
194 msg.body.rbody.reply.areply.reply_data.results.proc = pdu->zdr_decode_fn;
195
196 if (zdr_replymsg(rpc, zdr, &msg) == 0) {
197 rpc_set_error(rpc, "zdr_replymsg failed in rpc_process_reply: "
198 "%s", rpc_get_error(rpc));
199 pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server",
200 pdu->private_data);
201 if (pdu->zdr_decode_buf != NULL) {
202 free(pdu->zdr_decode_buf);
203 pdu->zdr_decode_buf = NULL;
dabf4152
AM
204 }
205 return 0;
206 }
ee872606 207 if (msg.body.rbody.stat != MSG_ACCEPTED) {
dabf4152
AM
208 pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data);
209 return 0;
210 }
ee872606 211 switch (msg.body.rbody.reply.areply.stat) {
dabf4152 212 case SUCCESS:
ee872606 213 pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->zdr_decode_buf, pdu->private_data);
dabf4152
AM
214 break;
215 case PROG_UNAVAIL:
216 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data);
217 break;
218 case PROG_MISMATCH:
219 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data);
220 break;
221 case PROC_UNAVAIL:
222 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data);
223 break;
224 case GARBAGE_ARGS:
225 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data);
226 break;
227 case SYSTEM_ERR:
228 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data);
229 break;
230 default:
231 pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data);
232 break;
233 }
234
235 return 0;
236}
237
238int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size)
239{
240 struct rpc_pdu *pdu;
ee872606 241 ZDR zdr;
5670ec6e 242 int pos, recordmarker = 0;
ee872606 243 uint32_t xid;
5670ec6e 244 char *reasbuf = NULL;
dabf4152 245
ee872606
RRS
246 assert(rpc->magic == RPC_CONTEXT_MAGIC);
247
248 memset(&zdr, 0, sizeof(ZDR));
dabf4152 249
ee872606 250 zdrmem_create(&zdr, buf, size, ZDR_DECODE);
dabf4152 251 if (rpc->is_udp == 0) {
ee872606
RRS
252 if (zdr_int(&zdr, &recordmarker) == 0) {
253 rpc_set_error(rpc, "zdr_int reading recordmarker failed");
254 zdr_destroy(&zdr);
dabf4152
AM
255 return -1;
256 }
5670ec6e 257 if (!(recordmarker&0x80000000)) {
ee872606 258 zdr_destroy(&zdr);
5670ec6e
AM
259 if (rpc_add_fragment(rpc, buf+4, size-4) != 0) {
260 rpc_set_error(rpc, "Failed to queue fragment for reassembly.");
261 return -1;
262 }
263 return 0;
264 }
dabf4152 265 }
5670ec6e
AM
266
267 /* reassembly */
268 if (recordmarker != 0 && rpc->fragments != NULL) {
269 struct rpc_fragment *fragment;
ee872606 270 uint32_t total = size - 4;
5670ec6e
AM
271 char *ptr;
272
ee872606 273 zdr_destroy(&zdr);
5670ec6e
AM
274 for (fragment = rpc->fragments; fragment; fragment = fragment->next) {
275 total += fragment->size;
276 }
277
278 reasbuf = malloc(total);
279 if (reasbuf == NULL) {
280 rpc_set_error(rpc, "Failed to reassemble PDU");
281 rpc_free_all_fragments(rpc);
282 return -1;
283 }
284 ptr = reasbuf;
285 for (fragment = rpc->fragments; fragment; fragment = fragment->next) {
286 memcpy(ptr, fragment->data, fragment->size);
287 ptr += fragment->size;
288 }
289 memcpy(ptr, buf + 4, size - 4);
ee872606 290 zdrmem_create(&zdr, reasbuf, total, ZDR_DECODE);
5670ec6e
AM
291 rpc_free_all_fragments(rpc);
292 }
293
ee872606
RRS
294 pos = zdr_getpos(&zdr);
295 if (zdr_int(&zdr, (int *)&xid) == 0) {
296 rpc_set_error(rpc, "zdr_int reading xid failed");
297 zdr_destroy(&zdr);
5670ec6e
AM
298 if (reasbuf != NULL) {
299 free(reasbuf);
300 }
dabf4152
AM
301 return -1;
302 }
ee872606 303 zdr_setpos(&zdr, pos);
dabf4152
AM
304
305 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
306 if (pdu->xid != xid) {
307 continue;
308 }
309 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
310 SLIST_REMOVE(&rpc->waitpdu, pdu);
311 }
ee872606 312 if (rpc_process_reply(rpc, pdu, &zdr) != 0) {
dabf4152
AM
313 rpc_set_error(rpc, "rpc_procdess_reply failed");
314 }
ee872606 315 zdr_destroy(&zdr);
dabf4152
AM
316 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
317 rpc_free_pdu(rpc, pdu);
318 }
5670ec6e
AM
319 if (reasbuf != NULL) {
320 free(reasbuf);
321 }
dabf4152
AM
322 return 0;
323 }
324 rpc_set_error(rpc, "No matching pdu found for xid:%d", xid);
ee872606 325 zdr_destroy(&zdr);
5670ec6e
AM
326 if (reasbuf != NULL) {
327 free(reasbuf);
328 }
dabf4152
AM
329 return -1;
330}
331