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