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