8aefa5743d9d76f14f98b4d7e780d7ffb09d132a
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
)
35 printf("trying to allocate rpc pdu on NULL context\n");
39 pdu
= malloc(sizeof(struct rpc_pdu
));
41 printf("Failed to allocate pdu structure\n");
44 bzero(pdu
, sizeof(struct rpc_pdu
));
45 pdu
->xid
= rpc
->xid
++;
47 pdu
->private_data
= private_data
;
48 pdu
->xdr_decode_fn
= xdr_decode_fn
;
49 pdu
->xdr_decode_bufsize
= xdr_decode_bufsize
;
51 xdrmem_create(&pdu
->xdr
, rpc
->encodebuf
, rpc
->encodebuflen
, XDR_ENCODE
);
52 xdr_setpos(&pdu
->xdr
, 4); /* skip past the record marker */
54 bzero(&msg
, sizeof(struct rpc_msg
));
55 msg
.rm_xid
= pdu
->xid
;
56 msg
.rm_direction
= CALL
;
57 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
58 msg
.rm_call
.cb_prog
= program
;
59 msg
.rm_call
.cb_vers
= version
;
60 msg
.rm_call
.cb_proc
= procedure
;
61 msg
.rm_call
.cb_cred
= rpc
->auth
->ah_cred
;
62 msg
.rm_call
.cb_verf
= rpc
->auth
->ah_verf
;
64 if (xdr_callmsg(&pdu
->xdr
, &msg
) == 0) {
65 printf("xdr_callmsg failed\n");
66 xdr_destroy(&pdu
->xdr
);
74 void rpc_free_pdu(struct rpc_context
*rpc _U_
, struct rpc_pdu
*pdu
)
76 if (pdu
->outdata
.data
!= NULL
) {
77 free(pdu
->outdata
.data
);
78 pdu
->outdata
.data
= NULL
;
81 if (pdu
->xdr_decode_buf
!= NULL
) {
82 xdr_free(pdu
->xdr_decode_fn
, pdu
->xdr_decode_buf
);
83 free(pdu
->xdr_decode_buf
);
84 pdu
->xdr_decode_buf
= NULL
;
87 xdr_destroy(&pdu
->xdr
);
92 int rpc_queue_pdu(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
)
94 int size
, recordmarker
;
96 size
= xdr_getpos(&pdu
->xdr
);
98 /* write recordmarker */
99 xdr_setpos(&pdu
->xdr
, 0);
100 recordmarker
= (size
- 4) | 0x80000000;
101 xdr_int(&pdu
->xdr
, &recordmarker
);
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
);
111 memcpy(pdu
->outdata
.data
, rpc
->encodebuf
, pdu
->outdata
.size
);
112 SLIST_ADD_END(&rpc
->outqueue
, pdu
);
117 int rpc_get_pdu_size(char *buf
)
121 size
= ntohl(*(uint32_t *)buf
);
123 if ((size
& 0x80000000) == 0) {
124 printf("cant handle oncrpc fragments\n");
128 return (size
& 0x7fffffff) + 4;
131 static int rpc_process_reply(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
, XDR
*xdr
)
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
) {
140 printf("xdr_replymsg failed in portmap_getport_reply\n");
141 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Failed to allocate buffer for decoding of XDR reply", pdu
->private_data
);
144 bzero(pdu
->xdr_decode_buf
, pdu
->xdr_decode_bufsize
);
146 msg
.acpted_rply
.ar_results
.where
= pdu
->xdr_decode_buf
;
147 msg
.acpted_rply
.ar_results
.proc
= pdu
->xdr_decode_fn
;
149 if (xdr_replymsg(xdr
, &msg
) == 0) {
150 printf("xdr_replymsg failed in portmap_getport_reply\n");
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
;
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
);
162 switch (msg
.rm_reply
.rp_acpt
.ar_stat
) {
164 pdu
->cb(rpc
, RPC_STATUS_SUCCESS
, pdu
->xdr_decode_buf
, pdu
->private_data
);
167 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program not available", pdu
->private_data
);
170 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program version mismatch", pdu
->private_data
);
173 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Procedure not available", pdu
->private_data
);
176 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Garbage arguments", pdu
->private_data
);
179 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: System Error", pdu
->private_data
);
182 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Unknown rpc response from server", pdu
->private_data
);
189 int rpc_process_pdu(struct rpc_context
*rpc
, char *buf
, int size
)
193 int pos
, recordmarker
;
196 bzero(&xdr
, sizeof(XDR
));
198 xdrmem_create(&xdr
, buf
, size
, XDR_DECODE
);
199 if (xdr_int(&xdr
, &recordmarker
) == 0) {
200 printf("xdr_int reading recordmarker failed\n");
204 pos
= xdr_getpos(&xdr
);
205 if (xdr_int(&xdr
, (int *)&xid
) == 0) {
206 printf("xdr_int reading xid failed\n");
210 xdr_setpos(&xdr
, pos
);
212 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
213 if (pdu
->xid
!= xid
) {
216 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
217 if (rpc_process_reply(rpc
, pdu
, &xdr
) != 0) {
218 printf("rpc_procdess_reply failed\n");
221 rpc_free_pdu(rpc
, pdu
);
224 printf("No matching pdu found for xid:%d\n", xid
);