ZDR: New builtin replacement for RPC/XDR called ZDR
[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>
763cd6e3
RS
28#include <string.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
a669857d 31#include <errno.h>
84004dbf 32#include "slist.h"
763cd6e3 33#include "libnfs-zdr.h"
84004dbf
RS
34#include "libnfs.h"
35#include "libnfs-raw.h"
36#include "libnfs-private.h"
37
763cd6e3 38struct 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)
84004dbf
RS
39{
40 struct rpc_pdu *pdu;
41 struct rpc_msg msg;
42
43 if (rpc == NULL) {
84004dbf
RS
44 return NULL;
45 }
46
47 pdu = malloc(sizeof(struct rpc_pdu));
48 if (pdu == NULL) {
1896d37b 49 rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure");
84004dbf
RS
50 return NULL;
51 }
ea98629a 52 memset(pdu, 0, sizeof(struct rpc_pdu));
84004dbf
RS
53 pdu->xid = rpc->xid++;
54 pdu->cb = cb;
55 pdu->private_data = private_data;
763cd6e3
RS
56 pdu->zdr_decode_fn = zdr_decode_fn;
57 pdu->zdr_decode_bufsize = zdr_decode_bufsize;
84004dbf 58
763cd6e3 59 zdrmem_create(&pdu->zdr, rpc->encodebuf, rpc->encodebuflen, ZDR_ENCODE);
a1992412 60 if (rpc->is_udp == 0) {
763cd6e3 61 zdr_setpos(&pdu->zdr, 4); /* skip past the record marker */
a1992412 62 }
84004dbf 63
ea98629a 64 memset(&msg, 0, sizeof(struct rpc_msg));
84004dbf
RS
65 msg.rm_xid = pdu->xid;
66 msg.rm_direction = CALL;
67 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
68 msg.rm_call.cb_prog = program;
69 msg.rm_call.cb_vers = version;
70 msg.rm_call.cb_proc = procedure;
71 msg.rm_call.cb_cred = rpc->auth->ah_cred;
72 msg.rm_call.cb_verf = rpc->auth->ah_verf;
73
763cd6e3
RS
74 if (zdr_callmsg(&pdu->zdr, &msg) == 0) {
75 rpc_set_error(rpc, "zdr_callmsg failed");
76 zdr_destroy(&pdu->zdr);
84004dbf
RS
77 free(pdu);
78 return NULL;
79 }
80
81 return pdu;
82}
83
84void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu)
85{
86 if (pdu->outdata.data != NULL) {
87 free(pdu->outdata.data);
88 pdu->outdata.data = NULL;
89 }
90
763cd6e3
RS
91 if (pdu->zdr_decode_buf != NULL) {
92 zdr_free(pdu->zdr_decode_fn, pdu->zdr_decode_buf);
93 free(pdu->zdr_decode_buf);
94 pdu->zdr_decode_buf = NULL;
84004dbf
RS
95 }
96
763cd6e3 97 zdr_destroy(&pdu->zdr);
df5af25f 98
84004dbf
RS
99 free(pdu);
100}
101
102
103int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
104{
105 int size, recordmarker;
106
763cd6e3 107 size = zdr_getpos(&pdu->zdr);
84004dbf 108
a669857d
RS
109 /* for udp we dont queue, we just send it straight away */
110 if (rpc->is_udp != 0) {
763cd6e3 111// XXX add a rpc->udp_dest_sock_size and get rid of sys/socket.h and netinet/in.h
a669857d
RS
112 if (sendto(rpc->fd, rpc->encodebuf, size, MSG_DONTWAIT, rpc->udp_dest, sizeof(struct sockaddr_in)) < 0) {
113 rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno));
114 rpc_free_pdu(rpc, pdu);
115 return -1;
116 }
117 SLIST_ADD_END(&rpc->waitpdu, pdu);
118 return 0;
119 }
120
84004dbf 121 /* write recordmarker */
763cd6e3 122 zdr_setpos(&pdu->zdr, 0);
84004dbf 123 recordmarker = (size - 4) | 0x80000000;
763cd6e3 124 zdr_int(&pdu->zdr, &recordmarker);
84004dbf
RS
125
126 pdu->outdata.size = size;
127 pdu->outdata.data = malloc(pdu->outdata.size);
128 if (pdu->outdata.data == NULL) {
129 rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n");
130 rpc_free_pdu(rpc, pdu);
131 return -1;
132 }
133
134 memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size);
135 SLIST_ADD_END(&rpc->outqueue, pdu);
136
137 return 0;
138}
139
140int rpc_get_pdu_size(char *buf)
141{
142 uint32_t size;
143
144 size = ntohl(*(uint32_t *)buf);
145
84004dbf
RS
146 return (size & 0x7fffffff) + 4;
147}
148
763cd6e3 149static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, ZDR *zdr)
84004dbf
RS
150{
151 struct rpc_msg msg;
152
ea98629a 153 memset(&msg, 0, sizeof(struct rpc_msg));
84004dbf 154 msg.acpted_rply.ar_verf = _null_auth;
763cd6e3
RS
155 if (pdu->zdr_decode_bufsize > 0) {
156 if (pdu->zdr_decode_buf != NULL) {
157 free(pdu->zdr_decode_buf);
1b9917b8 158 }
763cd6e3
RS
159 pdu->zdr_decode_buf = malloc(pdu->zdr_decode_bufsize);
160 if (pdu->zdr_decode_buf == NULL) {
161 rpc_set_error(rpc, "zdr_replymsg failed in portmap_getport_reply");
162 pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of ZDR reply", pdu->private_data);
84004dbf
RS
163 return 0;
164 }
763cd6e3 165 memset(pdu->zdr_decode_buf, 0, pdu->zdr_decode_bufsize);
84004dbf 166 }
763cd6e3
RS
167 msg.acpted_rply.ar_results.where = pdu->zdr_decode_buf;
168 msg.acpted_rply.ar_results.proc = pdu->zdr_decode_fn;
84004dbf 169
763cd6e3
RS
170 if (zdr_replymsg(zdr, &msg) == 0) {
171 rpc_set_error(rpc, "zdr_replymsg failed in portmap_getport_reply");
84004dbf 172 pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data);
763cd6e3
RS
173 if (pdu->zdr_decode_buf != NULL) {
174 free(pdu->zdr_decode_buf);
175 pdu->zdr_decode_buf = NULL;
84004dbf
RS
176 }
177 return 0;
178 }
179 if (msg.rm_reply.rp_stat != MSG_ACCEPTED) {
180 pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data);
181 return 0;
182 }
183 switch (msg.rm_reply.rp_acpt.ar_stat) {
184 case SUCCESS:
763cd6e3 185 pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->zdr_decode_buf, pdu->private_data);
84004dbf
RS
186 break;
187 case PROG_UNAVAIL:
188 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data);
189 break;
190 case PROG_MISMATCH:
191 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data);
192 break;
193 case PROC_UNAVAIL:
194 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data);
195 break;
196 case GARBAGE_ARGS:
197 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data);
198 break;
199 case SYSTEM_ERR:
200 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data);
201 break;
202 default:
203 pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data);
204 break;
205 }
206
207 return 0;
208}
209
210int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size)
211{
212 struct rpc_pdu *pdu;
763cd6e3 213 ZDR zdr;
d678b73e 214 int pos, recordmarker = 0;
84004dbf 215 unsigned int xid;
d678b73e 216 char *reasbuf = NULL;
84004dbf 217
763cd6e3 218 memset(&zdr, 0, sizeof(ZDR));
84004dbf 219
763cd6e3 220 zdrmem_create(&zdr, buf, size, ZDR_DECODE);
a669857d 221 if (rpc->is_udp == 0) {
763cd6e3
RS
222 if (zdr_int(&zdr, &recordmarker) == 0) {
223 rpc_set_error(rpc, "zdr_int reading recordmarker failed");
224 zdr_destroy(&zdr);
a669857d
RS
225 return -1;
226 }
d678b73e 227 if (!(recordmarker&0x80000000)) {
763cd6e3 228 zdr_destroy(&zdr);
d678b73e
RS
229 if (rpc_add_fragment(rpc, buf+4, size-4) != 0) {
230 rpc_set_error(rpc, "Failed to queue fragment for reassembly.");
231 return -1;
232 }
233 return 0;
234 }
84004dbf 235 }
d678b73e
RS
236
237 /* reassembly */
238 if (recordmarker != 0 && rpc->fragments != NULL) {
239 struct rpc_fragment *fragment;
183451cf 240 uint64_t total = size - 4;
d678b73e
RS
241 char *ptr;
242
763cd6e3 243 zdr_destroy(&zdr);
d678b73e
RS
244 for (fragment = rpc->fragments; fragment; fragment = fragment->next) {
245 total += fragment->size;
246 }
247
248 reasbuf = malloc(total);
249 if (reasbuf == NULL) {
250 rpc_set_error(rpc, "Failed to reassemble PDU");
251 rpc_free_all_fragments(rpc);
252 return -1;
253 }
254 ptr = reasbuf;
255 for (fragment = rpc->fragments; fragment; fragment = fragment->next) {
256 memcpy(ptr, fragment->data, fragment->size);
257 ptr += fragment->size;
258 }
259 memcpy(ptr, buf + 4, size - 4);
763cd6e3 260 zdrmem_create(&zdr, reasbuf, total, ZDR_DECODE);
d678b73e
RS
261 rpc_free_all_fragments(rpc);
262 }
263
763cd6e3
RS
264 pos = zdr_getpos(&zdr);
265 if (zdr_int(&zdr, (int *)&xid) == 0) {
266 rpc_set_error(rpc, "zdr_int reading xid failed");
267 zdr_destroy(&zdr);
d678b73e
RS
268 if (reasbuf != NULL) {
269 free(reasbuf);
270 }
84004dbf
RS
271 return -1;
272 }
763cd6e3 273 zdr_setpos(&zdr, pos);
84004dbf
RS
274
275 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
276 if (pdu->xid != xid) {
277 continue;
278 }
a669857d
RS
279 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
280 SLIST_REMOVE(&rpc->waitpdu, pdu);
281 }
763cd6e3 282 if (rpc_process_reply(rpc, pdu, &zdr) != 0) {
1896d37b 283 rpc_set_error(rpc, "rpc_procdess_reply failed");
84004dbf 284 }
763cd6e3 285 zdr_destroy(&zdr);
a669857d
RS
286 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
287 rpc_free_pdu(rpc, pdu);
288 }
d678b73e
RS
289 if (reasbuf != NULL) {
290 free(reasbuf);
291 }
84004dbf
RS
292 return 0;
293 }
1896d37b 294 rpc_set_error(rpc, "No matching pdu found for xid:%d", xid);
763cd6e3 295 zdr_destroy(&zdr);
d678b73e
RS
296 if (reasbuf != NULL) {
297 free(reasbuf);
298 }
84004dbf
RS
299 return -1;
300}
301