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