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