2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <rpc/rpc_msg.h>
22 #include "dlinklist.h"
24 #include "libnfs-raw.h"
25 #include "libnfs-private.h"
27 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)
33 printf("trying to allocate rpc pdu on NULL context\n");
37 pdu = malloc(sizeof(struct rpc_pdu));
39 printf("Failed to allocate pdu structure\n");
42 bzero(pdu, sizeof(struct rpc_pdu));
43 pdu->xid = rpc->xid++;
45 pdu->private_data = private_data;
46 pdu->xdr_decode_fn = xdr_decode_fn;
47 pdu->xdr_decode_bufsize = xdr_decode_bufsize;
49 xdrmem_create(&pdu->xdr, rpc->encodebuf, rpc->encodebuflen, XDR_ENCODE);
50 xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */
52 bzero(&msg, sizeof(struct rpc_msg));
53 msg.rm_xid = pdu->xid;
54 msg.rm_direction = CALL;
55 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
56 msg.rm_call.cb_prog = program;
57 msg.rm_call.cb_vers = version;
58 msg.rm_call.cb_proc = procedure;
59 msg.rm_call.cb_cred = rpc->auth->ah_cred;
60 msg.rm_call.cb_verf = rpc->auth->ah_verf;
62 if (xdr_callmsg(&pdu->xdr, &msg) == 0) {
63 printf("xdr_callmsg failed\n");
64 xdr_destroy(&pdu->xdr);
72 void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu)
74 if (pdu->outdata.data != NULL) {
75 free(pdu->outdata.data);
76 pdu->outdata.data = NULL;
79 if (pdu->xdr_decode_buf != NULL) {
80 xdr_free(pdu->xdr_decode_fn, pdu->xdr_decode_buf);
81 free(pdu->xdr_decode_buf);
82 pdu->xdr_decode_buf = NULL;
85 xdr_destroy(&pdu->xdr);
90 int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
92 int size, recordmarker;
94 size = xdr_getpos(&pdu->xdr);
96 /* write recordmarker */
97 xdr_setpos(&pdu->xdr, 0);
98 recordmarker = (size - 4) | 0x80000000;
99 xdr_int(&pdu->xdr, &recordmarker);
101 pdu->outdata.size = size;
102 pdu->outdata.data = malloc(pdu->outdata.size);
103 if (pdu->outdata.data == NULL) {
104 rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n");
105 rpc_free_pdu(rpc, pdu);
109 memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size);
110 DLIST_ADD_END(rpc->outqueue, pdu, NULL);
115 int rpc_get_pdu_size(char *buf)
119 size = ntohl(*(uint32_t *)buf);
121 if ((size & 0x80000000) == 0) {
122 printf("cant handle oncrpc fragments\n");
126 return (size & 0x7fffffff) + 4;
129 static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr)
133 bzero(&msg, sizeof(struct rpc_msg));
134 msg.acpted_rply.ar_verf = _null_auth;
135 if (pdu->xdr_decode_bufsize > 0) {
136 pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize);
137 if (pdu->xdr_decode_buf == NULL) {
138 printf("xdr_replymsg failed in portmap_getport_reply\n");
139 pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data);
142 bzero(pdu->xdr_decode_buf, pdu->xdr_decode_bufsize);
144 msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf;
145 msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn;
147 if (xdr_replymsg(xdr, &msg) == 0) {
148 printf("xdr_replymsg failed in portmap_getport_reply\n");
149 pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data);
150 if (pdu->xdr_decode_buf != NULL) {
151 free(pdu->xdr_decode_buf);
152 pdu->xdr_decode_buf = NULL;
156 if (msg.rm_reply.rp_stat != MSG_ACCEPTED) {
157 pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data);
160 switch (msg.rm_reply.rp_acpt.ar_stat) {
162 pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data);
165 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data);
168 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data);
171 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data);
174 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data);
177 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data);
180 pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data);
187 int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size)
191 int pos, recordmarker;
194 bzero(&xdr, sizeof(XDR));
196 xdrmem_create(&xdr, buf, size, XDR_DECODE);
197 if (xdr_int(&xdr, &recordmarker) == 0) {
198 printf("xdr_int reading recordmarker failed\n");
202 pos = xdr_getpos(&xdr);
203 if (xdr_int(&xdr, (int *)&xid) == 0) {
204 printf("xdr_int reading xid failed\n");
208 xdr_setpos(&xdr, pos);
210 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
211 if (pdu->xid != xid) {
214 DLIST_REMOVE(rpc->waitpdu, pdu);
215 if (rpc_process_reply(rpc, pdu, &xdr) != 0) {
216 printf("rpc_procdess_reply failed\n");
219 rpc_free_pdu(rpc, pdu);
222 printf("No matching pdu found for xid:%d\n", xid);