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