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