in allocate pdu, if context is udp, dont allocate space for a record marker
[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
18 #include <stdio.h>
19 #include <strings.h>
20 #include <stdlib.h>
21 #include <rpc/rpc.h>
22 #include <rpc/xdr.h>
23 #include <rpc/rpc_msg.h>
24 #include "slist.h"
25 #include "libnfs.h"
26 #include "libnfs-raw.h"
27 #include "libnfs-private.h"
28
29 struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, xdrproc_t xdr_decode_fn, int xdr_decode_bufsize)
30 {
31 struct rpc_pdu *pdu;
32 struct rpc_msg msg;
33
34 if (rpc == NULL) {
35 return NULL;
36 }
37
38 pdu = malloc(sizeof(struct rpc_pdu));
39 if (pdu == NULL) {
40 rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure");
41 return NULL;
42 }
43 bzero(pdu, sizeof(struct rpc_pdu));
44 pdu->xid = rpc->xid++;
45 pdu->cb = cb;
46 pdu->private_data = private_data;
47 pdu->xdr_decode_fn = xdr_decode_fn;
48 pdu->xdr_decode_bufsize = xdr_decode_bufsize;
49
50 xdrmem_create(&pdu->xdr, rpc->encodebuf, rpc->encodebuflen, XDR_ENCODE);
51 if (rpc->is_udp == 0) {
52 xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */
53 }
54
55 bzero(&msg, sizeof(struct rpc_msg));
56 msg.rm_xid = pdu->xid;
57 msg.rm_direction = CALL;
58 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
59 msg.rm_call.cb_prog = program;
60 msg.rm_call.cb_vers = version;
61 msg.rm_call.cb_proc = procedure;
62 msg.rm_call.cb_cred = rpc->auth->ah_cred;
63 msg.rm_call.cb_verf = rpc->auth->ah_verf;
64
65 if (xdr_callmsg(&pdu->xdr, &msg) == 0) {
66 rpc_set_error(rpc, "xdr_callmsg failed");
67 xdr_destroy(&pdu->xdr);
68 free(pdu);
69 return NULL;
70 }
71
72 return pdu;
73 }
74
75 void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu)
76 {
77 if (pdu->outdata.data != NULL) {
78 free(pdu->outdata.data);
79 pdu->outdata.data = NULL;
80 }
81
82 if (pdu->xdr_decode_buf != NULL) {
83 xdr_free(pdu->xdr_decode_fn, pdu->xdr_decode_buf);
84 free(pdu->xdr_decode_buf);
85 pdu->xdr_decode_buf = NULL;
86 }
87
88 xdr_destroy(&pdu->xdr);
89
90 free(pdu);
91 }
92
93
94 int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
95 {
96 int size, recordmarker;
97
98 size = xdr_getpos(&pdu->xdr);
99
100 /* write recordmarker */
101 xdr_setpos(&pdu->xdr, 0);
102 recordmarker = (size - 4) | 0x80000000;
103 xdr_int(&pdu->xdr, &recordmarker);
104
105 pdu->outdata.size = size;
106 pdu->outdata.data = malloc(pdu->outdata.size);
107 if (pdu->outdata.data == NULL) {
108 rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n");
109 rpc_free_pdu(rpc, pdu);
110 return -1;
111 }
112
113 memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size);
114 SLIST_ADD_END(&rpc->outqueue, pdu);
115
116 return 0;
117 }
118
119 int rpc_get_pdu_size(char *buf)
120 {
121 uint32_t size;
122
123 size = ntohl(*(uint32_t *)buf);
124
125 if ((size & 0x80000000) == 0) {
126 /* cant handle oncrpc fragments */
127 return -1;
128 }
129
130 return (size & 0x7fffffff) + 4;
131 }
132
133 static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr)
134 {
135 struct rpc_msg msg;
136
137 bzero(&msg, sizeof(struct rpc_msg));
138 msg.acpted_rply.ar_verf = _null_auth;
139 if (pdu->xdr_decode_bufsize > 0) {
140 pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize);
141 if (pdu->xdr_decode_buf == NULL) {
142 rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply");
143 pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data);
144 return 0;
145 }
146 bzero(pdu->xdr_decode_buf, pdu->xdr_decode_bufsize);
147 }
148 msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf;
149 msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn;
150
151 if (xdr_replymsg(xdr, &msg) == 0) {
152 rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply");
153 pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data);
154 if (pdu->xdr_decode_buf != NULL) {
155 free(pdu->xdr_decode_buf);
156 pdu->xdr_decode_buf = NULL;
157 }
158 return 0;
159 }
160 if (msg.rm_reply.rp_stat != MSG_ACCEPTED) {
161 pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data);
162 return 0;
163 }
164 switch (msg.rm_reply.rp_acpt.ar_stat) {
165 case SUCCESS:
166 pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data);
167 break;
168 case PROG_UNAVAIL:
169 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data);
170 break;
171 case PROG_MISMATCH:
172 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data);
173 break;
174 case PROC_UNAVAIL:
175 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data);
176 break;
177 case GARBAGE_ARGS:
178 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data);
179 break;
180 case SYSTEM_ERR:
181 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data);
182 break;
183 default:
184 pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data);
185 break;
186 }
187
188 return 0;
189 }
190
191 int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size)
192 {
193 struct rpc_pdu *pdu;
194 XDR xdr;
195 int pos, recordmarker;
196 unsigned int xid;
197
198 bzero(&xdr, sizeof(XDR));
199
200 xdrmem_create(&xdr, buf, size, XDR_DECODE);
201 if (xdr_int(&xdr, &recordmarker) == 0) {
202 rpc_set_error(rpc, "xdr_int reading recordmarker failed");
203 xdr_destroy(&xdr);
204 return -1;
205 }
206 pos = xdr_getpos(&xdr);
207 if (xdr_int(&xdr, (int *)&xid) == 0) {
208 rpc_set_error(rpc, "xdr_int reading xid failed");
209 xdr_destroy(&xdr);
210 return -1;
211 }
212 xdr_setpos(&xdr, pos);
213
214 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
215 if (pdu->xid != xid) {
216 continue;
217 }
218 SLIST_REMOVE(&rpc->waitpdu, pdu);
219 if (rpc_process_reply(rpc, pdu, &xdr) != 0) {
220 rpc_set_error(rpc, "rpc_procdess_reply failed");
221 }
222 xdr_destroy(&xdr);
223 rpc_free_pdu(rpc, pdu);
224 return 0;
225 }
226 rpc_set_error(rpc, "No matching pdu found for xid:%d", xid);
227 xdr_destroy(&xdr);
228 return -1;
229 }
230