2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
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.
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.
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/>.
23 #include <rpc/rpc_msg.h>
26 #include "libnfs-raw.h"
27 #include "libnfs-private.h"
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
)
38 pdu
= malloc(sizeof(struct rpc_pdu
));
40 rpc_set_error(rpc
, "Out of memory: Failed to allocate pdu structure");
43 bzero(pdu
, sizeof(struct rpc_pdu
));
44 pdu
->xid
= rpc
->xid
++;
46 pdu
->private_data
= private_data
;
47 pdu
->xdr_decode_fn
= xdr_decode_fn
;
48 pdu
->xdr_decode_bufsize
= xdr_decode_bufsize
;
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 */
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
;
65 if (xdr_callmsg(&pdu
->xdr
, &msg
) == 0) {
66 rpc_set_error(rpc
, "xdr_callmsg failed");
67 xdr_destroy(&pdu
->xdr
);
75 void rpc_free_pdu(struct rpc_context
*rpc _U_
, struct rpc_pdu
*pdu
)
77 if (pdu
->outdata
.data
!= NULL
) {
78 free(pdu
->outdata
.data
);
79 pdu
->outdata
.data
= NULL
;
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
;
88 xdr_destroy(&pdu
->xdr
);
94 int rpc_queue_pdu(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
)
96 int size
, recordmarker
;
98 size
= xdr_getpos(&pdu
->xdr
);
100 /* write recordmarker */
101 xdr_setpos(&pdu
->xdr
, 0);
102 recordmarker
= (size
- 4) | 0x80000000;
103 xdr_int(&pdu
->xdr
, &recordmarker
);
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
);
113 memcpy(pdu
->outdata
.data
, rpc
->encodebuf
, pdu
->outdata
.size
);
114 SLIST_ADD_END(&rpc
->outqueue
, pdu
);
119 int rpc_get_pdu_size(char *buf
)
123 size
= ntohl(*(uint32_t *)buf
);
125 if ((size
& 0x80000000) == 0) {
126 /* cant handle oncrpc fragments */
130 return (size
& 0x7fffffff) + 4;
133 static int rpc_process_reply(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
, XDR
*xdr
)
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
);
146 bzero(pdu
->xdr_decode_buf
, pdu
->xdr_decode_bufsize
);
148 msg
.acpted_rply
.ar_results
.where
= pdu
->xdr_decode_buf
;
149 msg
.acpted_rply
.ar_results
.proc
= pdu
->xdr_decode_fn
;
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
;
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
);
164 switch (msg
.rm_reply
.rp_acpt
.ar_stat
) {
166 pdu
->cb(rpc
, RPC_STATUS_SUCCESS
, pdu
->xdr_decode_buf
, pdu
->private_data
);
169 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program not available", pdu
->private_data
);
172 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program version mismatch", pdu
->private_data
);
175 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Procedure not available", pdu
->private_data
);
178 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Garbage arguments", pdu
->private_data
);
181 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: System Error", pdu
->private_data
);
184 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Unknown rpc response from server", pdu
->private_data
);
191 int rpc_process_pdu(struct rpc_context
*rpc
, char *buf
, int size
)
195 int pos
, recordmarker
;
198 bzero(&xdr
, sizeof(XDR
));
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");
206 pos
= xdr_getpos(&xdr
);
207 if (xdr_int(&xdr
, (int *)&xid
) == 0) {
208 rpc_set_error(rpc
, "xdr_int reading xid failed");
212 xdr_setpos(&xdr
, pos
);
214 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
215 if (pdu
->xid
!= xid
) {
218 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
219 if (rpc_process_reply(rpc
, pdu
, &xdr
) != 0) {
220 rpc_set_error(rpc
, "rpc_procdess_reply failed");
223 rpc_free_pdu(rpc
, pdu
);
226 rpc_set_error(rpc
, "No matching pdu found for xid:%d", xid
);